V002. Some diagnostic messages may contain incorrect line number.
The analyzer can sometimes issue an error "Some diagnostic messages may contain incorrect line number". This occurs when it encounters multiline #pragma directives, on all supported versions of Microsoft Visual Studio.
Any code analyzer works only with preprocessed files, i.e. with those files in which all (#define) macros are expanded and all included files are substituted (#include). Also in the pre-processed file there is information about the substituted files and their positions. That means, in the preprocessed files there is information about line numbers.
Preprocessing is carried out in any case. For the user this procedure looks quite transparent. Sometimes the preprocessor is a part of the code analyzer and sometimes (like in the case with PVS-Studio) external preprocessor is used. In PVS-Studio we use the preprocessor by Microsoft Visual Studio or Clang. The analyzer starts the command line compiler cl.exe/clang.exe for each C/C++ file being processed and generates a preprocessed file with "i" extension.
Here is one the situation where the message "Some diagnostic messages may contain incorrect line number" is issued and a failure in positioning diagnostic messages occurs. It happens because of multiline directives #pragma of a special type. Here is an example of correct code:
#pragma warning(push)
void test()
{
int a;
if (a == 1) // PVS-Studio will inform about the error here
return;
}
If #pragma directive is written in two lines, PVS-Studio will point to an error in an incorrect fragment (there will be shift by one line):
#pragma \
warning(push)
void test()
{
int a; // PVS-Studio will show the error here,
if (a == 1) // actually, however, the error should be here.
return;
}
However, in another case there will be no error caused by the multiline #pragma directive:
#pragma warning \
(push)
void test()
{
int a;
if (a == 1) // PVS-Studio will inform about the error in this line
return;
}
Our recommendation here is either not to use the multiline #pragma directives at all, or to use them in such a way that they can be correctly processed.
The code analyzer tries to detect a failure in lines numbering in the processed file. This mechanism is a heuristic one and it cannot guarantee correct determination of diagnostic messages positioning in the program code. However, if it is possible to find out that a particular file contains multiline pragmas, and there exists a positioning error, then the message "Some diagnostic messages may contain incorrect line number" is issued.
This mechanism works in the following way.
The analyzer opens the source C/C++ file and searches for the very last token. It selects only those tokens that are not shorter than three symbols in order to ignore closing parentheses, etc. E.g., for the following code the "return" operator will be considered as the last token:
01 #include "stdafx.h"
02
03 int foo(int a)
04 {
05 assert(a >= 0 &&
06 a <= 1000);
07 int b = a + 1;
08 return b;
09 }
Having found the last token, the analyzer will determine the number of the line which contains it. In this very case it is line 8. Further on, the analyzer searches for the last token in the file which has already been preprocessed. If the last tokens do not coincide, then most likely the macro in the end of file was not expanded; the analyzer is unable to understand whether the lines are arranged correctly, and ignores the given situation. However, such situations occur very rarely and last tokens almost always coincide in the source and preprocessed files. If it is so, the line number is determined, in which the token in the preprocessed file is situated.
Thus, we have the line numbers in which the last token is located in the source file and in the preprocessed file. If these line numbers do not coincide, then there has been a failure in lines numbering. In this case, the analyzer will notify the user about it with the message "Some diagnostic messages may contain incorrect line number".
Please consider that if a multiline #pragma-directive is situated in the file after all the dangerous code areas are found, then all the line numbers for the found errors will be correct. Even though the analyzer issues the message "Some diagnostic messages may contain incorrect line number for file", this will not prevent you from analyzing the diagnostic messages issued by it.
Please pay attention that this error may lead to incorrect work of the code analyzer, although it is not an error of PVS-Studio itself.