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 Oct 2, 2024
1 parent c4dafe7 commit ce6709d
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 3 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 @@ -18,14 +18,61 @@
import cpp
import codingstandards.cpp.autosar

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

class CompilationWithNoWarnings extends Compilation {
CompilationWithNoWarnings() {
getAnArgument() = "-w" or
not getAnArgument().regexpMatch("-W[\\w=-]+")
not exists(EnableWarningFlag enableFlag |
this.getAnArgument() = enableFlag and
not exists(DisableWarningFlag disableFlag |
this.getAnArgument() = disableFlag and
enableFlag.getWarningType() = disableFlag.getWarningType()
)
)
}
}

predicate hasResponseFileArgument(Compilation c) { c.getAnArgument().matches("@%") }
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
}
}

from File f
where
Expand Down
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Wall-Wno-format.cpp:0:0:0:0 | Wall-Wno-format.cpp | No warning-level options were used in the compilation of 'Wall-Wno-format.cpp'. |
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Wall-Wno-format.cpp:0:0:0:0 | Wall-Wno-format.cpp | No warning-level options were used in the compilation of 'Wall-Wno-format.cpp'. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/A1-1-2/CompilerWarningLevelNotInCompliance.ql
14 changes: 14 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,14 @@
// semmle-extractor-options: --clang -std=c++14 -Wall -Wno-format
// COMPLIANT

// NOTE: When tested with `codeql test run`, the test extractor provides `-w`
// which overrides `-Wcast-function-type` and causes this test case to be
// non-compliant.
//
// However, when tested with our compiler matrix tests, this test db is built
// via `codeql database create --command="..."`, and the `-w` flag will NOT be
// used. This means the `-Wcast-function-type` flag is active and the test case
// is compliant.
//
// Therefore, the .expected file for this test expects non-compliance, and the
// .expected.gcc and .expected.clang files expect this test to be 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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| Wall.cpp:0:0:0:0 | Wall.cpp | No warning-level options were used in the compilation of 'Wall.cpp'. |
| Wall.cpp:0:0:0:0 | Wall.cpp | No warning-level options were used in the compilation of 'Wall.cpp'. |

0 comments on commit ce6709d

Please sign in to comment.