2011-03-16 73 views
5

如果我安裝了Windows 7的全新副本和Visual Studio 2010高級版SP1中,創建一個嚮導生成的「C++控制檯應用程序」,並增加3頭到.cpp文件:如何在Visual Studio 2010 for C++中正確使用「代碼分析」?

#include <winsock2.h> 
#include <WS2tcpip.h> 
#include <wspiapi.h> 

執行「運行代碼分析」會告訴我在微軟的源代碼警告(項目本身是嚮導生成的,所以沒有錯誤):

1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\ws2tcpip.h(729): warning C6386: Buffer overrun: accessing 'argument 1', the writable size is '1*4' bytes, but '4294967272' bytes might be written: Lines: 703, 704, 705, 707, 713, 714, 715, 720, 721, 722, 724, 727, 728, 729 
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wspiapi.h(294): warning C6386: Buffer overrun: accessing 'argument 1', the writable size is '1' bytes, but '1025' bytes might be written: Lines: 263, 264, 265, 267, 268, 270, 271, 273, 294 
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wspiapi.h(236): warning C6387: '*pptResult' might be '0': this does not adhere to the specification for the function 'WspiapiQueryDNS': Lines: 263, 264, 265, 267, 268, 270, 271, 273, 294, 296 
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wspiapi.h(687): warning C6387: 'argument 1' might be '0': this does not adhere to the specification for the function 'WspiapiLegacyFreeAddrInfo': Lines: 504, 505, 506, 507, 508, 509, 510, 512, 513, 514, 515, 516, 520, 528, 532, 538, 550, 551, 555, 556, 560, 563, 568, 575, 577, 578, 589, 591, 592, 593, 596, 598, 599, 600, 604, 607, 610, 611, 627, 662, 664, 680, 685, 687 
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wspiapi.h(481): warning C6387: '*pptResult' might be '0': this does not adhere to the specification for the function 'WspiapiLegacyGetAddrInfo': Lines: 504, 505, 506, 507, 508, 509, 510, 512, 513, 514, 515, 516, 520, 528, 532, 538, 550, 551, 555, 556, 560, 563, 568, 575, 577, 578, 589, 591, 592, 593, 596, 598, 599, 600, 604, 607, 610, 611, 627, 662, 664, 680, 685, 687, 688, 691 

這使得代碼分析不能用於大項目 - 我得到成千上萬的警告在微軟的頭文件,我不能找到我自己的警告之間:(是否有任何方式排除微軟的頭s/SDK從代碼分析,所以它會變得可用?

P.S.我知道C++不是Visual Studio的良好支持語言,但由於現有項目需要支持和改進,因此我無法更改語言:(。

回答

7

您可以禁用代碼分析警告, 「T的PREfast清潔與warning()#pragma:??

#include <codeanalysis\warnings.h> 
#pragma warning(push) 
#pragma warning (disable: ALL_CODE_ANALYSIS_WARNINGS) 

// include headers 

#pragma warning(pop) 
+0

謝謝,這徹底解決了我的問題^ _^ – grigoryvp 2011-03-16 13:23:28

+0

這非常有用。謝謝。我得到了一個來自windows功能的警告,這是不正確的。警告說我不能指定0作爲#3參數,但在msdn中我說可以這樣做。 – James 2016-04-03 13:12:39

0

您是否嘗試過使用CAExcludePath環境變量除去那些包含系統頭文件目錄

CAExcludePath='C:\Program Files (x86)\Windows Kits\10\Include\10.0.10586.0\ucrt;c:\program files (x86)\microsoft visual studio 14.0\vc\include;c:\program files (x86)\windows kits\8.1\include\um;c:\program files (x86)\microsoft visual studio 14.0\vc\include\sys;' 
相關問題