mirror of
https://github.com/github/codeql.git
synced 2026-04-23 15:55:18 +02:00
C++: Simplify consteval if to be just a single class with an isNot predicate
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
---
|
||||
category: feature
|
||||
---
|
||||
* New classes `ConstevalIfStmt` and `NotConstevalIfStmt` were introduced, which represent the C++23 `if consteval` and `if ! consteval` statements.
|
||||
* A new class `ConstevalIfStmt` was introduced, which represents the C++23 `if consteval` and `if ! consteval` statements.
|
||||
|
||||
@@ -912,9 +912,9 @@ private predicate namedStmtChildPredicates(Locatable s, Element e, string pred)
|
||||
or
|
||||
s.(ConstexprIfStmt).getElse() = e and pred = "getElse()"
|
||||
or
|
||||
s.(ConstevalOrNotConstevalIfStmt).getThen() = e and pred = "getThen()"
|
||||
s.(ConstevalIfStmt).getThen() = e and pred = "getThen()"
|
||||
or
|
||||
s.(ConstevalOrNotConstevalIfStmt).getElse() = e and pred = "getElse()"
|
||||
s.(ConstevalIfStmt).getElse() = e and pred = "getElse()"
|
||||
or
|
||||
s.(Handler).getParameter() = e and pred = "getParameter()"
|
||||
or
|
||||
|
||||
@@ -876,8 +876,8 @@ private predicate subEdge(Pos p1, Node n1, Node n2, Pos p2) {
|
||||
p2.nodeAfter(n2, s)
|
||||
)
|
||||
or
|
||||
// ConstevalOrNotConstevalIfStmt -> { then, else } ->
|
||||
exists(ConstevalOrNotConstevalIfStmt s |
|
||||
// NotConstevalIfStmt -> { then, else } ->
|
||||
exists(ConstevalIfStmt s |
|
||||
p1.nodeAt(n1, s) and
|
||||
p2.nodeBefore(n2, s.getThen())
|
||||
or
|
||||
|
||||
@@ -1098,8 +1098,8 @@ class TranslatedConstExprIfStmt extends TranslatedIfLikeStmt {
|
||||
override predicate hasElse() { exists(stmt.getElse()) }
|
||||
}
|
||||
|
||||
class TranslatedConstevalOrNotConstevalIfStmt extends TranslatedStmt {
|
||||
override ConstevalOrNotConstevalIfStmt stmt;
|
||||
class TranslatedConstevalIfStmt extends TranslatedStmt {
|
||||
override ConstevalIfStmt stmt;
|
||||
|
||||
override Instruction getFirstInstruction(EdgeKind kind) {
|
||||
if not this.hasEvaluatedBranch()
|
||||
|
||||
@@ -446,7 +446,27 @@ class ConstexprIfStmt extends ConditionalStmt, @stmt_constexpr_if {
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
class ConstevalOrNotConstevalIfStmt extends Stmt, @stmt_consteval_or_not_consteval_if {
|
||||
class ConstevalIfStmt extends Stmt, @stmt_consteval_or_not_consteval_if {
|
||||
override string getAPrimaryQlClass() { result = "ConstevalIfStmt" }
|
||||
|
||||
override string toString() {
|
||||
if this.isNot() then result = "if ! consteval ..." else result = "if consteval ..."
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if this is a 'not consteval if' statement.
|
||||
*
|
||||
* For example, this holds for
|
||||
* ```cpp
|
||||
* if ! consteval { return true; }
|
||||
* ```
|
||||
* but not for
|
||||
* ```cpp
|
||||
* if consteval { return true; }
|
||||
* ```
|
||||
*/
|
||||
predicate isNot() { this instanceof @stmt_not_consteval_if }
|
||||
|
||||
/**
|
||||
* Gets the 'then' statement of this '(not) consteval if' statement.
|
||||
*
|
||||
@@ -515,7 +535,9 @@ class ConstevalOrNotConstevalIfStmt extends Stmt, @stmt_consteval_or_not_constev
|
||||
* ```
|
||||
* there is no result.
|
||||
*/
|
||||
Stmt getCompileTimeEvaluatedBranch() { none() }
|
||||
Stmt getCompileTimeEvaluatedBranch() {
|
||||
if this.isNot() then result = this.getElse() else result = this.getThen()
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if this '(not) constexpr if' statement has a compile time evaluated statement.
|
||||
@@ -544,7 +566,9 @@ class ConstevalOrNotConstevalIfStmt extends Stmt, @stmt_consteval_or_not_constev
|
||||
* ```
|
||||
* there is no result.
|
||||
*/
|
||||
Stmt getRuntimeEvaluatedBranch() { none() }
|
||||
Stmt getRuntimeEvaluatedBranch() {
|
||||
if this.isNot() then result = this.getThen() else result = this.getElse()
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if this '(not) constexpr if' statement has a runtime evaluated statement.
|
||||
@@ -561,44 +585,6 @@ class ConstevalOrNotConstevalIfStmt extends Stmt, @stmt_consteval_or_not_constev
|
||||
predicate hasRuntimeEvaluatedBranch() { exists(this.getRuntimeEvaluatedBranch()) }
|
||||
}
|
||||
|
||||
/**
|
||||
* A C/C++ 'consteval if'. For example, the `if consteval` statement
|
||||
* in the following code:
|
||||
* ```cpp
|
||||
* if consteval {
|
||||
* ...
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
class ConstevalIfStmt extends ConstevalOrNotConstevalIfStmt, @stmt_consteval_if {
|
||||
override string getAPrimaryQlClass() { result = "ConstevalIfStmt" }
|
||||
|
||||
override string toString() { result = "if consteval ..." }
|
||||
|
||||
override Stmt getCompileTimeEvaluatedBranch() { result = this.getThen() }
|
||||
|
||||
override Stmt getRuntimeEvaluatedBranch() { result = this.getElse() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A C/C++ 'not consteval if'. For example, the `if ! consteval` statement
|
||||
* in the following code:
|
||||
* ```cpp
|
||||
* if ! consteval {
|
||||
* ...
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
class NotConstevalIfStmt extends ConstevalOrNotConstevalIfStmt, @stmt_not_consteval_if {
|
||||
override string getAPrimaryQlClass() { result = "NotConstevalIfStmt" }
|
||||
|
||||
override string toString() { result = "if ! consteval ..." }
|
||||
|
||||
override Stmt getCompileTimeEvaluatedBranch() { result = this.getElse() }
|
||||
|
||||
override Stmt getRuntimeEvaluatedBranch() { result = this.getThen() }
|
||||
}
|
||||
|
||||
private class TLoop = @stmt_while or @stmt_end_test_while or @stmt_range_based_for or @stmt_for;
|
||||
|
||||
/**
|
||||
|
||||
@@ -24138,7 +24138,7 @@ ir23.cpp:
|
||||
# 10| [TopLevelFunction] bool consteval_2()
|
||||
# 10| <params>:
|
||||
# 11| getEntryPoint(): [BlockStmt] { ... }
|
||||
# 12| getStmt(0): [NotConstevalIfStmt] if ! consteval ...
|
||||
# 12| getStmt(0): [ConstevalIfStmt] if ! consteval ...
|
||||
# 12| getThen(): [BlockStmt] { ... }
|
||||
# 13| getStmt(0): [ReturnStmt] return ...
|
||||
# 13| getExpr(): [Literal] 1
|
||||
@@ -24169,7 +24169,7 @@ ir23.cpp:
|
||||
# 28| [TopLevelFunction] bool consteval_4()
|
||||
# 28| <params>:
|
||||
# 29| getEntryPoint(): [BlockStmt] { ... }
|
||||
# 30| getStmt(0): [NotConstevalIfStmt] if ! consteval ...
|
||||
# 30| getStmt(0): [ConstevalIfStmt] if ! consteval ...
|
||||
# 30| getThen(): [BlockStmt] { ... }
|
||||
# 31| getStmt(0): [ReturnStmt] return ...
|
||||
# 31| getExpr(): [Literal] 1
|
||||
@@ -24192,7 +24192,7 @@ ir23.cpp:
|
||||
# 39| Type = [BoolType] bool
|
||||
# 39| Value = [Literal] 1
|
||||
# 39| ValueCategory = prvalue
|
||||
# 41| getStmt(1): [NotConstevalIfStmt] if ! consteval ...
|
||||
# 41| getStmt(1): [ConstevalIfStmt] if ! consteval ...
|
||||
# 41| getThen(): [BlockStmt] { ... }
|
||||
# 42| getStmt(0): [ExprStmt] ExprStmt
|
||||
# 42| getExpr(): [AssignExpr] ... = ...
|
||||
|
||||
Reference in New Issue
Block a user