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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -20,13 +20,56 @@ 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
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
Loading