Skip to content

Commit 0c2f3d5

Browse files
committed
Declarations5: add RULE-5-2
1 parent d5cfc67 commit 0c2f3d5

File tree

7 files changed

+110
-2
lines changed

7 files changed

+110
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @id c/misra/identifiers-declared-in-the-same-scope-not-distinct
3+
* @name RULE-5-2: Identifiers declared in the same scope and name space shall be distinct
4+
* @description Using nondistinct identifiers results in undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-5-2
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.Identifiers
18+
19+
from InterestingIdentifiers d, InterestingIdentifiers d2
20+
where
21+
not isExcluded(d, Declarations5Package::identifiersDeclaredInTheSameScopeNotDistinctQuery()) and
22+
not isExcluded(d2, Declarations5Package::identifiersDeclaredInTheSameScopeNotDistinctQuery()) and
23+
//this rule does not apply if both are external identifiers
24+
//that is covered by RULE-5-3
25+
not (
26+
d instanceof ExternalIdentifiers and
27+
d2 instanceof ExternalIdentifiers
28+
) and
29+
d.getNamespace() = d2.getNamespace() and
30+
d.getParentScope() = d2.getParentScope() and
31+
not d = d2 and
32+
d.getLocation().getStartLine() >= d2.getLocation().getStartLine() and
33+
//first 63 chars in the name as per C99
34+
d.getSignificantNameComparedToMacro() = d2.getSignificantNameComparedToMacro() and
35+
not d.getName() = d2.getName()
36+
select d,
37+
"Identifer " + d.getName() + " is nondistinct in characters at or over 63 limit, compared to $@",
38+
d2, d2.getName()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:8:5:8:68 | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB | Identifer iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB is nondistinct in characters at or over 63 limit, compared to $@ | test.c:2:5:2:68 | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-2/IdentifiersDeclaredInTheSameScopeNotDistinct.ql

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
extern int
2+
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA; // NON_COMPLIANT
3+
// -
4+
// length
5+
// 64
6+
7+
static int
8+
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB; // NON_COMPLIANT
9+
// -
10+
// length
11+
// 64
12+
13+
void f() {
14+
int iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyC; // COMPLIANT
15+
// -
16+
// length
17+
// 64
18+
// but
19+
// diff
20+
// scope
21+
}
22+
23+
static int
24+
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjy_C; // COMPLIANT length <63
25+
static int
26+
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjy_D; // COMPLIANT length <63

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

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

6-
newtype Declarations5Query = TExternalObjectOrFunctionNotDeclaredInOneFileQuery()
6+
newtype Declarations5Query =
7+
TIdentifiersDeclaredInTheSameScopeNotDistinctQuery() or
8+
TExternalObjectOrFunctionNotDeclaredInOneFileQuery()
79

810
predicate isDeclarations5QueryMetadata(Query query, string queryId, string ruleId) {
11+
query =
12+
// `Query` instance for the `identifiersDeclaredInTheSameScopeNotDistinct` query
13+
Declarations5Package::identifiersDeclaredInTheSameScopeNotDistinctQuery() and
14+
queryId =
15+
// `@id` for the `identifiersDeclaredInTheSameScopeNotDistinct` query
16+
"c/misra/identifiers-declared-in-the-same-scope-not-distinct" and
17+
ruleId = "RULE-5-2"
18+
or
919
query =
1020
// `Query` instance for the `externalObjectOrFunctionNotDeclaredInOneFile` query
1121
Declarations5Package::externalObjectOrFunctionNotDeclaredInOneFileQuery() and
@@ -16,6 +26,13 @@ predicate isDeclarations5QueryMetadata(Query query, string queryId, string ruleI
1626
}
1727

1828
module Declarations5Package {
29+
Query identifiersDeclaredInTheSameScopeNotDistinctQuery() {
30+
//autogenerate `Query` type
31+
result =
32+
// `Query` type for `identifiersDeclaredInTheSameScopeNotDistinct` query
33+
TQueryC(TDeclarations5PackageQuery(TIdentifiersDeclaredInTheSameScopeNotDistinctQuery()))
34+
}
35+
1936
Query externalObjectOrFunctionNotDeclaredInOneFileQuery() {
2037
//autogenerate `Query` type
2138
result =

rule_packages/c/Declarations5.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
{
22
"MISRA-C-2012": {
3+
"RULE-5-2": {
4+
"properties": {
5+
"obligation": "required"
6+
},
7+
"queries": [
8+
{
9+
"description": "Using nondistinct identifiers results in undefined behaviour.",
10+
"kind": "problem",
11+
"name": "Identifiers declared in the same scope and name space shall be distinct",
12+
"precision": "very-high",
13+
"severity": "warning",
14+
"short_name": "IdentifiersDeclaredInTheSameScopeNotDistinct",
15+
"tags": [
16+
"correctness",
17+
"maintainability",
18+
"readability"
19+
],
20+
"implementation_scope": {
21+
"description": "This query considers the first 63 characters of identifiers as significant, as per C99 for nonexternal identifiers and reports the case when names are longer than 63 characters and differ in those characters past the 63 first only. This query does not consider universal or extended source characters.",
22+
"items": []
23+
}
24+
}
25+
],
26+
"title": "Identifiers declared in the same scope and name space shall be distinct"
27+
},
328
"RULE-8-5": {
429
"properties": {
530
"obligation": "required"

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ c,MISRA-C-2012,RULE-3-2,Yes,Required,,,Line-splicing shall not be used in // com
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,
634634
c,MISRA-C-2012,RULE-5-1,Yes,Required,,,External identifiers shall be distinct,,Declarations1,Medium,
635-
c,MISRA-C-2012,RULE-5-2,Yes,Required,,,Identifiers declared in the same scope and name space shall be distinct,,Declarations,Medium,
635+
c,MISRA-C-2012,RULE-5-2,Yes,Required,,,Identifiers declared in the same scope and name space shall be distinct,,Declarations5,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,
638638
c,MISRA-C-2012,RULE-5-5,Yes,Required,,,Identifiers shall be distinct from macro names,,Declarations3,Easy,

0 commit comments

Comments
 (0)