Skip to content

Commit 1eb0b49

Browse files
committed
C++: Improve parameter naming
1 parent bdec751 commit 1eb0b49

File tree

6 files changed

+26
-29
lines changed

6 files changed

+26
-29
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class Parameter extends LocalScopeVariable, @parameter {
7373
}
7474

7575
private VariableDeclarationEntry getANamedDeclarationEntry() {
76-
result = this.getAnEffectiveDeclarationEntry() and result.getName() != ""
76+
result = this.getAnEffectiveDeclarationEntry() and
77+
exists(string name | var_decls(unresolveElement(result), _, _, name, _) | name != "")
7778
}
7879

7980
/**

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ class VariableDeclarationEntry extends DeclarationEntry, @var_decl {
241241
name != "" and result = name
242242
or
243243
name = "" and result = this.getVariable().(LocalVariable).getName()
244+
or
245+
name = "" and
246+
not this instanceof ParameterDeclarationEntry and
247+
result = this.getVariable().(Parameter).getName()
244248
)
245249
)
246250
}
@@ -295,19 +299,11 @@ class ParameterDeclarationEntry extends VariableDeclarationEntry {
295299

296300
private string getAnonymousParameterDescription() {
297301
not exists(this.getName()) and
298-
exists(string idx |
299-
idx =
300-
((this.getIndex() + 1).toString() + "th")
301-
.replaceAll("1th", "1st")
302-
.replaceAll("2th", "2nd")
303-
.replaceAll("3th", "3rd")
304-
.replaceAll("11st", "11th")
305-
.replaceAll("12nd", "12th")
306-
.replaceAll("13rd", "13th") and
302+
exists(string anon |
303+
anon = "(unnamed parameter " + this.getIndex().toString() + ")" and
307304
if exists(this.getCanonicalName())
308-
then
309-
result = "declaration of " + this.getCanonicalName() + " as anonymous " + idx + " parameter"
310-
else result = "declaration of " + idx + " parameter"
305+
then result = "declaration of " + this.getCanonicalName() + " as " + anon
306+
else result = "declaration of " + anon
311307
)
312308
}
313309

cpp/ql/test/library-tests/declarationEntry/declarationEntry/roundTrip.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
| declarationEntry.cpp:39:7:39:7 | declaration of operator= | declarationEntry.cpp:39:7:39:7 | operator= | yes |
2626
| declarationEntry.cpp:39:7:39:13 | definition of myClass | declarationEntry.cpp:39:7:39:13 | myClass | yes |
2727
| declarationEntry.cpp:42:6:42:21 | definition of myMemberVariable | declarationEntry.cpp:42:6:42:21 | myMemberVariable | yes |
28-
| file://:0:0:0:0 | declaration of 1st parameter | file://:0:0:0:0 | (unnamed parameter 0) | yes |
29-
| file://:0:0:0:0 | declaration of 1st parameter | file://:0:0:0:0 | (unnamed parameter 0) | yes |
28+
| file://:0:0:0:0 | declaration of (unnamed parameter 0) | file://:0:0:0:0 | (unnamed parameter 0) | yes |
29+
| file://:0:0:0:0 | declaration of (unnamed parameter 0) | file://:0:0:0:0 | (unnamed parameter 0) | yes |
3030
| file://:0:0:0:0 | definition of fp_offset | file://:0:0:0:0 | fp_offset | yes |
3131
| file://:0:0:0:0 | definition of gp_offset | file://:0:0:0:0 | gp_offset | yes |
3232
| file://:0:0:0:0 | definition of overflow_arg_area | file://:0:0:0:0 | overflow_arg_area | yes |

cpp/ql/test/library-tests/declarationEntry/more/declarationEntry.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
| file://:0:0:0:0 | declaration of 1st parameter |
2-
| file://:0:0:0:0 | declaration of 1st parameter |
3-
| file://:0:0:0:0 | declaration of 1st parameter |
4-
| file://:0:0:0:0 | declaration of 1st parameter |
1+
| file://:0:0:0:0 | declaration of (unnamed parameter 0) |
2+
| file://:0:0:0:0 | declaration of (unnamed parameter 0) |
3+
| file://:0:0:0:0 | declaration of (unnamed parameter 0) |
4+
| file://:0:0:0:0 | declaration of (unnamed parameter 0) |
55
| file://:0:0:0:0 | definition of fp_offset |
66
| file://:0:0:0:0 | definition of gp_offset |
77
| file://:0:0:0:0 | definition of overflow_arg_area |

cpp/ql/test/library-tests/parameters/toStrings/params.expected

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
| test.c:2:8:2:10 | declaration of 1st parameter |
2-
| test.c:2:13:2:15 | declaration of 2nd parameter |
3-
| test.c:2:18:2:20 | declaration of 3rd parameter |
4-
| test.c:2:23:2:25 | declaration of 4th parameter |
5-
| test.c:3:8:3:10 | declaration of y1 as anonymous 1st parameter |
6-
| test.c:3:13:3:15 | declaration of y2 as anonymous 2nd parameter |
7-
| test.c:3:18:3:20 | declaration of y3 as anonymous 3rd parameter |
8-
| test.c:3:23:3:25 | declaration of y4 as anonymous 4th parameter |
1+
| test.c:2:8:2:10 | declaration of (unnamed parameter 0) |
2+
| test.c:2:13:2:15 | declaration of (unnamed parameter 1) |
3+
| test.c:2:18:2:20 | declaration of (unnamed parameter 2) |
4+
| test.c:2:23:2:25 | declaration of (unnamed parameter 3) |
5+
| test.c:3:8:3:10 | declaration of y1 as (unnamed parameter 0) |
6+
| test.c:3:13:3:15 | declaration of y2 as (unnamed parameter 1) |
7+
| test.c:3:18:3:20 | declaration of y3 as (unnamed parameter 2) |
8+
| test.c:3:23:3:25 | declaration of y4 as (unnamed parameter 3) |
99
| test.c:4:12:4:13 | declaration of x1 |
1010
| test.c:4:20:4:21 | declaration of x2 |
1111
| test.c:4:28:4:29 | declaration of x3 |

cpp/ql/test/query-tests/Metrics/Dependencies/dependencies.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| file://:0:0:0:0 | declaration of 1st parameter | LibB/libb_internal.h:5:8:5:12 | thing |
2-
| file://:0:0:0:0 | declaration of 1st parameter | LibB/libb_internal.h:5:8:5:12 | thing |
1+
| file://:0:0:0:0 | declaration of (unnamed parameter 0) | LibB/libb_internal.h:5:8:5:12 | thing |
2+
| file://:0:0:0:0 | declaration of (unnamed parameter 0) | LibB/libb_internal.h:5:8:5:12 | thing |
33
| include.h:3:25:3:33 | num | LibD/libd.h:5:12:5:14 | num |
44
| main.cpp:8:31:8:31 | call to container | LibC/libc.h:9:3:9:3 | container |
55
| main.cpp:8:31:8:31 | definition of x | LibB/libb_internal.h:5:8:5:12 | thing |

0 commit comments

Comments
 (0)