Skip to content

Commit dbd3fe6

Browse files
committed
Scope: Fix the parent scope of catch blocks
We now tie the Handler into the TryStmt, and catch-block parameters into the Handler for a consistent AST hierarchy.
1 parent b5ff407 commit dbd3fe6

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,25 @@ module Internal {
3838
or
3939
exists(SwitchStmt switchStmt | switchStmt.getStmt() = e and result = switchStmt)
4040
or
41+
// A catch-block parameter, whose parent is the `Handler`
42+
exists(CatchBlock c | c.getParameter() = e and result = c.getParent())
43+
or
44+
// A catch-block `Handler`, whose parent is the `TryStmt`
45+
e.(Handler).getParent() = result
46+
or
4147
not exists(Loop loop | loop.getAChild() = e) and
4248
not exists(IfStmt ifStmt | ifStmt.getThen() = e or ifStmt.getElse() = e) and
4349
not exists(SwitchStmt switchStmt | switchStmt.getStmt() = e) and
50+
not exists(CatchBlock c | c.getParameter() = e) and
51+
not e instanceof Handler and
4452
if exists(e.getParentScope())
4553
then result = e.getParentScope()
4654
else (
47-
// Statements do no have a parent scope, so return the enclosing block.
55+
// Statements do not have a parent scope, so return the enclosing block.
4856
result = e.(Stmt).getEnclosingBlock()
4957
or
58+
// Expressions do not have a parent scope, so return the enclosing block.
5059
result = e.(Expr).getEnclosingBlock()
51-
or
52-
// Catch block parameters don't have an enclosing scope, so attach them to the
53-
// the block itself
54-
exists(CatchBlock cb |
55-
e = cb.getParameter() and
56-
result = cb
57-
)
5860
)
5961
}
6062
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@
8383
| test.cpp:27:42:27:44 | id1 | test.cpp:27:36:27:52 | { ... } |
8484
| test.cpp:27:47:27:49 | 10 | test.cpp:27:36:27:52 | { ... } |
8585
| test.cpp:27:52:27:52 | return ... | test.cpp:27:36:27:52 | { ... } |
86-
| test.cpp:28:24:28:26 | id1 | test.cpp:28:29:30:11 | { ... } |
87-
| test.cpp:28:29:30:11 | <handler> | test.cpp:13:9:31:9 | { ... } |
86+
| test.cpp:28:24:28:26 | id1 | test.cpp:28:29:30:11 | <handler> |
87+
| test.cpp:28:29:30:11 | <handler> | test.cpp:26:11:28:11 | try { ... } |
8888
| test.cpp:28:29:30:11 | { ... } | test.cpp:13:9:31:9 | { ... } |
8989
| test.cpp:29:13:29:20 | declaration | test.cpp:28:29:30:11 | { ... } |
9090
| test.cpp:29:17:29:19 | id1 | test.cpp:28:29:30:11 | { ... } |

0 commit comments

Comments
 (0)