Skip to content

Commit 08fe14e

Browse files
authored
Merge pull request #517 from knewbury01/knewbury01/A16-0-1
A16-0-1: exclusions for handling else and elif
2 parents f6b4b89 + 644afaf commit 08fe14e

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`A16-0-1` - `PreProcessorShallOnlyBeUsedForCertainDirectivesPatterns.ql`:
2+
- Exclude all preprocessor elses and also consider elifs separately (ie do not affect valid ifs) but not valid if not meeting the same criteria as an ifdef etc.

cpp/autosar/src/rules/A16-0-1/PreProcessorShallOnlyBeUsedForCertainDirectivesPatterns.ql

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,26 @@ import cpp
2121
import codingstandards.cpp.autosar
2222
import codingstandards.cpp.FunctionLikeMacro
2323

24+
class PermittedInnerDirectiveType extends PreprocessorDirective {
25+
PermittedInnerDirectiveType() {
26+
//permissive listing for directives that can be used in a valid wrapper
27+
this instanceof MacroWrapper or
28+
this instanceof PreprocessorEndif or
29+
this instanceof Include or
30+
this instanceof PermittedMacro or
31+
this instanceof PreprocessorElif or
32+
this instanceof PreprocessorElse
33+
}
34+
}
35+
2436
class PermittedDirectiveType extends PreprocessorDirective {
2537
PermittedDirectiveType() {
2638
//permissive listing in case directive types modelled in ql ever expands (example non valid directives)
2739
this instanceof MacroWrapper or
2840
this instanceof PreprocessorEndif or
2941
this instanceof Include or
30-
this instanceof PermittedMacro
42+
this instanceof PermittedMacro or
43+
this instanceof PreprocessorElse
3144
}
3245
}
3346

@@ -40,9 +53,9 @@ pragma[noinline]
4053
predicate isPreprocConditionalRange(
4154
PreprocessorBranch pb, string filepath, int startLine, int endLine
4255
) {
43-
exists(PreprocessorEndif end | pb.getEndIf() = end |
44-
isPreprocFileAndLine(pb, filepath, startLine) and
45-
isPreprocFileAndLine(end, filepath, endLine)
56+
isPreprocFileAndLine(pb, filepath, startLine) and
57+
exists(PreprocessorDirective end |
58+
pb.getNext() = end and isPreprocFileAndLine(end, filepath, endLine)
4659
)
4760
}
4861

@@ -73,7 +86,7 @@ class MacroWrapper extends PreprocessorIfndef {
7386
class AcceptableWrapper extends PreprocessorBranch {
7487
AcceptableWrapper() {
7588
forall(Element inner | not inner instanceof Comment and this = getAGuard(inner) |
76-
inner instanceof PermittedDirectiveType
89+
inner instanceof PermittedInnerDirectiveType
7790
)
7891
}
7992
}

cpp/autosar/test/rules/A16-0-1/PreProcessorShallOnlyBeUsedForCertainDirectivesPatterns.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
| test.cpp:9:1:9:26 | #define OBJECTLIKE_MACRO 1 | Preprocessor directive used for conditional compilation. |
33
| test.cpp:10:1:10:35 | #define FUNCTIONLIKE_MACRO(X) X + 1 | Preprocessor directive used for conditional compilation. |
44
| test.cpp:11:1:11:37 | #define FUNCTIONLIKE_MACROTWO() 1 + 1 | Preprocessor directive used for conditional compilation. |
5+
| test.cpp:31:1:31:26 | #elif OBJECTLIKE_MACRO > 0 | Preprocessor directive used for conditional compilation. |
6+
| test.cpp:37:1:37:26 | #elif OBJECTLIKE_MACRO > 0 | Preprocessor directive used for conditional compilation. |
7+
| test.cpp:41:1:41:23 | #ifdef OBJECTLIKE_MACRO | Preprocessor directive used for conditional compilation. |
8+
| test.cpp:58:1:58:27 | #elif MACRO_ENABLED_OTHER_1 | Preprocessor directive used for conditional compilation. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options:--clang -std=c++14 --edg --diag_error=implicit_func_decl -nostdinc -I../../../../../cpp/common/test/includes/standard-library -D MACRO_ENABLED_NON_1 -D MACRO_ENABLED_OTHER_1

cpp/autosar/test/rules/A16-0-1/test.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,59 @@ int g;
1717
#ifndef TESTHEADER // COMPLIANT
1818
#include <string> //COMPLIANT
1919
#endif // COMPLIANT
20+
21+
#ifdef MACRO_ENABLED // COMPLIANT
22+
#include <string> // COMPLIANT
23+
#else // COMPLIANT
24+
#include <string> // COMPLIANT
25+
#endif // COMPLIANT
26+
27+
#ifdef MACRO_ENABLED_NON // COMPLIANT
28+
#include <string> // COMPLIANT
29+
#elif MACRO_ENABLED_OTHER // COMPLIANT
30+
#include <string> // COMPLIANT
31+
#elif OBJECTLIKE_MACRO > 0 // NON_COMPLIANT
32+
int x00 = 1; // present
33+
#endif // COMPLIANT
34+
35+
#ifdef OBJECTLIKE_MACRO_NO // COMPLIANT
36+
int x0 = 0; // not present
37+
#elif OBJECTLIKE_MACRO > 0 // NON_COMPLIANT
38+
int x0 = 1; // present
39+
#endif // COMPLIANT
40+
41+
#ifdef OBJECTLIKE_MACRO // NON_COMPLIANT
42+
int x1 = 0; // present
43+
#elif OBJECTLIKE_MACRO > -1 // NON_COMPLIANT[FALSE_NEGATIVE] - known due to
44+
// database not containing elements
45+
int x1 = 1; // not present
46+
#endif // COMPLIANT
47+
48+
// case 1 - first present only
49+
#ifdef MACRO_ENABLED_NON_1 // COMPLIANT
50+
#include <string> //present
51+
#elif MACRO_ENABLED_OTHER // NON_COMPLIANT[FALSE_NEGATIVE]
52+
int x = 1; // not present
53+
#endif
54+
55+
// case 2 - second present only
56+
#ifdef MACRO_ENABLED_NON // COMPLIANT
57+
#include <string> //not present
58+
#elif MACRO_ENABLED_OTHER_1 // NON_COMPLIANT
59+
int x = 1; // present
60+
#endif
61+
62+
// case 3 - neither present
63+
#ifdef MACRO_ENABLED_NON // COMPLIANT
64+
#include <string> //not present
65+
#elif MACRO_ENABLED_OTHER // NON_COMPLIANT[FALSE_NEGATIVE]
66+
int x = 1; // not present
67+
#endif
68+
69+
// case 4 - both look present but the second still not bc the condition is not
70+
// required to be evaluated
71+
#ifdef MACRO_ENABLED_NON_1 // COMPLIANT
72+
#include <string> //present
73+
#elif MACRO_ENABLED_OTHER_1 // NON_COMPLIANT[FALSE_NEGATIVE]
74+
int x = 1; // not present
75+
#endif

0 commit comments

Comments
 (0)