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 all commits
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` - `PreProcessorShallOnlyBeUsedForCertainDirectivesPatterns.ql`:
- 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 All @@ -40,9 +53,9 @@ pragma[noinline]
predicate isPreprocConditionalRange(
PreprocessorBranch pb, string filepath, int startLine, int endLine
) {
exists(PreprocessorEndif end | pb.getEndIf() = end |
isPreprocFileAndLine(pb, filepath, startLine) and
isPreprocFileAndLine(end, filepath, endLine)
isPreprocFileAndLine(pb, filepath, startLine) and
exists(PreprocessorDirective end |
pb.getNext() = end and isPreprocFileAndLine(end, filepath, endLine)
)
}

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,7 @@
| 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:31:1:31:26 | #elif OBJECTLIKE_MACRO > 0 | Preprocessor directive used for conditional compilation. |
| test.cpp:37:1:37:26 | #elif OBJECTLIKE_MACRO > 0 | Preprocessor directive used for conditional compilation. |
| test.cpp:41:1:41:23 | #ifdef OBJECTLIKE_MACRO | Preprocessor directive used for conditional compilation. |
| test.cpp:58:1:58:27 | #elif MACRO_ENABLED_OTHER_1 | Preprocessor directive used for conditional compilation. |
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A16-0-1/options
Original file line number Diff line number Diff line change
@@ -0,0 +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
56 changes: 56 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,59 @@ 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
#elif OBJECTLIKE_MACRO > 0 // NON_COMPLIANT
int x00 = 1; // present
#endif // COMPLIANT

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

#ifdef OBJECTLIKE_MACRO // NON_COMPLIANT
int x1 = 0; // present
#elif OBJECTLIKE_MACRO > -1 // NON_COMPLIANT[FALSE_NEGATIVE] - known due to
// database not containing elements
int x1 = 1; // not present
#endif // COMPLIANT

// case 1 - first present only
#ifdef MACRO_ENABLED_NON_1 // COMPLIANT
#include <string> //present
#elif MACRO_ENABLED_OTHER // NON_COMPLIANT[FALSE_NEGATIVE]
int x = 1; // not present
#endif

// case 2 - second present only
#ifdef MACRO_ENABLED_NON // COMPLIANT
#include <string> //not present
#elif MACRO_ENABLED_OTHER_1 // NON_COMPLIANT
int x = 1; // present
#endif

// case 3 - neither present
#ifdef MACRO_ENABLED_NON // COMPLIANT
#include <string> //not present
#elif MACRO_ENABLED_OTHER // NON_COMPLIANT[FALSE_NEGATIVE]
int x = 1; // not present
#endif

// case 4 - both look present but the second still not bc the condition is not
// required to be evaluated
#ifdef MACRO_ENABLED_NON_1 // COMPLIANT
#include <string> //present
#elif MACRO_ENABLED_OTHER_1 // NON_COMPLIANT[FALSE_NEGATIVE]
int x = 1; // not present
#endif