Skip to content

Commit 4b23b50

Browse files
committed
Add RULE-6-7-1
1 parent 9ee2c07 commit 4b23b50

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @id cpp/misra/local-variable-static-storage-duration
3+
* @name RULE-6-7-1: Local variables shall not have static storage duration
4+
* @description Local variables that have static storage duration can be harder to reason about and
5+
* can lead to undefined behavior.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-6-7-1
10+
* correctness
11+
* maintainability
12+
* readability
13+
* scope/single-translation-unit
14+
* external/misra/enforcement/decidable
15+
* external/misra/obligation/required
16+
*/
17+
18+
import cpp
19+
import codingstandards.cpp.misra
20+
import codingstandards.cpp.lifetimes.CppObjects
21+
22+
from VariableObjectIdentity v
23+
where
24+
not isExcluded(v, Declarations2Package::localVariableStaticStorageDurationQuery()) and
25+
v.getStorageDuration().isStatic() and
26+
v instanceof LocalVariable and
27+
not v.(Variable).isConst() and
28+
not v.(Variable).isConstexpr()
29+
select v, "Local variable with static storage duration."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:7:14:7:15 | i1 | Local variable with static storage duration. |
2+
| test.cpp:14:14:14:14 | a | Local variable with static storage duration. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-6-7-1/LocalVariableStaticStorageDuration.ql
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
int f();
2+
3+
int g = 0; // COMPLIANT
4+
5+
void f1() {
6+
int i = 0; // COMPLIANT
7+
static int i1 = 0; // NON_COMPLIANT
8+
static constexpr int i2 = 0; // COMPLIANT
9+
static const int i3 = f(); // COMPLIANT
10+
}
11+
12+
class A {
13+
static A theClass() {
14+
static A a; // NON_COMPLIANT
15+
return a;
16+
}
17+
};

0 commit comments

Comments
 (0)