Skip to content

Commit 6b87a58

Browse files
committed
C++: Require orphaned variables when computing compiler-generated initializers.
1 parent e0ac3d5 commit 6b87a58

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,13 @@ class Initializer extends ControlFlowNode, @initialiser {
5656
predicate isBraced() { braced_initialisers(underlyingElement(this)) }
5757

5858
/** Holds if this initializer is generated by the compiler. */
59-
predicate isCompilerGenerated() { this.getDeclaration().(Variable).isCompilerGenerated() }
59+
predicate isCompilerGenerated() {
60+
exists(Variable v |
61+
v = this.getDeclaration() and
62+
// We require the variable to be an orphan to not mark the initializer
63+
// from a desugared ranged for loop as compiler generated.
64+
orphaned_variables(unresolveElement(v), _) and
65+
v.isCompilerGenerated()
66+
)
67+
}
6068
}

cpp/ql/test/library-tests/compiler_generated/compilerGenerated.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@
3232
| cpp.cpp:31:5:33:5 | declaration | Stmt |
3333
| cpp.cpp:31:5:33:5 | declaration | Stmt |
3434
| cpp.cpp:31:17:31:18 | (reference to) | Expr |
35-
| cpp.cpp:31:17:31:18 | vs | Expr |
3635
| cpp.cpp:34:1:34:1 | return ... | Stmt |
3736
| file://:0:0:0:0 | (__begin) | Expr |
3837
| file://:0:0:0:0 | (__begin) | Expr |
3938
| file://:0:0:0:0 | (__begin) | Expr |
4039
| file://:0:0:0:0 | (__end) | Expr |
41-
| file://:0:0:0:0 | (__range) | Expr |
4240
| file://:0:0:0:0 | (reference dereference) | Expr |
4341
| file://:0:0:0:0 | (reference dereference) | Expr |
4442
| file://:0:0:0:0 | * ... | Expr |
45-
| file://:0:0:0:0 | ... + ... | Expr |
4643
| file://:0:0:0:0 | array to pointer conversion | Expr |
4744
| file://:0:0:0:0 | array to pointer conversion | Expr |
48-
| file://:0:0:0:0 | initializer for (__begin) | Initializer |
49-
| file://:0:0:0:0 | initializer for (__end) | Initializer |
50-
| file://:0:0:0:0 | initializer for (__range) | Initializer |
5145
| file://:0:0:0:0 | operator delete | Function |
5246
| file://:0:0:0:0 | operator new | Function |

0 commit comments

Comments
 (0)