Skip to content

Commit 57d83bc

Browse files
committed
Declarations4: add RULE-8-3
1 parent 2f23fe2 commit 57d83bc

17 files changed

+293
-1
lines changed

.vscode/tasks.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@
205205
"Declarations",
206206
"Declarations1",
207207
"Declarations2",
208+
"Declarations3",
209+
"Declarations4",
208210
"Exceptions1",
209211
"Exceptions2",
210212
"Expressions",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import cpp
2+
3+
/*
4+
* This is a copy of the `arithTypesMatch` predicate from the standard set of
5+
* queries as of the `codeql-cli/2.9.4` tag in `github/codeql`.
6+
*/
7+
8+
pragma[inline]
9+
predicate arithTypesMatch(Type t1, Type t2) {
10+
t1 = t2
11+
or
12+
t1.getSize() = t2.getSize() and
13+
(
14+
t1 instanceof IntegralOrEnumType and
15+
t2 instanceof IntegralOrEnumType
16+
or
17+
t1 instanceof FloatingPointType and
18+
t2 instanceof FloatingPointType
19+
)
20+
}
21+
22+
predicate typesCompatible(Type t1, Type t2) {
23+
if t1 instanceof BuiltInType and t2 instanceof BuiltInType
24+
then
25+
//for simple types consider compatible
26+
arithTypesMatch(t1, t2)
27+
else
28+
//otherwise include type qualifiers and typedef names
29+
t1 = t2
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @id c/misra/declarations-of-a-function-same-name-and-type
3+
* @name RULE-8-3: All declarations of a function shall use the same names and type qualifiers
4+
* @description Using different types across the same declarations disallows strong type checking
5+
* and can lead to undefined behaviour.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-3
10+
* correctness
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import Compatible
17+
18+
from FunctionDeclarationEntry f1, FunctionDeclarationEntry f2, string case
19+
where
20+
not isExcluded(f1, Declarations4Package::declarationsOfAFunctionSameNameAndTypeQuery()) and
21+
not isExcluded(f2, Declarations4Package::declarationsOfAFunctionSameNameAndTypeQuery()) and
22+
not f1 = f2 and
23+
f1.getDeclaration() = f2.getDeclaration() and
24+
//return type check
25+
(
26+
not typesCompatible(f1.getType(), f2.getType()) and
27+
case = "return type"
28+
or
29+
//parameter type check
30+
exists(ParameterDeclarationEntry p1, ParameterDeclarationEntry p2, int i |
31+
p1 = f1.getParameterDeclarationEntry(i) and
32+
p2 = f2.getParameterDeclarationEntry(i)
33+
|
34+
not typesCompatible(p1.getType(), p2.getType())
35+
) and
36+
case = "parameter types"
37+
or
38+
//parameter name check
39+
exists(ParameterDeclarationEntry p1, ParameterDeclarationEntry p2, int i |
40+
p1 = f1.getParameterDeclarationEntry(i) and
41+
p2 = f2.getParameterDeclarationEntry(i)
42+
|
43+
not p1.getName() = p2.getName()
44+
) and
45+
case = "parameter names"
46+
)
47+
select f1, "The " + case + " of re-declaration of $@ is not compatible with declaration $@", f1,
48+
f1.getName(), f2, f2.getName()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id c/misra/declarations-of-an-object-same-name-and-type
3+
* @name RULE-8-3: All declarations of an object shall use the same names and type qualifiers
4+
* @description Using different types across the same declarations disallows strong type checking
5+
* and can lead to undefined behaviour.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-3
10+
* correctness
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import Compatible
17+
18+
from VariableDeclarationEntry decl1, VariableDeclarationEntry decl2
19+
where
20+
not isExcluded(decl1, Declarations4Package::declarationsOfAnObjectSameNameAndTypeQuery()) and
21+
not isExcluded(decl2, Declarations4Package::declarationsOfAnObjectSameNameAndTypeQuery()) and
22+
not decl1 = decl2 and
23+
decl1.getVariable().getQualifiedName() = decl2.getVariable().getQualifiedName() and
24+
not typesCompatible(decl1.getType(), decl2.getType())
25+
select decl1,
26+
"The object $@ of type " + decl1.getType().toString() +
27+
" is not compatible with re-declaration $@ of type " + decl2.getType().toString(), decl1,
28+
decl1.getName(), decl2, decl2.getName()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
| function1.c:6:6:6:7 | declaration of f3 | The return type of re-declaration of $@ is not compatible with declaration $@ | function1.c:6:6:6:7 | declaration of f3 | f3 | function1.c:8:4:8:5 | declaration of f3 | f3 |
2+
| function1.c:8:4:8:5 | declaration of f3 | The return type of re-declaration of $@ is not compatible with declaration $@ | function1.c:8:4:8:5 | declaration of f3 | f3 | function1.c:6:6:6:7 | declaration of f3 | f3 |
3+
| function1.c:8:4:8:5 | declaration of f3 | The return type of re-declaration of $@ is not compatible with declaration $@ | function1.c:8:4:8:5 | declaration of f3 | f3 | function2.c:4:6:4:7 | declaration of f3 | f3 |
4+
| function1.c:9:6:9:7 | declaration of f4 | The return type of re-declaration of $@ is not compatible with declaration $@ | function1.c:9:6:9:7 | declaration of f4 | f4 | function2.c:5:5:5:6 | declaration of f4 | f4 |
5+
| function1.c:13:5:13:6 | definition of f6 | The return type of re-declaration of $@ is not compatible with declaration $@ | function1.c:13:5:13:6 | definition of f6 | f6 | function2.c:9:6:9:7 | definition of f6 | f6 |
6+
| function1.c:21:3:21:5 | definition of f21 | The parameter types of re-declaration of $@ is not compatible with declaration $@ | function1.c:21:3:21:5 | definition of f21 | f21 | function2.c:17:10:17:12 | declaration of f21 | f21 |
7+
| function1.c:25:6:25:8 | definition of f22 | The parameter names of re-declaration of $@ is not compatible with declaration $@ | function1.c:25:6:25:8 | definition of f22 | f22 | function2.c:19:13:19:15 | declaration of f22 | f22 |
8+
| function2.c:4:6:4:7 | declaration of f3 | The return type of re-declaration of $@ is not compatible with declaration $@ | function2.c:4:6:4:7 | declaration of f3 | f3 | function1.c:8:4:8:5 | declaration of f3 | f3 |
9+
| function2.c:5:5:5:6 | declaration of f4 | The return type of re-declaration of $@ is not compatible with declaration $@ | function2.c:5:5:5:6 | declaration of f4 | f4 | function1.c:9:6:9:7 | declaration of f4 | f4 |
10+
| function2.c:9:6:9:7 | definition of f6 | The return type of re-declaration of $@ is not compatible with declaration $@ | function2.c:9:6:9:7 | definition of f6 | f6 | function1.c:13:5:13:6 | definition of f6 | f6 |
11+
| function2.c:17:10:17:12 | declaration of f21 | The parameter types of re-declaration of $@ is not compatible with declaration $@ | function2.c:17:10:17:12 | declaration of f21 | f21 | function1.c:21:3:21:5 | definition of f21 | f21 |
12+
| function2.c:19:13:19:15 | declaration of f22 | The parameter names of re-declaration of $@ is not compatible with declaration $@ | function2.c:19:13:19:15 | declaration of f22 | f22 | function1.c:25:6:25:8 | definition of f22 | f22 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-3/DeclarationsOfAFunctionSameNameAndType.ql
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
| object1.c:3:6:3:7 | definition of a3 | The object $@ of type long is not compatible with re-declaration $@ of type LL | object1.c:3:6:3:7 | definition of a3 | a3 | object2.c:9:11:9:12 | declaration of a3 | a3 |
2+
| object1.c:4:6:4:7 | definition of a4 | The object $@ of type long is not compatible with re-declaration $@ of type int | object1.c:4:6:4:7 | definition of a4 | a4 | object2.c:11:12:11:13 | declaration of a4 | a4 |
3+
| object1.c:5:5:5:6 | definition of a5 | The object $@ of type int is not compatible with re-declaration $@ of type long | object1.c:5:5:5:6 | definition of a5 | a5 | object2.c:13:13:13:14 | declaration of a5 | a5 |
4+
| object1.c:6:6:6:7 | definition of a6 | The object $@ of type long is not compatible with re-declaration $@ of type int | object1.c:6:6:6:7 | definition of a6 | a6 | object2.c:17:1:17:3 | declaration of a6 | a6 |
5+
| object1.c:7:5:7:6 | definition of a7 | The object $@ of type int is not compatible with re-declaration $@ of type LL | object1.c:7:5:7:6 | definition of a7 | a7 | object2.c:19:11:19:12 | declaration of a7 | a7 |
6+
| object1.c:13:5:13:7 | definition of a10 | The object $@ of type int[100] is not compatible with re-declaration $@ of type LI[100] | object1.c:13:5:13:7 | definition of a10 | a10 | object2.c:22:4:22:6 | definition of a10 | a10 |
7+
| object1.c:14:5:14:7 | definition of a11 | The object $@ of type int[100] is not compatible with re-declaration $@ of type int[101] | object1.c:14:5:14:7 | definition of a11 | a11 | object2.c:23:12:23:14 | declaration of a11 | a11 |
8+
| object1.c:17:12:17:14 | definition of a13 | The object $@ of type int *const is not compatible with re-declaration $@ of type int * | object1.c:17:12:17:14 | definition of a13 | a13 | object2.c:26:13:26:15 | declaration of a13 | a13 |
9+
| object2.c:9:11:9:12 | declaration of a3 | The object $@ of type LL is not compatible with re-declaration $@ of type long | object2.c:9:11:9:12 | declaration of a3 | a3 | object1.c:3:6:3:7 | definition of a3 | a3 |
10+
| object2.c:11:12:11:13 | declaration of a4 | The object $@ of type int is not compatible with re-declaration $@ of type long | object2.c:11:12:11:13 | declaration of a4 | a4 | object1.c:4:6:4:7 | definition of a4 | a4 |
11+
| object2.c:13:13:13:14 | declaration of a5 | The object $@ of type long is not compatible with re-declaration $@ of type int | object2.c:13:13:13:14 | declaration of a5 | a5 | object1.c:5:5:5:6 | definition of a5 | a5 |
12+
| object2.c:17:1:17:3 | declaration of a6 | The object $@ of type int is not compatible with re-declaration $@ of type long | object2.c:17:1:17:3 | declaration of a6 | a6 | object1.c:6:6:6:7 | definition of a6 | a6 |
13+
| object2.c:19:11:19:12 | declaration of a7 | The object $@ of type LL is not compatible with re-declaration $@ of type int | object2.c:19:11:19:12 | declaration of a7 | a7 | object1.c:7:5:7:6 | definition of a7 | a7 |
14+
| object2.c:22:4:22:6 | definition of a10 | The object $@ of type LI[100] is not compatible with re-declaration $@ of type int[100] | object2.c:22:4:22:6 | definition of a10 | a10 | object1.c:13:5:13:7 | definition of a10 | a10 |
15+
| object2.c:23:12:23:14 | declaration of a11 | The object $@ of type int[101] is not compatible with re-declaration $@ of type int[100] | object2.c:23:12:23:14 | declaration of a11 | a11 | object1.c:14:5:14:7 | definition of a11 | a11 |
16+
| object2.c:26:13:26:15 | declaration of a13 | The object $@ of type int * is not compatible with re-declaration $@ of type int *const | object2.c:26:13:26:15 | declaration of a13 | a13 | object1.c:17:12:17:14 | definition of a13 | a13 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-3/DeclarationsOfAnObjectSameNameAndType.ql
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
typedef long LL;
2+
3+
int f1(); // COMPLIANT
4+
int f2(int f2a); // COMPLIANT
5+
6+
long f3(); // NON_COMPLIANT
7+
8+
LL f3(); // NON_COMPLIANT
9+
long f4(int f4a); // NON_COMPLIANT
10+
11+
long f5(int f5a) { return 0; } // COMPLIANT
12+
13+
int f6(int f6a) { return 0; } // NON_COMPLIANT
14+
15+
int f20(int f20a); // COMPLIANT - overloaded function
16+
17+
typedef int wi;
18+
typedef int hi;
19+
typedef long a;
20+
21+
a f21(wi w, wi h) { // NON_COMPLIANT
22+
return (a)w * h;
23+
}
24+
25+
void f22(int f22b, int f22a) { // NON_COMPLIANT
26+
return;
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
int f1(); // COMPLIANT
2+
int f2(int f2a); // COMPLIANT
3+
4+
long f3(); // NON_COMPLIANT
5+
int f4(int f4a); // NON_COMPLIANT
6+
7+
long f5(int f5a) { return 0; } // COMPLIANT
8+
9+
long f6(int f6a) { return 0; } // NON_COMPLIANT
10+
11+
int f20(int f20a, int f20b); // COMPLIANT -- overloaded function
12+
13+
typedef int wi;
14+
typedef int hi;
15+
typedef long a;
16+
17+
extern a f21(wi w, hi h); // NON_COMPLIANT
18+
19+
extern void f22(int f22a, int f22b); // NON_COMPLIANT

0 commit comments

Comments
 (0)