Skip to content

Commit 3b4443e

Browse files
committed
Declarations4: add RULE-8-4
1 parent b601c91 commit 3b4443e

16 files changed

+213
-19
lines changed

c/misra/src/rules/RULE-8-3/DeclarationsOfAFunctionSameNameAndType.ql

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,6 @@ import cpp
1515
import codingstandards.c.misra
1616
import codingstandards.cpp.Compatible
1717

18-
predicate parameterTypesIncompatible(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
19-
exists(ParameterDeclarationEntry p1, ParameterDeclarationEntry p2, int i |
20-
p1 = f1.getParameterDeclarationEntry(i) and
21-
p2 = f2.getParameterDeclarationEntry(i)
22-
|
23-
not typesCompatible(p1.getType(), p2.getType())
24-
)
25-
}
26-
27-
predicate parameterNamesIncompatible(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
28-
exists(ParameterDeclarationEntry p1, ParameterDeclarationEntry p2, int i |
29-
p1 = f1.getParameterDeclarationEntry(i) and
30-
p2 = f2.getParameterDeclarationEntry(i)
31-
|
32-
not p1.getName() = p2.getName()
33-
)
34-
}
35-
3618
from FunctionDeclarationEntry f1, FunctionDeclarationEntry f2, string case
3719
where
3820
not isExcluded(f1, Declarations4Package::declarationsOfAFunctionSameNameAndTypeQuery()) and
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @id c/misra/compatible-declaration-function-defined
3+
* @name RULE-8-4: A compatible declaration shall be visible when a function with external linkage is defined
4+
* @description A compatible declaration shall be visible when a function with external linkage is
5+
* defined, otherwise program behaviour may be undefined.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-4
10+
* readability
11+
* maintainability
12+
* correctness
13+
* external/misra/obligation/required
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
import codingstandards.cpp.Identifiers
19+
import codingstandards.cpp.Compatible
20+
21+
from FunctionDeclarationEntry f1
22+
where
23+
not isExcluded(f1, Declarations4Package::compatibleDeclarationFunctionDefinedQuery()) and
24+
f1.isDefinition() and
25+
f1.getDeclaration() instanceof ExternalIdentifiers and
26+
//no declaration matches exactly
27+
(
28+
not exists(FunctionDeclarationEntry f2 |
29+
not f2.isDefinition() and
30+
f2.getDeclaration() = f1.getDeclaration()
31+
)
32+
or
33+
//or one exists that is close but incompatible in some way
34+
exists(FunctionDeclarationEntry f2 |
35+
f1.getName() = f2.getName() and
36+
not f2.isDefinition() and
37+
f2.getDeclaration() = f1.getDeclaration() and
38+
//return types differ
39+
(
40+
not typesCompatible(f1.getType(), f2.getType())
41+
or
42+
//parameter types differ
43+
parameterTypesIncompatible(f1, f2)
44+
or
45+
//parameter names differ
46+
parameterNamesIncompatible(f1, f2)
47+
)
48+
)
49+
)
50+
select f1, "No separate compatible declaration found for this definition."
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @id c/misra/compatible-declaration-object-defined
3+
* @name RULE-8-4: A compatible declaration shall be visible when an object with external linkage is defined
4+
* @description A compatible declaration shall be visible when an object with external linkage is
5+
* defined, otherwise program behaviour may be undefined.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-4
10+
* readability
11+
* maintainability
12+
* correctness
13+
* external/misra/obligation/required
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
import codingstandards.cpp.Identifiers
19+
import codingstandards.cpp.Compatible
20+
21+
from VariableDeclarationEntry decl1
22+
where
23+
not isExcluded(decl1, Declarations4Package::compatibleDeclarationObjectDefinedQuery()) and
24+
decl1.isDefinition() and
25+
decl1.getDeclaration() instanceof ExternalIdentifiers and
26+
(
27+
//no declaration matches exactly
28+
not exists(VariableDeclarationEntry decl2 |
29+
not decl2.isDefinition() and decl2.getDeclaration() = decl1.getDeclaration()
30+
) and
31+
//and none is close enough
32+
not exists(VariableDeclarationEntry decl2 |
33+
not decl2.isDefinition() and
34+
decl1.getVariable().getQualifiedName() = decl2.getVariable().getQualifiedName() and
35+
typesCompatible(decl1.getType(), decl2.getType())
36+
)
37+
)
38+
select decl1, "No separate compatible declaration found for this definition."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| function2.c:5:6:5:7 | definition of f3 | |
2+
| function2.c:7:6:7:7 | definition of f4 | |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-4/CompatibleDeclarationFunctionDefined.ql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| object1.c:4:12:4:13 | definition of i1 | No separate compatible declaration found for this definition. |
2+
| object1.c:6:5:6:6 | definition of i2 | No separate compatible declaration found for this definition. |
3+
| object2.c:1:7:1:8 | definition of i3 | No separate compatible declaration found for this definition. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-4/CompatibleDeclarationObjectDefined.ql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern void f1(); // COMPLIANT
2+
extern void f2(int x, int y); // COMPLIANT
3+
extern void f3(int x, int y); // NON_COMPLIANT
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void f1() {} // COMPLIANT
2+
3+
void f2(int x, int y) {} // COMPLIANT
4+
5+
void f3(short x, int y) {} // NON_COMPLIANT
6+
7+
void f4() {} // NON_COMPLIANT
8+
9+
static void f5() {} // COMPLIANT

c/misra/test/rules/RULE-8-4/object1.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extern int i;
2+
i = 0; // COMPLIANT
3+
4+
extern int i1 = 0; // NON_COMPLIANT
5+
6+
int i2 = 0; // NON_COMPLIANT
7+
8+
extern int i3; // NON_COMPLIANT
9+
10+
extern int i4; // COMPLIANT

0 commit comments

Comments
 (0)