Different techniques are used in the static analysis methodology. One of them is the preprocessing of files just before they are analyzed. Preprocessing files are created by a compiler that runs in a special mode of operation. Unfortunately, this mode is not well tested, as shown by our many years of experience in developing a static code analyzer. In this article I will give an example of a freshly found bug in the C ++ compiler from Microsoft.
Introduction
To demonstrate the capabilities of the PVS-Studio static analyzer, our team checks the source code of Open Source projects. This is a significant
contribution to the quality of open source software, additional advertising and analyzer testing. Sometimes we reveal very unusual problems in compilers with which it is difficult to do something on the side of the analyzer. So, a colleague recently wrote an article “The
file with the 'import' directive (compiler internal error 'msc1.cpp') was stopped analyzing. What to do? ” To help our users in solving the “alien” problem.
And here CSS?
No less interesting bug was found by me just when checking a large project. The Microsoft compiler for C / C ++ version 19.16.27027.1 (Visual Studio v15.9.9) produced the following error when analyzing several files:
fatal error C1021: invalid preprocessor command 'tooltiphint'
Obviously, this is not a preprocessor directive, but what is it? This is a CSS code snippet:
')
#tooltiphint { position: fixed; width: 50em; margin-left: -25em; left: 50%; padding: 10px; border: 1px solid #b0b0b0; border-radius: 2px; box-shadow: 1px 1px 7px black; background-color: #c0c0c0; z-index: 2; }
After viewing the fragment, it became clear that the compiler was mistaken during the preprocessing of the file, but the code was compiled successfully. The CSS code snippet is part of the C ++ string literal code. This is how the code example looks like to repeat the error:
std::string test = R"<<<( <style type="text/css"> body { color:#000000; background-color:#ffffff } body { font-family:Helvetica, sans-serif; font-size:10pt } #tooltiphint { position: fixed; width: 50em; margin-left: -25em; left: 50%; padding: 10px; border: 1px solid #b0b0b0; border-radius: 2px; box-shadow: 1px 1px 7px black; background-color: #c0c0c0; z-index: 2; } .macro { color: darkmagenta; background-color:LemonChiffon; /* Macros are position: relative to provide base for expansions. */ position: relative; } </style> </head> <body>)<<<";
The above code snippet does not interfere with successful compilation, but, at the same time, an error occurs in the preprocessing mode (flag
/ P ).
This is such a difficult life for developers of static analyzers :). It seems that PVS-Studio is not to blame, but we still have to deal with similar problems. However, this is not something new. You can get acquainted with some other similar cases in the article "
PVS-Studio and hostile habitat ".
Conclusion
This problem will be sent to the official bug tracker, but prompt resolution of the problem is hardly possible. For example, the problem with the
#import directive that we discovered a few months ago, which I wrote about at the beginning, will be fixed only in the next release of Visual Studio. Because The release of the new Visual Studio 2019 will take place in a week, most likely, this bug will not have time to be fixed by this date. We also recommend using the
PVS_STUDIO macro for PVS-Studio
users .

If you want to share this article with an English-speaking audience, then please use the link to the translation: Svyatoslav Razmyslov.
How the CSS markup fragment broke the C ++ compiler