Code Analysis for C/C++ is a very verbose tool. Even with tiny bits of code, expect a lot of warning messages. These false positive errors are usually referred to as noise. Microsoft devised three primary design goals for the Code Analysis for C/C++ tool:
Extensibility and customization
Wide developer adoption
Reduction of noise
The wide adoption requirement is satisfied through the integration of C/C++ code analysis in Visual Studio 2005. To satisfy the extensibility and noise reduction requirements, Code Analysis for C/C++ offers many customization options to developers. We've examined two of these features earlier in the chapter:
Unlike other versions of PREfast, Team System's C/C++ static code analysis engine does not support custom filters.
Now that you have a firm grasp of the basics, let's look at a common source of noise and use customization techniques to reduce the levels in order to track down the serious errors!
Library header files are known to generate many warnings. These unintentional warnings can cause havoc in your debug process as you try to identify the serious problems in your code. Fortunately, #pragma directives can help you filter out the noise. Here is a practical example:
When you compile your application, one of the libraries you are using (libtmp.h) is throwing the following error codes:
warning C6031: return value ignored warning C6059: Incorrect length parameter in call to getprice.
The library header file is from a trusted source and shouldn't normally be generating warnings. You try to lower the sensitivity of the warning levels to level 4 during compile time using the following command:
> cl.exe yourapp.cpp /analyze /W4
Unfortunately, some of the errors are persisting. In order to block these warnings, you decide to add a couple of #pragma directives in your library file. These directives effectively block the warnings appearing in the Test Explorer:
#pragma warning (disable:6031) #pragma warning (disable:6059)
When you recompile the code, it runs normally with no warnings or errors.