Skip to content

Commit 40d18f1

Browse files
committed
A16-0-1: improve testcase and query logic
1 parent 18a297b commit 40d18f1

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,12 @@ pragma[noinline]
5353
predicate isPreprocConditionalRange(
5454
PreprocessorBranch pb, string filepath, int startLine, int endLine
5555
) {
56-
//the range of an if with an elif ends at the elif to avoid reporting things twice
57-
if exists(PreprocessorElif elif | elif.getIf() = pb)
58-
then
59-
exists(PreprocessorElif end | end.getIf() = pb |
60-
isPreprocFileAndLine(pb, filepath, startLine) and
61-
isPreprocFileAndLine(end, filepath, endLine)
62-
)
63-
else
64-
exists(PreprocessorEndif end | pb.getEndIf() = end |
65-
isPreprocFileAndLine(pb, filepath, startLine) and
66-
isPreprocFileAndLine(end, filepath, endLine)
56+
isPreprocFileAndLine(pb, filepath, startLine) and
57+
endLine =
58+
min(int elifEndLine, PreprocessorDirective end |
59+
pb.getNext() = end and isPreprocFileAndLine(end, filepath, elifEndLine)
60+
|
61+
elifEndLine
6762
)
6863
}
6964

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

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

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ int g;
2424
#include <string> // COMPLIANT
2525
#endif // COMPLIANT
2626

27-
#ifdef MACRO_ENABLED_NON // COMPLIANT
28-
#include <string> // COMPLIANT
29-
#elif MACRO_ENABLED_OTHER // COMPLIANT
30-
#include <string> // COMPLIANT
31-
#endif // COMPLIANT
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
3234

3335
#ifdef OBJECTLIKE_MACRO_NO // COMPLIANT
3436
int x0 = 0; // not present

0 commit comments

Comments
 (0)