Skip to content

Commit

Permalink
Merge pull request #732 from fjatWbyT/fix-constexpr-array-size-a0-1-1
Browse files Browse the repository at this point in the history
A0-1-1: Fix useless assignment false positive on constexpr array size
  • Loading branch information
lcartey authored Oct 9, 2024
2 parents 2e8a503 + 7dfb343 commit 7736c34
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions change_notes/2024-10-04-fix-constexpr-arr-size-fp-a0-1-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `A0-1-1` - `UselessAssignments.qll`:
- Remove (dead code) useless assignment false positive when integer constant expression is used to define the size of an array.
2 changes: 2 additions & 0 deletions cpp/autosar/test/rules/A0-1-1/UselessAssignment.expected
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
| test.cpp:94:11:94:17 | new | Definition of $@ is unused. | test.cpp:94:6:94:7 | b4 | b4 |
| test.cpp:95:11:95:17 | 0 | Definition of $@ is unused. | test.cpp:95:6:95:7 | b5 | b5 |
| test.cpp:103:11:103:17 | 0 | Definition of $@ is unused. | test.cpp:103:6:103:7 | c5 | c5 |
| test.cpp:132:43:132:45 | {...} | Definition of $@ is unused. | test.cpp:132:7:132:18 | unused_array | unused_array |
| test.cpp:134:29:134:31 | 0 | Definition of $@ is unused. | test.cpp:134:17:134:26 | unused_int | unused_int |
14 changes: 13 additions & 1 deletion cpp/autosar/test/rules/A0-1-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,16 @@ template <typename T> void test_range_based_for_loop_template() {
// template
elem;
}
}
}

#include <cstdint>

std::int32_t test_constexpr_array_size() {
constexpr int constexpr_array_size = 7; // COMPLIANT
int unused_array[constexpr_array_size] = {}; // NON_COMPLIANT

constexpr int unused_int = {}; // NON_COMPLIANT

std::int32_t used_array[] = {-1, 0, 1}; // COMPLIANT
return used_array[1];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import cpp
import codingstandards.cpp.deadcode.UnusedVariables
import codingstandards.cpp.enhancements.ControlFlowGraphEnhancements

/** If a variable may escape from the local context */
Expand Down Expand Up @@ -47,7 +48,9 @@ class InterestingStackVariable extends StackVariable {
// 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()
not this.isCompilerGenerated() and
// Explicitly ignore (propagated) constants that may be used to define sizes of local arrays
not countUsesInLocalArraySize(this) > 0
}
}

Expand Down

0 comments on commit 7736c34

Please sign in to comment.