From b8a527cacc3e9118c14aec32af7336780797fa06 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Wed, 25 Oct 2023 13:28:34 +0100 Subject: [PATCH 1/5] A0-1-1: Ignore incomplete or compiler generated vars --- cpp/autosar/test/rules/A0-1-1/test.cpp | 13 ++++++++++++- .../cpp/deadcode/UselessAssignments.qll | 6 +++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cpp/autosar/test/rules/A0-1-1/test.cpp b/cpp/autosar/test/rules/A0-1-1/test.cpp index 824d649c6a..021b1bf792 100644 --- a/cpp/autosar/test/rules/A0-1-1/test.cpp +++ b/cpp/autosar/test/rules/A0-1-1/test.cpp @@ -112,4 +112,15 @@ int test_useless_assignment(int &x, int p) { return y; } -int main() { return 0; } \ No newline at end of file +int main() { return 0; } + +#include +template void test_range_based_for_loop_template() { + std::vector values_; + for (auto &elem : values_) { // COMPLIANT - should not report either elem or + // the compiler generated (__range) + // variable in the uninstantiated + // template + elem; + } +} \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/deadcode/UselessAssignments.qll b/cpp/common/src/codingstandards/cpp/deadcode/UselessAssignments.qll index e326f814be..465b023f3f 100644 --- a/cpp/common/src/codingstandards/cpp/deadcode/UselessAssignments.qll +++ b/cpp/common/src/codingstandards/cpp/deadcode/UselessAssignments.qll @@ -43,7 +43,11 @@ class InterestingStackVariable extends StackVariable { // A reference parameter can have an affect outside the enclosing function not mayEscape(this) and // Not a loop control variable, explicitly excluded - not this instanceof LoopControlVariable + not this instanceof LoopControlVariable and + // Ignore variables in uninstantiated templates + not this.isFromUninstantiatedTemplate(_) and + // Ignore compiler generated variables, such as those generated for range based for loops + not this.isCompilerGenerated() } } From bbc7a86f73490eb727f985e2afd1c1570d910617 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Wed, 25 Oct 2023 13:31:11 +0100 Subject: [PATCH 2/5] A0-1-1: Apply deviations to definition not variable --- cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql b/cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql index f6219abe4b..755f72075d 100644 --- a/cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql +++ b/cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql @@ -20,6 +20,7 @@ import codingstandards.cpp.deadcode.UselessAssignments from SsaDefinition ultimateDef, InterestingStackVariable v where + not isExcluded(ultimateDef, DeadCodePackage::uselessAssignmentQuery()) and not isExcluded(v, DeadCodePackage::uselessAssignmentQuery()) and isUselessSsaDefinition(ultimateDef, v) select ultimateDef, "Definition of $@ is unused.", v, v.getName() From 9caece34adfb5eb0162ce2dff3f9a383f90e8c6c Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Wed, 25 Oct 2023 13:34:29 +0100 Subject: [PATCH 3/5] Add change notes --- change_notes/2023-10-25-a0-1-1.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 change_notes/2023-10-25-a0-1-1.md diff --git a/change_notes/2023-10-25-a0-1-1.md b/change_notes/2023-10-25-a0-1-1.md new file mode 100644 index 0000000000..91695ea524 --- /dev/null +++ b/change_notes/2023-10-25-a0-1-1.md @@ -0,0 +1,4 @@ + * `A0-1-1` - address a number of false positive issues: + * Exclude compiler-generated variables, such as those generated for range-based for loops. + * Exclude variables in uninstantiated templates, for which we have no precise data on uses. + * Deviations can now be applied to the useless assignment as well as the variable itself. \ No newline at end of file From 298d3600b6a55ddb4a025d899ff347e950e267bc Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Tue, 7 Nov 2023 17:09:50 -0800 Subject: [PATCH 4/5] Remove unnecessary isExcluded Applying the exclusion to the variable is confusing and unnecessary in this particular case: int x = 0; // Does the deviation apply to the assignment or var? --- cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql | 1 - 1 file changed, 1 deletion(-) diff --git a/cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql b/cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql index 755f72075d..a1c6fb1fa8 100644 --- a/cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql +++ b/cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql @@ -21,6 +21,5 @@ import codingstandards.cpp.deadcode.UselessAssignments from SsaDefinition ultimateDef, InterestingStackVariable v where not isExcluded(ultimateDef, DeadCodePackage::uselessAssignmentQuery()) and - not isExcluded(v, DeadCodePackage::uselessAssignmentQuery()) and isUselessSsaDefinition(ultimateDef, v) select ultimateDef, "Definition of $@ is unused.", v, v.getName() From 5d427e4aa32373283377bfeb26b2cbb5c224d5ae Mon Sep 17 00:00:00 2001 From: Luke Cartey <5377966+lcartey@users.noreply.github.com> Date: Tue, 21 Nov 2023 08:25:25 +0000 Subject: [PATCH 5/5] Update change notes --- change_notes/2023-10-25-a0-1-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/change_notes/2023-10-25-a0-1-1.md b/change_notes/2023-10-25-a0-1-1.md index 91695ea524..bd85f52143 100644 --- a/change_notes/2023-10-25-a0-1-1.md +++ b/change_notes/2023-10-25-a0-1-1.md @@ -1,4 +1,4 @@ * `A0-1-1` - address a number of false positive issues: * Exclude compiler-generated variables, such as those generated for range-based for loops. * Exclude variables in uninstantiated templates, for which we have no precise data on uses. - * Deviations can now be applied to the useless assignment as well as the variable itself. \ No newline at end of file + * Deviations should now be applied to the useless assignment instead of the variable itself.