Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e37a13c

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

File tree

45 files changed

+884
-287
lines changed
Filter options

Some content is hidden

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

45 files changed

+884
-287
lines changed

‎.vscode/tasks.json

Copy file name to clipboardExpand all lines: .vscode/tasks.json
+1Lines 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
+110Lines 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+
}
+38Lines 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()
+38Lines 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"
+22Lines 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+
}
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @id c/misra/missing-static-specifier-object-redeclaration-c
3+
* @name RULE-8-8: If an object has internal linkage then all re-declarations shall include the static storage class
4+
* @description If an object 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+
17+
from VariableDeclarationEntry redeclaration, VariableDeclarationEntry de
18+
where
19+
not isExcluded(redeclaration,
20+
Declarations5Package::missingStaticSpecifierObjectRedeclarationCQuery()) and
21+
//following implies de != redeclaration
22+
de.hasSpecifier("static") and
23+
not redeclaration.hasSpecifier("static") and
24+
de.getDeclaration().isTopLevel() and
25+
redeclaration.getDeclaration() = de.getDeclaration()
26+
select redeclaration, "The redeclaration of $@ with internal linkage misses the static specifier.",
27+
de, de.getName()
+22Lines 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+
}
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:8:5:8:68 | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB | Identifer iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB is nondistinct in characters at or over 63 limit, compared to $@ | test.c:2:5:2:68 | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA |
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-2/IdentifiersDeclaredInTheSameScopeNotDistinct.ql

‎c/misra/test/rules/RULE-5-2/test.c

Copy file name to clipboard
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
extern int
2+
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA; // NON_COMPLIANT
3+
// -
4+
// length
5+
// 64
6+
7+
static int
8+
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB; // NON_COMPLIANT
9+
// -
10+
// length
11+
// 64
12+
13+
void f() {
14+
int iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyC; // COMPLIANT
15+
// -
16+
// length
17+
// 64
18+
// but
19+
// diff
20+
// scope
21+
}
22+
23+
static int
24+
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjy_C; // COMPLIANT length <63
25+
static int
26+
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjy_D; // COMPLIANT length <63
27+
28+
#define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA // COMPLIANT
29+
// -
30+
// this
31+
// rule
32+
// does
33+
// not
34+
// consider
35+
// macros
36+
extern int
37+
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA; // COMPLIANT
38+
// - this
39+
// rule
40+
// does
41+
// not
42+
// consider
43+
// when
44+
// both
45+
// identifiers
46+
// are
47+
// external
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:8:12:8:13 | declaration of g3 | The variable declaration g3 is declared in multiple files and has an additional $@. | test1.c:1:12:1:13 | declaration of g3 | declaration |
2+
| test.h:1:12:1:12 | declaration of g | The variable declaration g is declared in multiple files and has an additional $@. | test1.h:1:12:1:12 | declaration of g | declaration |
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-5/ExternalObjectOrFunctionNotDeclaredInOneFile.ql

‎c/misra/test/rules/RULE-8-5/test.c

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "test.h"
2+
#include "test1.h"
3+
4+
int g = 1; // COMPLIANT
5+
6+
extern int g1; // COMPLIANT
7+
8+
extern int g3; // NON_COMPLIANT

‎c/misra/test/rules/RULE-8-5/test.h

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern int g; // NON_COMPLIANT
2+
3+
int g2; // COMPLIANT

‎c/misra/test/rules/RULE-8-5/test1.c

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern int g3; // NON_COMPLIANT

‎c/misra/test/rules/RULE-8-5/test1.h

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern int g; // NON_COMPLIANT
2+
3+
int g2; // COMPLIANT
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/missingstaticspecifierfunctionredeclarationshared/MissingStaticSpecifierFunctionRedeclarationShared.ql
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:2:12:2:12 | declaration of g | The redeclaration of $@ with internal linkage misses the static specifier. | test.c:1:12:1:12 | definition of g | g |
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-8/MissingStaticSpecifierObjectRedeclarationC.ql

‎c/misra/test/rules/RULE-8-8/test.c

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
static int g = 0;
2+
extern int g; // NON_COMPLIANT
3+
4+
static int g1;
5+
static int g1 = 0; // COMPLIANT
6+
7+
int g2;
8+
int g2 = 0; // COMPLIANT
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.