From 1eb0b49634d894957b6a5730bd549576541bd9a5 Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Tue, 15 Oct 2024 18:02:06 +0200 Subject: [PATCH] C++: Improve parameter naming --- cpp/ql/lib/semmle/code/cpp/Parameter.qll | 3 ++- cpp/ql/lib/semmle/code/cpp/Variable.qll | 20 ++++++++----------- .../declarationEntry/roundTrip.expected | 4 ++-- .../more/declarationEntry.expected | 8 ++++---- .../parameters/toStrings/params.expected | 16 +++++++-------- .../Dependencies/dependencies.expected | 4 ++-- 6 files changed, 26 insertions(+), 29 deletions(-) diff --git a/cpp/ql/lib/semmle/code/cpp/Parameter.qll b/cpp/ql/lib/semmle/code/cpp/Parameter.qll index 49e47d40ef0a..e45dd56e7204 100644 --- a/cpp/ql/lib/semmle/code/cpp/Parameter.qll +++ b/cpp/ql/lib/semmle/code/cpp/Parameter.qll @@ -73,7 +73,8 @@ class Parameter extends LocalScopeVariable, @parameter { } private VariableDeclarationEntry getANamedDeclarationEntry() { - result = this.getAnEffectiveDeclarationEntry() and result.getName() != "" + result = this.getAnEffectiveDeclarationEntry() and + exists(string name | var_decls(unresolveElement(result), _, _, name, _) | name != "") } /** diff --git a/cpp/ql/lib/semmle/code/cpp/Variable.qll b/cpp/ql/lib/semmle/code/cpp/Variable.qll index 96bfabb4de07..b3b619c4ccc5 100644 --- a/cpp/ql/lib/semmle/code/cpp/Variable.qll +++ b/cpp/ql/lib/semmle/code/cpp/Variable.qll @@ -241,6 +241,10 @@ class VariableDeclarationEntry extends DeclarationEntry, @var_decl { name != "" and result = name or name = "" and result = this.getVariable().(LocalVariable).getName() + or + name = "" and + not this instanceof ParameterDeclarationEntry and + result = this.getVariable().(Parameter).getName() ) ) } @@ -295,19 +299,11 @@ class ParameterDeclarationEntry extends VariableDeclarationEntry { private string getAnonymousParameterDescription() { not exists(this.getName()) and - exists(string idx | - idx = - ((this.getIndex() + 1).toString() + "th") - .replaceAll("1th", "1st") - .replaceAll("2th", "2nd") - .replaceAll("3th", "3rd") - .replaceAll("11st", "11th") - .replaceAll("12nd", "12th") - .replaceAll("13rd", "13th") and + exists(string anon | + anon = "(unnamed parameter " + this.getIndex().toString() + ")" and if exists(this.getCanonicalName()) - then - result = "declaration of " + this.getCanonicalName() + " as anonymous " + idx + " parameter" - else result = "declaration of " + idx + " parameter" + then result = "declaration of " + this.getCanonicalName() + " as " + anon + else result = "declaration of " + anon ) } diff --git a/cpp/ql/test/library-tests/declarationEntry/declarationEntry/roundTrip.expected b/cpp/ql/test/library-tests/declarationEntry/declarationEntry/roundTrip.expected index 2f9ea170973e..29366af69930 100644 --- a/cpp/ql/test/library-tests/declarationEntry/declarationEntry/roundTrip.expected +++ b/cpp/ql/test/library-tests/declarationEntry/declarationEntry/roundTrip.expected @@ -25,8 +25,8 @@ | declarationEntry.cpp:39:7:39:7 | declaration of operator= | declarationEntry.cpp:39:7:39:7 | operator= | yes | | declarationEntry.cpp:39:7:39:13 | definition of myClass | declarationEntry.cpp:39:7:39:13 | myClass | yes | | declarationEntry.cpp:42:6:42:21 | definition of myMemberVariable | declarationEntry.cpp:42:6:42:21 | myMemberVariable | yes | -| file://:0:0:0:0 | declaration of 1st parameter | file://:0:0:0:0 | (unnamed parameter 0) | yes | -| file://:0:0:0:0 | declaration of 1st parameter | file://:0:0:0:0 | (unnamed parameter 0) | yes | +| file://:0:0:0:0 | declaration of (unnamed parameter 0) | file://:0:0:0:0 | (unnamed parameter 0) | yes | +| file://:0:0:0:0 | declaration of (unnamed parameter 0) | file://:0:0:0:0 | (unnamed parameter 0) | yes | | file://:0:0:0:0 | definition of fp_offset | file://:0:0:0:0 | fp_offset | yes | | file://:0:0:0:0 | definition of gp_offset | file://:0:0:0:0 | gp_offset | yes | | file://:0:0:0:0 | definition of overflow_arg_area | file://:0:0:0:0 | overflow_arg_area | yes | diff --git a/cpp/ql/test/library-tests/declarationEntry/more/declarationEntry.expected b/cpp/ql/test/library-tests/declarationEntry/more/declarationEntry.expected index b4817f60ad14..767da646f5e0 100644 --- a/cpp/ql/test/library-tests/declarationEntry/more/declarationEntry.expected +++ b/cpp/ql/test/library-tests/declarationEntry/more/declarationEntry.expected @@ -1,7 +1,7 @@ -| file://:0:0:0:0 | declaration of 1st parameter | -| file://:0:0:0:0 | declaration of 1st parameter | -| file://:0:0:0:0 | declaration of 1st parameter | -| file://:0:0:0:0 | declaration of 1st parameter | +| file://:0:0:0:0 | declaration of (unnamed parameter 0) | +| file://:0:0:0:0 | declaration of (unnamed parameter 0) | +| file://:0:0:0:0 | declaration of (unnamed parameter 0) | +| file://:0:0:0:0 | declaration of (unnamed parameter 0) | | file://:0:0:0:0 | definition of fp_offset | | file://:0:0:0:0 | definition of gp_offset | | file://:0:0:0:0 | definition of overflow_arg_area | diff --git a/cpp/ql/test/library-tests/parameters/toStrings/params.expected b/cpp/ql/test/library-tests/parameters/toStrings/params.expected index 908089b31e68..f5687137e87b 100644 --- a/cpp/ql/test/library-tests/parameters/toStrings/params.expected +++ b/cpp/ql/test/library-tests/parameters/toStrings/params.expected @@ -1,11 +1,11 @@ -| test.c:2:8:2:10 | declaration of 1st parameter | -| test.c:2:13:2:15 | declaration of 2nd parameter | -| test.c:2:18:2:20 | declaration of 3rd parameter | -| test.c:2:23:2:25 | declaration of 4th parameter | -| test.c:3:8:3:10 | declaration of y1 as anonymous 1st parameter | -| test.c:3:13:3:15 | declaration of y2 as anonymous 2nd parameter | -| test.c:3:18:3:20 | declaration of y3 as anonymous 3rd parameter | -| test.c:3:23:3:25 | declaration of y4 as anonymous 4th parameter | +| test.c:2:8:2:10 | declaration of (unnamed parameter 0) | +| test.c:2:13:2:15 | declaration of (unnamed parameter 1) | +| test.c:2:18:2:20 | declaration of (unnamed parameter 2) | +| test.c:2:23:2:25 | declaration of (unnamed parameter 3) | +| test.c:3:8:3:10 | declaration of y1 as (unnamed parameter 0) | +| test.c:3:13:3:15 | declaration of y2 as (unnamed parameter 1) | +| test.c:3:18:3:20 | declaration of y3 as (unnamed parameter 2) | +| test.c:3:23:3:25 | declaration of y4 as (unnamed parameter 3) | | test.c:4:12:4:13 | declaration of x1 | | test.c:4:20:4:21 | declaration of x2 | | test.c:4:28:4:29 | declaration of x3 | diff --git a/cpp/ql/test/query-tests/Metrics/Dependencies/dependencies.expected b/cpp/ql/test/query-tests/Metrics/Dependencies/dependencies.expected index 6c93362a0961..9e7f88376f88 100644 --- a/cpp/ql/test/query-tests/Metrics/Dependencies/dependencies.expected +++ b/cpp/ql/test/query-tests/Metrics/Dependencies/dependencies.expected @@ -1,5 +1,5 @@ -| file://:0:0:0:0 | declaration of 1st parameter | LibB/libb_internal.h:5:8:5:12 | thing | -| file://:0:0:0:0 | declaration of 1st parameter | LibB/libb_internal.h:5:8:5:12 | thing | +| file://:0:0:0:0 | declaration of (unnamed parameter 0) | LibB/libb_internal.h:5:8:5:12 | thing | +| file://:0:0:0:0 | declaration of (unnamed parameter 0) | LibB/libb_internal.h:5:8:5:12 | thing | | include.h:3:25:3:33 | num | LibD/libd.h:5:12:5:14 | num | | main.cpp:8:31:8:31 | call to container | LibC/libc.h:9:3:9:3 | container | | main.cpp:8:31:8:31 | definition of x | LibB/libb_internal.h:5:8:5:12 | thing |