Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I dump gcc warnings into a structured format?

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.

like image 443
Trevor Hickey Avatar asked Apr 15 '16 22:04

Trevor Hickey


People also ask

Which option can be used to display compiler warnings?

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.

How does GCC treat warning errors?

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.)

Which option of GCC inhibit all warning messages?

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.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.


1 Answers

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/

like image 198
Anton Kochkov Avatar answered Sep 20 '22 02:09

Anton Kochkov