Skip to content

Commit e37a13c

Browse files
authored
Merge pull request #146 from knewbury01/knewbury01/Declarations5
Implement C Declarations5 package
2 parents 7d20e9c + e9b606d commit e37a13c

File tree

45 files changed

+884
-287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+884
-287
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
"Declarations2",
209209
"Declarations3",
210210
"Declarations4",
211+
"Declarations5",
211212
"Exceptions1",
212213
"Exceptions2",
213214
"Expressions",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:2:6:2:7 | definition of f1 | The redeclaration of $@ with internal linkage misses the static specifier. | test.c:1:13:1:14 | declaration of f1 | function |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.missingstaticspecifierfunctionredeclarationshared.MissingStaticSpecifierFunctionRedeclarationShared
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @id c/misra/identifiers-declared-in-the-same-scope-not-distinct
3+
* @name RULE-5-2: Identifiers declared in the same scope and name space shall be distinct
4+
* @description Using nondistinct identifiers results in undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-5-2
9+
* correctness
10+
* maintainability
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.Identifiers
18+
19+
from InterestingIdentifiers d, InterestingIdentifiers d2
20+
where
21+
not isExcluded(d, Declarations5Package::identifiersDeclaredInTheSameScopeNotDistinctQuery()) and
22+
not isExcluded(d2, Declarations5Package::identifiersDeclaredInTheSameScopeNotDistinctQuery()) and
23+
//this rule does not apply if both are external identifiers
24+
//that is covered by RULE-5-3
25+
not (
26+
d instanceof ExternalIdentifiers and
27+
d2 instanceof ExternalIdentifiers
28+
) and
29+
d.getNamespace() = d2.getNamespace() and
30+
d.getParentScope() = d2.getParentScope() and
31+
not d = d2 and
32+
d.getLocation().getStartLine() >= d2.getLocation().getStartLine() and
33+
//first 63 chars in the name as per C99
34+
d.getSignificantNameComparedToMacro() = d2.getSignificantNameComparedToMacro() and
35+
not d.getName() = d2.getName()
36+
select d,
37+
"Identifer " + d.getName() + " is nondistinct in characters at or over 63 limit, compared to $@",
38+
d2, d2.getName()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @id c/misra/external-object-or-function-not-declared-in-one-file
3+
* @name RULE-8-5: An external object or function shall be declared once in one and only one file
4+
* @description Declarations in multiple files can lead to unexpected program behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-8-5
9+
* correctness
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
16+
from DeclarationEntry de, DeclarationEntry otherDeclaration, string kind
17+
where
18+
not isExcluded(de, Declarations5Package::externalObjectOrFunctionNotDeclaredInOneFileQuery()) and
19+
//this rule applies to non-defining declarations only
20+
not de.isDefinition() and
21+
not otherDeclaration.isDefinition() and
22+
exists(Declaration d |
23+
de.getDeclaration() = d and
24+
otherDeclaration.getDeclaration() = d and
25+
de.getFile() != otherDeclaration.getFile()
26+
) and
27+
(
28+
de.getDeclaration() instanceof Function and kind = "function"
29+
or
30+
de.getDeclaration() instanceof Variable and
31+
not de.getDeclaration() instanceof Parameter and
32+
kind = "variable"
33+
) and
34+
// Apply an ordering based on location to enforce that (de1, de2) = (de2, de1) and we only report (de1, de2).
35+
de.getFile().getAbsolutePath() < otherDeclaration.getFile().getAbsolutePath()
36+
select de,
37+
"The " + kind + " declaration " + de.getName() +
38+
" is declared in multiple files and has an additional $@.", otherDeclaration, "declaration"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/missing-static-specifier-function-redeclaration-c
3+
* @name RULE-8-8: If a function has internal linkage then all re-declarations shall include the static storage class
4+
* @description If a function has internal linkage then all re-declarations shall include the static
5+
* storage class specifier to make the internal linkage explicit.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-8-8
10+
* readability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.rules.missingstaticspecifierfunctionredeclarationshared.MissingStaticSpecifierFunctionRedeclarationShared
17+
18+
class MissingStaticSpecifierFunctionRedeclarationCQuery extends MissingStaticSpecifierFunctionRedeclarationSharedSharedQuery {
19+
MissingStaticSpecifierFunctionRedeclarationCQuery() {
20+
this = Declarations5Package::missingStaticSpecifierFunctionRedeclarationCQuery()
21+
}
22+
}

0 commit comments

Comments
 (0)