Skip to content

Commit 6e090fe

Browse files
authored
Merge pull request #660 from rak3-sh/fix-m0-1-3-658
M0-1-3: Consider local variable usages in array size and template function arguments
2 parents 2b1c295 + 3b062af commit 6e090fe

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M0-1-3` - `UnusedLocalVariable.ql`:
2+
- Fixes #658. Considers usage of const/constexpr variables in array size and function parameters that are used in arguments of template functions.

cpp/autosar/src/rules/M0-1-3/UnusedLocalVariable.ql

+18-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import cpp
1818
import codingstandards.cpp.autosar
1919
import codingstandards.cpp.deadcode.UnusedVariables
2020

21-
/** Gets the constant value of a constexpr variable. */
21+
/** Gets the constant value of a constexpr/const variable. */
2222
private string getConstExprValue(Variable v) {
2323
result = v.getInitializer().getExpr().getValue() and
24-
v.isConstexpr()
24+
(v.isConst() or v.isConstexpr())
2525
}
2626

2727
// This predicate is similar to getUseCount for M0-1-4 except that it also
@@ -41,13 +41,28 @@ int getUseCountConservatively(Variable v) {
4141
) +
4242
// For static asserts too, check if there is a child which has the same value
4343
// as the constexpr variable.
44-
count(StaticAssert s | s.getCondition().getAChild*().getValue() = getConstExprValue(v))
44+
count(StaticAssert s | s.getCondition().getAChild*().getValue() = getConstExprValue(v)) +
45+
// In case an array type uses a constant in the same scope as the constexpr variable,
46+
// consider it as used.
47+
count(ArrayType at, LocalVariable arrayVariable |
48+
arrayVariable.getType().resolveTypedefs() = at and
49+
v.(PotentiallyUnusedLocalVariable).getFunction() = arrayVariable.getFunction() and
50+
at.getArraySize().toString() = getConstExprValue(v)
51+
)
4552
}
4653

4754
from PotentiallyUnusedLocalVariable v
4855
where
4956
not isExcluded(v, DeadCodePackage::unusedLocalVariableQuery()) and
5057
// Local variable is never accessed
5158
not exists(v.getAnAccess()) and
59+
// Sometimes multiple objects representing the same entities are created in
60+
// the AST. Check if those are not accessed as well. Refer issue #658
61+
not exists(LocalScopeVariable another |
62+
another.getDefinitionLocation() = v.getDefinitionLocation() and
63+
another.hasName(v.getName()) and
64+
exists(another.getAnAccess()) and
65+
another != v
66+
) and
5267
getUseCountConservatively(v) = 0
5368
select v, "Local variable '" + v.getName() + "' in '" + v.getFunction().getName() + "' is not used."
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
| test.cpp:7:7:7:7 | y | Local variable 'y' in 'test_simple' is not used. |
2-
| test.cpp:14:13:14:13 | y | Local variable 'y' in 'test_const' is not used. |
3-
| test.cpp:17:7:17:7 | z | Local variable 'z' in 'test_const' is not used. |
4-
| test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. |
5-
| test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. |
6-
| test.cpp:44:6:44:6 | a | Local variable 'a' in 'test_side_effect_init' is not used. |
7-
| test.cpp:91:5:91:5 | t | Local variable 't' in 'template_function' is not used. |
2+
| test.cpp:15:7:15:7 | z | Local variable 'z' in 'test_const' is not used. |
3+
| test.cpp:21:5:21:5 | t | Local variable 't' in 'f1' is not used. |
4+
| test.cpp:21:5:21:5 | t | Local variable 't' in 'f1' is not used. |
5+
| test.cpp:42:6:42:6 | a | Local variable 'a' in 'test_side_effect_init' is not used. |
6+
| test.cpp:89:5:89:5 | t | Local variable 't' in 'template_function' is not used. |

cpp/autosar/test/rules/M0-1-3/test.cpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ int test_simple() {
1111

1212
int test_const() {
1313
const int x = 1; // COMPLIANT - used below
14-
const int y = 2; // COMPLIANT[FALSE_POSITIVE] - used in array initialization,
15-
// but the database does not contain sufficient information
16-
// for this case
14+
const int y = 2; // COMPLIANT - used in array initialization,
1715
int z[y]; // NON_COMPLIANT - never used
1816
return x;
1917
}
@@ -98,4 +96,20 @@ class ClassT {
9896
void test() {}
9997
};
10098

101-
void test_template_function() { template_function<ClassT>(); }
99+
void test_template_function() { template_function<ClassT>(); }
100+
101+
int foo() {
102+
constexpr int arrayDim = 10; // COMPLIANT - used in array size below
103+
static int array[arrayDim]{};
104+
return array[4];
105+
}
106+
107+
template <typename T> static T another_templ_function() { return T(); }
108+
109+
template <typename T, typename First, typename... Rest>
110+
static T another_templ_function(const First &first, const Rest &...rest) {
111+
return first +
112+
another_templ_function<T>(rest...); // COMPLIANT - 'rest' is used here
113+
}
114+
115+
static int templ_fnc2() { return another_templ_function<int>(1, 2, 3, 4, 5); }

0 commit comments

Comments
 (0)