Skip to content

Commit 2193a22

Browse files
committed
A16-0-1: improve range logic
1 parent bfcacf7 commit 2193a22

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,18 @@ pragma[noinline]
5353
predicate isPreprocConditionalRange(
5454
PreprocessorBranch pb, string filepath, int startLine, int endLine
5555
) {
56-
exists(PreprocessorEndif end | pb.getEndIf() = end |
57-
isPreprocFileAndLine(pb, filepath, startLine) and
58-
isPreprocFileAndLine(end, filepath, endLine)
59-
)
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)
67+
)
6068
}
6169

6270
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
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. |
65
| test.cpp:35:1:35:26 | #elif OBJECTLIKE_MACRO > 0 | Preprocessor directive used for conditional compilation. |
76
| 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ int g;
3030
#include <string> // COMPLIANT
3131
#endif // COMPLIANT
3232

33-
#ifdef OBJECTLIKE_MACRO_NO // NON_COMPLIANT
34-
int x = 0; // not present
33+
#ifdef OBJECTLIKE_MACRO_NO // COMPLIANT
34+
int x0 = 0; // not present
3535
#elif OBJECTLIKE_MACRO > 0 // NON_COMPLIANT
36-
int x = 1; // present
36+
int x0 = 1; // present
3737
#endif // COMPLIANT
3838

3939
#ifdef OBJECTLIKE_MACRO // NON_COMPLIANT

0 commit comments

Comments
 (0)