Skip to content

A16-0-1: exclusions for handling else and elif #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2024-01-31-exclusion-a16-0-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`A16-0-1`: `cpp/autosar/pre-processor-shall-only-be-used-for-certain-directives-patterns`
- 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.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,26 @@ import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.FunctionLikeMacro

class PermittedInnerDirectiveType extends PreprocessorDirective {
PermittedInnerDirectiveType() {
//permissive listing for directives that can be used in a valid wrapper
this instanceof MacroWrapper or
this instanceof PreprocessorEndif or
this instanceof Include or
this instanceof PermittedMacro or
this instanceof PreprocessorElif or
this instanceof PreprocessorElse
}
}

class PermittedDirectiveType extends PreprocessorDirective {
PermittedDirectiveType() {
//permissive listing in case directive types modelled in ql ever expands (example non valid directives)
this instanceof MacroWrapper or
this instanceof PreprocessorEndif or
this instanceof Include or
this instanceof PermittedMacro
this instanceof PermittedMacro or
this instanceof PreprocessorElse
}
}

Expand Down Expand Up @@ -73,7 +86,7 @@ class MacroWrapper extends PreprocessorIfndef {
class AcceptableWrapper extends PreprocessorBranch {
AcceptableWrapper() {
forall(Element inner | not inner instanceof Comment and this = getAGuard(inner) |
inner instanceof PermittedDirectiveType
inner instanceof PermittedInnerDirectiveType
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
| test.cpp:9:1:9:26 | #define OBJECTLIKE_MACRO 1 | Preprocessor directive used for conditional compilation. |
| test.cpp:10:1:10:35 | #define FUNCTIONLIKE_MACRO(X) X + 1 | Preprocessor directive used for conditional compilation. |
| test.cpp:11:1:11:37 | #define FUNCTIONLIKE_MACROTWO() 1 + 1 | Preprocessor directive used for conditional compilation. |
| test.cpp:33:1:33:26 | #ifdef OBJECTLIKE_MACRO_NO | Preprocessor directive used for conditional compilation. |
| test.cpp:35:1:35:26 | #elif OBJECTLIKE_MACRO > 0 | Preprocessor directive used for conditional compilation. |
| test.cpp:39:1:39:23 | #ifdef OBJECTLIKE_MACRO | Preprocessor directive used for conditional compilation. |
25 changes: 25 additions & 0 deletions cpp/autosar/test/rules/A16-0-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,28 @@ int g;
#ifndef TESTHEADER // COMPLIANT
#include <string> //COMPLIANT
#endif // COMPLIANT

#ifdef MACRO_ENABLED // COMPLIANT
#include <string> // COMPLIANT
#else // COMPLIANT
#include <string> // COMPLIANT
#endif // COMPLIANT

#ifdef MACRO_ENABLED_NON // COMPLIANT
#include <string> // COMPLIANT
#elif MACRO_ENABLED_OTHER // COMPLIANT
#include <string> // COMPLIANT
#endif // COMPLIANT

#ifdef OBJECTLIKE_MACRO_NO // NON_COMPLIANT
int x = 0; // not present
#elif OBJECTLIKE_MACRO > 0 // NON_COMPLIANT
int x = 1; // present
#endif // COMPLIANT

#ifdef OBJECTLIKE_MACRO // NON_COMPLIANT
int x1 = 0; // present
#elif OBJECTLIKE_MACRO > \
-1 // COMPLIANT - by technicality of conditional compilation
int x1 = 1; // not present
#endif // COMPLIANT