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+
}

cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import cpp
33
import RuleMetadata
44
import codingstandards.cpp.exclusions.RuleMetadata
55

6-
newtype Declarations6Query = TFunctionDeclaredImplicitlyQuery()
6+
newtype Declarations6Query =
7+
TFunctionDeclaredImplicitlyQuery() or
8+
TIdentifiersWithExternalLinkageNotUniqueQuery() or
9+
TIdentifiersWithInternalLinkageNotUniqueQuery()
710

811
predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleId, string category) {
912
query =
@@ -14,6 +17,24 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI
1417
"c/misra/function-declared-implicitly" and
1518
ruleId = "RULE-17-3" and
1619
category = "mandatory"
20+
or
21+
query =
22+
// `Query` instance for the `identifiersWithExternalLinkageNotUnique` query
23+
Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery() and
24+
queryId =
25+
// `@id` for the `identifiersWithExternalLinkageNotUnique` query
26+
"c/misra/identifiers-with-external-linkage-not-unique" and
27+
ruleId = "RULE-5-8" and
28+
category = "required"
29+
or
30+
query =
31+
// `Query` instance for the `identifiersWithInternalLinkageNotUnique` query
32+
Declarations6Package::identifiersWithInternalLinkageNotUniqueQuery() and
33+
queryId =
34+
// `@id` for the `identifiersWithInternalLinkageNotUnique` query
35+
"c/misra/identifiers-with-internal-linkage-not-unique" and
36+
ruleId = "RULE-5-9" and
37+
category = "advisory"
1738
}
1839

1940
module Declarations6Package {
@@ -23,4 +44,18 @@ module Declarations6Package {
2344
// `Query` type for `functionDeclaredImplicitly` query
2445
TQueryC(TDeclarations6PackageQuery(TFunctionDeclaredImplicitlyQuery()))
2546
}
47+
48+
Query identifiersWithExternalLinkageNotUniqueQuery() {
49+
//autogenerate `Query` type
50+
result =
51+
// `Query` type for `identifiersWithExternalLinkageNotUnique` query
52+
TQueryC(TDeclarations6PackageQuery(TIdentifiersWithExternalLinkageNotUniqueQuery()))
53+
}
54+
55+
Query identifiersWithInternalLinkageNotUniqueQuery() {
56+
//autogenerate `Query` type
57+
result =
58+
// `Query` type for `identifiersWithInternalLinkageNotUnique` query
59+
TQueryC(TDeclarations6PackageQuery(TIdentifiersWithInternalLinkageNotUniqueQuery()))
60+
}
2661
}

rule_packages/c/Declarations6.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,46 @@
1919
}
2020
],
2121
"title": "A function shall not be declared implicitly"
22+
},
23+
"RULE-5-8": {
24+
"properties": {
25+
"obligation": "required"
26+
},
27+
"queries": [
28+
{
29+
"description": "Using non-unique identifiers can lead to developer confusion.",
30+
"kind": "problem",
31+
"name": "Identifiers that define objects or functions with external linkage shall be unique",
32+
"precision": "very-high",
33+
"severity": "error",
34+
"short_name": "IdentifiersWithExternalLinkageNotUnique",
35+
"tags": [
36+
"maintainability",
37+
"readability"
38+
]
39+
}
40+
],
41+
"title": "Identifiers that define objects or functions with external linkage shall be unique"
42+
},
43+
"RULE-5-9": {
44+
"properties": {
45+
"obligation": "advisory"
46+
},
47+
"queries": [
48+
{
49+
"description": "Using non-unique identifiers can lead to developer confusion.",
50+
"kind": "problem",
51+
"name": "Identifiers that define objects or functions with internal linkage should be unique",
52+
"precision": "very-high",
53+
"severity": "error",
54+
"short_name": "IdentifiersWithInternalLinkageNotUnique",
55+
"tags": [
56+
"maintainability",
57+
"readability"
58+
]
59+
}
60+
],
61+
"title": "Identifiers that define objects or functions with internal linkage should be unique"
2262
}
2363
}
2464
}

rules.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,8 @@ c,MISRA-C-2012,RULE-5-4,Yes,Required,,,Macro identifiers shall be distinct,,Decl
638638
c,MISRA-C-2012,RULE-5-5,Yes,Required,,,Identifiers shall be distinct from macro names,,Declarations3,Easy,
639639
c,MISRA-C-2012,RULE-5-6,Yes,Required,,,A typedef name shall be a unique identifier,,Declarations3,Easy,
640640
c,MISRA-C-2012,RULE-5-7,Yes,Required,,,A tag name shall be a unique identifier,,Declarations3,Easy,
641-
c,MISRA-C-2012,RULE-5-8,Yes,Required,,,Identifiers that define objects or functions with external linkage shall be unique,,Declarations,Easy,
642-
c,MISRA-C-2012,RULE-5-9,Yes,Advisory,,,Identifiers that define objects or functions with internal linkage should be unique,,Declarations,Easy,
641+
c,MISRA-C-2012,RULE-5-8,Yes,Required,,,Identifiers that define objects or functions with external linkage shall be unique,,Declarations6,Easy,
642+
c,MISRA-C-2012,RULE-5-9,Yes,Advisory,,,Identifiers that define objects or functions with internal linkage should be unique,,Declarations6,Easy,
643643
c,MISRA-C-2012,RULE-6-1,Yes,Required,,,Bit-fields shall only be declared with an appropriate type,M9-6-4,Types,Medium,
644644
c,MISRA-C-2012,RULE-6-2,Yes,Required,,,Single-bit named bit fields shall not be of a signed type,M9-6-4,Types,Import,
645645
c,MISRA-C-2012,RULE-7-1,Yes,Required,,,Octal constants shall not be used,M2-13-2,Banned,Import,

0 commit comments

Comments
 (0)