Skip to content

Commit ce6709d

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 c4dafe7 commit ce6709d

18 files changed

+79
-3
lines changed
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.

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

+49-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,61 @@
1818
import cpp
1919
import codingstandards.cpp.autosar
2020

21+
predicate hasResponseFileArgument(Compilation c) { c.getAnArgument().matches("@%") }
22+
2123
class CompilationWithNoWarnings extends Compilation {
2224
CompilationWithNoWarnings() {
2325
getAnArgument() = "-w" or
24-
not getAnArgument().regexpMatch("-W[\\w=-]+")
26+
not exists(EnableWarningFlag enableFlag |
27+
this.getAnArgument() = enableFlag and
28+
not exists(DisableWarningFlag disableFlag |
29+
this.getAnArgument() = disableFlag and
30+
enableFlag.getWarningType() = disableFlag.getWarningType()
31+
)
32+
)
2533
}
2634
}
2735

28-
predicate hasResponseFileArgument(Compilation c) { c.getAnArgument().matches("@%") }
36+
class CompilationArgument extends string {
37+
Compilation compilation;
38+
39+
CompilationArgument() {
40+
this = compilation.getAnArgument()
41+
}
42+
}
43+
44+
/**
45+
* Compiler flags of type -Wfoo or -Wfoo=bar, which enables the `foo` warning.
46+
*/
47+
class EnableWarningFlag extends CompilationArgument {
48+
string warningType;
49+
50+
EnableWarningFlag() {
51+
warningType = regexpCapture("^-W([\\w-]+)(=.*)?$", 1)
52+
and not this instanceof DisableWarningFlag
53+
}
54+
55+
string getWarningType() {
56+
result = warningType
57+
}
58+
}
59+
60+
/**
61+
* Compiler flags of type -Wno-foo or -Wfoo=0, which disables the `foo` warning
62+
* and overrules -Wfoo.
63+
*/
64+
class DisableWarningFlag extends CompilationArgument {
65+
string warningType;
66+
67+
DisableWarningFlag() {
68+
warningType = regexpCapture("^-Wno-([\\w-]+)", 1) or
69+
warningType = regexpCapture("^-W([\\w-]+)=0", 1)
70+
}
71+
72+
string getWarningType() {
73+
result = warningType
74+
}
75+
}
2976

3077
from File f
3178
where
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wformat=0 -Wno-format-security
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wformat=0 -Wno-format-security
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wno-format -Wno-format-security
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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'. |

cpp/autosar/test/rules/A1-1-2.5/CompilerWarningLevelNotInCompliance.expected.clang

Whitespace-only changes.

cpp/autosar/test/rules/A1-1-2.5/CompilerWarningLevelNotInCompliance.expected.gcc

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 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,14 @@
1+
// semmle-extractor-options: --clang -std=c++14 -Wall -Wno-format
2+
// COMPLIANT
3+
4+
// NOTE: When tested with `codeql test run`, the test extractor provides `-w`
5+
// which overrides `-Wcast-function-type` and causes this test case to be
6+
// non-compliant.
7+
//
8+
// However, when tested with our compiler matrix tests, this test db is built
9+
// via `codeql database create --command="..."`, and the `-w` flag will NOT be
10+
// used. This means the `-Wcast-function-type` flag is active and the test case
11+
// is compliant.
12+
//
13+
// Therefore, the .expected file for this test expects non-compliance, and the
14+
// .expected.gcc and .expected.clang files expect this test to be compliant.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wall -Wno-format
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wall -Wno-format
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wall -Wno-format
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| Wall.cpp:0:0:0:0 | Wall.cpp | No warning-level options were used in the compilation of 'Wall.cpp'. |
1+
| Wall.cpp:0:0:0:0 | Wall.cpp | No warning-level options were used in the compilation of 'Wall.cpp'. |

0 commit comments

Comments
 (0)