Like many, I build my project with the an abundance of warning flags.
Since not all warning flags are detrimental, the compilation becomes noisy.
Warnings such as "unused variables", "shadowing members in initialization lists", "missing switch defaults", are all important to log, but they create too much clutter during builds, and it is hard to spot the important warnings.
Given a large project, there can be thousands of warnings mixed in with build statements, and parsing though it afterwards becomes burdensome. It's equally undesirable to maintain compiler pragmas and push/pop warnings inside code.
How can I dump compiler warnings in a structured format?
Whether it be
XML, JSON, YAML, CSV, is there a way to tell the compiler to dump all emitted warnings? A format like this would allow me to view warnings more efficiently, and sort them by type, file, amount, etc.
You can use a #pragma warning directive to control the level of warning that's reported at compile time in specific source files. Warning pragma directives in source code are unaffected by the /w option.
The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)
If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option. Inhibit all warning messages. Make all warnings into errors. Make the specified warning into an error.
gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.
GCC 9 added[1] support for outputting warnings and error messages in JSON format, just use -fdiagnostics-format=json
option.
Compare the output of
$ gcc-9 -c cve-2014-1266.c -Wall
cve-2014-1266.c: In function ‘SSLVerifySignedServerKeyExchange’:
cve-2014-1266.c:629:2: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
629 | if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
| ^~
cve-2014-1266.c:631:3: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
631 | goto fail;
| ^~~~
with JSON-formatted one:
[
{
"children": [
{
"kind": "note",
"locations": [
{
"caret": {
"column": 3,
"file": "cve-2014-1266.c",
"line": 631
},
"finish": {
"column": 6,
"file": "cve-2014-1266.c",
"line": 631
}
}
],
"message": "...this statement, but the latter is misleadingly indented as if it were guarded by the \u2018if\u2019"
}
],
"kind": "warning",
"locations": [
{
"caret": {
"column": 2,
"file": "cve-2014-1266.c",
"line": 629
},
"finish": {
"column": 3,
"file": "cve-2014-1266.c",
"line": 629
}
}
],
"message": "this \u2018if\u2019 clause does not guard...",
"option": "-Wmisleading-indentation"
}
]
[1] https://developers.redhat.com/blog/2019/03/08/usability-improvements-in-gcc-9/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With