From 17f3a32f121e47e53015448e2c5e3aaca31105f4 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Fri, 7 Jun 2024 10:57:06 +0100 Subject: [PATCH 1/2] M0-1-3: Exclude uninstantiated variable templates These cause false positives because they are never considered "used". --- .../rules/M0-1-3/UnusedLocalVariable.expected | 1 + cpp/autosar/test/rules/M0-1-3/test.cpp | 21 +++++++++++++++++++ .../cpp/deadcode/UnusedVariables.qll | 15 +++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cpp/autosar/test/rules/M0-1-3/UnusedLocalVariable.expected b/cpp/autosar/test/rules/M0-1-3/UnusedLocalVariable.expected index 77eb030716..d6f398369f 100644 --- a/cpp/autosar/test/rules/M0-1-3/UnusedLocalVariable.expected +++ b/cpp/autosar/test/rules/M0-1-3/UnusedLocalVariable.expected @@ -4,3 +4,4 @@ | test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. | | test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. | | test.cpp:44:6:44:6 | a | Local variable 'a' in 'test_side_effect_init' is not used. | +| test.cpp:91:5:91:5 | t | Local variable 't' in 'template_function' is not used. | diff --git a/cpp/autosar/test/rules/M0-1-3/test.cpp b/cpp/autosar/test/rules/M0-1-3/test.cpp index 9dbe9692cd..a2b0dda35d 100644 --- a/cpp/autosar/test/rules/M0-1-3/test.cpp +++ b/cpp/autosar/test/rules/M0-1-3/test.cpp @@ -78,3 +78,24 @@ int baz() { test_constexpr_in_static_assert(); return 0; } + +template extern constexpr bool all_of_v = true; // COMPLIANT + +template +extern constexpr bool all_of_v = + B1 && all_of_v; // COMPLIANT + +void test_template_variable() { all_of_v; } + +template void template_function() { + T t; // NON_COMPLIANT - t is never used + T t2; // COMPLIANT - t is used + t2.test(); // Call may not be resolved in uninstantiated template +} + +class ClassT { +public: + void test() {} +}; + +void test_template_function() { template_function(); } \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll b/cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll index 077c35a2aa..e4048472d3 100644 --- a/cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll +++ b/cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll @@ -49,6 +49,11 @@ class PotentiallyUnusedLocalVariable extends LocalVariable { // Ignore functions with error expressions as they indicate expressions that the extractor couldn't process not any(ErrorExpr e).getEnclosingFunction() = f ) + // ) and + // // exclude uninstantiated template members + // not this.isFromUninstantiatedTemplate(_) and + // // Do not report compiler generated variables + // not this.isCompilerGenerated() } } @@ -95,7 +100,9 @@ class PotentiallyUnusedMemberVariable extends MemberVariable { // Lambda captures are not "real" member variables - it's an implementation detail that they are represented that way not this = any(LambdaCapture lc).getField() and // exclude uninstantiated template members - not this.isFromUninstantiatedTemplate(_) + not this.isFromUninstantiatedTemplate(_) and + // Do not report compiler generated variables + not this.isCompilerGenerated() } } @@ -107,7 +114,11 @@ class PotentiallyUnusedGlobalOrNamespaceVariable extends GlobalOrNamespaceVariab // Not declared in a macro expansion not isInMacroExpansion() and // No side-effects from declaration - not declarationHasSideEffects(this) + not declarationHasSideEffects(this) and + // exclude uninstantiated template members + not this.isFromUninstantiatedTemplate(_) and + // Do not report compiler generated variables + not this.isCompilerGenerated() } } From f5a1fc2a0758a977eec0e2b4e40a57b12845ad08 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Wed, 26 Jun 2024 10:56:52 +0100 Subject: [PATCH 2/2] Address review comments --- .../2024-06-07-m0-1-3-uninstantiated-templates.md | 2 ++ cpp/autosar/test/rules/M0-1-3/test.cpp | 2 +- .../codingstandards/cpp/deadcode/UnusedVariables.qll | 11 +++++------ 3 files changed, 8 insertions(+), 7 deletions(-) create mode 100644 change_notes/2024-06-07-m0-1-3-uninstantiated-templates.md diff --git a/change_notes/2024-06-07-m0-1-3-uninstantiated-templates.md b/change_notes/2024-06-07-m0-1-3-uninstantiated-templates.md new file mode 100644 index 0000000000..0dcb7c1c1a --- /dev/null +++ b/change_notes/2024-06-07-m0-1-3-uninstantiated-templates.md @@ -0,0 +1,2 @@ + - `M0-1-3` - `UnusedGlobalOrNamespaceVariable.ql` + - Reduces false positives by excluding compiler generated variables, and variables in uninstantiated templates. \ No newline at end of file diff --git a/cpp/autosar/test/rules/M0-1-3/test.cpp b/cpp/autosar/test/rules/M0-1-3/test.cpp index a2b0dda35d..a591c7e82b 100644 --- a/cpp/autosar/test/rules/M0-1-3/test.cpp +++ b/cpp/autosar/test/rules/M0-1-3/test.cpp @@ -83,7 +83,7 @@ template extern constexpr bool all_of_v = true; // COMPLIANT template extern constexpr bool all_of_v = - B1 && all_of_v; // COMPLIANT + B1 &&all_of_v; // COMPLIANT void test_template_variable() { all_of_v; } diff --git a/cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll b/cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll index e4048472d3..f4607d82cb 100644 --- a/cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll +++ b/cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll @@ -48,12 +48,11 @@ class PotentiallyUnusedLocalVariable extends LocalVariable { not exists(AsmStmt s | f = s.getEnclosingFunction()) and // Ignore functions with error expressions as they indicate expressions that the extractor couldn't process not any(ErrorExpr e).getEnclosingFunction() = f - ) - // ) and - // // exclude uninstantiated template members - // not this.isFromUninstantiatedTemplate(_) and - // // Do not report compiler generated variables - // not this.isCompilerGenerated() + ) and + // exclude uninstantiated template members + not this.isFromUninstantiatedTemplate(_) and + // Do not report compiler generated variables + not this.isCompilerGenerated() } }