mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Merge pull request #19329 from github/idrissrio/designated-initializer
C++: add predicate to distinguish designator-based initializations
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
class Expr extends @expr {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
class AggregateLiteral extends Expr, @aggregateliteral {
|
||||
override string toString() { none() }
|
||||
}
|
||||
|
||||
from AggregateLiteral aggregate, Expr initializer, int element_index, int position
|
||||
where aggregate_array_init(aggregate, initializer, element_index, position, _)
|
||||
select aggregate, initializer, element_index, position
|
||||
@@ -0,0 +1,15 @@
|
||||
class Expr extends @expr {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
class AggregateLiteral extends Expr, @aggregateliteral {
|
||||
override string toString() { none() }
|
||||
}
|
||||
|
||||
class MemberVariable extends @membervariable {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
from AggregateLiteral aggregate, Expr initializer, MemberVariable field, int position
|
||||
where aggregate_field_init(aggregate, initializer, field, position, _)
|
||||
select aggregate, initializer, field, position
|
||||
2448
cpp/downgrades/2e2d805ef93d060b813403cb9b51dc72455a4c68/old.dbscheme
Normal file
2448
cpp/downgrades/2e2d805ef93d060b813403cb9b51dc72455a4c68/old.dbscheme
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,4 @@
|
||||
description: add `hasDesignator` predicate to `ArrayOrVectorAggregateLiteral` and `ClassAggregateLiteral`
|
||||
compatibility: backwards
|
||||
aggregate_array_init.rel: run aggregate_array_init.qlo
|
||||
aggregate_field_init.rel: run aggregate_field_init.qlo
|
||||
@@ -0,0 +1,4 @@
|
||||
---
|
||||
category: feature
|
||||
---
|
||||
* Introduced `hasDesignator()` predicates to distinguish between designated and positional initializations for both struct/union fields and array elements.
|
||||
@@ -213,7 +213,24 @@ class ClassAggregateLiteral extends AggregateLiteral {
|
||||
Expr getFieldExpr(Field field, int position) {
|
||||
field = classType.getAField() and
|
||||
aggregate_field_init(underlyingElement(this), unresolveElement(result), unresolveElement(field),
|
||||
position)
|
||||
position, _)
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if the `position`-th initialization of `field` in this aggregate initializer
|
||||
* uses a designated (e.g., `.x = ...`) rather than a positional initializer.
|
||||
*
|
||||
* For example, in:
|
||||
* ```c
|
||||
* struct S { int x, y; };
|
||||
* struct S s = { .x = 1, 2 };
|
||||
* ```
|
||||
* - `.x = 1` is a designated initializer, therefore `hasDesignator(x, 0)` holds.
|
||||
* - `2` is a positional initializer for `s.y`, therefore `hasDesignator(y, 1)` does not hold.
|
||||
*/
|
||||
predicate hasDesignator(Field field, int position) {
|
||||
field = classType.getAField() and
|
||||
aggregate_field_init(underlyingElement(this), _, unresolveElement(field), position, true)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -304,7 +321,24 @@ class ArrayOrVectorAggregateLiteral extends AggregateLiteral {
|
||||
* - `a.getElementExpr(0, 2)` gives `789`.
|
||||
*/
|
||||
Expr getElementExpr(int elementIndex, int position) {
|
||||
aggregate_array_init(underlyingElement(this), unresolveElement(result), elementIndex, position)
|
||||
aggregate_array_init(underlyingElement(this), unresolveElement(result), elementIndex, position,
|
||||
_)
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if the `position`-th initialization of the array element at `elementIndex`
|
||||
* in this aggregate initializer uses a designated (e.g., `[0] = ...`) rather than
|
||||
* a positional initializer.
|
||||
*
|
||||
* For example, in:
|
||||
* ```c
|
||||
* int x[] = { [0] = 1, 2 };
|
||||
* ```
|
||||
* - `[0] = 1` is a designated initializer, therefore `hasDesignator(0, 0)` holds.
|
||||
* - `2` is a positional initializer for `x[1]`, therefore `hasDesignator(1, 1)` does not hold.
|
||||
*/
|
||||
predicate hasDesignator(int elementIndex, int position) {
|
||||
aggregate_array_init(underlyingElement(this), _, elementIndex, position, true)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2039,7 +2039,8 @@ aggregate_field_init(
|
||||
int aggregate: @aggregateliteral ref,
|
||||
int initializer: @expr ref,
|
||||
int field: @membervariable ref,
|
||||
int position: int ref
|
||||
int position: int ref,
|
||||
boolean is_designated: boolean ref
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -2051,7 +2052,8 @@ aggregate_array_init(
|
||||
int aggregate: @aggregateliteral ref,
|
||||
int initializer: @expr ref,
|
||||
int element_index: int ref,
|
||||
int position: int ref
|
||||
int position: int ref,
|
||||
boolean is_designated: boolean ref
|
||||
);
|
||||
|
||||
@ctorinit = @ctordirectinit
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
||||
class Expr extends @expr {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
class AggregateLiteral extends Expr, @aggregateliteral {
|
||||
override string toString() { none() }
|
||||
}
|
||||
|
||||
from AggregateLiteral aggregate, Expr initializer, int element_index, int position
|
||||
where aggregate_array_init(aggregate, initializer, element_index, position)
|
||||
select aggregate, initializer, element_index, position, false
|
||||
@@ -0,0 +1,15 @@
|
||||
class Expr extends @expr {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
class AggregateLiteral extends Expr, @aggregateliteral {
|
||||
override string toString() { none() }
|
||||
}
|
||||
|
||||
class MemberVariable extends @membervariable {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
from AggregateLiteral aggregate, Expr initializer, MemberVariable field, int position
|
||||
where aggregate_field_init(aggregate, initializer, field, position)
|
||||
select aggregate, initializer, field, position, false
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,4 @@
|
||||
description: add `hasDesignator` predicate to `ArrayOrVectorAggregateLiteral` and `ClassAggregateLiteral`
|
||||
compatibility: backwards
|
||||
aggregate_array_init.rel: run aggregate_array_init.qlo
|
||||
aggregate_field_init.rel: run aggregate_field_init.qlo
|
||||
Reference in New Issue
Block a user