Skip to content

Commit 117d0fb

Browse files
committed
Scope: Extend getParentScope for lambda expressions
Lambda functions are tied into the parent statement of their declaring lambda expression, which enables Scope's hiding predicates to calculate hiding behaviour for lambda expressions.
1 parent 9b7e129 commit 117d0fb

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

cpp/common/src/codingstandards/cpp/Scope.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,22 @@ module Internal {
5757
// A catch-block `Handler`, whose parent is the `TryStmt`
5858
e.(Handler).getParent() = result
5959
or
60+
// The parent scope of a lambda is the scope in which the lambda expression is defined.
61+
//
62+
// Lambda functions are defined in a generated `Closure` class, as the `operator()` function. We choose the
63+
// enclosing statement of the lambda expression as the parent scope of the lambda function. This is so we can
64+
// determine the order of definition if a variable is defined in the same scope as the lambda expression.
65+
exists(Closure lambdaClosure |
66+
lambdaClosure.getLambdaFunction() = e and
67+
lambdaClosure.getLambdaExpression().getEnclosingStmt() = result
68+
)
69+
or
6070
not exists(Loop loop | loop.getAChild() = e) and
6171
not exists(IfStmt ifStmt | ifStmt.getThen() = e or ifStmt.getElse() = e) and
6272
not exists(SwitchStmt switchStmt | switchStmt.getStmt() = e) and
6373
not exists(CatchBlock c | c.getParameter() = e) and
6474
not e instanceof Handler and
75+
not exists(Closure lambdaClosure | lambdaClosure.getLambdaFunction() = e) and
6576
if exists(e.getParentScope())
6677
then result = e.getParentScope()
6778
else (

cpp/common/test/library/codingstandards/cpp/scope/ParentScope.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
| test.cpp:27:28:27:28 | operator= | file://:0:0:0:0 | decltype([...](...){...}) |
7878
| test.cpp:27:29:27:29 | id1 | file://:0:0:0:0 | decltype([...](...){...}) |
7979
| test.cpp:27:29:27:31 | id1 | test.cpp:26:15:28:11 | { ... } |
80-
| test.cpp:27:33:27:33 | operator() | file://:0:0:0:0 | decltype([...](...){...}) |
80+
| test.cpp:27:33:27:33 | operator() | test.cpp:27:13:27:53 | declaration |
8181
| test.cpp:27:36:27:52 | { ... } | test.cpp:27:33:27:33 | operator() |
8282
| test.cpp:27:38:27:50 | declaration | test.cpp:27:36:27:52 | { ... } |
8383
| test.cpp:27:42:27:44 | id1 | test.cpp:27:36:27:52 | { ... } |

0 commit comments

Comments
 (0)