Skip to content
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

A16-0-1: Fix handling of #else.. directives #387

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions change_notes/2023-10-04-a16-0-1-handle-else.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `A16-0-1` - address false positives by permitting #else and #elif within an otherwise valid #if preprocessor conditional.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ class PermittedDirectiveType extends PreprocessorDirective {
//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 PreprocessorElse or
this instanceof Include or
this instanceof PermittedMacro
}
}

pragma[noinline]
predicate isPreprocFileAndLine(Element pd, string filepath, int startLine) {
predicate isPreprocFileAndLine(Locatable pd, string filepath, int startLine) {
pd.getLocation().hasLocationInfo(filepath, startLine, _, _, _)
}

Expand All @@ -49,7 +50,7 @@ predicate isPreprocConditionalRange(
/**
* An optimised version of `PreprocessorBranchDirective.getAGuard()`.
*/
private PreprocessorBranch getAGuard(Element guardedElement) {
private PreprocessorBranch getAGuard(Locatable guardedElement) {
exists(string filepath, int ifStartLine, int guardedElementStartLine, int endifStartLine |
isPreprocConditionalRange(result, filepath, ifStartLine, endifStartLine) and
isPreprocFileAndLine(guardedElement, filepath, guardedElementStartLine) and
Expand All @@ -72,8 +73,11 @@ class MacroWrapper extends PreprocessorIfndef {

class AcceptableWrapper extends PreprocessorBranch {
AcceptableWrapper() {
forall(Element inner | not inner instanceof Comment and this = getAGuard(inner) |
forall(Locatable inner | not inner instanceof Comment and this = getAGuard(inner) |
inner instanceof PermittedDirectiveType
or
// Ignore elifs, as they will be considered separately
inner instanceof PreprocessorElif
)
}
}
Expand Down
6 changes: 6 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,9 @@ int g;
#ifndef TESTHEADER // COMPLIANT
#include <string> //COMPLIANT
#endif // COMPLIANT

#ifdef MACRO_ENABLED // COMPLIANT
#include "test1.h" //COMPLIANT
#else
#include "test2.h" //COMPLIANT
#endif
4 changes: 4 additions & 0 deletions cpp/autosar/test/rules/A16-0-1/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef _TEST_HEADER_INCLUDE // COMPLIANT - header guard
#define _TEST_HEADER_INCLUDE // COMPLIANT
int x;
#endif // COMPLIANT
Empty file.
Loading