33

很多时候,当我编译有错字或其他类型不匹配的东西时,我会收到标准的“错误:'functionname' in ...”错误。这很棒。然后,特别是在函数和运算符重载的情况下,g++ 继续列出 10 页候选者,这些候选者只是可怕的海量模板定义。

错误消息很棒,但是有什么方法可以禁止它提示其他功能变体?

4

3 回答 3

16

据我所知,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);
}

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

于 2012-07-05T18:19:35.767 回答
16

-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)
于 2012-07-11T16:27:08.580 回答
14

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

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"
        i=$[i+1]
    fi
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.

于 2012-07-07T22:58:13.217 回答