Skip to content

Commit f1a350c

Browse files
committed
C++: Print handler parameters in PrintAST
1 parent d0f73ac commit f1a350c

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ private Declaration getAnEnclosingDeclaration(Locatable ast) {
8080
or
8181
result = ast.(Parameter).getFunction()
8282
or
83+
result = ast.(Parameter).getCatchBlock().getEnclosingFunction()
84+
or
8385
result = ast.(Expr).getEnclosingDeclaration()
8486
or
8587
result = ast.(Initializer).getDeclaration()
@@ -510,6 +512,22 @@ class DeclStmtNode extends StmtNode {
510512
}
511513
}
512514

515+
/**
516+
* A node representing a `Handler`.
517+
*/
518+
class HandlerNode extends ChildStmtNode {
519+
Handler handler;
520+
521+
HandlerNode() { handler = stmt }
522+
523+
override BaseAstNode getChildInternal(int childIndex) {
524+
result = super.getChildInternal(childIndex)
525+
or
526+
childIndex = -1 and
527+
result.getAst() = handler.getParameter()
528+
}
529+
}
530+
513531
/**
514532
* A node representing a `Parameter`.
515533
*/
@@ -754,6 +772,8 @@ private predicate namedStmtChildPredicates(Locatable s, Element e, string pred)
754772
or
755773
s.(ConstexprIfStmt).getElse() = e and pred = "getElse()"
756774
or
775+
s.(Handler).getParameter() = e and pred = "getParameter()"
776+
or
757777
s.(IfStmt).getInitialization() = e and pred = "getInitialization()"
758778
or
759779
s.(IfStmt).getCondition() = e and pred = "getCondition()"

cpp/ql/test/examples/expressions/PrintAST.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,8 @@ Throw.cpp:
870870
# 8| Type = [BoolType] bool
871871
# 8| ValueCategory = prvalue
872872
# 12| getChild(1): [Handler] <handler>
873+
# 12| getParameter(): [Parameter] e
874+
# 12| Type = [PointerType] E *
873875
# 12| getBlock(): [CatchBlock] { ... }
874876
# 13| getStmt(0): [ExprStmt] ExprStmt
875877
# 13| getExpr(): [ReThrowExpr] re-throw exception

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9055,6 +9055,8 @@ ir.cpp:
90559055
# 733| Value = [Literal] 7
90569056
# 733| ValueCategory = prvalue
90579057
# 735| getChild(1): [Handler] <handler>
9058+
# 735| getParameter(): [Parameter] s
9059+
# 735| Type = [PointerType] const char *
90589060
# 735| getBlock(): [CatchBlock] { ... }
90599061
# 736| getStmt(0): [ExprStmt] ExprStmt
90609062
# 736| getExpr(): [ThrowExpr] throw ...
@@ -9067,6 +9069,8 @@ ir.cpp:
90679069
# 736| Type = [PointerType] const char *
90689070
# 736| ValueCategory = prvalue(load)
90699071
# 738| getChild(2): [Handler] <handler>
9072+
# 738| getParameter(): [Parameter] e
9073+
# 738| Type = [LValueReferenceType] const String &
90709074
# 738| getBlock(): [CatchBlock] { ... }
90719075
# 740| getChild(3): [Handler] <handler>
90729076
# 740| getBlock(): [CatchAnyBlock] { ... }
@@ -12852,6 +12856,8 @@ ir.cpp:
1285212856
# 1200| Value = [Literal] 7
1285312857
# 1200| ValueCategory = prvalue
1285412858
# 1202| getChild(1): [Handler] <handler>
12859+
# 1202| getParameter(): [Parameter] s
12860+
# 1202| Type = [PointerType] const char *
1285512861
# 1202| getBlock(): [CatchBlock] { ... }
1285612862
# 1203| getStmt(0): [ExprStmt] ExprStmt
1285712863
# 1203| getExpr(): [ThrowExpr] throw ...
@@ -12864,6 +12870,8 @@ ir.cpp:
1286412870
# 1203| Type = [PointerType] const char *
1286512871
# 1203| ValueCategory = prvalue(load)
1286612872
# 1205| getChild(2): [Handler] <handler>
12873+
# 1205| getParameter(): [Parameter] e
12874+
# 1205| Type = [LValueReferenceType] const String &
1286712875
# 1205| getBlock(): [CatchBlock] { ... }
1286812876
# 1207| getStmt(1): [ReturnStmt] return ...
1286912877
# 1211| [TopLevelFunction] void VectorTypes(int)
@@ -20586,6 +20594,8 @@ ir.cpp:
2058620594
# 2281| Type = [Struct] String
2058720595
# 2281| ValueCategory = lvalue
2058820596
# 2282| getChild(1): [Handler] <handler>
20597+
# 2282| getParameter(): [Parameter] s
20598+
# 2282| Type = [PointerType] const char *
2058920599
# 2282| getBlock(): [CatchBlock] { ... }
2059020600
# 2283| getStmt(0): [ExprStmt] ExprStmt
2059120601
# 2283| getExpr(): [ThrowExpr] throw ...
@@ -20598,6 +20608,8 @@ ir.cpp:
2059820608
# 2283| Type = [PointerType] const char *
2059920609
# 2283| ValueCategory = prvalue(load)
2060020610
# 2285| getChild(2): [Handler] <handler>
20611+
# 2285| getParameter(): [Parameter] e
20612+
# 2285| Type = [LValueReferenceType] const String &
2060120613
# 2285| getBlock(): [CatchBlock] { ... }
2060220614
# 2287| getChild(3): [Handler] <handler>
2060320615
# 2287| getBlock(): [CatchAnyBlock] { ... }
@@ -22845,6 +22857,8 @@ ir.cpp:
2284522857
# 2537| Value = [Literal] 42
2284622858
# 2537| ValueCategory = prvalue
2284722859
# 2539| getChild(1): [Handler] <handler>
22860+
# 2539| getParameter(): [Parameter] (unnamed parameter 0)
22861+
# 2539| Type = [PlainCharType] char
2284822862
# 2539| getBlock(): [CatchBlock] { ... }
2284922863
# 2541| getImplicitDestructorCall(0): [DestructorCall] call to ~ClassWithDestructor
2285022864
# 2541| Type = [VoidType] void

0 commit comments

Comments
 (0)