Skip to content

Commit 3341312

Browse files
committed
C++: For unnamed local variable declaration entries consider the name of the variable
1 parent 48ea94b commit 3341312

File tree

3 files changed

+24
-41
lines changed

3 files changed

+24
-41
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,16 @@ class VariableDeclarationEntry extends DeclarationEntry, @var_decl {
234234
* int f(int y) { return y; }
235235
* ```
236236
*/
237-
override string getName() { var_decls(underlyingElement(this), _, _, result, _) and result != "" }
237+
override string getName() {
238+
exists(string name |
239+
var_decls(underlyingElement(this), _, _, name, _) and
240+
(
241+
name != "" and result = name
242+
or
243+
name = "" and result = this.getVariable().(LocalVariable).getName()
244+
)
245+
)
246+
}
238247

239248
/**
240249
* Gets the type of the variable which is being declared or defined.

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9479,7 +9479,7 @@ ir.cpp:
94799479
# 1079| getEntryPoint(): [BlockStmt] { ... }
94809480
# 1080| getStmt(0): [RangeBasedForStmt] for(...:...) ...
94819481
# 1080| getChild(0): [DeclStmt] declaration
9482-
# 1080| getDeclarationEntry(0): (no string representation)
9482+
# 1080| getDeclarationEntry(0): [VariableDeclarationEntry] declaration of (__range)
94839483
# 1080| Type = [LValueReferenceType] const vector<int> &
94849484
#-----| getVariable().getInitializer(): [Initializer] initializer for (__range)
94859485
# 1080| getExpr(): [VariableAccess] v
@@ -9492,7 +9492,7 @@ ir.cpp:
94929492
# 1080| Type = [SpecifiedType] const vector<int>
94939493
# 1080| ValueCategory = lvalue
94949494
# 1080| getBeginEndDeclaration(): [DeclStmt] declaration
9495-
# 1080| getDeclarationEntry(0): (no string representation)
9495+
# 1080| getDeclarationEntry(0): [VariableDeclarationEntry] declaration of (__begin)
94969496
# 1080| Type = [NestedStruct] iterator
94979497
#-----| getVariable().getInitializer(): [Initializer] initializer for (__begin)
94989498
# 1080| getExpr(): [FunctionCall] call to begin
@@ -9504,7 +9504,7 @@ ir.cpp:
95049504
#-----| getQualifier().getFullyConverted(): [ReferenceDereferenceExpr] (reference dereference)
95059505
#-----| Type = [SpecifiedType] const vector<int>
95069506
#-----| ValueCategory = lvalue
9507-
# 1080| getDeclarationEntry(1): (no string representation)
9507+
# 1080| getDeclarationEntry(1): [VariableDeclarationEntry] declaration of (__end)
95089508
# 1080| Type = [NestedStruct] iterator
95099509
#-----| getVariable().getInitializer(): [Initializer] initializer for (__end)
95109510
# 1080| getExpr(): [FunctionCall] call to end
@@ -9572,7 +9572,7 @@ ir.cpp:
95729572
# 1080| ValueCategory = lvalue
95739573
# 1086| getStmt(1): [RangeBasedForStmt] for(...:...) ...
95749574
# 1086| getChild(0): [DeclStmt] declaration
9575-
# 1086| getDeclarationEntry(0): (no string representation)
9575+
# 1086| getDeclarationEntry(0): [VariableDeclarationEntry] declaration of (__range)
95769576
# 1086| Type = [LValueReferenceType] const vector<int> &
95779577
#-----| getVariable().getInitializer(): [Initializer] initializer for (__range)
95789578
# 1086| getExpr(): [VariableAccess] v
@@ -9585,7 +9585,7 @@ ir.cpp:
95859585
# 1086| Type = [SpecifiedType] const vector<int>
95869586
# 1086| ValueCategory = lvalue
95879587
# 1086| getBeginEndDeclaration(): [DeclStmt] declaration
9588-
# 1086| getDeclarationEntry(0): (no string representation)
9588+
# 1086| getDeclarationEntry(0): [VariableDeclarationEntry] declaration of (__begin)
95899589
# 1086| Type = [NestedStruct] iterator
95909590
#-----| getVariable().getInitializer(): [Initializer] initializer for (__begin)
95919591
# 1086| getExpr(): [FunctionCall] call to begin
@@ -9597,7 +9597,7 @@ ir.cpp:
95979597
#-----| getQualifier().getFullyConverted(): [ReferenceDereferenceExpr] (reference dereference)
95989598
#-----| Type = [SpecifiedType] const vector<int>
95999599
#-----| ValueCategory = lvalue
9600-
# 1086| getDeclarationEntry(1): (no string representation)
9600+
# 1086| getDeclarationEntry(1): [VariableDeclarationEntry] declaration of (__end)
96019601
# 1086| Type = [NestedStruct] iterator
96029602
#-----| getVariable().getInitializer(): [Initializer] initializer for (__end)
96039603
# 1086| getExpr(): [FunctionCall] call to end
@@ -11814,7 +11814,7 @@ ir.cpp:
1181411814
# 1465| ValueCategory = prvalue
1181511815
# 1467| getStmt(1): [BlockStmt] { ... }
1181611816
# 1468| getStmt(0): [DeclStmt] declaration
11817-
# 1468| getDeclarationEntry(0): (no string representation)
11817+
# 1468| getDeclarationEntry(0): [VariableDeclarationEntry] definition of (unnamed local variable)
1181811818
# 1468| Type = [LValueReferenceType] int(&)[2]
1181911819
# 1468| getVariable().getInitializer(): [Initializer] initializer for (unnamed local variable)
1182011820
# 1468| getExpr(): [VariableAccess] xs
@@ -12160,7 +12160,7 @@ ir.cpp:
1216012160
# 1502| ValueCategory = prvalue
1216112161
# 1504| getStmt(1): [BlockStmt] { ... }
1216212162
# 1505| getStmt(0): [DeclStmt] declaration
12163-
# 1505| getDeclarationEntry(0): (no string representation)
12163+
# 1505| getDeclarationEntry(0): [VariableDeclarationEntry] definition of (unnamed local variable)
1216412164
# 1505| Type = [Struct] StructuredBindingDataMemberStruct
1216512165
# 1505| getVariable().getInitializer(): [Initializer] initializer for (unnamed local variable)
1216612166
# 1505| getExpr(): [VariableAccess] s
@@ -12645,7 +12645,7 @@ ir.cpp:
1264512645
# 1582| ValueCategory = prvalue
1264612646
# 1584| getStmt(1): [BlockStmt] { ... }
1264712647
# 1585| getStmt(0): [DeclStmt] declaration
12648-
# 1585| getDeclarationEntry(0): (no string representation)
12648+
# 1585| getDeclarationEntry(0): [VariableDeclarationEntry] definition of (unnamed local variable)
1264912649
# 1585| Type = [Struct] StructuredBindingTupleRefGet
1265012650
# 1585| getVariable().getInitializer(): [Initializer] initializer for (unnamed local variable)
1265112651
# 1585| getExpr(): [VariableAccess] t
@@ -13010,7 +13010,7 @@ ir.cpp:
1301013010
# 1648| ValueCategory = prvalue
1301113011
# 1650| getStmt(1): [BlockStmt] { ... }
1301213012
# 1651| getStmt(0): [DeclStmt] declaration
13013-
# 1651| getDeclarationEntry(0): (no string representation)
13013+
# 1651| getDeclarationEntry(0): [VariableDeclarationEntry] definition of (unnamed local variable)
1301413014
# 1651| Type = [LValueReferenceType] StructuredBindingTupleNoRefGet &
1301513015
# 1651| getVariable().getInitializer(): [Initializer] initializer for (unnamed local variable)
1301613016
# 1651| getExpr(): [VariableAccess] t
@@ -13309,7 +13309,7 @@ ir.cpp:
1330913309
# 1675| Value = [Literal] 2
1331013310
# 1675| ValueCategory = prvalue
1331113311
# 1676| getStmt(1): [DeclStmt] declaration
13312-
# 1676| getDeclarationEntry(0): (no string representation)
13312+
# 1676| getDeclarationEntry(0): [VariableDeclarationEntry] definition of (unnamed local variable)
1331313313
# 1676| Type = [ArrayType] int[2]
1331413314
# 1676| getVariable().getInitializer(): [Initializer] initializer for (unnamed local variable)
1331513315
# 1676| getExpr(): [VariableAccess] xs
@@ -16521,7 +16521,7 @@ ir.cpp:
1652116521
# 2152| ValueCategory = prvalue
1652216522
# 2153| getStmt(5): [RangeBasedForStmt] for(...:...) ...
1652316523
# 2153| getChild(0): [DeclStmt] declaration
16524-
# 2153| getDeclarationEntry(0): (no string representation)
16524+
# 2153| getDeclarationEntry(0): [VariableDeclarationEntry] declaration of (__range)
1652516525
# 2153| Type = [LValueReferenceType] vector<ClassWithDestructor> &
1652616526
#-----| getVariable().getInitializer(): [Initializer] initializer for (__range)
1652716527
# 2153| getExpr(): [VariableAccess] ys
@@ -16531,7 +16531,7 @@ ir.cpp:
1653116531
# 2153| Type = [LValueReferenceType] vector<ClassWithDestructor> &
1653216532
# 2153| ValueCategory = prvalue
1653316533
# 2153| getBeginEndDeclaration(): [DeclStmt] declaration
16534-
# 2153| getDeclarationEntry(0): (no string representation)
16534+
# 2153| getDeclarationEntry(0): [VariableDeclarationEntry] declaration of (__begin)
1653516535
# 2153| Type = [NestedStruct] iterator
1653616536
#-----| getVariable().getInitializer(): [Initializer] initializer for (__begin)
1653716537
# 2153| getExpr(): [FunctionCall] call to begin
@@ -16547,7 +16547,7 @@ ir.cpp:
1654716547
#-----| getExpr(): [ReferenceDereferenceExpr] (reference dereference)
1654816548
#-----| Type = [ClassTemplateInstantiation,Struct] vector<ClassWithDestructor>
1654916549
#-----| ValueCategory = lvalue
16550-
# 2153| getDeclarationEntry(1): (no string representation)
16550+
# 2153| getDeclarationEntry(1): [VariableDeclarationEntry] declaration of (__end)
1655116551
# 2153| Type = [NestedStruct] iterator
1655216552
#-----| getVariable().getInitializer(): [Initializer] initializer for (__end)
1655316553
# 2153| getExpr(): [FunctionCall] call to end

cpp/ql/test/library-tests/syntax-zoo/dataflow-ir-consistency.expected

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,11 @@ uniqueNodeLocation
55
missingLocation
66
uniqueNodeToString
77
| builtin.c:5:5:5:11 | (no string representation) | Node should have one toString but has 0. |
8-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
9-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
10-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
11-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
12-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
13-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
14-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
15-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
16-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
17-
| cpp11.cpp:6:5:6:5 | (no string representation) | Node should have one toString but has 0. |
18-
| cpp11.cpp:28:5:28:5 | (no string representation) | Node should have one toString but has 0. |
19-
| cpp11.cpp:28:5:28:5 | (no string representation) | Node should have one toString but has 0. |
20-
| cpp11.cpp:28:5:28:5 | (no string representation) | Node should have one toString but has 0. |
21-
| cpp11.cpp:28:5:28:5 | (no string representation) | Node should have one toString but has 0. |
22-
| cpp11.cpp:28:5:28:5 | (no string representation) | Node should have one toString but has 0. |
23-
| cpp11.cpp:28:5:28:5 | (no string representation) | Node should have one toString but has 0. |
24-
| cpp11.cpp:28:5:28:5 | (no string representation) | Node should have one toString but has 0. |
258
| misc.c:227:7:227:28 | (no string representation) | Node should have one toString but has 0. |
269
| static_init_templates.cpp:80:18:80:23 | (no string representation) | Node should have one toString but has 0. |
2710
| static_init_templates.cpp:80:18:80:23 | (no string representation) | Node should have one toString but has 0. |
2811
| static_init_templates.cpp:89:18:89:23 | (no string representation) | Node should have one toString but has 0. |
2912
| static_init_templates.cpp:89:18:89:23 | (no string representation) | Node should have one toString but has 0. |
30-
| stream_it.cpp:11:3:11:3 | (no string representation) | Node should have one toString but has 0. |
31-
| stream_it.cpp:11:3:11:3 | (no string representation) | Node should have one toString but has 0. |
32-
| stream_it.cpp:11:3:11:3 | (no string representation) | Node should have one toString but has 0. |
33-
| stream_it.cpp:11:3:11:3 | (no string representation) | Node should have one toString but has 0. |
34-
| stream_it.cpp:11:3:11:3 | (no string representation) | Node should have one toString but has 0. |
35-
| stream_it.cpp:11:3:11:3 | (no string representation) | Node should have one toString but has 0. |
36-
| stream_it.cpp:11:3:11:3 | (no string representation) | Node should have one toString but has 0. |
37-
| stream_it.cpp:11:3:11:3 | (no string representation) | Node should have one toString but has 0. |
38-
| stream_it.cpp:11:3:11:3 | (no string representation) | Node should have one toString but has 0. |
3913
parameterCallable
4014
localFlowIsLocal
4115
readStepIsLocal

0 commit comments

Comments
 (0)