Skip to content

Commit a1380bb

Browse files
committed
Declarations1: add rule 5-1
1 parent 24547a7 commit a1380bb

File tree

7 files changed

+103
-19
lines changed

7 files changed

+103
-19
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @id c/misra/external-identifiers-not-distinct
3+
* @name RULE-5-1: External identifiers shall be distinct
4+
* @description Using nondistinct external identifiers results in undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-5-1
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.Linkage
18+
19+
class ExternalIdentifiers extends Declaration {
20+
ExternalIdentifiers() { hasExternalLinkage(this) }
21+
}
22+
23+
class ExternalIdentifiersLong extends ExternalIdentifiers {
24+
ExternalIdentifiersLong() { this.getName().length() >= 32 }
25+
}
26+
27+
predicate notSame(ExternalIdentifiers d, ExternalIdentifiers d2) { not d = d2 }
28+
29+
from ExternalIdentifiers d, ExternalIdentifiers d2
30+
where
31+
not isExcluded(d, Declarations1Package::externalIdentifiersNotDistinctQuery()) and
32+
notSame(d, d2) and
33+
if d instanceof ExternalIdentifiersLong and d2 instanceof ExternalIdentifiersLong
34+
then d.getName().substring(0, 30) = d2.getName().substring(0, 30)
35+
else d.getName() = d2.getName()
36+
select d, "External identifer is nondistinct " + d.getName() + " compared to $@.", d2, d.getName()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:1:5:1:36 | iltiqzxgfqsgigwfuyntzghvzltueeeA | External identifer is nondistinct iltiqzxgfqsgigwfuyntzghvzltueeeA compared to $@. | test.c:2:5:2:36 | iltiqzxgfqsgigwfuyntzghvzltueeeB | iltiqzxgfqsgigwfuyntzghvzltueeeA |
2+
| test.c:2:5:2:36 | iltiqzxgfqsgigwfuyntzghvzltueeeB | External identifer is nondistinct iltiqzxgfqsgigwfuyntzghvzltueeeB compared to $@. | test.c:1:5:1:36 | iltiqzxgfqsgigwfuyntzghvzltueeeA | iltiqzxgfqsgigwfuyntzghvzltueeeB |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-1/ExternalIdentifiersNotDistinct.ql

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int iltiqzxgfqsgigwfuyntzghvzltueeeA; // NON_COMPLIANT
2+
int iltiqzxgfqsgigwfuyntzghvzltueeeB; // NON_COMPLIANT
3+
4+
int var1; // COMPLIANT
5+
int var2; // COMPLIANT

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ newtype Declarations1Query =
77
TDeclareIdentifiersBeforeUsingThemQuery() or
88
TDoNotDeclareOrDefineAReservedIdentifierQuery() or
99
TDoNotDeclareAReservedIdentifierQuery() or
10+
TExternalIdentifiersNotDistinctQuery() or
1011
TMacroIdentifiersNotDistinctQuery() or
1112
TMacroIdentifierNotDistinctFromParameterQuery()
1213

@@ -35,6 +36,14 @@ predicate isDeclarations1QueryMetadata(Query query, string queryId, string ruleI
3536
"c/misra/do-not-declare-a-reserved-identifier" and
3637
ruleId = "RULE-21-2"
3738
or
39+
query =
40+
// `Query` instance for the `externalIdentifiersNotDistinct` query
41+
Declarations1Package::externalIdentifiersNotDistinctQuery() and
42+
queryId =
43+
// `@id` for the `externalIdentifiersNotDistinct` query
44+
"c/misra/external-identifiers-not-distinct" and
45+
ruleId = "RULE-5-1"
46+
or
3847
query =
3948
// `Query` instance for the `macroIdentifiersNotDistinct` query
4049
Declarations1Package::macroIdentifiersNotDistinctQuery() and
@@ -74,6 +83,13 @@ module Declarations1Package {
7483
TQueryC(TDeclarations1PackageQuery(TDoNotDeclareAReservedIdentifierQuery()))
7584
}
7685

86+
Query externalIdentifiersNotDistinctQuery() {
87+
//autogenerate `Query` type
88+
result =
89+
// `Query` type for `externalIdentifiersNotDistinct` query
90+
TQueryC(TDeclarations1PackageQuery(TExternalIdentifiersNotDistinctQuery()))
91+
}
92+
7793
Query macroIdentifiersNotDistinctQuery() {
7894
//autogenerate `Query` type
7995
result =

rule_packages/c/Declarations1.json

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,43 +74,67 @@
7474
],
7575
"title": "A reserved identifier or reserved macro name shall not be declared"
7676
},
77-
"RULE-5-4": {
77+
"RULE-5-1": {
7878
"properties": {
7979
"obligation": "required"
8080
},
8181
"queries": [
8282
{
83-
"description": "Declaring multiple macros with the same name leads to undefined behaviour.",
83+
"description": "Using nondistinct external identifiers results in undefined behaviour.",
8484
"kind": "problem",
85-
"name": "Macro identifiers shall be distinct",
85+
"name": "External identifiers shall be distinct",
8686
"precision": "very-high",
8787
"severity": "warning",
88-
"short_name": "MacroIdentifiersNotDistinct",
88+
"short_name": "ExternalIdentifiersNotDistinct",
8989
"tags": [
9090
"correctness",
9191
"maintainability",
9292
"readability"
93-
]
94-
,
95-
"implementation_scope": {
96-
"description": "This query checks the first 63 characters of macro identifiers as significant, as per C99. Distinctness of parameters within the same function like macro are checked by compiler and therefore not checked by this rule.",
97-
"items": []
98-
}
93+
],
94+
"implementation_scope": {
95+
"description": "This query checks the first 31 characters of identifiers as significant, as per C99. This query does not consider universal or extended source characters.",
96+
"items": []
97+
}
98+
}
99+
],
100+
"title": "External identifiers shall be distinct"
101+
},
102+
"RULE-5-4": {
103+
"properties": {
104+
"obligation": "required"
99105
},
100-
{
101-
"description": "Macros with the same name as their parameters are less readable.",
106+
"queries": [
107+
{
108+
"description": "Declaring multiple macros with the same name leads to undefined behaviour.",
102109
"kind": "problem",
103110
"name": "Macro identifiers shall be distinct",
104111
"precision": "very-high",
105112
"severity": "warning",
106-
"short_name": "MacroIdentifierNotDistinctFromParameter",
113+
"short_name": "MacroIdentifiersNotDistinct",
107114
"tags": [
115+
"correctness",
108116
"maintainability",
109117
"readability"
110-
]
111-
}
112-
],
113-
"title": "Macro identifiers shall be distinct"
114-
}
118+
],
119+
"implementation_scope": {
120+
"description": "This query checks the first 63 characters of macro identifiers as significant, as per C99. Distinctness of parameters within the same function like macro are checked by compiler and therefore not checked by this rule.",
121+
"items": []
122+
}
123+
},
124+
{
125+
"description": "Macros with the same name as their parameters are less readable.",
126+
"kind": "problem",
127+
"name": "Macro identifiers shall be distinct",
128+
"precision": "very-high",
129+
"severity": "warning",
130+
"short_name": "MacroIdentifierNotDistinctFromParameter",
131+
"tags": [
132+
"maintainability",
133+
"readability"
134+
]
135+
}
136+
],
137+
"title": "Macro identifiers shall be distinct"
138+
}
115139
}
116140
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ c,MISRA-C-2012,RULE-3-1,Yes,Required,,,The character sequences /* and // shall n
631631
c,MISRA-C-2012,RULE-3-2,Yes,Required,,,Line-splicing shall not be used in // comments,,Syntax,Easy,
632632
c,MISRA-C-2012,RULE-4-1,Yes,Required,,,Octal and hexadecimal escape sequences shall be terminated,A2-13-1 M2-13-2,Syntax,Medium,
633633
c,MISRA-C-2012,RULE-4-2,No,Advisory,,,Trigraphs should not be used,A2-5-1,,Import,
634-
c,MISRA-C-2012,RULE-5-1,Yes,Required,,,External identifiers shall be distinct,,Declarations,Medium,
634+
c,MISRA-C-2012,RULE-5-1,Yes,Required,,,External identifiers shall be distinct,,Declarations1,Medium,
635635
c,MISRA-C-2012,RULE-5-2,Yes,Required,,,Identifiers declared in the same scope and name space shall be distinct,,Declarations,Medium,
636636
c,MISRA-C-2012,RULE-5-3,Yes,Required,,,An identifier declared in an inner scope shall not hide an identifier declared in an outer scope,A2-10-1,Declarations,Import,
637637
c,MISRA-C-2012,RULE-5-4,Yes,Required,,,Macro identifiers shall be distinct,,Declarations1,Easy,

0 commit comments

Comments
 (0)