Skip to content

Commit fb0a848

Browse files
author
Robert Marsh
committed
C++: fix inconsistency with global var constructor
1 parent 9d4aac6 commit fb0a848

File tree

10 files changed

+28
-51
lines changed

10 files changed

+28
-51
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class Expr extends StmtParent, @expr {
4949
/** Gets the enclosing variable of this expression, if any. */
5050
Variable getEnclosingVariable() { result = exprEnclosingElement(this) }
5151

52+
/** Gets the enclosing variable or function of this expression. */
53+
Declaration getEnclosingDeclaration() { result = exprEnclosingElement(this) }
54+
5255
/** Gets a child of this expression. */
5356
Expr getAChild() { exists(int n | result = this.getChild(n)) }
5457

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ abstract class TranslatedSideEffects extends TranslatedElement {
180180
/** DEPRECATED: Alias for getAst */
181181
deprecated override Locatable getAST() { result = getAst() }
182182

183-
final override Function getFunction() { result = getExpr().getEnclosingFunction() }
183+
final override Declaration getFunction() { result = getExpr().getEnclosingDeclaration() }
184184

185185
final override TranslatedElement getChild(int i) {
186186
result =
@@ -375,7 +375,7 @@ abstract class TranslatedSideEffect extends TranslatedElement {
375375
kind instanceof GotoEdge
376376
}
377377

378-
final override Function getFunction() { result = getParent().getFunction() }
378+
final override Declaration getFunction() { result = getParent().getFunction() }
379379

380380
final override Instruction getPrimaryInstructionForSideEffect(InstructionTag tag) {
381381
tag = OnlyInstructionTag() and
@@ -436,13 +436,6 @@ abstract class TranslatedArgumentSideEffect extends TranslatedSideEffect {
436436
result = index
437437
}
438438

439-
/**
440-
* Gets the `TranslatedFunction` containing this expression.
441-
*/
442-
final TranslatedFunction getEnclosingFunction() {
443-
result = getTranslatedFunction(call.getEnclosingFunction())
444-
}
445-
446439
final override predicate sideEffectInstruction(Opcode opcode, CppType type) {
447440
opcode = sideEffectOpcode and
448441
(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ abstract class TranslatedExpr extends TranslatedElement {
8181
deprecated override Locatable getAST() { result = this.getAst() }
8282

8383
final override Declaration getFunction() {
84-
result = expr.getEnclosingFunction() or
85-
result = expr.getEnclosingVariable().(GlobalOrNamespaceVariable)
84+
result = expr.getEnclosingDeclaration()
8685
}
8786

8887
/**

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ unexplainedLoop
1313
unnecessaryPhiInstruction
1414
memoryOperandDefinitionIsUnmodeled
1515
operandAcrossFunctions
16-
| ir.cpp:1705:18:1705:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_4' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1705:18:1705:25 | constructor_only global_4 | constructor_only global_4 |
17-
| ir.cpp:1707:18:1707:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_5' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1707:18:1707:25 | constructor_only global_5 | constructor_only global_5 |
1816
instructionWithoutUniqueBlock
1917
containsLoopOfForwardEdges
2018
lostReachability
@@ -25,10 +23,6 @@ notMarkedAsConflated
2523
wronglyMarkedAsConflated
2624
invalidOverlap
2725
nonUniqueEnclosingIRFunction
28-
| ir.cpp:1705:27:1705:28 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
29-
| ir.cpp:1705:27:1705:28 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
30-
| ir.cpp:1707:28:1707:47 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
31-
| ir.cpp:1707:28:1707:47 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
3226
fieldAddressOnNonPointer
3327
thisArgumentIsNonPointer
3428
missingCanonicalLanguageType

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ unexplainedLoop
1313
unnecessaryPhiInstruction
1414
memoryOperandDefinitionIsUnmodeled
1515
operandAcrossFunctions
16-
| ir.cpp:1705:18:1705:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_4' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1705:18:1705:25 | constructor_only global_4 | constructor_only global_4 |
17-
| ir.cpp:1707:18:1707:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_5' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1707:18:1707:25 | constructor_only global_5 | constructor_only global_5 |
1816
instructionWithoutUniqueBlock
1917
containsLoopOfForwardEdges
2018
lostReachability
@@ -25,10 +23,6 @@ notMarkedAsConflated
2523
wronglyMarkedAsConflated
2624
invalidOverlap
2725
nonUniqueEnclosingIRFunction
28-
| ir.cpp:1705:27:1705:28 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
29-
| ir.cpp:1705:27:1705:28 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
30-
| ir.cpp:1707:28:1707:47 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
31-
| ir.cpp:1707:28:1707:47 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
3226
fieldAddressOnNonPointer
3327
thisArgumentIsNonPointer
3428
missingCanonicalLanguageType

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7735,15 +7735,23 @@
77357735
| ir.cpp:1703:22:1703:22 | StoreValue | r1703_4 |
77367736
| ir.cpp:1705:18:1705:25 | Address | &:r1705_3 |
77377737
| ir.cpp:1705:18:1705:25 | Arg(this) | this:r1705_3 |
7738-
| ir.cpp:1705:18:1705:25 | SideEffect | ~m1705_2 |
7738+
| ir.cpp:1705:18:1705:25 | SideEffect | ~m1705_10 |
77397739
| ir.cpp:1705:27:1705:27 | Arg(0) | 0:r1705_5 |
77407740
| ir.cpp:1705:27:1705:28 | CallTarget | func:r1705_4 |
7741-
| ir.cpp:1705:27:1705:28 | SideEffect | ~m? |
7741+
| ir.cpp:1705:27:1705:28 | ChiPartial | partial:m1705_7 |
7742+
| ir.cpp:1705:27:1705:28 | ChiPartial | partial:m1705_9 |
7743+
| ir.cpp:1705:27:1705:28 | ChiTotal | total:m1705_2 |
7744+
| ir.cpp:1705:27:1705:28 | ChiTotal | total:m1705_8 |
7745+
| ir.cpp:1705:27:1705:28 | SideEffect | ~m1705_2 |
77427746
| ir.cpp:1707:18:1707:25 | Address | &:r1707_3 |
77437747
| ir.cpp:1707:18:1707:25 | Arg(this) | this:r1707_3 |
7744-
| ir.cpp:1707:18:1707:25 | SideEffect | ~m1707_2 |
7748+
| ir.cpp:1707:18:1707:25 | SideEffect | ~m1707_10 |
77457749
| ir.cpp:1707:28:1707:47 | CallTarget | func:r1707_4 |
7746-
| ir.cpp:1707:28:1707:47 | SideEffect | ~m? |
7750+
| ir.cpp:1707:28:1707:47 | ChiPartial | partial:m1707_7 |
7751+
| ir.cpp:1707:28:1707:47 | ChiPartial | partial:m1707_9 |
7752+
| ir.cpp:1707:28:1707:47 | ChiTotal | total:m1707_2 |
7753+
| ir.cpp:1707:28:1707:47 | ChiTotal | total:m1707_8 |
7754+
| ir.cpp:1707:28:1707:47 | SideEffect | ~m1707_2 |
77477755
| ir.cpp:1707:46:1707:46 | Arg(0) | 0:r1707_5 |
77487756
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
77497757
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ unexplainedLoop
2020
unnecessaryPhiInstruction
2121
memoryOperandDefinitionIsUnmodeled
2222
operandAcrossFunctions
23-
| ir.cpp:1705:18:1705:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_4' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1705:18:1705:25 | constructor_only global_4 | constructor_only global_4 |
24-
| ir.cpp:1707:18:1707:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_5' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1707:18:1707:25 | constructor_only global_5 | constructor_only global_5 |
2523
instructionWithoutUniqueBlock
2624
containsLoopOfForwardEdges
2725
lostReachability
@@ -52,10 +50,6 @@ notMarkedAsConflated
5250
wronglyMarkedAsConflated
5351
invalidOverlap
5452
nonUniqueEnclosingIRFunction
55-
| ir.cpp:1705:27:1705:28 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
56-
| ir.cpp:1705:27:1705:28 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
57-
| ir.cpp:1707:28:1707:47 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
58-
| ir.cpp:1707:28:1707:47 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
5953
fieldAddressOnNonPointer
6054
thisArgumentIsNonPointer
6155
missingCanonicalLanguageType

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9048,9 +9048,11 @@ ir.cpp:
90489048
# 1705| r1705_4(glval<unknown>) = FunctionAddress[constructor_only] :
90499049
# 1705| r1705_5(int) = Constant[1] :
90509050
# 1705| v1705_6(void) = Call[constructor_only] : func:r1705_4, this:r1705_3, 0:r1705_5
9051-
# 1705| v1705_7(void) = ReturnVoid :
9052-
# 1705| v1705_8(void) = AliasedUse : ~m?
9053-
# 1705| v1705_9(void) = ExitFunction :
9051+
# 1705| mu1705_7(unknown) = ^CallSideEffect : ~m?
9052+
# 1705| mu1705_8(constructor_only) = ^IndirectMayWriteSideEffect[-1] : &:r1705_3
9053+
# 1705| v1705_9(void) = ReturnVoid :
9054+
# 1705| v1705_10(void) = AliasedUse : ~m?
9055+
# 1705| v1705_11(void) = ExitFunction :
90549056

90559057
# 1707| constructor_only global_5
90569058
# 1707| Block 0
@@ -9060,9 +9062,11 @@ ir.cpp:
90609062
# 1707| r1707_4(glval<unknown>) = FunctionAddress[constructor_only] :
90619063
# 1707| r1707_5(int) = Constant[2] :
90629064
# 1707| v1707_6(void) = Call[constructor_only] : func:r1707_4, this:r1707_3, 0:r1707_5
9063-
# 1707| v1707_7(void) = ReturnVoid :
9064-
# 1707| v1707_8(void) = AliasedUse : ~m?
9065-
# 1707| v1707_9(void) = ExitFunction :
9065+
# 1707| mu1707_7(unknown) = ^CallSideEffect : ~m?
9066+
# 1707| mu1707_8(constructor_only) = ^IndirectMayWriteSideEffect[-1] : &:r1707_3
9067+
# 1707| v1707_9(void) = ReturnVoid :
9068+
# 1707| v1707_10(void) = AliasedUse : ~m?
9069+
# 1707| v1707_11(void) = ExitFunction :
90669070

90679071
perf-regression.cpp:
90689072
# 6| void Big::Big()

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ unexplainedLoop
1313
unnecessaryPhiInstruction
1414
memoryOperandDefinitionIsUnmodeled
1515
operandAcrossFunctions
16-
| ir.cpp:1705:18:1705:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_4' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1705:18:1705:25 | constructor_only global_4 | constructor_only global_4 |
17-
| ir.cpp:1707:18:1707:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_5' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1707:18:1707:25 | constructor_only global_5 | constructor_only global_5 |
1816
instructionWithoutUniqueBlock
1917
containsLoopOfForwardEdges
2018
lostReachability
@@ -25,10 +23,6 @@ notMarkedAsConflated
2523
wronglyMarkedAsConflated
2624
invalidOverlap
2725
nonUniqueEnclosingIRFunction
28-
| ir.cpp:1705:27:1705:28 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
29-
| ir.cpp:1705:27:1705:28 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
30-
| ir.cpp:1707:28:1707:47 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
31-
| ir.cpp:1707:28:1707:47 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
3226
fieldAddressOnNonPointer
3327
thisArgumentIsNonPointer
3428
missingCanonicalLanguageType

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ unexplainedLoop
1313
unnecessaryPhiInstruction
1414
memoryOperandDefinitionIsUnmodeled
1515
operandAcrossFunctions
16-
| ir.cpp:1705:18:1705:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_4' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1705:18:1705:25 | constructor_only global_4 | constructor_only global_4 |
17-
| ir.cpp:1707:18:1707:25 | Address | Operand 'Address' is used on instruction 'IndirectMayWriteSideEffect: call to constructor_only' in function '$@', but is defined on instruction 'VariableAddress: global_5' in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> | ir.cpp:1707:18:1707:25 | constructor_only global_5 | constructor_only global_5 |
1816
instructionWithoutUniqueBlock
1917
containsLoopOfForwardEdges
2018
lostReachability
@@ -25,10 +23,6 @@ notMarkedAsConflated
2523
wronglyMarkedAsConflated
2624
invalidOverlap
2725
nonUniqueEnclosingIRFunction
28-
| ir.cpp:1705:27:1705:28 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
29-
| ir.cpp:1705:27:1705:28 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
30-
| ir.cpp:1707:28:1707:47 | CallSideEffect: call to constructor_only | Instruction 'CallSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
31-
| ir.cpp:1707:28:1707:47 | IndirectMayWriteSideEffect: call to constructor_only | Instruction 'IndirectMayWriteSideEffect: call to constructor_only' has 0 results for `getEnclosingIRFunction()` in function '$@'. | file://:0:0:0:0 | <Missing IRFunction> | <Missing IRFunction> |
3226
fieldAddressOnNonPointer
3327
thisArgumentIsNonPointer
3428
missingCanonicalLanguageType

0 commit comments

Comments
 (0)