Skip to content

Commit eaf9ccb

Browse files
committed
Fix #658
1 parent 02dfac9 commit eaf9ccb

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

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

Lines changed: 10 additions & 3 deletions
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,7 +41,14 @@ 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
Lines changed: 5 additions & 6 deletions
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

Lines changed: 18 additions & 4 deletions
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); }

cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ class PotentiallyUnusedLocalVariable extends LocalVariable {
5252
// exclude uninstantiated template members
5353
not this.isFromUninstantiatedTemplate(_) and
5454
// Do not report compiler generated variables
55-
not this.isCompilerGenerated()
55+
not this.isCompilerGenerated() and
56+
not exists(LocalScopeVariable another |
57+
another.getDefinitionLocation() = this.getDefinitionLocation() and
58+
another.hasName(this.getName()) and
59+
exists(another.getAnAccess())
60+
)
5661
}
5762
}
5863

0 commit comments

Comments
 (0)