Skip to content

Commit 8ca4a3e

Browse files
committed
Declarations3: add RULE-5-6
1 parent d86b532 commit 8ca4a3e

File tree

9 files changed

+116
-2
lines changed

9 files changed

+116
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @id c/misra/typedef-name-not-unique
3+
* @name RULE-5-6: A typedef name shall be a unique identifier
4+
* @description Reusing a typedef 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-6
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
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+
}
30+
31+
from TypedefType t, InterestingIdentifiers d
32+
where
33+
not isExcluded(t, Declarations3Package::typedefNameNotUniqueQuery()) and
34+
not isExcluded(d, Declarations3Package::typedefNameNotUniqueQuery()) and
35+
not t.getADeclarationLocation() = d.getADeclarationLocation() and
36+
t.getName() = d.getName() and
37+
//exception cases
38+
not d.(Struct).getName() = t.getBaseType().toString() and
39+
not d.(Enum).getName() = t.getBaseType().toString()
40+
select t, "Typedef name is nonunique compared to $@.", d, d.getName()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:11:15:11:19 | test1 | Typedef name is nonunique compared to $@. | test.c:13:17:13:21 | test1 | test1 |
2+
| test.c:30:3:30:7 | chain | Typedef name is nonunique compared to $@. | test.c:26:10:26:14 | chain | chain |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-6/TypedefNameNotUnique.ql

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "test.h"
2+
void f() {
3+
{
4+
typedef unsigned char test; // NON_COMPLIANT
5+
}
6+
{
7+
typedef unsigned char test; // NON_COMPLIANT
8+
}
9+
}
10+
11+
typedef float test1; // NON_COMPLIANT
12+
13+
void f2() { int test1 = 0; }
14+
15+
typedef struct list {
16+
int i;
17+
} list; // COMPLIANT
18+
19+
typedef struct BIGList1 {
20+
int i;
21+
} list1; // COMPLIANT
22+
23+
typedef enum enum1 { testenum } enum1; // COMPLIANT
24+
25+
typedef struct {
26+
struct chain {
27+
int ii;
28+
} s1;
29+
int i;
30+
} chain; // NON_COMPLIANT
31+
32+
typedef void (*pointercase)(int i); // COMPLIANT

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
typedef int headertest; // COMPLIANT

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "test.h"

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

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

6-
newtype Declarations3Query = TIdentifierHidingCQuery()
6+
newtype Declarations3Query =
7+
TIdentifierHidingCQuery() or
8+
TTypedefNameNotUniqueQuery()
79

810
predicate isDeclarations3QueryMetadata(Query query, string queryId, string ruleId) {
911
query =
@@ -13,6 +15,14 @@ predicate isDeclarations3QueryMetadata(Query query, string queryId, string ruleI
1315
// `@id` for the `identifierHidingC` query
1416
"c/misra/identifier-hiding-c" and
1517
ruleId = "RULE-5-3"
18+
or
19+
query =
20+
// `Query` instance for the `typedefNameNotUnique` query
21+
Declarations3Package::typedefNameNotUniqueQuery() and
22+
queryId =
23+
// `@id` for the `typedefNameNotUnique` query
24+
"c/misra/typedef-name-not-unique" and
25+
ruleId = "RULE-5-6"
1626
}
1727

1828
module Declarations3Package {
@@ -22,4 +32,11 @@ module Declarations3Package {
2232
// `Query` type for `identifierHidingC` query
2333
TQueryC(TDeclarations3PackageQuery(TIdentifierHidingCQuery()))
2434
}
35+
36+
Query typedefNameNotUniqueQuery() {
37+
//autogenerate `Query` type
38+
result =
39+
// `Query` type for `typedefNameNotUnique` query
40+
TQueryC(TDeclarations3PackageQuery(TTypedefNameNotUniqueQuery()))
41+
}
2542
}

rule_packages/c/Declarations3.json

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

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ c,MISRA-C-2012,RULE-5-2,Yes,Required,,,Identifiers declared in the same scope an
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,
638638
c,MISRA-C-2012,RULE-5-5,Yes,Required,,,Identifiers shall be distinct from macro names,,Declarations,Easy,
639-
c,MISRA-C-2012,RULE-5-6,Yes,Required,,,A typedef name shall be a unique identifier,,Declarations,Easy,
639+
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,,Declarations,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,

0 commit comments

Comments
 (0)