2012-07-03 23 views
20

很多時候,當我編寫一些有打字錯誤或某些其他類型不匹配的東西時,我會得到標準的「錯誤:無法匹配'functionname'in ...」錯誤。這很棒。然後,特別是在函數和運算符超載的情況下,g ++繼續並列出10個頁面,這些頁面只是可怕的和大量的模板定義。禁用g ++「備註候選者是......」編譯器消息

錯誤信息很好,但有什麼辦法可以禁止它建議其他功能變種?

+19

等待,你想錯誤信息有*少*信息? –

+7

它建議的函數定義隱藏在10層模板之後(尤其是在boost)的情況下,並且很難找到編譯器實際識別錯誤行的位置。我希望它能告訴我錯誤在哪裏以及錯在哪裏,但我並不需要它來建議如何解決它。 – sshannin

+4

通過grep管道它,只有在他們有'錯誤:'的行上匹配? FWIW我的開發環境作爲一項功能「跳到了錯誤的界限」,作爲額外的獎勵,候選人名單的頂部顯示在屏幕上。 – Flexo

回答

13

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

作爲一個受過教育的猜測,我認爲你只需要註釋掉函數體。

+0

+1對於雙LOL,我首先想到它只是一個口頭問題的答案,然後我意識到使用gcc它是* common *從源碼構建編譯器,並且曾經我知道!現在令我困惑的唯一事情就是現代C/C++函數頭了,我記得(但我可能是錯誤的)它曾經是舊的K&R C語法無處不在? –

+0

@ Cheersandhth.-Alf這是我第一次查看GCC代碼庫,它可能是_too clean_對於這個大小的項目。至於K&R C語法,我10年太年輕甚至知道它是什麼:) – Gigi

+0

@ Cheersandhth.-Alf yes!代碼進化了很多,從2010年5月開始,甚至允許使用一些C++特性。並且我同意代碼基地實際上非常清晰! – log0

10

我的答案並不像補丁那麼酷。如果你想得到一個不太詳細的錯誤信息,這個腳本將刪除醜陋的代碼,並留下候選人的行號。

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 "[email protected]" ; 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

+0

當然你只想增加$ i第一個'其他'? – cdyson37

+1

@ cdyson37:我實際上正在形成一個新的參數列表,刪除'-fterse-notes'和'-fno-terse-notes'的出現。 'bash'似乎可以處理我寫的好東西,但是也許技術上更正確的實現只是在'ARGS [$ i] =「$ arg」'語句之後的'else'子句中增加它。 – jxh

10

請問-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)