>
>
PVS-Studio refers to error "Some d…

Andrey Karpov
Articles: 674

PVS-Studio refers to error "Some diagnostic messages may contain incorrect line number for file ..."

Sometimes PVS-Studio code analyzer can refer to this error: "Some diagnostic messages may contain incorrect line number for file ...". It may occur when using Microsoft Visual Studio 2005 without Visual Studio Service Pack 1. It is an error and not a diagnostic warning. In this note we will tell you about this error and how to react to it.

Any code analyzer, including PVS-Studio, deals only with preprocessed files, i.e. those files where all the macros are defined and all the included files are arranged (#include). A preprocessed file contains information about particular locations where all the files have been placed. That is, preprocessed files contain information about the lines' numbers.

Preprocessing is executed in any case. But for a user this procedure is transparent. Sometimes the preprocessor is included into the code analyzer as part of it and sometimes (as in case of PVS-Studio) an external preprocessor is used. In PVS-Studio we use a preprocessor by Microsoft Visual Studio. For every C/C++ file being processed the analyzer launches the command line compiler cl.exe and generates a preprocessed file with "i" extension with its help.

There is an error in the preprocessor by Microsoft Visual Studio 2005. If you perform preprocessing from the command line (as in PVS-Studio) the information about the numbers of the lines gets wrong when a program contains multi-line macros. This may cause incorrect positioning of the code analyzer in a file. That is, the code analyzer will find a real problem but will refer to a wrong number of the line it is detected in.

Let's explain it by an example. We use assert in a simple code and the expression in parentheses is split into several lines. For example:

int _tmain(int argc, _TCHAR* argv[])
{
  int a = 0;
  int b = 1;
  size_t c = 2;
  assert(a ==
     b);
  a++;   // The analyzer refers to an error in this safe line.
  c = a; // Although the diagnostic warning should actually
         // refer to the line "c = a;".
  return 0;
}

As you can see in the comments, because of the multi-line assert the analyzer says that there is an error (and it is right) but refers to the line above the one which actually contains the error. Of course, it can mislead the user.

If we write the same code with assert in one line there will be no problem:

int _tmain(int argc, _TCHAR* argv[])
{
  int a = 0;
  int b = 1;
  size_t c = 2;
  assert (a == b);
  a++;
  c = a; // The diagnostic warning refers to the right line.
  return 0;
}

This error occurs only when using PVS-Studio in Microsoft Visual Studio 2005 without the service pack Visual Studio Service Pack 1. This problem is absent in Visual Studio 2005 Service Pack1 as well as in elder versions (Visual Studio 2008 and higher). So, to solve this problem we recommend that you install Visual Studio 2005 Service Pack 1 on Visual Studio 2005. Then the code with multi-line macros will be correctly processed by PVS-Studio analyzer.

However, sometimes it is impossible to install Visual Studio 2005 Service Pack 1 due to some reasons. In this case PVS-Studio analyzer tries to detect this error in the file being processed. This is a heuristic mechanism and it cannot guarantee correct definition of diagnostic warnings' positioning in the program code. But if it understands that a particular file contains multi-line macros and there is a positioning error, you see the warning: "Some diagnostic messages may contain incorrect line number for file ...".

This mechanism works in the following way.

The analyzer opens the source C/C++ file and searches the last lexeme. It selects lexemes not shorter than three symbols to ignore the closing parentheses and so on. For example, for the next code the last lexeme is "return" operator:

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 lexeme the analyzer identifies the number of the line it is located in. In this case it is line number 8. Further, the analyzer searches the last lexeme in the already preprocessed file. If the last lexemes do not coincide, a macro must have opened in the end of the file and the analyzer cannot understand if the lines are correct and ignores this situation. But it occurs rather rarely and nearly in all the cases the last lexemes in the source file and in the preprocessed file coincide. If it is so, the analyzer can identify the number of the line where the lexeme is situated in the preprocessed file.

So, we have the numbers of the lines containing the last lexeme in the source file and in the preprocessed file. If these numbers do not coincide there is a failure in line numbering during macros' opening. And in this case the analyzer warns the user about it with the warning "Some diagnostic messages may contain incorrect line number for file ...".

You should keep in mind that if a multi-line assert or any other multi-line macro will be situated in the file below all the dangerous code sections detected, all the numbers of the lines for the errors detected will be correct. And although the analyzer will show the warning "Some diagnostic messages may contain incorrect line number for file ...", it will not prevent you from analyzing the diagnostic warnings received.

Pay attention, that although it is not a fault of PVS-Studio analyzer itself, still it causes incorrect operation of the code analyzer. The best solution of the problem is installation of Microsoft Visual Studio 2005 Service Pack 1.