Skip to content

Commit 4b85ea3

Browse files
authored
Merge pull request github#18502 from jketema/consteval
C++: Support `if consteval` and `if ! consteval`
2 parents ef034bc + a9e0f20 commit 4b85ea3

File tree

25 files changed

+11929
-1216
lines changed

25 files changed

+11929
-1216
lines changed

cpp/downgrades/1aa71a4a687fc93f807d4dfeeef70feceeced242/old.dbscheme

Lines changed: 2429 additions & 0 deletions
Large diffs are not rendered by default.

cpp/downgrades/1aa71a4a687fc93f807d4dfeeef70feceeced242/semmlecode.cpp.dbscheme

Lines changed: 2415 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Stmt extends @stmt {
2+
string toString() { none() }
3+
}
4+
5+
class Location extends @location_stmt {
6+
string toString() { none() }
7+
}
8+
9+
predicate isConstevalIf(Stmt stmt) {
10+
exists(int kind | stmts(stmt, kind, _) | kind = 38 or kind = 39)
11+
}
12+
13+
from Stmt stmt, int kind, int kind_new, Location location
14+
where
15+
stmts(stmt, kind, location) and
16+
if isConstevalIf(stmt) then kind_new = 7 else kind_new = kind // Turns consteval if into a block with two block statements in it
17+
select stmt, kind_new, location
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
description: Support (not) consteval if
2+
compatibility: full
3+
consteval_if_then.rel: delete
4+
consteval_if_else.rel: delete
5+
stmts.rel: run stmts.qlo
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,10 @@ private predicate namedStmtChildPredicates(Locatable s, Element e, string pred)
912912
or
913913
s.(ConstexprIfStmt).getElse() = e and pred = "getElse()"
914914
or
915+
s.(ConstevalIfStmt).getThen() = e and pred = "getThen()"
916+
or
917+
s.(ConstevalIfStmt).getElse() = e and pred = "getElse()"
918+
or
915919
s.(Handler).getParameter() = e and pred = "getParameter()"
916920
or
917921
s.(IfStmt).getInitialization() = e and pred = "getInitialization()"

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,25 @@ private predicate subEdge(Pos p1, Node n1, Node n2, Pos p2) {
876876
p2.nodeAfter(n2, s)
877877
)
878878
or
879+
// NotConstevalIfStmt -> { then, else } ->
880+
exists(ConstevalIfStmt s |
881+
p1.nodeAt(n1, s) and
882+
p2.nodeBefore(n2, s.getThen())
883+
or
884+
p1.nodeAt(n1, s) and
885+
p2.nodeBefore(n2, s.getElse())
886+
or
887+
p1.nodeAt(n1, s) and
888+
not exists(s.getElse()) and
889+
p2.nodeAfter(n2, s)
890+
or
891+
p1.nodeAfter(n1, s.getThen()) and
892+
p2.nodeAfter(n2, s)
893+
or
894+
p1.nodeAfter(n1, s.getElse()) and
895+
p2.nodeAfter(n2, s)
896+
)
897+
or
879898
// WhileStmt -> condition ; body -> condition ; after dtors -> after
880899
exists(WhileStmt s |
881900
p1.nodeAt(n1, s) and

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstract class TranslatedFlexibleCondition extends TranslatedCondition, Conditio
4747
{
4848
TranslatedFlexibleCondition() { this = TTranslatedFlexibleCondition(expr) }
4949

50-
final override predicate handlesDestructorsExplicitly() { none() } // TODO: this needs to be revisted when we get unnamed destructors
50+
final override predicate handlesDestructorsExplicitly() { none() } // TODO: this needs to be revisited when we get unnamed destructors
5151

5252
final override TranslatedElement getChild(int id) { id = 0 and result = this.getOperand() }
5353

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private predicate ignoreExpr(Expr expr) {
166166
}
167167

168168
/**
169-
* Holds if the side effects of `expr` should be ignoredf for the purposes of IR generation.
169+
* Holds if the side effects of `expr` should be ignored for the purposes of IR generation.
170170
*
171171
* In cases involving `constexpr`, a call can wind up as a constant expression. `ignoreExpr()` will
172172
* not hold for such a call, since we do need to translate the call (as a constant), but we need to

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,61 @@ class TranslatedConstExprIfStmt extends TranslatedIfLikeStmt {
10981098
override predicate hasElse() { exists(stmt.getElse()) }
10991099
}
11001100

1101+
class TranslatedConstevalIfStmt extends TranslatedStmt {
1102+
override ConstevalIfStmt stmt;
1103+
1104+
override Instruction getFirstInstruction(EdgeKind kind) {
1105+
if not this.hasEvaluatedBranch()
1106+
then
1107+
kind instanceof GotoEdge and
1108+
result = this.getInstruction(OnlyInstructionTag())
1109+
else result = this.getEvaluatedBranch().getFirstInstruction(kind)
1110+
}
1111+
1112+
override TranslatedElement getChildInternal(int id) {
1113+
id = 0 and
1114+
result = this.getThen()
1115+
or
1116+
id = 1 and
1117+
result = this.getElse()
1118+
}
1119+
1120+
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
1121+
not this.hasEvaluatedBranch() and
1122+
opcode instanceof Opcode::NoOp and
1123+
tag = OnlyInstructionTag() and
1124+
resultType = getVoidType()
1125+
}
1126+
1127+
override Instruction getALastInstructionInternal() {
1128+
if not this.hasEvaluatedBranch()
1129+
then result = this.getInstruction(OnlyInstructionTag())
1130+
else result = this.getEvaluatedBranch().getALastInstruction()
1131+
}
1132+
1133+
override TranslatedElement getLastChild() { result = this.getEvaluatedBranch() }
1134+
1135+
override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
1136+
tag = OnlyInstructionTag() and
1137+
result = this.getParent().getChildSuccessor(this, kind)
1138+
}
1139+
1140+
override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
1141+
(child = this.getThen() or child = this.getElse()) and
1142+
result = this.getParent().getChildSuccessor(this, kind)
1143+
}
1144+
1145+
TranslatedStmt getEvaluatedBranch() {
1146+
result = getTranslatedStmt(stmt.getRuntimeEvaluatedBranch())
1147+
}
1148+
1149+
predicate hasEvaluatedBranch() { stmt.hasRuntimeEvaluatedBranch() }
1150+
1151+
TranslatedStmt getThen() { result = getTranslatedStmt(stmt.getThen()) }
1152+
1153+
TranslatedStmt getElse() { result = getTranslatedStmt(stmt.getElse()) }
1154+
}
1155+
11011156
abstract class TranslatedLoop extends TranslatedStmt, ConditionContext {
11021157
override Loop stmt;
11031158

0 commit comments

Comments
 (0)