mirror of
https://github.com/github/codeql.git
synced 2026-04-27 09:45:15 +02:00
C++: Support C++20 range-based for initializers
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
---
|
||||
category: feature
|
||||
---
|
||||
* A `getInitialization` predicate was added to the `RangeBasedForStmt` class that yields the C++20-style initializer of the range-based `for` statement when it exists.
|
||||
@@ -735,7 +735,9 @@ private predicate namedStmtChildPredicates(Locatable s, Element e, string pred)
|
||||
or
|
||||
s.(ForStmt).getStmt() = e and pred = "getStmt()"
|
||||
or
|
||||
s.(RangeBasedForStmt).getChild(0) = e and pred = "getChild(0)"
|
||||
s.(RangeBasedForStmt).getInitialization() = e and pred = "getInitialization()"
|
||||
or
|
||||
s.(RangeBasedForStmt).getChild(1) = e and pred = "getChild(1)"
|
||||
or
|
||||
s.(RangeBasedForStmt).getBeginEndDeclaration() = e and pred = "getBeginEndDeclaration()"
|
||||
or
|
||||
@@ -743,7 +745,7 @@ private predicate namedStmtChildPredicates(Locatable s, Element e, string pred)
|
||||
or
|
||||
s.(RangeBasedForStmt).getUpdate() = e and pred = "getUpdate()"
|
||||
or
|
||||
s.(RangeBasedForStmt).getChild(4) = e and pred = "getChild(4)"
|
||||
s.(RangeBasedForStmt).getChild(5) = e and pred = "getChild(5)"
|
||||
or
|
||||
s.(RangeBasedForStmt).getStmt() = e and pred = "getStmt()"
|
||||
or
|
||||
|
||||
@@ -637,8 +637,10 @@ private predicate straightLineSparse(Node scope, int i, Node ni, Spec spec) {
|
||||
any(RangeBasedForStmt for |
|
||||
i = -1 and ni = for and spec.isAt()
|
||||
or
|
||||
i = 0 and ni = for.getInitialization() and spec.isAround()
|
||||
or
|
||||
exists(DeclStmt s | s.getADeclaration() = for.getRangeVariable() |
|
||||
i = 0 and ni = s and spec.isAround()
|
||||
i = 1 and ni = s and spec.isAround()
|
||||
)
|
||||
or
|
||||
exists(DeclStmt s |
|
||||
@@ -649,22 +651,22 @@ private predicate straightLineSparse(Node scope, int i, Node ni, Spec spec) {
|
||||
// DeclStmt in that case.
|
||||
exists(s.getADeclaration())
|
||||
|
|
||||
i = 1 and ni = s and spec.isAround()
|
||||
i = 2 and ni = s and spec.isAround()
|
||||
)
|
||||
or
|
||||
i = 2 and ni = for.getCondition() and spec.isBefore()
|
||||
i = 3 and ni = for.getCondition() and spec.isBefore()
|
||||
or
|
||||
i = 3 and /* BARRIER */ ni = for and spec.isBarrier()
|
||||
i = 4 and /* BARRIER */ ni = for and spec.isBarrier()
|
||||
or
|
||||
exists(DeclStmt declStmt | declStmt.getADeclaration() = for.getVariable() |
|
||||
i = 4 and ni = declStmt and spec.isAfter()
|
||||
i = 5 and ni = declStmt and spec.isAfter()
|
||||
)
|
||||
or
|
||||
i = 5 and ni = for.getStmt() and spec.isAround()
|
||||
i = 6 and ni = for.getStmt() and spec.isAround()
|
||||
or
|
||||
i = 6 and ni = for.getUpdate() and spec.isAround()
|
||||
i = 7 and ni = for.getUpdate() and spec.isAround()
|
||||
or
|
||||
i = 7 and ni = for.getCondition() and spec.isBefore()
|
||||
i = 8 and ni = for.getCondition() and spec.isBefore()
|
||||
)
|
||||
or
|
||||
scope =
|
||||
|
||||
@@ -960,25 +960,38 @@ class TranslatedRangeBasedForStmt extends TranslatedStmt, ConditionContext {
|
||||
override RangeBasedForStmt stmt;
|
||||
|
||||
override TranslatedElement getChild(int id) {
|
||||
id = 0 and result = this.getRangeVariableDeclStmt()
|
||||
id = 0 and result = this.getInitialization()
|
||||
or
|
||||
id = 1 and result = this.getRangeVariableDeclStmt()
|
||||
or
|
||||
// Note: `__begin` and `__end` are declared by the same `DeclStmt`
|
||||
id = 1 and result = this.getBeginEndVariableDeclStmt()
|
||||
id = 2 and result = this.getBeginEndVariableDeclStmt()
|
||||
or
|
||||
id = 2 and result = this.getCondition()
|
||||
id = 3 and result = this.getCondition()
|
||||
or
|
||||
id = 3 and result = this.getUpdate()
|
||||
id = 4 and result = this.getUpdate()
|
||||
or
|
||||
id = 4 and result = this.getVariableDeclStmt()
|
||||
id = 5 and result = this.getVariableDeclStmt()
|
||||
or
|
||||
id = 5 and result = this.getBody()
|
||||
id = 6 and result = this.getBody()
|
||||
}
|
||||
|
||||
private predicate hasInitialization() { exists(stmt.getInitialization()) }
|
||||
|
||||
private TranslatedStmt getInitialization() {
|
||||
result = getTranslatedStmt(stmt.getInitialization())
|
||||
}
|
||||
|
||||
override Instruction getFirstInstruction(EdgeKind kind) {
|
||||
result = this.getRangeVariableDeclStmt().getFirstInstruction(kind)
|
||||
if this.hasInitialization()
|
||||
then result = this.getInitialization().getFirstInstruction(kind)
|
||||
else result = this.getFirstRangeVariableDeclStmtInstruction(kind)
|
||||
}
|
||||
|
||||
override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) {
|
||||
child = this.getInitialization() and
|
||||
result = this.getFirstRangeVariableDeclStmtInstruction(kind)
|
||||
or
|
||||
child = this.getRangeVariableDeclStmt() and
|
||||
result = this.getBeginEndVariableDeclStmt().getFirstInstruction(kind)
|
||||
or
|
||||
@@ -1018,6 +1031,10 @@ class TranslatedRangeBasedForStmt extends TranslatedStmt, ConditionContext {
|
||||
)
|
||||
}
|
||||
|
||||
private Instruction getFirstRangeVariableDeclStmtInstruction(EdgeKind kind) {
|
||||
result = this.getRangeVariableDeclStmt().getFirstInstruction(kind)
|
||||
}
|
||||
|
||||
private TranslatedDeclStmt getBeginEndVariableDeclStmt() {
|
||||
exists(IRVariableDeclarationEntry entry |
|
||||
entry.getStmt() = stmt.getBeginEndDeclaration() and
|
||||
|
||||
@@ -892,6 +892,26 @@ class DoStmt extends Loop, @stmt_end_test_while {
|
||||
class RangeBasedForStmt extends Loop, @stmt_range_based_for {
|
||||
override string getAPrimaryQlClass() { result = "RangeBasedForStmt" }
|
||||
|
||||
/**
|
||||
* Gets the initialization statement of this 'for' statement, if any.
|
||||
*
|
||||
* For example, for
|
||||
* ```
|
||||
* for (int x = y; auto z : ... ) { }
|
||||
* ```
|
||||
* the result is `int x = y;`.
|
||||
*
|
||||
* Does not hold if the initialization statement is missing or an empty statement, as in
|
||||
* ```
|
||||
* for (auto z : ...) { }
|
||||
* ```
|
||||
* or
|
||||
* ```
|
||||
* for (; auto z : ) { }
|
||||
* ```
|
||||
*/
|
||||
Stmt getInitialization() { for_initialization(underlyingElement(this), unresolveElement(result)) }
|
||||
|
||||
/**
|
||||
* Gets the 'body' statement of this range-based 'for' statement.
|
||||
*
|
||||
@@ -901,7 +921,7 @@ class RangeBasedForStmt extends Loop, @stmt_range_based_for {
|
||||
* ```
|
||||
* the result is the `BlockStmt` `{ y += x; }`.
|
||||
*/
|
||||
override Stmt getStmt() { result = this.getChild(5) }
|
||||
override Stmt getStmt() { result = this.getChild(6) }
|
||||
|
||||
override string toString() { result = "for(...:...) ..." }
|
||||
|
||||
@@ -914,7 +934,7 @@ class RangeBasedForStmt extends Loop, @stmt_range_based_for {
|
||||
* ```
|
||||
* the result is `int x`.
|
||||
*/
|
||||
LocalVariable getVariable() { result = this.getChild(4).(DeclStmt).getADeclaration() }
|
||||
LocalVariable getVariable() { result = this.getChild(5).(DeclStmt).getADeclaration() }
|
||||
|
||||
/**
|
||||
* Gets the expression giving the range to iterate over.
|
||||
@@ -928,7 +948,7 @@ class RangeBasedForStmt extends Loop, @stmt_range_based_for {
|
||||
Expr getRange() { result = this.getRangeVariable().getInitializer().getExpr() }
|
||||
|
||||
/** Gets the compiler-generated `__range` variable after desugaring. */
|
||||
LocalVariable getRangeVariable() { result = this.getChild(0).(DeclStmt).getADeclaration() }
|
||||
LocalVariable getRangeVariable() { result = this.getChild(1).(DeclStmt).getADeclaration() }
|
||||
|
||||
/**
|
||||
* Gets the compiler-generated `__begin != __end` which is the
|
||||
@@ -936,7 +956,7 @@ class RangeBasedForStmt extends Loop, @stmt_range_based_for {
|
||||
* It will be either an `NEExpr` or a call to a user-defined
|
||||
* `operator!=`.
|
||||
*/
|
||||
override Expr getCondition() { result = this.getChild(2) }
|
||||
override Expr getCondition() { result = this.getChild(3) }
|
||||
|
||||
override Expr getControllingExpr() { result = this.getCondition() }
|
||||
|
||||
@@ -945,7 +965,7 @@ class RangeBasedForStmt extends Loop, @stmt_range_based_for {
|
||||
* `__end`, initializing them to the values they have before entering the
|
||||
* desugared loop.
|
||||
*/
|
||||
DeclStmt getBeginEndDeclaration() { result = this.getChild(1) }
|
||||
DeclStmt getBeginEndDeclaration() { result = this.getChild(2) }
|
||||
|
||||
/** Gets the compiler-generated `__begin` variable after desugaring. */
|
||||
LocalVariable getBeginVariable() { result = this.getBeginEndDeclaration().getDeclaration(0) }
|
||||
@@ -959,7 +979,7 @@ class RangeBasedForStmt extends Loop, @stmt_range_based_for {
|
||||
* be either a `PrefixIncrExpr` or a call to a user-defined
|
||||
* `operator++`.
|
||||
*/
|
||||
Expr getUpdate() { result = this.getChild(3) }
|
||||
Expr getUpdate() { result = this.getChild(4) }
|
||||
|
||||
/** Gets the compiler-generated `__begin` variable after desugaring. */
|
||||
LocalVariable getAnIterationVariable() { result = this.getBeginVariable() }
|
||||
|
||||
@@ -2050,8 +2050,11 @@ switch_body(
|
||||
int body_id: @stmt ref
|
||||
);
|
||||
|
||||
@stmt_for_or_range_based_for = @stmt_for
|
||||
| @stmt_range_based_for;
|
||||
|
||||
for_initialization(
|
||||
unique int for_stmt: @stmt_for ref,
|
||||
unique int for_stmt: @stmt_for_or_range_based_for ref,
|
||||
int init_id: @stmt ref
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
class Element extends @element {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
class Expr extends @expr {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
class Stmt extends @stmt {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
predicate isStmtWithInitializer(Stmt stmt) { exists(int kind | stmts(stmt, kind, _) | kind = 29) }
|
||||
|
||||
from Expr child, int index, int index_new, Element parent
|
||||
where
|
||||
exprparents(child, index, parent) and
|
||||
if isStmtWithInitializer(parent) then index_new = index + 1 else index_new = index
|
||||
select child, index_new, parent
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,15 @@
|
||||
class Element extends @element {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
class Stmt extends @stmt {
|
||||
string toString() { none() }
|
||||
}
|
||||
|
||||
predicate isStmtWithInitializer(Stmt stmt) { exists(int kind | stmts(stmt, kind, _) | kind = 29) }
|
||||
|
||||
from Stmt child, int index, int index_new, Element parent
|
||||
where
|
||||
stmtparents(child, index, parent) and
|
||||
if isStmtWithInitializer(parent) then index_new = index + 1 else index_new = index
|
||||
select child, index_new, parent
|
||||
@@ -0,0 +1,4 @@
|
||||
description: Support C++20 range-based for initializers
|
||||
compatibility: partial
|
||||
exprparents.rel: run exprparents.qlo
|
||||
stmtparents.rel: run stmtparents.qlo
|
||||
Reference in New Issue
Block a user