Skip to content

Commit

Permalink
C++: Simplify consteval if to be just a single class with an isNot
Browse files Browse the repository at this point in the history
…predicate
  • Loading branch information
jketema committed Jan 20, 2025
1 parent a74189f commit a9e0f20
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cpp/ql/lib/change-notes/2024-01-16-consteval-if.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions cpp/ql/lib/semmle/code/cpp/PrintAST.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cpp/ql/lib/semmle/code/cpp/controlflow/internal/CFG.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
68 changes: 27 additions & 41 deletions cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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;

/**
Expand Down
6 changes: 3 additions & 3 deletions cpp/ql/test/library-tests/ir/ir/PrintAST.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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] ... = ...
Expand Down

0 comments on commit a9e0f20

Please sign in to comment.