Skip to content

Commit 6a06a24

Browse files
committed
Declarations6: add RULE-5-8 and RULE-5-9
1 parent 6f91612 commit 6a06a24

13 files changed

+164
-3
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id c/misra/identifiers-with-external-linkage-not-unique
3+
* @name RULE-5-8: Identifiers that define objects or functions with external linkage shall be unique
4+
* @description Using non-unique identifiers can lead to developer confusion.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-5-8
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Identifiers
17+
18+
from Declaration de, ExternalIdentifiers e
19+
where
20+
not isExcluded(de, Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery()) and
21+
not isExcluded(e, Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery()) and
22+
not de = e and
23+
de.getName() = e.getName()
24+
select de, "Identifier conflicts with external identifier $@", e, e.getName()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @id c/misra/identifiers-with-internal-linkage-not-unique
3+
* @name RULE-5-9: Identifiers that define objects or functions with internal linkage should be unique
4+
* @description Using non-unique identifiers can lead to developer confusion.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-5-9
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/advisory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
17+
from Declaration d1, Declaration d2
18+
where
19+
not isExcluded(d1, Declarations6Package::identifiersWithInternalLinkageNotUniqueQuery()) and
20+
not isExcluded(d2, Declarations6Package::identifiersWithInternalLinkageNotUniqueQuery()) and
21+
d1.isStatic() and
22+
d1.isTopLevel() and
23+
not d1 = d2 and
24+
d1.getName() = d2.getName() and
25+
// Apply an ordering based on location to enforce that (d1, d2) = (d2, d1) and we only report (d1, d2).
26+
(
27+
d1.getFile().getAbsolutePath() < d2.getFile().getAbsolutePath()
28+
or
29+
d1.getFile().getAbsolutePath() = d2.getFile().getAbsolutePath() and
30+
d1.getLocation().getStartLine() < d2.getLocation().getStartLine()
31+
)
32+
select d2, "Identifier conflicts with identifier $@ with internal linkage.", d1, d1.getName()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test1.c:1:13:1:13 | f | Identifier conflicts with external identifier $@ | test.c:3:6:3:6 | f | f |
2+
| test1.c:2:7:2:7 | g | Identifier conflicts with external identifier $@ | test.c:1:5:1:5 | g | g |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.ql

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int g;
2+
extern int g1; // COMPLIANT
3+
void f() { int i; }

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
static void f() { // NON_COMPLIANT
2+
int g; // NON_COMPLIANT
3+
int i; // COMPLIANT
4+
}
5+
int g1; //COMPLIANT
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test1.c:3:12:3:13 | g1 | Identifier conflicts with identifier $@ with internal linkage. | test.c:2:12:2:13 | g1 | g1 |
2+
| test1.c:5:13:5:13 | f | Identifier conflicts with identifier $@ with internal linkage. | test.c:3:13:3:13 | f | f |
3+
| test1.c:6:7:6:7 | g | Identifier conflicts with identifier $@ with internal linkage. | test1.c:2:12:2:12 | g | g |
4+
| test1.c:11:7:11:7 | g | Identifier conflicts with identifier $@ with internal linkage. | test1.c:2:12:2:12 | g | g |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.ql

c/misra/test/rules/RULE-5-9/test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
static int g1; // NON_COMPLIANT
2+
static void f(); // NON_COMPLIANT

c/misra/test/rules/RULE-5-9/test1.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
static int g; // COMPLIANT
2+
static int g1; // NON_COMPLIANT
3+
4+
static void f() { // NON_COMPLIANT
5+
int g; // NON_COMPLIANT
6+
int g2; // COMPLIANT
7+
}
8+
9+
void f1() { // COMPLIANT
10+
int g; // NON_COMPLIANT
11+
int g2; // COMPLIANT
12+
}

0 commit comments

Comments
 (0)