如何将gcc警告转换为结构化格式?

时间:2016-04-15 22:31:59

标签: c++ gcc g++ compiler-warnings compiler-flags

和许多人一样,我用大量的警示标志来构建我的项目 由于并非所有警告标志都是有害的,因此编译会变得很嘈杂。

诸如“未使用的变量”,“初始化列表中的阴影成员”,“缺少交换机默认值”等警告对于记录都很重要,但它们在构建期间会产生太多混乱,并且很难发现重要警告。

鉴于一个大型项目,可能会有数千个警告与构建语句混合在一起,然后解析虽然后来变得很麻烦。同样不希望在代码中维护编译器编译指示和推/弹警告。

如何以结构化格式转储编译器警告?
无论是 XML,JSON,YAML,CSV,有没有办法告诉编译器转储所有发出的警告?这样的格式可以让我更有效地查看警告,并按类型,文件,金额等对它们进行排序。

2 个答案:

答案 0 :(得分:2)

在此期间可能对你有所帮助的是turn those compiler warnings you deem critical into errors using -Werror=,所以你会注意到它们会超出警告的噪音。

答案 1 :(得分:2)

添加了GCC 9 [1]支持以JSON格式输出警告和错误消息,只需使用-fdiagnostics-format=json选项即可。

比较输出

$ 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;
      |   ^~~~

带有JSON格式的一个:

[
    {
        "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/