Skip to content

Commit 311eb92

Browse files
committed
Declarations3: add RULE-5-5
1 parent c388bae commit 311eb92

File tree

8 files changed

+80
-1
lines changed

8 files changed

+80
-1
lines changed

c/common/src/codingstandards/c/Identifiers.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ class InterestingIdentifiers extends Declaration {
1111
not this.hasName("main") and
1212
exists(this.getADeclarationLocation())
1313
}
14+
15+
string getSignificantName() { result = this.getName().prefix(31) }
1416
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @id c/misra/identifiers-not-distinct-from-macro-names
3+
* @name RULE-5-5: Identifiers shall be distinct from macro names
4+
* @description Reusing a macro name compared to the name of any other identifier can cause
5+
* confusion and make code harder to read.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-5-5
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.c.Identifiers
18+
19+
from Macro m, InterestingIdentifiers i, string mName, string iName
20+
where
21+
not isExcluded(m, Declarations3Package::identifiersNotDistinctFromMacroNamesQuery()) and
22+
not isExcluded(i, Declarations3Package::identifiersNotDistinctFromMacroNamesQuery()) and
23+
mName = iName and
24+
(
25+
//C99 states the first 31 characters of external identifiers are significant
26+
//C90 states the first 6 characters of external identifiers are significant and case is not required to be significant
27+
//C90 is not currently considered by this rule
28+
if m.getName().length() > 31 then mName = m.getName().prefix(31) else mName = m.getName()
29+
) and
30+
if i.getName().length() > 31 then iName = i.getSignificantName() else iName = i.getName()
31+
select m, "Macro name is nonunique compared to $@.", i, i.getName()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:1:1:1:23 | #define Sum(x,y) x + y | Macro name is nonunique compared to $@. | test.c:4:5:4:7 | Sum | Sum |
2+
| test.c:6:1:6:42 | #define iltiqzxgfqsgigwfuyntzghvzltueeaZ ; | Macro name is nonunique compared to $@. | test.c:7:12:7:43 | iltiqzxgfqsgigwfuyntzghvzltueeaQ | iltiqzxgfqsgigwfuyntzghvzltueeaQ |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-5/IdentifiersNotDistinctFromMacroNames.ql

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define Sum(x, y) x + y // NON_COMPLIANT
2+
#undef Sum
3+
4+
int Sum;
5+
6+
#define iltiqzxgfqsgigwfuyntzghvzltueeaZ ; // NON_COMPLIANT - length 32
7+
static int iltiqzxgfqsgigwfuyntzghvzltueeaQ; // NON_COMPLIANT - length 32

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import codingstandards.cpp.exclusions.RuleMetadata
55

66
newtype Declarations3Query =
77
TIdentifierHidingCQuery() or
8+
TIdentifiersNotDistinctFromMacroNamesQuery() or
89
TTypedefNameNotUniqueQuery() or
910
TTagNameNotUniqueQuery()
1011

@@ -17,6 +18,14 @@ predicate isDeclarations3QueryMetadata(Query query, string queryId, string ruleI
1718
"c/misra/identifier-hiding-c" and
1819
ruleId = "RULE-5-3"
1920
or
21+
query =
22+
// `Query` instance for the `identifiersNotDistinctFromMacroNames` query
23+
Declarations3Package::identifiersNotDistinctFromMacroNamesQuery() and
24+
queryId =
25+
// `@id` for the `identifiersNotDistinctFromMacroNames` query
26+
"c/misra/identifiers-not-distinct-from-macro-names" and
27+
ruleId = "RULE-5-5"
28+
or
2029
query =
2130
// `Query` instance for the `typedefNameNotUnique` query
2231
Declarations3Package::typedefNameNotUniqueQuery() and
@@ -42,6 +51,13 @@ module Declarations3Package {
4251
TQueryC(TDeclarations3PackageQuery(TIdentifierHidingCQuery()))
4352
}
4453

54+
Query identifiersNotDistinctFromMacroNamesQuery() {
55+
//autogenerate `Query` type
56+
result =
57+
// `Query` type for `identifiersNotDistinctFromMacroNames` query
58+
TQueryC(TDeclarations3PackageQuery(TIdentifiersNotDistinctFromMacroNamesQuery()))
59+
}
60+
4561
Query typedefNameNotUniqueQuery() {
4662
//autogenerate `Query` type
4763
result =

rule_packages/c/Declarations3.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@
2424
],
2525
"title": "An identifier declared in an inner scope shall not hide an identifier declared in an outer scope."
2626
},
27+
"RULE-5-5": {
28+
"properties": {
29+
"obligation": "required"
30+
},
31+
"queries": [
32+
{
33+
"description": "Reusing a macro name compared to the name of any other identifier can cause confusion and make code harder to read.",
34+
"kind": "problem",
35+
"name": "Identifiers shall be distinct from macro names",
36+
"precision": "very-high",
37+
"severity": "error",
38+
"short_name": "IdentifiersNotDistinctFromMacroNames",
39+
"tags": [
40+
"readability",
41+
"maintainability"
42+
]
43+
}
44+
],
45+
"title": "Identifiers shall be distinct from macro names"
46+
},
2747
"RULE-5-6": {
2848
"properties": {
2949
"obligation": "required"

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ c,MISRA-C-2012,RULE-5-1,Yes,Required,,,External identifiers shall be distinct,,D
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,Declarations3,Import,
637637
c,MISRA-C-2012,RULE-5-4,Yes,Required,,,Macro identifiers shall be distinct,,Declarations1,Easy,
638-
c,MISRA-C-2012,RULE-5-5,Yes,Required,,,Identifiers shall be distinct from macro names,,Declarations,Easy,
638+
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,
641641
c,MISRA-C-2012,RULE-5-8,Yes,Required,,,Identifiers that define objects or functions with external linkage shall be unique,,Declarations,Easy,

0 commit comments

Comments
 (0)