缩短GCC错误消息

时间:2018-04-04 14:09:26

标签: c++ gcc

每当gcc找不到具有多个重载的函数的匹配重载时,它就会给出错误行和行,解释尝试了哪个重载以及为什么不使用它。

虽然它通常很有用,但它通常也不是,因为问题就像呼叫站点上的简单错误一样。在这种特殊情况下,它甚至没有帮助,因为甚至需要花费相当长的时间来找出哪一行最终导致了这个问题。

是否有任何命令行切换到GCC以缩短输出并仅包含实际的触发线?例如:

#include <string>
#include <iostream>

struct Z{};

void foo() {

    std::string s;
    Z z;

    std::cout << z; // typo - meant s 
}

请参阅错误输出:https://godbolt.org/g/wz5vL2

小额添加:第三方解决方案(STLFilt,gccfilter等)不符合要求,因为a)我的工作环境不欢迎安装第三方应用程序和b)它们往往成为unmaintained并停止使用下一个编译器升级

1 个答案:

答案 0 :(得分:5)

一种方法是使用-Wfatal-errors。它会从

更改错误消息
<source>: In function 'void foo()':

<source>:11:15: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Z')

     std::cout << z; // typo - meant s

     ~~~~~~~~~~^~~~

In file included from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/iostream:39:0,

                 from <source>:2:

/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]

       operator<<(__ostream_type& (*__pf)(__ostream_type&))

many more lines of errors

    <source>: In function 'void foo()':

<source>:11:15: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Z')

     std::cout << z; // typo - meant s

     ~~~~~~~~~~^~~~

compilation terminated due to -Wfatal-errors.

Compiler returned: 1

唯一的缺点是你只会得到第一个错误。如果您的编译时间很长,那么这不是最好的,因为在修复第一个错误并重新编译之前,您将无法修复任何其他错误。