Skip to content

Commit 0ee3c99

Browse files
authored
Merge pull request github#15539 from MathiasVP/fix-ir-gen-for-conditional-decl-expr-without-var-access
C++: Fix IR generation when `ConditionDeclExpr` does not have an immediate `VariableAccess`
2 parents 23677b2 + df181f2 commit 0ee3c99

14 files changed

+144
-47
lines changed

cpp/ql/lib/semmle/code/cpp/exprs/Assignment.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,15 @@ class ConditionDeclExpr extends Expr, @condition_decl {
244244

245245
/**
246246
* Gets the compiler-generated variable access that conceptually occurs after
247-
* the initialization of the declared variable.
247+
* the initialization of the declared variable, if any.
248248
*/
249-
VariableAccess getVariableAccess() { result = this.getChild(0) }
249+
VariableAccess getVariableAccess() { result = this.getExpr() }
250+
251+
/**
252+
* Gets the expression that is evaluated after the initialization of the declared
253+
* variable.
254+
*/
255+
Expr getExpr() { result = this.getChild(0) }
250256

251257
/**
252258
* Gets the expression that initializes the declared variable. This predicate

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3173,7 +3173,7 @@ class TranslatedConditionDeclExpr extends TranslatedNonConstantExpr {
31733173
private TranslatedConditionDecl getDecl() { result = getTranslatedConditionDecl(expr) }
31743174

31753175
private TranslatedExpr getConditionExpr() {
3176-
result = getTranslatedExpr(expr.getVariableAccess().getFullyConverted())
3176+
result = getTranslatedExpr(expr.getExpr().getFullyConverted())
31773177
}
31783178
}
31793179

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16132,6 +16132,31 @@ ir.cpp:
1613216132
# 2112| getExpr(): [VariableAccess] end
1613316133
# 2112| Type = [CharPointerType] char *
1613416134
# 2112| ValueCategory = prvalue(load)
16135+
# 2115| [CopyAssignmentOperator] HasOperatorBool& HasOperatorBool::operator=(HasOperatorBool const&)
16136+
# 2115| <params>:
16137+
#-----| getParameter(0): [Parameter] (unnamed parameter 0)
16138+
#-----| Type = [LValueReferenceType] const HasOperatorBool &
16139+
# 2115| [MoveAssignmentOperator] HasOperatorBool& HasOperatorBool::operator=(HasOperatorBool&&)
16140+
# 2115| <params>:
16141+
#-----| getParameter(0): [Parameter] (unnamed parameter 0)
16142+
#-----| Type = [RValueReferenceType] HasOperatorBool &&
16143+
# 2116| [ConversionOperator] bool HasOperatorBool::operator bool()
16144+
# 2116| <params>:
16145+
# 2119| [TopLevelFunction] void call_as_child_of_ConditionDeclExpr()
16146+
# 2119| <params>:
16147+
# 2119| getEntryPoint(): [BlockStmt] { ... }
16148+
# 2120| getStmt(0): [IfStmt] if (...) ...
16149+
# 2120| getCondition(): [ConditionDeclExpr] (condition decl)
16150+
# 2120| Type = [BoolType] bool
16151+
# 2120| ValueCategory = prvalue
16152+
# 2120| getChild(0): [FunctionCall] call to operator bool
16153+
# 2120| Type = [BoolType] bool
16154+
# 2120| ValueCategory = prvalue
16155+
# 2120| getQualifier(): [VariableAccess] b
16156+
# 2120| Type = [Struct] HasOperatorBool
16157+
# 2120| ValueCategory = prvalue(load)
16158+
# 2120| getThen(): [BlockStmt] { ... }
16159+
# 2121| getStmt(1): [ReturnStmt] return ...
1613516160
perf-regression.cpp:
1613616161
# 4| [CopyAssignmentOperator] Big& Big::operator=(Big const&)
1613716162
# 4| <params>:

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12441,6 +12441,38 @@ ir.cpp:
1244112441
# 2109| v2109_12(void) = AliasedUse : m2109_3
1244212442
# 2109| v2109_13(void) = ExitFunction :
1244312443

12444+
# 2119| void call_as_child_of_ConditionDeclExpr()
12445+
# 2119| Block 0
12446+
# 2119| v2119_1(void) = EnterFunction :
12447+
# 2119| m2119_2(unknown) = AliasedDefinition :
12448+
# 2119| m2119_3(unknown) = InitializeNonLocal :
12449+
# 2119| m2119_4(unknown) = Chi : total:m2119_2, partial:m2119_3
12450+
# 2120| r2120_1(glval<HasOperatorBool>) = VariableAddress[b] :
12451+
# 2120| r2120_2(HasOperatorBool) = Constant[0] :
12452+
# 2120| m2120_3(HasOperatorBool) = Store[b] : &:r2120_1, r2120_2
12453+
# 2120| r2120_4(glval<HasOperatorBool>) = VariableAddress[b] :
12454+
# 2120| r2120_5(glval<unknown>) = FunctionAddress[operator bool] :
12455+
# 2120| r2120_6(bool) = Call[operator bool] : func:r2120_5, this:r2120_4
12456+
# 2120| m2120_7(unknown) = ^CallSideEffect : ~m2119_4
12457+
# 2120| m2120_8(unknown) = Chi : total:m2119_4, partial:m2120_7
12458+
# 2120| v2120_9(void) = ^IndirectReadSideEffect[-1] : &:r2120_4, m2120_3
12459+
# 2120| m2120_10(HasOperatorBool) = ^IndirectMayWriteSideEffect[-1] : &:r2120_4
12460+
# 2120| m2120_11(HasOperatorBool) = Chi : total:m2120_3, partial:m2120_10
12461+
# 2120| r2120_12(bool) = CopyValue : r2120_6
12462+
# 2120| v2120_13(void) = ConditionalBranch : r2120_12
12463+
#-----| False -> Block 2
12464+
#-----| True -> Block 1
12465+
12466+
# 2120| Block 1
12467+
# 2120| v2120_14(void) = NoOp :
12468+
#-----| Goto -> Block 2
12469+
12470+
# 2121| Block 2
12471+
# 2121| v2121_1(void) = NoOp :
12472+
# 2119| v2119_5(void) = ReturnVoid :
12473+
# 2119| v2119_6(void) = AliasedUse : ~m2120_8
12474+
# 2119| v2119_7(void) = ExitFunction :
12475+
1244412476
perf-regression.cpp:
1244512477
# 6| void Big::Big()
1244612478
# 6| Block 0

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ unnecessaryPhiInstruction
1212
memoryOperandDefinitionIsUnmodeled
1313
operandAcrossFunctions
1414
instructionWithoutUniqueBlock
15+
missingCanonicalLanguageType
16+
multipleCanonicalLanguageTypes
1517
containsLoopOfForwardEdges
18+
missingIRType
19+
multipleIRTypes
1620
lostReachability
1721
backEdgeCountMismatch
1822
useNotDominatedByDefinition
@@ -24,8 +28,4 @@ nonUniqueEnclosingIRFunction
2428
fieldAddressOnNonPointer
2529
thisArgumentIsNonPointer
2630
nonUniqueIRVariable
27-
missingCanonicalLanguageType
28-
multipleCanonicalLanguageTypes
29-
missingIRType
30-
multipleIRTypes
3131
missingCppType

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ unnecessaryPhiInstruction
1212
memoryOperandDefinitionIsUnmodeled
1313
operandAcrossFunctions
1414
instructionWithoutUniqueBlock
15+
missingCanonicalLanguageType
16+
multipleCanonicalLanguageTypes
1517
containsLoopOfForwardEdges
18+
missingIRType
19+
multipleIRTypes
1620
lostReachability
1721
backEdgeCountMismatch
1822
useNotDominatedByDefinition
@@ -24,8 +28,4 @@ nonUniqueEnclosingIRFunction
2428
fieldAddressOnNonPointer
2529
thisArgumentIsNonPointer
2630
nonUniqueIRVariable
27-
missingCanonicalLanguageType
28-
multipleCanonicalLanguageTypes
29-
missingIRType
30-
multipleIRTypes
3131
missingCppType

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,4 +2112,12 @@ char* test_strtod(char *s) {
21122112
return end;
21132113
}
21142114

2115+
struct HasOperatorBool {
2116+
operator bool();
2117+
};
2118+
2119+
void call_as_child_of_ConditionDeclExpr() {
2120+
if(HasOperatorBool b = HasOperatorBool()) {}
2121+
}
2122+
21152123
// semmle-extractor-options: -std=c++17 --clang

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10070,6 +10070,23 @@
1007010070
| ir.cpp:2112:10:2112:12 | Address | &:r2112_2 |
1007110071
| ir.cpp:2112:10:2112:12 | Load | m2111_11 |
1007210072
| ir.cpp:2112:10:2112:12 | StoreValue | r2112_3 |
10073+
| ir.cpp:2119:6:2119:39 | ChiPartial | partial:m2119_3 |
10074+
| ir.cpp:2119:6:2119:39 | ChiTotal | total:m2119_2 |
10075+
| ir.cpp:2119:6:2119:39 | SideEffect | ~m2120_8 |
10076+
| ir.cpp:2120:6:2120:42 | Address | &:r2120_1 |
10077+
| ir.cpp:2120:6:2120:42 | Condition | r2120_12 |
10078+
| ir.cpp:2120:22:2120:22 | Address | &:r2120_4 |
10079+
| ir.cpp:2120:22:2120:22 | Address | &:r2120_4 |
10080+
| ir.cpp:2120:22:2120:22 | Arg(this) | this:r2120_4 |
10081+
| ir.cpp:2120:22:2120:22 | CallTarget | func:r2120_5 |
10082+
| ir.cpp:2120:22:2120:22 | ChiPartial | partial:m2120_7 |
10083+
| ir.cpp:2120:22:2120:22 | ChiPartial | partial:m2120_10 |
10084+
| ir.cpp:2120:22:2120:22 | ChiTotal | total:m2119_4 |
10085+
| ir.cpp:2120:22:2120:22 | ChiTotal | total:m2120_3 |
10086+
| ir.cpp:2120:22:2120:22 | SideEffect | m2120_3 |
10087+
| ir.cpp:2120:22:2120:22 | SideEffect | ~m2119_4 |
10088+
| ir.cpp:2120:22:2120:22 | Unary | r2120_6 |
10089+
| ir.cpp:2120:25:2120:42 | StoreValue | r2120_2 |
1007310090
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
1007410091
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
1007510092
| perf-regression.cpp:6:3:6:5 | Address | &:r6_7 |

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11647,6 +11647,35 @@ ir.cpp:
1164711647
# 2109| v2109_11(void) = AliasedUse : ~m?
1164811648
# 2109| v2109_12(void) = ExitFunction :
1164911649

11650+
# 2119| void call_as_child_of_ConditionDeclExpr()
11651+
# 2119| Block 0
11652+
# 2119| v2119_1(void) = EnterFunction :
11653+
# 2119| mu2119_2(unknown) = AliasedDefinition :
11654+
# 2119| mu2119_3(unknown) = InitializeNonLocal :
11655+
# 2120| r2120_1(glval<HasOperatorBool>) = VariableAddress[b] :
11656+
# 2120| r2120_2(HasOperatorBool) = Constant[0] :
11657+
# 2120| mu2120_3(HasOperatorBool) = Store[b] : &:r2120_1, r2120_2
11658+
# 2120| r2120_4(glval<HasOperatorBool>) = VariableAddress[b] :
11659+
# 2120| r2120_5(glval<unknown>) = FunctionAddress[operator bool] :
11660+
# 2120| r2120_6(bool) = Call[operator bool] : func:r2120_5, this:r2120_4
11661+
# 2120| mu2120_7(unknown) = ^CallSideEffect : ~m?
11662+
# 2120| v2120_8(void) = ^IndirectReadSideEffect[-1] : &:r2120_4, ~m?
11663+
# 2120| mu2120_9(HasOperatorBool) = ^IndirectMayWriteSideEffect[-1] : &:r2120_4
11664+
# 2120| r2120_10(bool) = CopyValue : r2120_6
11665+
# 2120| v2120_11(void) = ConditionalBranch : r2120_10
11666+
#-----| False -> Block 2
11667+
#-----| True -> Block 1
11668+
11669+
# 2120| Block 1
11670+
# 2120| v2120_12(void) = NoOp :
11671+
#-----| Goto -> Block 2
11672+
11673+
# 2121| Block 2
11674+
# 2121| v2121_1(void) = NoOp :
11675+
# 2119| v2119_4(void) = ReturnVoid :
11676+
# 2119| v2119_5(void) = AliasedUse : ~m?
11677+
# 2119| v2119_6(void) = ExitFunction :
11678+
1165011679
perf-regression.cpp:
1165111680
# 6| void Big::Big()
1165211681
# 6| Block 0

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ unnecessaryPhiInstruction
1212
memoryOperandDefinitionIsUnmodeled
1313
operandAcrossFunctions
1414
instructionWithoutUniqueBlock
15+
missingCanonicalLanguageType
16+
multipleCanonicalLanguageTypes
1517
containsLoopOfForwardEdges
18+
missingIRType
19+
multipleIRTypes
1620
lostReachability
1721
backEdgeCountMismatch
1822
useNotDominatedByDefinition
@@ -24,8 +28,4 @@ nonUniqueEnclosingIRFunction
2428
fieldAddressOnNonPointer
2529
thisArgumentIsNonPointer
2630
nonUniqueIRVariable
27-
missingCanonicalLanguageType
28-
multipleCanonicalLanguageTypes
29-
missingIRType
30-
multipleIRTypes
3131
missingCppType

0 commit comments

Comments
 (0)