Skip to content

Commit 22b8e2c

Browse files
committed
C++: Add missing parent scope cases
1 parent bdec751 commit 22b8e2c

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Element extends ElementBase {
129129
* or certain kinds of `Statement`.
130130
*/
131131
Element getParentScope() {
132-
// result instanceof class
132+
// result instanceof Class
133133
exists(Declaration m |
134134
m = this and
135135
result = m.getDeclaringType() and
@@ -138,31 +138,40 @@ class Element extends ElementBase {
138138
or
139139
exists(TemplateClass tc | this = tc.getATemplateArgument() and result = tc)
140140
or
141-
// result instanceof namespace
141+
// result instanceof Namespace
142142
exists(Namespace n | result = n and n.getADeclaration() = this)
143143
or
144144
exists(FriendDecl d, Namespace n | this = d and n.getADeclaration() = d and result = n)
145145
or
146146
exists(Namespace n | this = n and result = n.getParentNamespace())
147147
or
148-
// result instanceof stmt
148+
// result instanceof Stmt
149149
exists(LocalVariable v |
150150
this = v and
151151
exists(DeclStmt ds | ds.getADeclaration() = v and result = ds.getParent())
152152
)
153153
or
154-
exists(Parameter p | this = p and result = p.getFunction())
154+
exists(Parameter p |
155+
this = p and
156+
(
157+
result = p.getFunction() or
158+
result = p.getCatchBlock().getParent().(Handler).getParent().(TryStmt).getParent() or
159+
result = p.getRequiresExpr().getEnclosingStmt().getParent()
160+
)
161+
)
155162
or
156163
exists(GlobalVariable g, Namespace n | this = g and n.getADeclaration() = g and result = n)
157164
or
165+
exists(TemplateVariable tv | this = tv.getATemplateArgument() and result = tv)
166+
or
158167
exists(EnumConstant e | this = e and result = e.getDeclaringEnum())
159168
or
160-
// result instanceof block|function
169+
// result instanceof Block|Function
161170
exists(BlockStmt b | this = b and blockscope(unresolveElement(b), unresolveElement(result)))
162171
or
163172
exists(TemplateFunction tf | this = tf.getATemplateArgument() and result = tf)
164173
or
165-
// result instanceof stmt
174+
// result instanceof Stmt
166175
exists(ControlStructure s | this = s and result = s.getParent())
167176
or
168177
using_container(unresolveElement(result), underlyingElement(this))

cpp/ql/test/library-tests/scopes/parents/parents.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ namespace foo {
1414
}
1515
}
1616

17+
template<typename T>
18+
T var = 42;
1719

18-
20+
int g() {
21+
requires(int l) { l; };
1922

23+
return var<int>;
24+
}
25+
26+
// semmle-extractor-options: -std=c++20

cpp/ql/test/library-tests/scopes/parents/parents.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
| 0 | file://:0:0:0:0 | (global namespace) | file://:0:0:0:0 | __va_list_tag |
22
| 0 | file://:0:0:0:0 | (global namespace) | parents.cpp:2:11:2:13 | foo |
3+
| 0 | file://:0:0:0:0 | (global namespace) | parents.cpp:18:3:18:3 | var |
4+
| 0 | file://:0:0:0:0 | (global namespace) | parents.cpp:18:7:18:7 | var |
5+
| 0 | file://:0:0:0:0 | (global namespace) | parents.cpp:20:5:20:5 | g |
36
| 1 | file://:0:0:0:0 | __va_list_tag | file://:0:0:0:0 | fp_offset |
47
| 1 | file://:0:0:0:0 | __va_list_tag | file://:0:0:0:0 | gp_offset |
58
| 1 | file://:0:0:0:0 | __va_list_tag | file://:0:0:0:0 | operator= |
@@ -14,7 +17,11 @@
1417
| 1 | parents.cpp:4:10:4:10 | f | parents.cpp:4:19:13:5 | { ... } |
1518
| 1 | parents.cpp:4:19:13:5 | { ... } | parents.cpp:5:11:5:11 | j |
1619
| 1 | parents.cpp:4:19:13:5 | { ... } | parents.cpp:6:11:10:7 | { ... } |
20+
| 1 | parents.cpp:4:19:13:5 | { ... } | parents.cpp:11:18:11:18 | e |
1721
| 1 | parents.cpp:4:19:13:5 | { ... } | parents.cpp:11:21:12:7 | { ... } |
1822
| 1 | parents.cpp:6:11:10:7 | { ... } | parents.cpp:7:9:9:9 | for(...;...;...) ... |
1923
| 1 | parents.cpp:6:11:10:7 | { ... } | parents.cpp:7:33:9:9 | { ... } |
2024
| 1 | parents.cpp:7:33:9:9 | { ... } | parents.cpp:8:15:8:15 | k |
25+
| 1 | parents.cpp:18:7:18:7 | var | parents.cpp:17:19:17:19 | T |
26+
| 1 | parents.cpp:20:5:20:5 | g | parents.cpp:20:9:24:1 | { ... } |
27+
| 1 | parents.cpp:20:9:24:1 | { ... } | parents.cpp:21:16:21:16 | l |

0 commit comments

Comments
 (0)