2012-11-19 52 views
22

實施棄用警告的一種方式是針對不推薦使用的函數調用產生警告,除非您是從已棄用的上下文中調用。通過這種方式,傳統代碼可以調用遺留代碼,而不會產生相當於噪聲的警告。如何擺脫GCC中不推薦使用的函數中的棄用警告?

這是一個合理的思路,它體現在OS X上的GCC 4.2(1)和Clang 4.0(2)以及Ubuntu上的Clang 3.0(3)的實現中。

  • (1):爲i686-蘋果darwin11-LLVM-G ++ - 4.2(GCC)4.2.1(基於蘋果公司建立5658)(LLVM建立2336.11.00)
  • (2):蘋果鏗鏘版4.0(tags/Apple/clang-421.0.57)(基於LLVM 3.1svn)
  • (3):Ubuntu clang 3.0-6ubuntu3(tags/RELEASE_30/final)(基於LLVM 3.0)

但是,當我在Ubuntu上使用GCC 4.6(4)進行編譯時,我對deprecat的所有調用都獲得了不推薦的警告獨立於上下文的編輯功能。這是功能的迴歸?有沒有我可以用來獲得其他行爲的編譯器選項?

  • (4):克++(Ubuntu的/ Linaro的4.6.3-1ubuntu5)4.6.3

實施例的程序:從GCC 4.2

int __attribute__((deprecated)) a() { 
    return 10; 
} 

int __attribute__((deprecated)) b() { 
    return a() * 2; //< I want to get rid of warnings from this line 
} 

int main() { 
    return b(); //< I expect a warning on this line only 
} 

輸出(是的,我確實得到了同樣的警告兩次,但我不在乎):

main.cpp: In function ‘int main()’: 
main.cpp:10: warning: ‘b’ is deprecated (declared at main.cpp:5) 
main.cpp:10: warning: ‘b’ is deprecated (declared at main.cpp:5) 
從GCC 4.

輸出:

main.cpp: In function 'int b()': 
main.cpp:6:9: warning: 'int a()' is deprecated (declared at main.cpp:1) [-Wdeprecated-declarations] 
main.cpp:6:11: warning: 'int a()' is deprecated (declared at main.cpp:1) [-Wdeprecated-declarations] 
main.cpp: In function 'int main()': 
main.cpp:10:9: warning: 'int b()' is deprecated (declared at main.cpp:5) [-Wdeprecated-declarations] 
main.cpp:10:11: warning: 'int b()' is deprecated (declared at main.cpp:5) [-Wdeprecated-declarations] 

我怎麼能說服GCC 4.6,它應該給我的輸出作爲GCC 4.2一樣嗎?

+0

這是完全有可能的,這從來沒有與FSF GCC的工作,使4.2的行爲,你所看到的是蘋果的補丁GCC。你有沒有在任何地方安裝FSF GCC 4.2? – hvd

+0

@ hvd你說得對。我想看看那個測試,但我手頭沒有4.2:/ –

回答

11

您在GCC 4.2中看到的行爲是由針對GCC的Apple特定修補程序引起的。 FSF GCC 4.2.4警告使用a。具體一點,蘋果GCC有FSF GCC不會爲:

--- a/gcc/toplev.c 
+++ b/gcc/toplev.c 
@@ -902,6 +902,9 @@ warn_deprecated_use (tree node) 
    if (node == 0 || !warn_deprecated_decl) 
    return; 

+ if (current_function_decl && TREE_DEPRECATED (current_function_decl)) 
+ return; 
+ 
    if (DECL_P (node)) 
    { 
     expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node)); 

(下GPLv2許可或更高版本可用)

您不妨把這個補丁適應GCC更高版本(也許沒有變化需要進行重大更改),並在應用此補丁的情況下從源代碼構建GCC。或者您可以將此報告爲FSF GCC bugzilla中的功能請求。

+0

整潔!這是一個很好的答案。我想我會把這個功能請求。你在哪裏找到差異? –

+0

下載FSF人士透露,蘋果的下載源,通過解包邊兩側,運行遞歸diff時,搜索結果中「過時」,並希望不會有太多的結果:)呵呵 – hvd

+0

,得到了它。我將不得不安排時間...... :)謝謝! –

26

-Wno-deprecated將刪除所有棄用警告

+10

這不是OP要求的。 – OmnipotentEntity

+1

@OmnipotentEntity,爲什麼不呢?您可以使用'-Wno-deprecated'編譯遺留代碼,而不使用新代碼。 – Lol4t0

+4

你的陳述無疑是真實的,但這是*所以不是*我所問的。我*要*希望GCC 4.2給出的棄用警告。 –

24

GCC 4.6添加診斷編譯指示,這將有助於解決這個問題:

#pragma GCC diagnostic push 
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" 
int __attribute__((deprecated)) b() { 
    return a() * 2; //< I want to get rid of warnings from this line 
} 
#pragma GCC diagnostic pop 

注:這僅適用於在GCC 4.6及更高版本。 pushpop是4.6擴展。使用gcc 4.5時,#pragma GCC diagnostic pushpop將被忽略(帶有警告)。不會被忽略的是#pragma GCC diagnostic ignored "-Wdeprecated-declarations" - 但現在這個功能一直持續到文件結束。

+1

+1 - 對於那些不想或不能修補GCC的人來說,這是一個非常好的解決方案。 – Riot

1

我遇到了同樣的問題。提出的解決方案如下

typedef OLD_A_NOT_TO_BE_USED a __attribute__((deprecated)); 

int OLD_A_NOT_TO_BE_USED() { 
    return 10; 
} 

int __attribute__((deprecated)) b() { 
    return OLD_A_NOT_TO_BE_USED() * 2; //< I want to get rid of warnings from this line 
} 

int main() { 
    return b(); //< I expect a warning on this line only 
} 

所以我只是將我的類重命名爲OLD_A_NOT_TO_BE_USED類。我僅在返回b()時收到警告;如果有人在使用他們,他們仍然會得到不推薦的警告。

相關問題