Skip to content

Commit 70c6b4e

Browse files
committed
Declarations3: add RULE-5-7 and fix testcase RULE-5-6
1 parent 8ca4a3e commit 70c6b4e

File tree

10 files changed

+120
-18
lines changed

10 files changed

+120
-18
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cpp
2+
3+
//Identifiers that are candidates for checking uniqueness
4+
class InterestingIdentifiers extends Declaration {
5+
InterestingIdentifiers() {
6+
not this.isFromTemplateInstantiation(_) and
7+
not this.isFromUninstantiatedTemplate(_) and
8+
not this instanceof TemplateParameter and
9+
not this.hasDeclaringType() and
10+
not this instanceof Operator and
11+
not this.hasName("main") and
12+
exists(this.getADeclarationLocation())
13+
}
14+
}

c/misra/src/rules/RULE-5-6/TypedefNameNotUnique.ql

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,7 @@
1414

1515
import cpp
1616
import codingstandards.c.misra
17-
18-
//exception cases additional to rule description
19-
class InterestingIdentifiers extends Declaration {
20-
InterestingIdentifiers() {
21-
not this.isFromTemplateInstantiation(_) and
22-
not this.isFromUninstantiatedTemplate(_) and
23-
not this instanceof TemplateParameter and
24-
not this.hasDeclaringType() and
25-
not this instanceof Operator and
26-
not this.hasName("main") and
27-
exists(this.getADeclarationLocation())
28-
}
29-
}
17+
import codingstandards.c.Identifiers
3018

3119
from TypedefType t, InterestingIdentifiers d
3220
where
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id c/misra/tag-name-not-unique
3+
* @name RULE-5-7: A tag name shall be a unique identifier
4+
* @description Reusing a tag name compared to the name of any tag can cause confusion and make code
5+
* harder to read.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-5-7
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 Struct s, InterestingIdentifiers s2
20+
where
21+
not isExcluded(s, Declarations3Package::tagNameNotUniqueQuery()) and
22+
not isExcluded(s2, Declarations3Package::tagNameNotUniqueQuery()) and
23+
not s = s2 and
24+
s.getName() = s2.getName() and
25+
not s.getName() = "struct <unnamed>" and
26+
not s.getName() = "union <unnamed>" and
27+
not s.getName() = s2.(TypedefType).getBaseType().toString()
28+
select s, "Tag name is nonunique compared to $@.", s2, s2.getName()

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,4 @@ typedef struct {
2727
int ii;
2828
} s1;
2929
int i;
30-
} chain; // NON_COMPLIANT
31-
32-
typedef void (*pointercase)(int i); // COMPLIANT
30+
} chain; // NON_COMPLIANT
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.c:5:8:5:9 | s1 | Tag name is nonunique compared to $@. | test.c:12:10:12:11 | s1 | s1 |
2+
| test.c:5:8:5:9 | s1 | Tag name is nonunique compared to $@. | test.c:17:17:17:18 | s1 | s1 |
3+
| test.c:12:10:12:11 | s1 | Tag name is nonunique compared to $@. | test.c:5:8:5:9 | s1 | s1 |
4+
| test.c:12:10:12:11 | s1 | Tag name is nonunique compared to $@. | test.c:17:17:17:18 | s1 | s1 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-7/TagNameNotUnique.ql

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
typedef struct s {
2+
int i;
3+
} s; // COMPLIANT
4+
5+
struct s1 { // NON_COMPLIANT
6+
int i;
7+
};
8+
9+
struct s1 a1 = {0}; // COMPLIANT
10+
11+
void f() {
12+
struct s1 { // NON_COMPLIANT
13+
int i;
14+
};
15+
}
16+
17+
void f1() { int s1 = 0; }
18+
19+
typedef struct {
20+
int i;
21+
} sunnamed; // COMPLIANT
22+
23+
typedef struct {
24+
int i;
25+
} sunnamed2; // COMPLIANT
26+
27+
typedef union {
28+
int i;
29+
} U; // COMPLIANT
30+
31+
typedef union {
32+
int i;
33+
}; // COMPLIANT

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import codingstandards.cpp.exclusions.RuleMetadata
55

66
newtype Declarations3Query =
77
TIdentifierHidingCQuery() or
8-
TTypedefNameNotUniqueQuery()
8+
TTypedefNameNotUniqueQuery() or
9+
TTagNameNotUniqueQuery()
910

1011
predicate isDeclarations3QueryMetadata(Query query, string queryId, string ruleId) {
1112
query =
@@ -23,6 +24,14 @@ predicate isDeclarations3QueryMetadata(Query query, string queryId, string ruleI
2324
// `@id` for the `typedefNameNotUnique` query
2425
"c/misra/typedef-name-not-unique" and
2526
ruleId = "RULE-5-6"
27+
or
28+
query =
29+
// `Query` instance for the `tagNameNotUnique` query
30+
Declarations3Package::tagNameNotUniqueQuery() and
31+
queryId =
32+
// `@id` for the `tagNameNotUnique` query
33+
"c/misra/tag-name-not-unique" and
34+
ruleId = "RULE-5-7"
2635
}
2736

2837
module Declarations3Package {
@@ -39,4 +48,11 @@ module Declarations3Package {
3948
// `Query` type for `typedefNameNotUnique` query
4049
TQueryC(TDeclarations3PackageQuery(TTypedefNameNotUniqueQuery()))
4150
}
51+
52+
Query tagNameNotUniqueQuery() {
53+
//autogenerate `Query` type
54+
result =
55+
// `Query` type for `tagNameNotUnique` query
56+
TQueryC(TDeclarations3PackageQuery(TTagNameNotUniqueQuery()))
57+
}
4258
}

rule_packages/c/Declarations3.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@
4343
}
4444
],
4545
"title": "A typedef name shall be a unique identifier"
46+
},
47+
"RULE-5-7": {
48+
"properties": {
49+
"obligation": "required"
50+
},
51+
"queries": [
52+
{
53+
"description": "Reusing a tag name compared to the name of any tag can cause confusion and make code harder to read.",
54+
"kind": "problem",
55+
"name": "A tag name shall be a unique identifier",
56+
"precision": "very-high",
57+
"severity": "error",
58+
"short_name": "TagNameNotUnique",
59+
"tags": [
60+
"readability",
61+
"maintainability"
62+
]
63+
}
64+
],
65+
"title": "A tag name shall be a unique identifier"
4666
}
4767
}
4868
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ c,MISRA-C-2012,RULE-5-3,Yes,Required,,,An identifier declared in an inner scope
637637
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,,Declarations3,Easy,
640-
c,MISRA-C-2012,RULE-5-7,Yes,Required,,,A tag name shall be a unique identifier,,Declarations,Easy,
640+
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,
642642
c,MISRA-C-2012,RULE-5-9,Yes,Advisory,,,Identifiers that define objects or functions with internal linkage should be unique,,Declarations,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,

0 commit comments

Comments
 (0)