Skip to content

Commit 3f3a73f

Browse files
authored
Merge branch 'main' into knewbury01/Declarations5
2 parents 855de3c + c9eb8d0 commit 3f3a73f

File tree

54 files changed

+756
-54
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+756
-54
lines changed

.github/pull_request_template.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ _**Author:**_ Is a change note required?
3232
- [ ] Yes
3333
- [ ] No
3434

35+
🚨🚨🚨
36+
_**Reviewer:**_ Confirm that format of *shared* queries (not the .qll file, the
37+
.ql file that imports it) is valid by running them within VS Code.
38+
- [ ] Confirmed
39+
40+
3541
_**Reviewer:**_ Confirm that either a change note is not required or the change note is required and has been added.
3642
- [ ] Confirmed
3743

.vscode/tasks.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@
206206
"Declarations",
207207
"Declarations1",
208208
"Declarations2",
209+
"Declarations3",
210+
"Declarations4",
209211
"Declarations5",
210212
"Exceptions1",
211213
"Exceptions2",
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 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.identifierwithexternallinkageonedefinitionshared.IdentifierWithExternalLinkageOneDefinitionShared
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
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/misra/src/rules/RULE-5-5/IdentifiersNotDistinctFromMacroNames.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 Macro m, InterestingIdentifiers i, string mName, string iName
2020
where

c/misra/src/rules/RULE-5-6/TypedefNameNotUnique.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 TypedefType t, InterestingIdentifiers d
2020
where

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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @id c/misra/function-types-not-in-prototype-form
3+
* @name RULE-8-2: Function types shall be in prototype form with named parameters
4+
* @description Omission of parameter types or names prevents the compiler from doing type checking
5+
* when those functions are used and therefore may result in undefined behaviour.
6+
* @kind problem
7+
* @precision medium
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-2
10+
* correctness
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Identifiers
17+
18+
/**
19+
* `Parameter`s without names
20+
*/
21+
class UnnamedParameter extends Parameter {
22+
UnnamedParameter() { not this.isNamed() }
23+
}
24+
25+
/*
26+
* This is a copy of the private `hasZeroParamDecl` predicate from the standard set of
27+
* queries as of the `codeql-cli/2.11.2` tag in `github/codeql`.
28+
*/
29+
30+
predicate hasZeroParamDecl(Function f) {
31+
exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
32+
not fde.isImplicit() and
33+
not fde.hasVoidParamList() and
34+
fde.getNumberOfParameters() = 0 and
35+
not fde.isDefinition()
36+
)
37+
}
38+
39+
from Function f, string msg
40+
where
41+
not isExcluded(f, Declarations4Package::functionTypesNotInPrototypeFormQuery()) and
42+
f instanceof InterestingIdentifiers and
43+
(
44+
f.getAParameter() instanceof UnnamedParameter and
45+
msg = "Function " + f + " declares parameter that is unnamed."
46+
or
47+
hasZeroParamDecl(f) and
48+
msg = "Function " + f + " does not specifiy void for no parameters present."
49+
or
50+
//parameters declared in declaration list (not in function signature)
51+
//have placeholder file location associated only
52+
exists(Parameter p |
53+
p.getFunction() = f and
54+
not p.getFile() = f.getFile() and
55+
msg = "Function " + f + " declares parameter in unsupported declaration list."
56+
)
57+
)
58+
select f, msg
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 codingstandards.cpp.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+
parameterTypesIncompatible(f1, f2) and
31+
case = "parameter types"
32+
or
33+
//parameter name check
34+
parameterNamesIncompatible(f1, f2) and
35+
case = "parameter names"
36+
)
37+
select f1, "The " + case + " of re-declaration of $@ is not compatible with declaration $@", f1,
38+
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 codingstandards.cpp.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: 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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 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.rules.identifierwithexternallinkageonedefinitionshared.IdentifierWithExternalLinkageOneDefinitionShared
17+
18+
class IdentifierWithExternalLinkageShallHaveOneDefinitionQuery extends IdentifierWithExternalLinkageOneDefinitionSharedSharedQuery {
19+
IdentifierWithExternalLinkageShallHaveOneDefinitionQuery() {
20+
this = Declarations4Package::identifierWithExternalLinkageOneDefinitionQuery()
21+
}
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.c:3:6:3:7 | f1 | Function f1 declares parameter that is unnamed. |
2+
| test.c:4:6:4:7 | f2 | Function f2 does not specifiy void for no parameters present. |
3+
| test.c:5:6:5:7 | f3 | Function f3 does not specifiy void for no parameters present. |
4+
| test.c:7:5:7:6 | f5 | Function f5 declares parameter in unsupported declaration list. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-2/FunctionTypesNotInPrototypeForm.ql

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void f(int x); // COMPLIANT
2+
void f0(void); // COMPLIANT
3+
void f1(int); // NON_COMPLIANT
4+
void f2(); // NON_COMPLIANT
5+
void f3(x); // NON_COMPLIANT
6+
void f4(const x); // NON_COMPLIANT[FALSE_NEGATIVE]
7+
int f5(x) // NON_COMPLIANT
8+
int x;
9+
{ return 1; }
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+
}

0 commit comments

Comments
 (0)