Skip to content

Commit b806d05

Browse files
Fix #689, false negatives for A1-1-2 thinking -Wno-foo is compliant.
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.
1 parent 015872e commit b806d05

14 files changed

+66
-2
lines changed
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A1-1-2` - `CompilerWarningLevelNotInCompliance.ql`:
2+
- Fixes #689 false negatives where '-Wno-foo' was treated as enabling, rather than disabling warnings.

Diff for: cpp/autosar/src/rules/A1-1-2/CompilerWarningLevelNotInCompliance.ql

+51-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,62 @@ import codingstandards.cpp.autosar
2020

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

23-
predicate hasWarningOption(Compilation c) { c.getAnArgument().regexpMatch("-W[\\w=-]+") }
23+
class CompilationArgument extends string {
24+
Compilation compilation;
25+
26+
CompilationArgument() {
27+
this = compilation.getAnArgument()
28+
}
29+
}
30+
31+
/**
32+
* Compiler flags of type -Wfoo or -Wfoo=bar, which enables the `foo` warning.
33+
*/
34+
class EnableWarningFlag extends CompilationArgument {
35+
string warningType;
36+
37+
EnableWarningFlag() {
38+
warningType = regexpCapture("^-W([\\w-]+)(=.*)?$", 1)
39+
and not this instanceof DisableWarningFlag
40+
}
41+
42+
string getWarningType() {
43+
result = warningType
44+
}
45+
}
46+
47+
/**
48+
* Compiler flags of type -Wno-foo or -Wfoo=0, which disables the `foo` warning
49+
* and overrules -Wfoo.
50+
*/
51+
class DisableWarningFlag extends CompilationArgument {
52+
string warningType;
53+
54+
DisableWarningFlag() {
55+
warningType = regexpCapture("^-Wno-([\\w-]+)", 1) or
56+
warningType = regexpCapture("^-W([\\w-]+)=0", 1)
57+
}
58+
59+
string getWarningType() {
60+
result = warningType
61+
}
62+
}
63+
64+
predicate hasEnabledWarning(Compilation c) {
65+
exists(EnableWarningFlag enableFlag |
66+
c.getAnArgument() = enableFlag and
67+
not exists(DisableWarningFlag disableFlag |
68+
c.getAnArgument() = disableFlag and
69+
enableFlag.getWarningType() = disableFlag.getWarningType()
70+
)
71+
)
72+
}
2473

2574
from File f
2675
where
2776
not isExcluded(f, ToolchainPackage::compilerWarningLevelNotInComplianceQuery()) and
2877
exists(Compilation c | f = c.getAFileCompiled() |
2978
not hasResponseFileArgument(c) and
30-
not hasWarningOption(c)
79+
not hasEnabledWarning(c)
3180
)
3281
select f, "No warning-level options were used in the compilation of '" + f.getBaseName() + "'."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/A1-1-2/CompilerWarningLevelNotInCompliance.ql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// semmle-extractor-options: --clang -std=c++14 -Wformat=0 -Wno-format-security
2+
// NON_COMPLIANT

Diff for: cpp/autosar/test/rules/A1-1-2.4/options.clang

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wformat=0 -Wno-format-security

Diff for: cpp/autosar/test/rules/A1-1-2.4/options.gcc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wformat=0 -Wno-format-security

Diff for: cpp/autosar/test/rules/A1-1-2.4/options.qcc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wno-format -Wno-format-security

Diff for: cpp/autosar/test/rules/A1-1-2.5/CompilerWarningLevelNotInCompliance.expected

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/A1-1-2/CompilerWarningLevelNotInCompliance.ql

Diff for: cpp/autosar/test/rules/A1-1-2.5/Wall-Wno-format.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// semmle-extractor-options: --clang -std=c++14 -Wall -Wno-format
2+
// COMPLIANT

Diff for: cpp/autosar/test/rules/A1-1-2.5/options.clang

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wall -Wno-format

Diff for: cpp/autosar/test/rules/A1-1-2.5/options.gcc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wall -Wno-format

Diff for: cpp/autosar/test/rules/A1-1-2.5/options.qcc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wall -Wno-format

0 commit comments

Comments
 (0)