Skip to content

Commit d5cfc67

Browse files
committed
Declarations5: add RULE-8-5
1 parent 7b4aede commit d5cfc67

File tree

12 files changed

+114
-1
lines changed

12 files changed

+114
-1
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
"Declarations",
207207
"Declarations1",
208208
"Declarations2",
209+
"Declarations5",
209210
"Exceptions1",
210211
"Exceptions2",
211212
"Expressions",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @id c/misra/external-object-or-function-not-declared-in-one-file
3+
* @name RULE-8-5: An external object or function shall be declared once in one and only one file
4+
* @description Declarations in multiple files can lead to unexpected program behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-8-5
9+
* correctness
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
16+
from DeclarationEntry de, DeclarationEntry otherDeclaration, string kind
17+
where
18+
not isExcluded(de, Declarations5Package::externalObjectOrFunctionNotDeclaredInOneFileQuery()) and
19+
//this rule applies to non-defining declarations only
20+
not de.isDefinition() and
21+
not otherDeclaration.isDefinition() and
22+
exists(Declaration d |
23+
de.getDeclaration() = d and
24+
otherDeclaration.getDeclaration() = d and
25+
de.getFile() != otherDeclaration.getFile()
26+
) and
27+
(
28+
de.getDeclaration() instanceof Function and kind = "function"
29+
or
30+
de.getDeclaration() instanceof Variable and
31+
not de.getDeclaration() instanceof Parameter and
32+
kind = "variable"
33+
) and
34+
// Apply an ordering based on location to enforce that (de1, de2) = (de2, de1) and we only report (de1, de2).
35+
(
36+
de.getFile().getAbsolutePath() < otherDeclaration.getFile().getAbsolutePath()
37+
or
38+
de.getFile().getAbsolutePath() = otherDeclaration.getFile().getAbsolutePath() and
39+
de.getLocation().getStartLine() < otherDeclaration.getLocation().getStartLine()
40+
)
41+
select de,
42+
"The " + kind + " declaration " + de.getName() +
43+
" is declared in multiple files and has an additional $@.", otherDeclaration, "declaration"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:8:12:8:13 | declaration of g3 | The variable declaration g3 is declared in multiple files and has an additional $@. | test1.c:1:12:1:13 | declaration of g3 | declaration |
2+
| test.h:1:12:1:12 | declaration of g | The variable declaration g is declared in multiple files and has an additional $@. | test1.h:1:12:1:12 | declaration of g | declaration |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-5/ExternalObjectOrFunctionNotDeclaredInOneFile.ql

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "test.h"
2+
#include "test1.h"
3+
4+
int g = 1; // COMPLIANT
5+
6+
extern int g1; // COMPLIANT
7+
8+
extern int g3; // NON_COMPLIANT

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern int g; // NON_COMPLIANT
2+
3+
int g2; // COMPLIANT

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern int g3; // NON_COMPLIANT

c/misra/test/rules/RULE-8-5/test1.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern int g; // NON_COMPLIANT
2+
3+
int g2; // COMPLIANT
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 Declarations5Query = TExternalObjectOrFunctionNotDeclaredInOneFileQuery()
7+
8+
predicate isDeclarations5QueryMetadata(Query query, string queryId, string ruleId) {
9+
query =
10+
// `Query` instance for the `externalObjectOrFunctionNotDeclaredInOneFile` query
11+
Declarations5Package::externalObjectOrFunctionNotDeclaredInOneFileQuery() and
12+
queryId =
13+
// `@id` for the `externalObjectOrFunctionNotDeclaredInOneFile` query
14+
"c/misra/external-object-or-function-not-declared-in-one-file" and
15+
ruleId = "RULE-8-5"
16+
}
17+
18+
module Declarations5Package {
19+
Query externalObjectOrFunctionNotDeclaredInOneFileQuery() {
20+
//autogenerate `Query` type
21+
result =
22+
// `Query` type for `externalObjectOrFunctionNotDeclaredInOneFile` query
23+
TQueryC(TDeclarations5PackageQuery(TExternalObjectOrFunctionNotDeclaredInOneFileQuery()))
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
@@ -15,6 +15,7 @@ import Contracts4
1515
import Declarations1
1616
import Declarations2
1717
import Declarations3
18+
import Declarations5
1819
import Expressions
1920
import IO1
2021
import IO2
@@ -52,6 +53,7 @@ newtype TCQuery =
5253
TDeclarations1PackageQuery(Declarations1Query q) or
5354
TDeclarations2PackageQuery(Declarations2Query q) or
5455
TDeclarations3PackageQuery(Declarations3Query q) or
56+
TDeclarations5PackageQuery(Declarations5Query q) or
5557
TExpressionsPackageQuery(ExpressionsQuery q) or
5658
TIO1PackageQuery(IO1Query q) or
5759
TIO2PackageQuery(IO2Query q) or
@@ -89,6 +91,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId) {
8991
isDeclarations1QueryMetadata(query, queryId, ruleId) or
9092
isDeclarations2QueryMetadata(query, queryId, ruleId) or
9193
isDeclarations3QueryMetadata(query, queryId, ruleId) or
94+
isDeclarations5QueryMetadata(query, queryId, ruleId) or
9295
isExpressionsQueryMetadata(query, queryId, ruleId) or
9396
isIO1QueryMetadata(query, queryId, ruleId) or
9497
isIO2QueryMetadata(query, queryId, ruleId) or

0 commit comments

Comments
 (0)