Skip to content

Commit 9367bd6

Browse files
committed
Declarations4: add RULE-8-6
and refactor RULE-5-7 for lib mv
1 parent 4081fc8 commit 9367bd6

File tree

12 files changed

+124
-24
lines changed

12 files changed

+124
-24
lines changed

c/misra/src/rules/RULE-5-7/TagNameNotUnique.ql

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

1515
import cpp
1616
import codingstandards.c.misra
17-
import codingstandards.c.Identifiers
17+
import codingstandards.cpp.Identifiers
1818

1919
from Struct s, InterestingIdentifiers s2
2020
where
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @id c/misra/identifier-with-external-linkage-one-definition
3+
* @name RULE-8-6: An identifier with external linkage shall have exactly one external definition
4+
* @description An identifier with multiple definitions in different translation units leads to
5+
* undefined behavior.
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-6
10+
* correctness
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Identifiers
17+
18+
from ExternalIdentifiers d, DeclarationEntry de1, DeclarationEntry de2
19+
where
20+
not isExcluded(d, Declarations4Package::identifierWithExternalLinkageOneDefinitionQuery()) and
21+
not isExcluded([de1, de2], Declarations4Package::identifierWithExternalLinkageOneDefinitionQuery()) and
22+
d.isTopLevel() and
23+
d = de1.getDeclaration() and
24+
d = de2.getDeclaration() and
25+
de1 != de2 and
26+
de1.isDefinition() and
27+
de2.isDefinition() and
28+
//exceptions
29+
(d instanceof Function implies not d.(Function).isInline()) and
30+
// Apply an ordering based on ___location to enforce that (de1, de2) = (de2, de1) and we only report (de1, de2).
31+
(
32+
de1.getFile().getAbsolutePath() < de2.getFile().getAbsolutePath()
33+
or
34+
de1.getFile().getAbsolutePath() = de2.getFile().getAbsolutePath() and
35+
de1.getLocation().getStartLine() < de2.getLocation().getStartLine()
36+
)
37+
select de1, "The identifier " + de1.getName() + " has external linkage and is redefined $@.", de2,
38+
"here"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:1:5:1:6 | definition of g1 | The identifier g1 has external linkage and is redefined $@. | test1.c:1:5:1:6 | definition of g1 | here |
2+
| test.c:6:6:6:7 | definition of f2 | The identifier f2 has external linkage and is redefined $@. | test1.c:6:6:6:7 | definition of f2 | here |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-6/IdentifierWithExternalLinkageOneDefinition.ql

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int g1 = 1; // NON_COMPLIANT
2+
static int g2 = 1; // COMPLIANT; internal linkage
3+
4+
inline void f1() {} // COMPLIANT; inline functions are an exception
5+
6+
void f2() {} // NON_COMPLIANT

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int g1 = 0; // NON_COMPLIANT
2+
static int g2 = 1; // COMPLIANT; internal linkage
3+
4+
inline void f1() {} // COMPLIANT; inline functions are an exception
5+
6+
void f2() {} // NON_COMPLIANT

c/common/src/codingstandards/c/Identifiers.qll renamed to cpp/common/src/codingstandards/cpp/Identifiers.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
import cpp
2+
import codingstandards.cpp.Linkage
3+
4+
class ExternalIdentifiers extends InterestingIdentifiers {
5+
ExternalIdentifiers() {
6+
hasExternalLinkage(this) and
7+
getNamespace() instanceof GlobalNamespace
8+
}
9+
10+
string getSignificantName() {
11+
//C99 states the first 31 characters of external identifiers are significant
12+
//C90 states the first 6 characters of external identifiers are significant and case is not required to be significant
13+
//C90 is not currently considered by this rule
14+
result = this.getName().prefix(31)
15+
}
16+
}
217

318
//Identifiers that are candidates for checking uniqueness
419
class InterestingIdentifiers extends Declaration {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Declarations4Query = TIdentifierWithExternalLinkageOneDefinitionQuery()
7+
8+
predicate isDeclarations4QueryMetadata(Query query, string queryId, string ruleId) {
9+
query =
10+
// `Query` instance for the `identifierWithExternalLinkageOneDefinition` query
11+
Declarations4Package::identifierWithExternalLinkageOneDefinitionQuery() and
12+
queryId =
13+
// `@id` for the `identifierWithExternalLinkageOneDefinition` query
14+
"c/misra/identifier-with-external-linkage-one-definition" and
15+
ruleId = "RULE-8-6"
16+
}
17+
18+
module Declarations4Package {
19+
Query identifierWithExternalLinkageOneDefinitionQuery() {
20+
//autogenerate `Query` type
21+
result =
22+
// `Query` type for `identifierWithExternalLinkageOneDefinition` query
23+
TQueryC(TDeclarations4PackageQuery(TIdentifierWithExternalLinkageOneDefinitionQuery()))
24+
}
25+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Contracts3
1212
import Declarations1
1313
import Declarations2
1414
import Declarations3
15+
import Declarations4
1516
import Expressions
1617
import IO1
1718
import IO2
@@ -44,6 +45,7 @@ newtype TCQuery =
4445
TDeclarations1PackageQuery(Declarations1Query q) or
4546
TDeclarations2PackageQuery(Declarations2Query q) or
4647
TDeclarations3PackageQuery(Declarations3Query q) or
48+
TDeclarations4PackageQuery(Declarations4Query q) or
4749
TExpressionsPackageQuery(ExpressionsQuery q) or
4850
TIO1PackageQuery(IO1Query q) or
4951
TIO2PackageQuery(IO2Query q) or
@@ -76,6 +78,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId) {
7678
isDeclarations1QueryMetadata(query, queryId, ruleId) or
7779
isDeclarations2QueryMetadata(query, queryId, ruleId) or
7880
isDeclarations3QueryMetadata(query, queryId, ruleId) or
81+
isDeclarations4QueryMetadata(query, queryId, ruleId) or
7982
isExpressionsQueryMetadata(query, queryId, ruleId) or
8083
isIO1QueryMetadata(query, queryId, ruleId) or
8184
isIO2QueryMetadata(query, queryId, ruleId) or

cpp/common/src/codingstandards/cpp/rules/notdistinctidentifier/NotDistinctIdentifier.qll

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,7 @@
55
import cpp
66
import codingstandards.cpp.Customizations
77
import codingstandards.cpp.Exclusions
8-
import codingstandards.cpp.Linkage
9-
10-
class ExternalIdentifiers extends Declaration {
11-
ExternalIdentifiers() {
12-
this.getName().length() >= 31 and
13-
hasExternalLinkage(this) and
14-
getNamespace() instanceof GlobalNamespace and
15-
not this.isFromTemplateInstantiation(_) and
16-
not this.isFromUninstantiatedTemplate(_) and
17-
not this.hasDeclaringType() and
18-
not this instanceof UserType and
19-
not this instanceof Operator and
20-
not this.hasName("main")
21-
}
22-
23-
string getSignificantName() {
24-
//C99 states the first 31 characters of external identifiers are significant
25-
//C90 states the first 6 characters of external identifiers are significant and case is not required to be significant
26-
//C90 is not currently considered by this rule
27-
result = this.getName().prefix(31)
28-
}
29-
}
8+
import codingstandards.cpp.Identifiers
309

3110
abstract class NotDistinctIdentifierSharedQuery extends Query { }
3211

@@ -35,6 +14,8 @@ Query getQuery() { result instanceof NotDistinctIdentifierSharedQuery }
3514
query predicate problems(ExternalIdentifiers d, ExternalIdentifiers d2, string message) {
3615
not isExcluded(d, getQuery()) and
3716
not isExcluded(d, getQuery()) and
17+
d.getName().length() >= 31 and
18+
d2.getName().length() >= 31 and
3819
not d = d2 and
3920
d.getLocation().getStartLine() >= d2.getLocation().getStartLine() and
4021
d.getSignificantName() = d2.getSignificantName() and

0 commit comments

Comments
 (0)