How to: Enable and Disable Code Analysis for Specific C/C++ Warnings

To manage warning state, list all code analysis warnings in a separate header file. Include the header file in the source file. Use warning pragma to override the settings in the header file.

To enable or disable a code analysis warning

  1. Create a header file that lists all the code analysis warnings and their initial state, as shown in the following code:

    // WarningState.h
       #pragma warning ( default : 6001 )
       #pragma warning ( disable : 6011 )
    // more warnings here 
    // end of file
    
  2. Include WarningState.h in the application header file. In this case, MyApplication.h represents the header file.

    // MyApplication.h file
       #include "WarningState.h"
    // ...
    // end of file
    
  3. Include MyApplication.h file in the source code file. In this case, MyApplication.cpp represents the source file.

    // MyApplication.cpp file
    #include "MyApplication.h"
    
  4. To modify the warning state, use the pragma warning-specifier in a .cpp file, as shown in the following code:

    // MyApplication.cpp file
    #include "MyApplication.h"
    #pragma warning ( disable: 6001 )
    #pragma warning ( default : 6001 )
    

Disable All Code Analysis Warnings

Your project might include files, such as third-party source code, for which you do not want to see code analysis warnings. The following code example disables all code analysis warnings for the included third-party files.

To disable all code analysis warnings for included third-party files

  • Add the following code to your header file.

    #include <codeanalysis\warnings.h>
    #pragma warning( push )
    #pragma warning ( disable : ALL_CODE_ANALYSIS_WARNINGS )
    #include <third-party include files here>
    #pragma warning( pop )
    

See Also

Reference

pragma (C++ Attributes Reference)