# V011. Presence of #line directives may cause some diagnostic messages to have incorrect file name and line number.

A #line directive is generated by the preprocessor and specifies the filename and line number that a particular line in the preprocessed file refers to.

This is demonstrated by the following example.

    #line 20 "a.h"
    void X(); // Function X is declared at line 20 in file a.h
    void Y(); // Function Y is declared at line 21 in file a.h
    void Z(); // Function Z is declared at line 22 in file a.h
    #line 5 "a.cpp"
    int foo; // Variable foo is declared at line 5 in file a.cpp
    int X() { // Definition of function X starts at line 6 in file a.cpp
      return 0; // Line 7
    } // Line 8

#line directives are used by various tools, including the PVS-Studio analyzer, to navigate the file.

Sometimes source files (*.c; *.cpp; *.h, etc.) happen to include #line directives as well. This may happen, for example, when the file is generated automatically by some code-generating software (example).

When preprocessing such a file, those #line directives will be added to the resulting *.i file. Suppose, for example, that we have a file named A.cpp:

    int a;
    #line 30 "My.y"
    int b = 10 / 0;

After the preprocessing, we get the file A.i with the following contents:

    #line 1 "A.cpp"
    int a;
    #line 30 "My.y"
    int b = 10 / 0;

This makes correct navigation impossible. On detecting a division by zero, the analyzer will report this error as occurring at line 30 in the My.y file. Technically speaking, the analyzer is correct, as the error is indeed a result of the incorrect code in the My.y file. However, with the navigation broken, you will not be able to view the My.y file since the project may simply have no such file. In addition, you will never know that currently, the division-by-zero error actually occurs at line 3 in the A.cpp file.

To fix this issue, we recommend deleting all #line directives in the source files of your project. These directives typically get there by accident and only hinder the work of various tools, such as code analyzers, rather than help.

V011 diagnostic was developed to detect such unwanted #line directives in the source code. The analyzer reports the first 10 #line's in a file. Reporting more makes no sense since you can easily find and delete the remaining #line directives using the search option of your editor.

This is the fixed code:

    int a;
    int b = 10 / 0;

After the preprocessing, you get the following *.i file:

    #line 1 "A.cpp"
    int a;
    int b = 10 / 0;

The navigation is fixed, and the analyzer will correctly report that the division by zero occurs at line 2 in the A.cpp file.