Skip to content

Commit 43f3137

Browse files
committed
A16-0-1: exclusions for handling else and elif
1 parent e1f822a commit 43f3137

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`A16-0-1`: `cpp/autosar/pre-processor-shall-only-be-used-for-certain-directives-patterns`
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: 15 additions & 2 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

@@ -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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
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:33:1:33:26 | #ifdef OBJECTLIKE_MACRO_NO | Preprocessor directive used for conditional compilation. |
6+
| test.cpp:35:1:35:26 | #elif OBJECTLIKE_MACRO > 0 | Preprocessor directive used for conditional compilation. |
7+
| test.cpp:39:1:39:23 | #ifdef OBJECTLIKE_MACRO | Preprocessor directive used for conditional compilation. |

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,28 @@ 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+
#endif // COMPLIANT
32+
33+
#ifdef OBJECTLIKE_MACRO_NO // NON_COMPLIANT
34+
int x = 0; // not present
35+
#elif OBJECTLIKE_MACRO > 0 // NON_COMPLIANT
36+
int x = 1; // present
37+
#endif // COMPLIANT
38+
39+
#ifdef OBJECTLIKE_MACRO // NON_COMPLIANT
40+
int x1 = 0; // present
41+
#elif OBJECTLIKE_MACRO > \
42+
-1 // COMPLIANT - by technicality of conditional compilation
43+
int x1 = 1; // not present
44+
#endif // COMPLIANT

0 commit comments

Comments
 (0)