Skip to content

Commit bc4acf4

Browse files
committed
Declarations1: refactor RULE-5-1
1 parent 4acfba8 commit bc4acf4

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

c/misra/src/rules/RULE-5-1/ExternalIdentifiersNotDistinct.ql

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,35 @@ import codingstandards.c.misra
1717
import codingstandards.cpp.Linkage
1818

1919
class ExternalIdentifiers extends Declaration {
20-
ExternalIdentifiers() { hasExternalLinkage(this) }
20+
ExternalIdentifiers() {
21+
this.getName().length() > 31 and
22+
hasExternalLinkage(this) and
23+
getNamespace() instanceof GlobalNamespace and
24+
not this.isFromTemplateInstantiation(_) and
25+
not this.isFromUninstantiatedTemplate(_) and
26+
not this.hasDeclaringType() and
27+
not this instanceof UserType and
28+
not this instanceof Operator and
29+
not this.hasName("main")
30+
}
2131

2232
string getSignificantName() {
2333
//C99 states the first 31 characters of external identifiers are significant
2434
//C90 states the first 6 characters of external identifiers are significant and case is not required to be significant
2535
//C90 is not currently considered by this rule
2636
result = this.getName().prefix(31)
2737
}
38+
39+
string getNonSignificantName() { result = this.getName().suffix(31) }
2840
}
2941

3042
from ExternalIdentifiers d, ExternalIdentifiers d2
3143
where
3244
not isExcluded(d, Declarations1Package::externalIdentifiersNotDistinctQuery()) and
3345
not d = d2 and
3446
d.getLocation().getStartLine() >= d2.getLocation().getStartLine() and
35-
d.getSignificantName() = d2.getSignificantName()
47+
d.getSignificantName() = d2.getSignificantName() and
48+
not d.getNonSignificantName() = d2.getNonSignificantName()
3649
select d,
3750
"External identifer " + d.getName() + " is nondistinct in first 31 characters, compared to $@.",
3851
d2, d2.getName()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
int iltiqzxgfqsgigwfuyntzghvzltueeeA; // NON_COMPLIANT
22
int iltiqzxgfqsgigwfuyntzghvzltueeeB; // NON_COMPLIANT
33

4+
int iltiqzxgfqsgigwfuyntzghvzltueee; // COMPLIANT
5+
int iltiqzxgfqsgigwfuyntzghvzltueee; // COMPLIANT
6+
47
int var1; // COMPLIANT
58
int var2; // COMPLIANT

0 commit comments

Comments
 (0)