Skip to content

Commit 0b2ecfc

Browse files
committed
Declarations1: add rule 5-4
1 parent cb339b5 commit 0b2ecfc

File tree

10 files changed

+137
-2
lines changed

10 files changed

+137
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/macro-identifier-not-distinct-from-parameter
3+
* @name RULE-5-4: Macro identifiers shall be distinct
4+
* @description Macros with the same name as their parameters are less readable.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-5-4
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.FunctionLikeMacro
17+
18+
from FunctionLikeMacro m
19+
where
20+
not isExcluded(m, Declarations1Package::macroIdentifierNotDistinctFromParameterQuery()) and
21+
exists(string p | p = m.(FunctionLikeMacro).getAParameter() and p = m.getName())
22+
select m, "Macro name matches parameter " + m.getName() + " ."
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @id c/misra/macro-identifiers-not-distinct
3+
* @name RULE-5-4: Macro identifiers shall be distinct
4+
* @description Declaring multiple macros with the same name leads to undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-5-4
9+
* correctness
10+
* maintainability
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
from Macro m
19+
where
20+
not isExcluded(m, Declarations1Package::macroIdentifiersNotDistinctQuery()) and
21+
exists(Macro m2 |
22+
not m = m2 and
23+
if m.getName().length() >= 64
24+
then m.getName().substring(0, 62) = m2.getName().substring(0, 62)
25+
else m.getName() = m2.getName()
26+
)
27+
select m, "Nondistinct macro identifer used " + m.getName() + " ."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:7:1:7:57 | #define FUNCTION_MACRO(FUNCTION_MACRO) FUNCTION_MACRO + 1 | Macro name matches parameter FUNCTION_MACRO . |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-4/MacroIdentifierNotDistinctFromParameter.ql
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.c:1:1:1:72 | #define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA | Nondistinct macro identifer used iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA . |
2+
| test.c:2:1:2:72 | #define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB | Nondistinct macro identifer used iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB . |
3+
| test.c:7:1:7:57 | #define FUNCTION_MACRO(FUNCTION_MACRO) FUNCTION_MACRO + 1 | Nondistinct macro identifer used FUNCTION_MACRO . |
4+
| test.c:8:1:8:31 | #define FUNCTION_MACRO(X) X + 1 | Nondistinct macro identifer used FUNCTION_MACRO . |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-4/MacroIdentifiersNotDistinct.ql

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA // NON_COMPLIANT
2+
#define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB // NON_COMPLIANT
3+
4+
#define MACRO1 // COMPLIANT
5+
#define MACRO2 // COMPLIANT
6+
7+
#define FUNCTION_MACRO(FUNCTION_MACRO) FUNCTION_MACRO + 1 // NON_COMPLIANT
8+
#define FUNCTION_MACRO(X) X + 1 // NON_COMPLIANT
9+
#define FUNCTION_MACRO2(X) X + 1 // COMPLIANT

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import codingstandards.cpp.exclusions.RuleMetadata
66
newtype Declarations1Query =
77
TDeclareIdentifiersBeforeUsingThemQuery() or
88
TDoNotDeclareOrDefineAReservedIdentifierQuery() or
9-
TDoNotDeclareAReservedIdentifierQuery()
9+
TDoNotDeclareAReservedIdentifierQuery() or
10+
TMacroIdentifiersNotDistinctQuery() or
11+
TMacroIdentifierNotDistinctFromParameterQuery()
1012

1113
predicate isDeclarations1QueryMetadata(Query query, string queryId, string ruleId) {
1214
query =
@@ -32,6 +34,22 @@ predicate isDeclarations1QueryMetadata(Query query, string queryId, string ruleI
3234
// `@id` for the `doNotDeclareAReservedIdentifier` query
3335
"c/misra/do-not-declare-a-reserved-identifier" and
3436
ruleId = "RULE-21-2"
37+
or
38+
query =
39+
// `Query` instance for the `macroIdentifiersNotDistinct` query
40+
Declarations1Package::macroIdentifiersNotDistinctQuery() and
41+
queryId =
42+
// `@id` for the `macroIdentifiersNotDistinct` query
43+
"c/misra/macro-identifiers-not-distinct" and
44+
ruleId = "RULE-5-4"
45+
or
46+
query =
47+
// `Query` instance for the `macroIdentifierNotDistinctFromParameter` query
48+
Declarations1Package::macroIdentifierNotDistinctFromParameterQuery() and
49+
queryId =
50+
// `@id` for the `macroIdentifierNotDistinctFromParameter` query
51+
"c/misra/macro-identifier-not-distinct-from-parameter" and
52+
ruleId = "RULE-5-4"
3553
}
3654

3755
module Declarations1Package {
@@ -55,4 +73,18 @@ module Declarations1Package {
5573
// `Query` type for `doNotDeclareAReservedIdentifier` query
5674
TQueryC(TDeclarations1PackageQuery(TDoNotDeclareAReservedIdentifierQuery()))
5775
}
76+
77+
Query macroIdentifiersNotDistinctQuery() {
78+
//autogenerate `Query` type
79+
result =
80+
// `Query` type for `macroIdentifiersNotDistinct` query
81+
TQueryC(TDeclarations1PackageQuery(TMacroIdentifiersNotDistinctQuery()))
82+
}
83+
84+
Query macroIdentifierNotDistinctFromParameterQuery() {
85+
//autogenerate `Query` type
86+
result =
87+
// `Query` type for `macroIdentifierNotDistinctFromParameter` query
88+
TQueryC(TDeclarations1PackageQuery(TMacroIdentifierNotDistinctFromParameterQuery()))
89+
}
5890
}

rule_packages/c/Declarations1.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,44 @@
7373
}
7474
],
7575
"title": "A reserved identifier or reserved macro name shall not be declared"
76+
},
77+
"RULE-5-4": {
78+
"properties": {
79+
"obligation": "required"
80+
},
81+
"queries": [
82+
{
83+
"description": "Declaring multiple macros with the same name leads to undefined behaviour.",
84+
"kind": "problem",
85+
"name": "Macro identifiers shall be distinct",
86+
"precision": "very-high",
87+
"severity": "warning",
88+
"short_name": "MacroIdentifiersNotDistinct",
89+
"tags": [
90+
"correctness",
91+
"maintainability",
92+
"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+
}
99+
},
100+
{
101+
"description": "Macros with the same name as their parameters are less readable.",
102+
"kind": "problem",
103+
"name": "Macro identifiers shall be distinct",
104+
"precision": "very-high",
105+
"severity": "warning",
106+
"short_name": "MacroIdentifierNotDistinctFromParameter",
107+
"tags": [
108+
"maintainability",
109+
"readability"
110+
]
111+
}
112+
],
113+
"title": "Macro identifiers shall be distinct"
76114
}
77115
}
78116
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ c,MISRA-C-2012,RULE-4-2,No,Advisory,,,Trigraphs should not be used,A2-5-1,,Impor
634634
c,MISRA-C-2012,RULE-5-1,Yes,Required,,,External identifiers shall be distinct,,Declarations,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,
637-
c,MISRA-C-2012,RULE-5-4,Yes,Required,,,Macro identifiers shall be distinct,,Declarations,Easy,
637+
c,MISRA-C-2012,RULE-5-4,Yes,Required,,,Macro identifiers shall be distinct,,Declarations1,Easy,
638638
c,MISRA-C-2012,RULE-5-5,Yes,Required,,,Identifiers shall be distinct from macro names,,Declarations,Easy,
639639
c,MISRA-C-2012,RULE-5-6,Yes,Required,,,A typedef name shall be a unique identifier,,Declarations,Easy,
640640
c,MISRA-C-2012,RULE-5-7,Yes,Required,,,A tag name shall be a unique identifier,,Declarations,Easy,

0 commit comments

Comments
 (0)