禁用g ++“note candidate are ..”编译器消息

时间:2012-07-03 14:54:40

标签: c++ g++

很多时候,当我使用拼写错误或其他类型的输入不匹配进行编译时,我得到了标准的“错误:'函数名'不匹配...”错误。这很棒。然后,特别是在重载函数和运算符的情况下,g ++继续并列出10页的候选项,这些页面只是可怕的和大量的模板定义。

错误信息很好,但有没有办法禁止它建议其他功能变体?

3 个答案:

答案 0 :(得分:14)

据我所知,GCC中没有编译标志可以在函数调用模糊时禁用建议的候选者。

你唯一的希望就是修补GCC源代码。

深入研究(版本: 4.7.1 ),我发现了gcc/cp/pt.c中似乎相关的功能:

void
print_candidates(tree fns)
{
  const char *str = NULL;
  print_candidates_1 (fns, false, &str);
  gcc_assert (str == NULL);
}

作为一个有根据的猜测,我认为你只需要注释掉函数体。

答案 1 :(得分:11)

我的答案并不像补丁那么酷。如果你想要一个不那么冗长的错误信息,这个脚本将删除丑陋的代码,只留下候选人的行号。

g++ test.cc 2>&1 | sed 's/^\([^ ]*:[0-9]*: note\):.*/\1/'

因此,它可以在这样的脚本中使用:

#!/bin/bash
GXX=/usr/bin/g++
ARGS=()
i=0
show_notes=yes
for arg in "$@" ; do
    if [ "$arg" = "-fterse-notes" ] ; then
        show_notes=no
    elif [ "$arg" = "-fno-terse-notes" ] ; then
        show_notes=yes
    else
        ARGS[$i]="$arg"
    fi
    i=$[i+1]
done
if [ $show_notes = yes ] ; then
    exec ${GXX} "${ARGS[@]}"
else
    exec ${GXX} "${ARGS[@]}" 2>&1 | sed 's/^\([^ ]*:[0-9]*: note\):.*/\1/'
fi

如果此脚本的名称为g++并且在您的路径中,则它应该像添加了名为-fterse-notes的命令行选项一样工作。

答案 2 :(得分:9)

-Wfatal-errors能做你想做的吗?

它在第一个之后停止所有错误,这与简单地抑制候选功能注释不同,但它会显着降低输出:

$ cat a.cc
void f() { }
void f(int) { }
void f(char) { }

int main()
{
  f((void*)0);
}
$ g++ a.cc
a.cc: In function ‘int main()’:
a.cc:7: error: call of overloaded ‘f(void*)’ is ambiguous
a.cc:2: note: candidates are: void f(int) <near match>
a.cc:3: note:                 void f(char) <near match>
$ g++ a.cc -Wfatal-errors
a.cc: In function ‘int main()’:
a.cc:7: error: call of overloaded ‘f(void*)’ is ambiguous
compilation terminated due to -Wfatal-errors.

或者,如果您想要修补GCC,则会添加-fno-candidate-functions开关:

--- gcc/c-family/c.opt.orig 2012-07-11 16:37:29.373417154 +0000
+++ gcc/c-family/c.opt      2012-07-11 17:09:47.340418384 +0000
@@ -752,6 +752,10 @@
 fbuiltin-
 C ObjC C++ ObjC++ Joined

+fcandidate-functions
+C++ ObjC++ Var(flag_candidates) Init(1)
+-fno-candidate-functions Do not print candidate functions when overload resolution fails
+
 fcheck-new
 C++ ObjC++ Var(flag_check_new)
 Check the return value of new
--- gcc/cp/call.c.orig      2012-07-11 17:08:34.186424089 +0000
+++ gcc/cp/call.c   2012-07-11 17:09:51.843444951 +0000
@@ -3317,6 +3317,9 @@
   for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
     n_candidates++;

+  if (!flag_candidates)
+    return;
+
   inform_n (loc, n_candidates, "candidate is:", "candidates are:");
   for (; candidates; candidates = candidates->next)
     print_z_candidate (loc, NULL, candidates);
--- gcc/cp/pt.c.orig        2012-07-11 16:37:35.658636650 +0000
+++ gcc/cp/pt.c     2012-07-11 17:10:20.910435942 +0000
@@ -1751,9 +1751,12 @@
 void
 print_candidates (tree fns)
 {
-  const char *str = NULL;
-  print_candidates_1 (fns, false, &str);
-  gcc_assert (str == NULL);
+  if (flag_candidates)
+    {
+      const char *str = NULL;
+      print_candidates_1 (fns, false, &str);
+      gcc_assert (str == NULL);
+    }
 }

 /* Returns the template (one of the functions given by TEMPLATE_ID)
相关问题