Skip to content

Commit a3ae304

Browse files
committed
C++: Handle getInitializingExpr in PrintAST
1 parent 33c524d commit a3ae304

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,12 @@ class ExprNode extends AstNode {
309309
override AstNode getChildInternal(int childIndex) {
310310
result.getAst() = expr.getChild(childIndex)
311311
or
312+
childIndex = max(int index | exists(expr.getChild(index)) or index = 0) + 1 and
313+
result.getAst() = expr.(ConditionDeclExpr).getInitializingExpr()
314+
or
312315
exists(int destructorIndex |
313316
result.getAst() = expr.getImplicitDestructorCall(destructorIndex) and
314-
childIndex = destructorIndex + max(int index | exists(expr.getChild(index)) or index = 0) + 1
317+
childIndex = destructorIndex + max(int index | exists(expr.getChild(index)) or index = 0) + 2
315318
)
316319
}
317320

@@ -686,6 +689,8 @@ private string getChildAccessorWithoutConversions(Locatable parent, Element chil
686689
not namedExprChildPredicates(expr, child, _) and
687690
exists(int n | expr.getChild(n) = child and result = "getChild(" + n + ")")
688691
or
692+
expr.(ConditionDeclExpr).getInitializingExpr() = child and result = "getInitializingExpr()"
693+
or
689694
exists(int n |
690695
expr.getImplicitDestructorCall(n) = child and
691696
result = "getImplicitDestructorCall(" + n + ")"

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8812,6 +8812,15 @@ ir.cpp:
88128812
# 976| getVariableAccess(): [VariableAccess] b
88138813
# 976| Type = [BoolType] bool
88148814
# 976| ValueCategory = prvalue(load)
8815+
# 976| getInitializingExpr(): [LTExpr] ... < ...
8816+
# 976| Type = [BoolType] bool
8817+
# 976| ValueCategory = prvalue
8818+
# 976| getLesserOperand(): [VariableAccess] x
8819+
# 976| Type = [IntType] int
8820+
# 976| ValueCategory = prvalue(load)
8821+
# 976| getGreaterOperand(): [VariableAccess] y
8822+
# 976| Type = [IntType] int
8823+
# 976| ValueCategory = prvalue(load)
88158824
# 976| getThen(): [BlockStmt] { ... }
88168825
# 977| getStmt(0): [ExprStmt] ExprStmt
88178826
# 977| getExpr(): [AssignExpr] ... = ...
@@ -8831,6 +8840,15 @@ ir.cpp:
88318840
# 979| getVariableAccess(): [VariableAccess] z
88328841
# 979| Type = [IntType] int
88338842
# 979| ValueCategory = prvalue(load)
8843+
# 979| getInitializingExpr(): [AddExpr] ... + ...
8844+
# 979| Type = [IntType] int
8845+
# 979| ValueCategory = prvalue
8846+
# 979| getLeftOperand(): [VariableAccess] x
8847+
# 979| Type = [IntType] int
8848+
# 979| ValueCategory = prvalue(load)
8849+
# 979| getRightOperand(): [VariableAccess] y
8850+
# 979| Type = [IntType] int
8851+
# 979| ValueCategory = prvalue(load)
88348852
# 979| getVariableAccess().getFullyConverted(): [CStyleCast] (bool)...
88358853
# 979| Conversion = [BoolConversion] conversion to bool
88368854
# 979| Type = [BoolType] bool
@@ -8854,6 +8872,12 @@ ir.cpp:
88548872
# 982| getVariableAccess(): [VariableAccess] p
88558873
# 982| Type = [IntPointerType] int *
88568874
# 982| ValueCategory = prvalue(load)
8875+
# 982| getInitializingExpr(): [AddressOfExpr] & ...
8876+
# 982| Type = [IntPointerType] int *
8877+
# 982| ValueCategory = prvalue
8878+
# 982| getOperand(): [VariableAccess] x
8879+
# 982| Type = [IntType] int
8880+
# 982| ValueCategory = lvalue
88578881
# 982| getVariableAccess().getFullyConverted(): [CStyleCast] (bool)...
88588882
# 982| Conversion = [BoolConversion] conversion to bool
88598883
# 982| Type = [BoolType] bool
@@ -8888,6 +8912,15 @@ ir.cpp:
88888912
# 988| getVariableAccess(): [VariableAccess] b
88898913
# 988| Type = [BoolType] bool
88908914
# 988| ValueCategory = prvalue(load)
8915+
# 988| getInitializingExpr(): [LTExpr] ... < ...
8916+
# 988| Type = [BoolType] bool
8917+
# 988| ValueCategory = prvalue
8918+
# 988| getLesserOperand(): [VariableAccess] x
8919+
# 988| Type = [IntType] int
8920+
# 988| ValueCategory = prvalue(load)
8921+
# 988| getGreaterOperand(): [VariableAccess] y
8922+
# 988| Type = [IntType] int
8923+
# 988| ValueCategory = prvalue(load)
88918924
# 988| getStmt(): [BlockStmt] { ... }
88928925
# 990| getStmt(1): [WhileStmt] while (...) ...
88938926
# 990| getCondition(): [ConditionDeclExpr] (condition decl)
@@ -8896,6 +8929,15 @@ ir.cpp:
88968929
# 990| getVariableAccess(): [VariableAccess] z
88978930
# 990| Type = [IntType] int
88988931
# 990| ValueCategory = prvalue(load)
8932+
# 990| getInitializingExpr(): [AddExpr] ... + ...
8933+
# 990| Type = [IntType] int
8934+
# 990| ValueCategory = prvalue
8935+
# 990| getLeftOperand(): [VariableAccess] x
8936+
# 990| Type = [IntType] int
8937+
# 990| ValueCategory = prvalue(load)
8938+
# 990| getRightOperand(): [VariableAccess] y
8939+
# 990| Type = [IntType] int
8940+
# 990| ValueCategory = prvalue(load)
88998941
# 990| getVariableAccess().getFullyConverted(): [CStyleCast] (bool)...
89008942
# 990| Conversion = [BoolConversion] conversion to bool
89018943
# 990| Type = [BoolType] bool
@@ -8908,6 +8950,12 @@ ir.cpp:
89088950
# 992| getVariableAccess(): [VariableAccess] p
89098951
# 992| Type = [IntPointerType] int *
89108952
# 992| ValueCategory = prvalue(load)
8953+
# 992| getInitializingExpr(): [AddressOfExpr] & ...
8954+
# 992| Type = [IntPointerType] int *
8955+
# 992| ValueCategory = prvalue
8956+
# 992| getOperand(): [VariableAccess] x
8957+
# 992| Type = [IntType] int
8958+
# 992| ValueCategory = lvalue
89118959
# 992| getVariableAccess().getFullyConverted(): [CStyleCast] (bool)...
89128960
# 992| Conversion = [BoolConversion] conversion to bool
89138961
# 992| Type = [BoolType] bool
@@ -14780,6 +14828,9 @@ ir.cpp:
1478014828
# 1816| getVariableAccess(): [VariableAccess] w2
1478114829
# 1816| Type = [IntType] int
1478214830
# 1816| ValueCategory = prvalue(load)
14831+
# 1816| getInitializingExpr(): [VariableAccess] w
14832+
# 1816| Type = [IntType] int
14833+
# 1816| ValueCategory = prvalue(load)
1478314834
# 1816| getVariableAccess().getFullyConverted(): [CStyleCast] (bool)...
1478414835
# 1816| Conversion = [BoolConversion] conversion to bool
1478514836
# 1816| Type = [BoolType] bool
@@ -14815,6 +14866,9 @@ ir.cpp:
1481514866
# 1820| getVariableAccess(): [VariableAccess] v2
1481614867
# 1820| Type = [IntType] int
1481714868
# 1820| ValueCategory = prvalue(load)
14869+
# 1820| getInitializingExpr(): [VariableAccess] v
14870+
# 1820| Type = [IntType] int
14871+
# 1820| ValueCategory = prvalue(load)
1481814872
# 1820| getVariableAccess().getFullyConverted(): [CStyleCast] (bool)...
1481914873
# 1820| Conversion = [BoolConversion] conversion to bool
1482014874
# 1820| Type = [BoolType] bool
@@ -14875,6 +14929,9 @@ ir.cpp:
1487514929
# 1829| getVariableAccess(): [VariableAccess] z2
1487614930
# 1829| Type = [IntType] int
1487714931
# 1829| ValueCategory = prvalue(load)
14932+
# 1829| getInitializingExpr(): [VariableAccess] z
14933+
# 1829| Type = [IntType] int
14934+
# 1829| ValueCategory = prvalue(load)
1487814935
# 1829| getVariableAccess().getFullyConverted(): [CStyleCast] (bool)...
1487914936
# 1829| Conversion = [BoolConversion] conversion to bool
1488014937
# 1829| Type = [BoolType] bool
@@ -14991,6 +15048,9 @@ ir.cpp:
1499115048
# 1846| getVariableAccess(): [VariableAccess] w2
1499215049
# 1846| Type = [IntType] int
1499315050
# 1846| ValueCategory = prvalue(load)
15051+
# 1846| getInitializingExpr(): [VariableAccess] w
15052+
# 1846| Type = [IntType] int
15053+
# 1846| ValueCategory = prvalue(load)
1499415054
# 1846| getStmt(): [BlockStmt] { ... }
1499515055
# 1847| getStmt(0): [SwitchCase] default:
1499615056
# 1848| getStmt(1): [ExprStmt] ExprStmt
@@ -15023,6 +15083,9 @@ ir.cpp:
1502315083
# 1851| getVariableAccess(): [VariableAccess] v2
1502415084
# 1851| Type = [IntType] int
1502515085
# 1851| ValueCategory = prvalue(load)
15086+
# 1851| getInitializingExpr(): [VariableAccess] v
15087+
# 1851| Type = [IntType] int
15088+
# 1851| ValueCategory = prvalue(load)
1502615089
# 1851| getStmt(): [BlockStmt] { ... }
1502715090
# 1852| getStmt(0): [SwitchCase] default:
1502815091
# 1853| getStmt(1): [ExprStmt] ExprStmt
@@ -15077,6 +15140,9 @@ ir.cpp:
1507715140
# 1862| getVariableAccess(): [VariableAccess] z2
1507815141
# 1862| Type = [IntType] int
1507915142
# 1862| ValueCategory = prvalue(load)
15143+
# 1862| getInitializingExpr(): [VariableAccess] z
15144+
# 1862| Type = [IntType] int
15145+
# 1862| ValueCategory = prvalue(load)
1508015146
# 1862| getStmt(): [BlockStmt] { ... }
1508115147
# 1863| getStmt(0): [SwitchCase] default:
1508215148
# 1864| getStmt(1): [ExprStmt] ExprStmt
@@ -17048,6 +17114,10 @@ ir.cpp:
1704817114
# 2169| getQualifier(): [VariableAccess] b
1704917115
# 2169| Type = [Struct] HasOperatorBool
1705017116
# 2169| ValueCategory = prvalue(load)
17117+
# 2169| getInitializingExpr(): [Literal] 0
17118+
# 2169| Type = [Struct] HasOperatorBool
17119+
# 2169| Value = [Literal] 0
17120+
# 2169| ValueCategory = prvalue
1705117121
# 2169| getThen(): [BlockStmt] { ... }
1705217122
# 2170| getStmt(1): [ReturnStmt] return ...
1705317123
# 2172| [CopyAssignmentOperator] ClassWithDestructor& ClassWithDestructor::operator=(ClassWithDestructor const&)
@@ -18650,6 +18720,12 @@ ir.cpp:
1865018720
# 2318| getQualifier(): [VariableAccess] B
1865118721
# 2318| Type = [Class] Bool
1865218722
# 2318| ValueCategory = prvalue(load)
18723+
# 2318| getInitializingExpr(): [ConstructorCall] call to Bool
18724+
# 2318| Type = [VoidType] void
18725+
# 2318| ValueCategory = prvalue
18726+
# 2318| getArgument(0): [VariableAccess] b
18727+
# 2318| Type = [BoolType] bool
18728+
# 2318| ValueCategory = prvalue(load)
1865318729
# 2318| getThen(): [BlockStmt] { ... }
1865418730
# 2319| getStmt(0): [DeclStmt] declaration
1865518731
# 2319| getDeclarationEntry(0): [VariableDeclarationEntry] definition of s1
@@ -18731,6 +18807,12 @@ ir.cpp:
1873118807
# 2334| getQualifier(): [VariableAccess] B
1873218808
# 2334| Type = [Class] Bool
1873318809
# 2334| ValueCategory = prvalue(load)
18810+
# 2334| getInitializingExpr(): [ConstructorCall] call to Bool
18811+
# 2334| Type = [VoidType] void
18812+
# 2334| ValueCategory = prvalue
18813+
# 2334| getArgument(0): [VariableAccess] b
18814+
# 2334| Type = [BoolType] bool
18815+
# 2334| ValueCategory = prvalue(load)
1873418816
# 2334| getStmt(): [BlockStmt] { ... }
1873518817
# 2335| getStmt(0): [ExprStmt] ExprStmt
1873618818
# 2335| getExpr(): [AssignExpr] ... = ...
@@ -19678,6 +19760,21 @@ ir.cpp:
1967819760
# 2397| getVariableAccess(): [VariableAccess] x
1967919761
# 2397| Type = [PlainCharType] char
1968019762
# 2397| ValueCategory = prvalue(load)
19763+
# 2397| getInitializingExpr(): [FunctionCall] call to get_x
19764+
# 2397| Type = [PlainCharType] char
19765+
# 2397| ValueCategory = prvalue
19766+
# 2397| getQualifier(): [ConstructorCall] call to ClassWithDestructor
19767+
# 2397| Type = [VoidType] void
19768+
# 2397| ValueCategory = prvalue
19769+
# 2397| getImplicitDestructorCall(0): [DestructorCall] call to ~ClassWithDestructor
19770+
# 2397| Type = [VoidType] void
19771+
# 2397| ValueCategory = prvalue
19772+
# 2397| getQualifier(): [ReuseExpr] reuse of temporary object
19773+
# 2397| Type = [Class] ClassWithDestructor
19774+
# 2397| ValueCategory = xvalue
19775+
# 2397| getQualifier().getFullyConverted(): [TemporaryObjectExpr] temporary object
19776+
# 2397| Type = [Class] ClassWithDestructor
19777+
# 2397| ValueCategory = prvalue(load)
1968119778
# 2397| getVariableAccess().getFullyConverted(): [CStyleCast] (bool)...
1968219779
# 2397| Conversion = [BoolConversion] conversion to bool
1968319780
# 2397| Type = [BoolType] bool
@@ -19749,6 +19846,21 @@ ir.cpp:
1974919846
# 2406| getVariableAccess(): [VariableAccess] x
1975019847
# 2406| Type = [PlainCharType] char
1975119848
# 2406| ValueCategory = prvalue(load)
19849+
# 2406| getInitializingExpr(): [FunctionCall] call to get_x
19850+
# 2406| Type = [PlainCharType] char
19851+
# 2406| ValueCategory = prvalue
19852+
# 2406| getQualifier(): [ConstructorCall] call to ClassWithDestructor
19853+
# 2406| Type = [VoidType] void
19854+
# 2406| ValueCategory = prvalue
19855+
# 2406| getImplicitDestructorCall(0): [DestructorCall] call to ~ClassWithDestructor
19856+
# 2406| Type = [VoidType] void
19857+
# 2406| ValueCategory = prvalue
19858+
# 2406| getQualifier(): [ReuseExpr] reuse of temporary object
19859+
# 2406| Type = [Class] ClassWithDestructor
19860+
# 2406| ValueCategory = xvalue
19861+
# 2406| getQualifier().getFullyConverted(): [TemporaryObjectExpr] temporary object
19862+
# 2406| Type = [Class] ClassWithDestructor
19863+
# 2406| ValueCategory = prvalue(load)
1975219864
# 2406| getVariableAccess().getFullyConverted(): [CStyleCast] (int)...
1975319865
# 2406| Conversion = [IntegralConversion] integral conversion
1975419866
# 2406| Type = [IntType] int

0 commit comments

Comments
 (0)