Skip to content

Commit

Permalink
Fix #689, false negatives for A1-1-2 thinking -Wno-foo is compliant.
Browse files Browse the repository at this point in the history
The presence of -Wno-foo should not mark the compilation compliant with
A1-1-2, nor should the presence of -Wfoo=0.

Easily check for all -Wfoo=bar flags, that foo is not no-baz, and bar is
not 0. Also check there is no -Wno-foo flag overruling it. Otherwise the
query functionality remains the same.

Add test cases for non-compliant scenarios -Wfoo=0 and -Wno-foo, and for
the compliant scenario -Wall -Wno-foo.

This will have some compatibility issues with PR #688, after one is
merged the other will need some small updates before this can be merged.
  • Loading branch information
MichaelRFairhurst committed Sep 18, 2024
1 parent 015872e commit b806d05
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 2 deletions.
2 changes: 2 additions & 0 deletions change_notes/2024-09-18-handle-warning-suppresion-flags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `A1-1-2` - `CompilerWarningLevelNotInCompliance.ql`:
- Fixes #689 false negatives where '-Wno-foo' was treated as enabling, rather than disabling warnings.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,62 @@ import codingstandards.cpp.autosar

predicate hasResponseFileArgument(Compilation c) { c.getAnArgument().matches("@%") }

predicate hasWarningOption(Compilation c) { c.getAnArgument().regexpMatch("-W[\\w=-]+") }
class CompilationArgument extends string {
Compilation compilation;

CompilationArgument() {
this = compilation.getAnArgument()
}
}

/**
* Compiler flags of type -Wfoo or -Wfoo=bar, which enables the `foo` warning.
*/
class EnableWarningFlag extends CompilationArgument {
string warningType;

EnableWarningFlag() {
warningType = regexpCapture("^-W([\\w-]+)(=.*)?$", 1)
and not this instanceof DisableWarningFlag
}

string getWarningType() {
result = warningType
}
}

/**
* Compiler flags of type -Wno-foo or -Wfoo=0, which disables the `foo` warning
* and overrules -Wfoo.
*/
class DisableWarningFlag extends CompilationArgument {
string warningType;

DisableWarningFlag() {
warningType = regexpCapture("^-Wno-([\\w-]+)", 1) or
warningType = regexpCapture("^-W([\\w-]+)=0", 1)
}

string getWarningType() {
result = warningType
}
}

predicate hasEnabledWarning(Compilation c) {
exists(EnableWarningFlag enableFlag |
c.getAnArgument() = enableFlag and
not exists(DisableWarningFlag disableFlag |
c.getAnArgument() = disableFlag and
enableFlag.getWarningType() = disableFlag.getWarningType()
)
)
}

from File f
where
not isExcluded(f, ToolchainPackage::compilerWarningLevelNotInComplianceQuery()) and
exists(Compilation c | f = c.getAFileCompiled() |
not hasResponseFileArgument(c) and
not hasWarningOption(c)
not hasEnabledWarning(c)
)
select f, "No warning-level options were used in the compilation of '" + f.getBaseName() + "'."
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Wformat=0-Wno-format-security.cpp:0:0:0:0 | Wformat=0-Wno-format-security.cpp | No warning-level options were used in the compilation of 'Wformat=0-Wno-format-security.cpp'. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/A1-1-2/CompilerWarningLevelNotInCompliance.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// semmle-extractor-options: --clang -std=c++14 -Wformat=0 -Wno-format-security
// NON_COMPLIANT
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A1-1-2.4/options.clang
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Wformat=0 -Wno-format-security
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A1-1-2.4/options.gcc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Wformat=0 -Wno-format-security
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A1-1-2.4/options.qcc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Wno-format -Wno-format-security
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/A1-1-2/CompilerWarningLevelNotInCompliance.ql
2 changes: 2 additions & 0 deletions cpp/autosar/test/rules/A1-1-2.5/Wall-Wno-format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// semmle-extractor-options: --clang -std=c++14 -Wall -Wno-format
// COMPLIANT
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A1-1-2.5/options.clang
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Wall -Wno-format
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A1-1-2.5/options.gcc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Wall -Wno-format
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A1-1-2.5/options.qcc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Wall -Wno-format

0 comments on commit b806d05

Please sign in to comment.