Skip to content

Commit a9e0f20

Browse files
committed
C++: Simplify consteval if to be just a single class with an isNot predicate
1 parent a74189f commit a9e0f20

File tree

6 files changed

+37
-51
lines changed

6 files changed

+37
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
category: feature
33
---
4-
* New classes `ConstevalIfStmt` and `NotConstevalIfStmt` were introduced, which represent the C++23 `if consteval` and `if ! consteval` statements.
4+
* A new class `ConstevalIfStmt` was introduced, which represents the C++23 `if consteval` and `if ! consteval` statements.

cpp/ql/lib/semmle/code/cpp/PrintAST.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -912,9 +912,9 @@ private predicate namedStmtChildPredicates(Locatable s, Element e, string pred)
912912
or
913913
s.(ConstexprIfStmt).getElse() = e and pred = "getElse()"
914914
or
915-
s.(ConstevalOrNotConstevalIfStmt).getThen() = e and pred = "getThen()"
915+
s.(ConstevalIfStmt).getThen() = e and pred = "getThen()"
916916
or
917-
s.(ConstevalOrNotConstevalIfStmt).getElse() = e and pred = "getElse()"
917+
s.(ConstevalIfStmt).getElse() = e and pred = "getElse()"
918918
or
919919
s.(Handler).getParameter() = e and pred = "getParameter()"
920920
or

cpp/ql/lib/semmle/code/cpp/controlflow/internal/CFG.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,8 @@ private predicate subEdge(Pos p1, Node n1, Node n2, Pos p2) {
876876
p2.nodeAfter(n2, s)
877877
)
878878
or
879-
// ConstevalOrNotConstevalIfStmt -> { then, else } ->
880-
exists(ConstevalOrNotConstevalIfStmt s |
879+
// NotConstevalIfStmt -> { then, else } ->
880+
exists(ConstevalIfStmt s |
881881
p1.nodeAt(n1, s) and
882882
p2.nodeBefore(n2, s.getThen())
883883
or

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedStmt.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,8 @@ class TranslatedConstExprIfStmt extends TranslatedIfLikeStmt {
10981098
override predicate hasElse() { exists(stmt.getElse()) }
10991099
}
11001100

1101-
class TranslatedConstevalOrNotConstevalIfStmt extends TranslatedStmt {
1102-
override ConstevalOrNotConstevalIfStmt stmt;
1101+
class TranslatedConstevalIfStmt extends TranslatedStmt {
1102+
override ConstevalIfStmt stmt;
11031103

11041104
override Instruction getFirstInstruction(EdgeKind kind) {
11051105
if not this.hasEvaluatedBranch()

cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll

+27-41
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,27 @@ class ConstexprIfStmt extends ConditionalStmt, @stmt_constexpr_if {
446446
* }
447447
* ```
448448
*/
449-
class ConstevalOrNotConstevalIfStmt extends Stmt, @stmt_consteval_or_not_consteval_if {
449+
class ConstevalIfStmt extends Stmt, @stmt_consteval_or_not_consteval_if {
450+
override string getAPrimaryQlClass() { result = "ConstevalIfStmt" }
451+
452+
override string toString() {
453+
if this.isNot() then result = "if ! consteval ..." else result = "if consteval ..."
454+
}
455+
456+
/**
457+
* Holds if this is a 'not consteval if' statement.
458+
*
459+
* For example, this holds for
460+
* ```cpp
461+
* if ! consteval { return true; }
462+
* ```
463+
* but not for
464+
* ```cpp
465+
* if consteval { return true; }
466+
* ```
467+
*/
468+
predicate isNot() { this instanceof @stmt_not_consteval_if }
469+
450470
/**
451471
* Gets the 'then' statement of this '(not) consteval if' statement.
452472
*
@@ -515,7 +535,9 @@ class ConstevalOrNotConstevalIfStmt extends Stmt, @stmt_consteval_or_not_constev
515535
* ```
516536
* there is no result.
517537
*/
518-
Stmt getCompileTimeEvaluatedBranch() { none() }
538+
Stmt getCompileTimeEvaluatedBranch() {
539+
if this.isNot() then result = this.getElse() else result = this.getThen()
540+
}
519541

520542
/**
521543
* 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
544566
* ```
545567
* there is no result.
546568
*/
547-
Stmt getRuntimeEvaluatedBranch() { none() }
569+
Stmt getRuntimeEvaluatedBranch() {
570+
if this.isNot() then result = this.getThen() else result = this.getElse()
571+
}
548572

549573
/**
550574
* 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
561585
predicate hasRuntimeEvaluatedBranch() { exists(this.getRuntimeEvaluatedBranch()) }
562586
}
563587

564-
/**
565-
* A C/C++ 'consteval if'. For example, the `if consteval` statement
566-
* in the following code:
567-
* ```cpp
568-
* if consteval {
569-
* ...
570-
* }
571-
* ```
572-
*/
573-
class ConstevalIfStmt extends ConstevalOrNotConstevalIfStmt, @stmt_consteval_if {
574-
override string getAPrimaryQlClass() { result = "ConstevalIfStmt" }
575-
576-
override string toString() { result = "if consteval ..." }
577-
578-
override Stmt getCompileTimeEvaluatedBranch() { result = this.getThen() }
579-
580-
override Stmt getRuntimeEvaluatedBranch() { result = this.getElse() }
581-
}
582-
583-
/**
584-
* A C/C++ 'not consteval if'. For example, the `if ! consteval` statement
585-
* in the following code:
586-
* ```cpp
587-
* if ! consteval {
588-
* ...
589-
* }
590-
* ```
591-
*/
592-
class NotConstevalIfStmt extends ConstevalOrNotConstevalIfStmt, @stmt_not_consteval_if {
593-
override string getAPrimaryQlClass() { result = "NotConstevalIfStmt" }
594-
595-
override string toString() { result = "if ! consteval ..." }
596-
597-
override Stmt getCompileTimeEvaluatedBranch() { result = this.getElse() }
598-
599-
override Stmt getRuntimeEvaluatedBranch() { result = this.getThen() }
600-
}
601-
602588
private class TLoop = @stmt_while or @stmt_end_test_while or @stmt_range_based_for or @stmt_for;
603589

604590
/**

cpp/ql/test/library-tests/ir/ir/PrintAST.expected

+3-3
Original file line numberDiff line numberDiff line change
@@ -24138,7 +24138,7 @@ ir23.cpp:
2413824138
# 10| [TopLevelFunction] bool consteval_2()
2413924139
# 10| <params>:
2414024140
# 11| getEntryPoint(): [BlockStmt] { ... }
24141-
# 12| getStmt(0): [NotConstevalIfStmt] if ! consteval ...
24141+
# 12| getStmt(0): [ConstevalIfStmt] if ! consteval ...
2414224142
# 12| getThen(): [BlockStmt] { ... }
2414324143
# 13| getStmt(0): [ReturnStmt] return ...
2414424144
# 13| getExpr(): [Literal] 1
@@ -24169,7 +24169,7 @@ ir23.cpp:
2416924169
# 28| [TopLevelFunction] bool consteval_4()
2417024170
# 28| <params>:
2417124171
# 29| getEntryPoint(): [BlockStmt] { ... }
24172-
# 30| getStmt(0): [NotConstevalIfStmt] if ! consteval ...
24172+
# 30| getStmt(0): [ConstevalIfStmt] if ! consteval ...
2417324173
# 30| getThen(): [BlockStmt] { ... }
2417424174
# 31| getStmt(0): [ReturnStmt] return ...
2417524175
# 31| getExpr(): [Literal] 1
@@ -24192,7 +24192,7 @@ ir23.cpp:
2419224192
# 39| Type = [BoolType] bool
2419324193
# 39| Value = [Literal] 1
2419424194
# 39| ValueCategory = prvalue
24195-
# 41| getStmt(1): [NotConstevalIfStmt] if ! consteval ...
24195+
# 41| getStmt(1): [ConstevalIfStmt] if ! consteval ...
2419624196
# 41| getThen(): [BlockStmt] { ... }
2419724197
# 42| getStmt(0): [ExprStmt] ExprStmt
2419824198
# 42| getExpr(): [AssignExpr] ... = ...

0 commit comments

Comments
 (0)