Error with #line directive in Visual C ++ compiler

image1.png

The #line directive is added by the preprocessor and then can be used to understand which file and line a particular piece of code in the preprocessed file belongs to. The #line directive tells code tools to change the internal stored line number and the name of the compiler file to the specified line number. The following lines will already count relative to the given position. Explicit preprocessing is mainly used for debugging or by various generators. In any case, an error in such a functionality can lead to various negative consequences. One of these problems occurred to the user of PVS-Studio in Visual Studio 2019.

Error history and description


The #line directive is an important part of the preprocessed files that are used by the PVS-Studio analyzer to issue warnings on the necessary lines of user code. This is where the story of the error search begins.

One of our users with a large code base began the translation of the entire project from Platform Toolset version v141 to v142 in Visual Studio 2019. After that, in the results of the analysis, a surge of V002 warnings appeared and several tens of other warnings were issued to the wrong lines of code (one line error )

Diagnostics V002 just detects such situations (incorrect line numbering). This usually occurs due to multi-line macros or other preprocessor directives (e.g. #pragma). The problem does not occur very often, but mainly on a compiler from Microsoft. Such a surge of problems on one project was unexpected, because our testing system did not show the presence of such a serious problem, and we began to debug the analyzer on the preprocessed user file.

After a few hours of debugging, we found that the analyzer was already working with incorrect data about lines of code and were able to make a test example:

#define MACRO(a, b)              // line1
                                 // line2
int main(int argc, char* argv[]) // line3
{                                // line4
    MACRO("Some string",         // line5
        42);                     // line6
    return 0;                    // line7 <=
}

The preprocessed file is as follows:

#line 1 "D:\\_Tests\\TestApp\\Project2\\rnd_bs.cpp"


int main(int argc, char* argv[])
{
  ;
#line 8 "D:\\_Tests\\TestApp\\Project2\\rnd_bs.cpp"
  return 0;
}

The return statement is on the 7th line in the source file, and the preprocessor points to the 8th line. This error occurred in the preprocessor, presumably due to a multi-line macro.

Released Version Release


At the time of writing, the problem was relevant for the latest versions of Visual Studio, up to version 16.4.3 (dated January 14, 2020). We opened the Bug Report " Invalid #line directive after preprocessing (VS2019, v142) " and after 4 days received an interesting answer:

This issue has been identified as a duplicate of an earlier reported feedback linked here: developercommunity.visualstudio.com/content/problem /738407/preprocessor-generates-incorrect-line-information.html . If you reported or voted here, your vote has been applied to the original issue. Everyone else can add their vote directly at the above linked feedback. Voting helps increase the priority of the issue by consolidating customer impact under one feedback. Thank you!

It turns out that this problem has already been reported, and on September 18, 2019 - almost half a year ago: " Preprocessor generates incorrect line information ". The user provided similar code examples to reproduce the error. According to the author, the error exists at least with Visual Studio 2019 16.2.

It is not known whether this is a coincidence or not, but after a few days of our activity, the error was assigned the status Fixed - Pending Release .

On May 19, 2020, a revised version of Visual Studio 2019 16.6.0 was released. We recommend that all users of PVS-Studio switch to it.


If you want to share this article with an English-speaking audience, then please use the link to the translation: Svyatoslav Razmyslov. A Bug Caused by the #line Directive in the Visual C ++ Compiler .

All Articles