Skip to content

Commit 947ea95

Browse files
authored
Merge branch 'main' into rvermeulen/update-pack-generation
2 parents 4b7e428 + 99c5762 commit 947ea95

28 files changed

+411
-38
lines changed

.vscode/tasks.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@
221221
"Pointers",
222222
"Preprocessor1",
223223
"Preprocessor2",
224+
"Preprocessor3",
225+
"Preprocessor4",
224226
"IntegerConversion",
225227
"Expressions",
226-
"DeadCode"
228+
"DeadCode",
227229
"VirtualFunctions"
228230
]
229231
},
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import cpp
2+
3+
/** Module to reason about keywords in standards C90, C99 and C11. */
4+
module Keywords {
5+
/** Holds if `s` is a keyword. */
6+
predicate isKeyword(string s) {
7+
s = "auto"
8+
or
9+
s = "break"
10+
or
11+
s = "case"
12+
or
13+
s = "char"
14+
or
15+
s = "const"
16+
or
17+
s = "continue"
18+
or
19+
s = "default"
20+
or
21+
s = "do"
22+
or
23+
s = "double"
24+
or
25+
s = "else"
26+
or
27+
s = "enum"
28+
or
29+
s = "extern"
30+
or
31+
s = "float"
32+
or
33+
s = "for"
34+
or
35+
s = "goto"
36+
or
37+
s = "if"
38+
or
39+
s = "inline"
40+
or
41+
s = "int"
42+
or
43+
s = "long"
44+
or
45+
s = "register"
46+
or
47+
s = "restrict"
48+
or
49+
s = "return"
50+
or
51+
s = "short"
52+
or
53+
s = "signed"
54+
or
55+
s = "sizeof"
56+
or
57+
s = "static"
58+
or
59+
s = "struct"
60+
or
61+
s = "switch"
62+
or
63+
s = "typedef"
64+
or
65+
s = "union"
66+
or
67+
s = "unsigned"
68+
or
69+
s = "void"
70+
or
71+
s = "volatile"
72+
or
73+
s = "while"
74+
or
75+
s = "_Alignas"
76+
or
77+
s = "_Alignof"
78+
or
79+
s = "_Atomic"
80+
or
81+
s = "_Bool"
82+
or
83+
s = "_Complex"
84+
or
85+
s = "_Generic"
86+
or
87+
s = "_Imaginary"
88+
or
89+
s = "_Noreturn"
90+
or
91+
s = "_Static_assert"
92+
or
93+
s = "_Thread_local"
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:5:3:11:3 | MACROFUNCTION(X) | Invocation of macro MACROFUNCTION includes a token "#else" that could be confused for an argument preprocessor directive. |
2+
| test.c:5:3:11:3 | MACROFUNCTION(X) | Invocation of macro MACROFUNCTION includes a token "#endif" that could be confused for an argument preprocessor directive. |
3+
| test.c:5:3:11:3 | MACROFUNCTION(X) | Invocation of macro MACROFUNCTION includes a token "#if NOTDEFINEDMACRO" that could be confused for an argument preprocessor directive. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.preprocessingdirectivewithinmacroargument.PreprocessingDirectiveWithinMacroArgument
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <string.h>
2+
#define MACROFUNCTION(X) strlen(X)
3+
4+
void f() {
5+
MACROFUNCTION(
6+
#if NOTDEFINEDMACRO // NON_COMPLIANT
7+
"longstringtest!test!"
8+
#else // NON_COMPLIANT
9+
"shortstring"
10+
#endif // NON_COMPLIANT
11+
);
12+
13+
MACROFUNCTION("alright"); // COMPLIANT
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id c/misra/macro-defined-with-the-same-name-as-keyword
3+
* @name RULE-20-4: A macro shall not be defined with the same name as a keyword
4+
* @description Redefinition of keywords is confusing and in the case where the standard library is
5+
* included where that keyword is defined, the redefinition will result in undefined
6+
* behaviour.
7+
* @kind problem
8+
* @precision very-high
9+
* @problem.severity warning
10+
* @tags external/misra/id/rule-20-4
11+
* correctness
12+
* readability
13+
* maintainability
14+
* external/misra/obligation/required
15+
*/
16+
17+
import cpp
18+
import codingstandards.c.misra
19+
import codingstandards.c.Keywords
20+
21+
from Macro m, string name
22+
where
23+
not isExcluded(m, Preprocessor4Package::macroDefinedWithTheSameNameAsKeywordQuery()) and
24+
m.hasName(name) and
25+
Keywords::isKeyword(name)
26+
select m, "Redefinition of keyword '" + name + "'."
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @id c/misra/function-like-macro-args-contain-hash-token-c-query
3+
* @name RULE-20-6: Tokens that look like a preprocessing directive shall not occur within a macro argument
4+
* @description Arguments to a function-like macro shall not contain tokens that look like
5+
* pre-processing directives or else behaviour after macro expansion is unpredictable.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-20-6
10+
* readability
11+
* correctness
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.rules.preprocessingdirectivewithinmacroargument.PreprocessingDirectiveWithinMacroArgument
18+
19+
class FunctionLikeMacroArgsContainHashTokenCQueryQuery extends PreprocessingDirectiveWithinMacroArgumentSharedQuery {
20+
FunctionLikeMacroArgsContainHashTokenCQueryQuery() {
21+
this = Preprocessor4Package::functionLikeMacroArgsContainHashTokenCQueryQuery()
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @id c/misra/define-and-undef-used-on-reserved-identifier-or-macro-name
3+
* @name RULE-21-1: #define and #undef shall not be used on a reserved identifier or reserved macro name
4+
* @description The use of #define and #undef on reserved identifiers or macro names can result in
5+
* undefined behaviour.
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-21-1
10+
* correctness
11+
* readability
12+
* maintainability
13+
* external/misra/obligation/required
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
import codingstandards.cpp.Naming
19+
20+
from PreprocessorDirective p, string name
21+
where
22+
not isExcluded(p, Preprocessor4Package::defineAndUndefUsedOnReservedIdentifierOrMacroNameQuery()) and
23+
(
24+
p.(Macro).hasName(name)
25+
or
26+
p.(PreprocessorUndef).getName() = name
27+
) and
28+
(
29+
Naming::Cpp14::hasStandardLibraryMacroName(name)
30+
or
31+
Naming::Cpp14::hasStandardLibraryObjectName(name)
32+
or
33+
name.regexpMatch("_.*")
34+
or
35+
name = "defined"
36+
)
37+
select p, "Reserved identifier '" + name + "' has been undefined or redefined."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:1:1:1:16 | #define int long | Redefinition of keyword 'int'. |
2+
| test.c:2:1:2:30 | #define while (E) for (; (E);) | Redefinition of keyword 'while'. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-20-4/MacroDefinedWithTheSameNameAsKeyword.ql

0 commit comments

Comments
 (0)