Skip to content

Commit 654f9fa

Browse files
committed
Declarations5: add RULE-8-9
1 parent f866dd5 commit 654f9fa

File tree

16 files changed

+474
-274
lines changed

16 files changed

+474
-274
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.c:4:12:4:13 | g2 | The declaration g2 should be moved from the global namespace scope$@ into the $@ too minimize its visibility. | file://:0:0:0:0 | (global namespace) | scope | test.c:59:11:59:25 | { ... } | scope |
2+
| test.c:7:7:7:7 | j | The declaration j should be moved from $@ into the $@ too minimize its visibility. | test.c:6:11:13:1 | { ... } | scope | test.c:8:13:12:3 | { ... } | scope |
3+
| test.c:62:7:62:7 | i | The declaration i should be moved from $@ into the $@ too minimize its visibility. | test.c:61:11:71:1 | { ... } | scope | test.c:64:13:70:3 | { ... } | scope |
4+
| test.c:73:8:73:9 | S1 | The declaration S1 should be moved from the global namespace scope$@ into the $@ too minimize its visibility. | file://:0:0:0:0 | (global namespace) | scope | test.c:77:12:77:28 | { ... } | scope |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.unnecessaryexposedidentifierdeclarationshared.UnnecessaryExposedIdentifierDeclarationShared
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <stdbool.h>
2+
extern void f1(int i);
3+
extern int g1; // COMPLIANT
4+
extern int g2; // NON_COMPLIANT; single use of a global variable
5+
bool f2() { return g1 == 1; }
6+
void f3() {
7+
int j = g1; // NON_COMPLIANT
8+
if (f2()) {
9+
int k; // COMPLIANT
10+
f1(j);
11+
f1(k);
12+
}
13+
}
14+
15+
void f4() {
16+
int j = g1; // COMPLIANT; value of g1 changed between
17+
// definition and use
18+
g1 = 1;
19+
if (f2()) {
20+
f1(j);
21+
}
22+
}
23+
24+
void f5() {
25+
int j = g1; // COMPLIANT; shouldn't be moved inside loop
26+
while (true) {
27+
int i = g1++;
28+
while (f2()) {
29+
i += j;
30+
}
31+
32+
if (i % 2)
33+
break;
34+
}
35+
}
36+
37+
void f6() {
38+
int j = g1; // COMPLIANT; can't moved into smaller scope
39+
#ifdef FOO
40+
if (g1) {
41+
g1 = j + 1;
42+
}
43+
#else
44+
if (g1) {
45+
g1 = j + 2;
46+
}
47+
#endif
48+
}
49+
50+
void f7() {
51+
int j = g1; // COMPLIANT; potentially stores previous value of
52+
// g1 so moving this would be incorrect.
53+
f1(1); // f1 may change the value of g1
54+
if (f2()) {
55+
f1(j);
56+
}
57+
}
58+
59+
void f8() { int i = g2; }
60+
61+
void f9() {
62+
int i; // NON_COMPLIANT
63+
64+
if (f2()) {
65+
if (f2()) {
66+
i++;
67+
} else {
68+
i--;
69+
}
70+
}
71+
}
72+
73+
struct S1 { // NON_COMPLIANT
74+
int i;
75+
};
76+
77+
void f10() { struct S1 l1; }
78+
79+
void f11() {
80+
struct S2 { // COMPLIANT
81+
int i;
82+
} l1;
83+
}
84+
85+
struct S3 {
86+
int i;
87+
};
88+
89+
struct S4 { // NON_COMPLIANT; single use in function f13
90+
int i;
91+
};
92+
93+
void f15() {
94+
int i; // COMPLIANT
95+
96+
if (i == 0) {
97+
i++;
98+
}
99+
}
100+
101+
void f17() {
102+
int i; // COMPLIANT
103+
int *ptr;
104+
{
105+
// Moving the declaration of i into the reduced scope will result in a
106+
// dangling pointer
107+
ptr = &i;
108+
}
109+
*ptr = 1;
110+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/unnecessary-exposed-identifier-declaration-c
3+
* @name RULE-8-9: An object should be defined at block scope if its identifier only appears in a single function
4+
* @description An identifier declared to be an object or type shall be defined in a block that
5+
* minimizes its visibility to prevent any accidental use of the identifier.
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-8-9
10+
* correctness
11+
* external/misra/obligation/advisory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.rules.unnecessaryexposedidentifierdeclarationshared.UnnecessaryExposedIdentifierDeclarationShared
17+
18+
class UnnecessaryExposedIdentifierDeclarationCQuery extends UnnecessaryExposedIdentifierDeclarationSharedSharedQuery {
19+
UnnecessaryExposedIdentifierDeclarationCQuery() {
20+
this = Declarations5Package::unnecessaryExposedIdentifierDeclarationCQuery()
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/unnecessaryexposedidentifierdeclarationshared/UnnecessaryExposedIdentifierDeclarationShared.ql

0 commit comments

Comments
 (0)