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

Fix #689, false negatives for A1-1-2 thinking -Wno-foo is compliant. #697

Merged
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-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,56 @@
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=-]+")
getAnArgument() = "-w"
or
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'. |
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'. |
Loading