33

タイプミスやその他の入力ミスのあるものをコンパイルすると、標準の「エラー:'functionname'in...」エラーが発生することがよくあります。これは素晴らしい。次に、特にオーバーロードされた関数と演算子の場合、g ++は続行され、恐ろしくて大規模なテンプレート定義である候補の10ページのようにリストされます。

エラーメッセージは素晴らしいですが、他の関数のバリエーションを提案しないようにする方法はありますか?

4

3 に答える 3

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 に答える
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 に答える
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 に答える