Unable to analyze a file with 'import' directive (compiler internal error 'msc1.cpp'). What shall I do?
Several times, we got complaints from users that the analysis of some parts of projects wasn't working any more. A common twist was the usage of the '#import' directive in problematic files. This note will briefly describe the reasons of the problem and what has to be done to resume the analysis.
Note. This note is relevant only for Windows when using the 'cl.exe' compiler for preprocessing.
PVS-Studio uses preprocessed files to search for bugs in code written in C, C++. To get them, the analyzer relies on third-party tools, including the cl.exe compiler. The '/P' flag is used to tell cl.exe to generate preprocessed files.
The problem is that starting from the Visual Studio 15.8 version (and the corresponding Visual C++ version - VC++ 2017 version 15.8 v14.15 toolset), when running cl.exe with the '/P' flag on the files, containing the '#import' directive, an error occurs in the compiler.
Nevertheless, when a compiler is not passed the '/P' flag, a build is performed successfully. From the outside, it might look something like this: a project is successfully built, but the PVS-Studio analyzer does not work. On problem files, the analyzer output will look like this:
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\comdef.h: fatal error C1001: An internal error has occurred in the compiler. (compiler file 'msc1.cpp', line 1518) Internal Compiler Error in c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\bin\hostx64\x64\cl.exe
If you faced such a problem, most likely, your project uses the Platform Toolset V141 and the problem occurred after updating Visual Studio and Visual C++ related packages (and, accordingly the compiler) to a newer version. As a result, the project build is still going successfully, but the analysis - isn't.
To make sure that the problem owns to the compiler, you can try setting the '/P' flag in the configuration options of the related project and perform the build. Setting the flag: Properties|C/C++|Preprocessor|Preprocess to a File -> Yes(/P).
By the way, there is a special topic on the Visual Studio forum: https://developercommunity.visualstudio.com/content/problem/313306/vs2017-158-internal-compiler-error-msc1cpp-line-15-1.html
Unfortunately, the described problem still exists and will be fixed, most likely, only in the next Visual Studio release. However, you can continue using PVS-Studio for checking projects, affected by this problem, using a small workaround.
Usage of the 'PVS_STUDIO' macro
When the analyzer runs the compiler in the preprocessing mode, an additional macro is specified - 'PVS_STUDIO'. By using this macro, you can wrap the problematic lines by the #ifdef directive - this will enable the preprocessor to ignore them, without affecting code compilation. At least, you should wrap the '#import' directive:
#if !defined(PVS_STUDIO) #import ... #endif
Note, that in this case the part of code, wrapped in the directive, won't get in the resulting preprocessed files. In most cases, this edit won't affect the analysis results.
Usage of the working compiler version
Note. This problem was solved in the compiler version as part of Visual Studio (2017) 15.9.14.
You can opt to use an earlier compiler version (or a new fixed one), in which there is no problem, described above. Let me note, that it appeared starting from the VC++ 2017 Version 15.8 v14.15 toolset.
Note, that Visual Studio 2017 allows installing several Platform Toolset V141 instances on this system, which will get you out of the need to roll back the Platform Toolset version in the project.
You can specify the desired compiler versions in the files <VsInstanceDir>\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.[txt|props], where <VsInstanceDir> - configuration directory of a specific Visual Studio release.