PVS-Studio learned to monitor how you program
PVS-Studio introduced a mode of operation that will help identify errors and typos as early as possible. The analyzer starts immediately after compiling the files and if something is wrong, it will blush with shame for your code. The feature is currently available only for users of Visual Studio 2010.
I have repeatedly written, the sooner the error is detected, the lower the cost of fixing it. However, in this I am not original, and the authors of many books and articles have been repeating and continue to repeat this. Take at least the same S. McConnell . So I will not repeat myself.
Previous versions of the PVS-Studio analyzer had to be started manually or integrated into the nightly builds. The fact that in the morning you can see the log and fix some kind of error is undoubtedly wonderful. But not perfect. This does not prevent you from planting a couple of small mistakes during the day and then looking for each of them for 10 minutes. PVS-Studio will not help much here. Running it only for modified files is inconvenient, but for the entire project it takes too long.
Fortunately, we are programmers ourselves and are sensitive to the needs of our keyboard colleagues. PVS-Studio 4.30 introduced a new incremental analysis mode. Verification is now almost combined with the compilation stage. The analyzer checks those files that are compiled by pressing the F7 button that we all love. Turning on this mode is not easy, but very simple. It is enough to check the box next to the item “Incremental Analysis After Build” in the PVS-Studio menu:
Now let's look at an example of how this works. Suppose that I was very happy with the programming process, I was in a hurry and made a mistake in the array index:
The result of this expression is always 'false'. But I do not notice. I write the code further and enjoy life. Then, with satisfaction from the work done, I press F7, and the modified file is successfully compiled. And VS2010 does not give any warnings. I calmly go to edit the next section of code.
But when the compilation is completed, quietly, in the background, the PVS-Studio analyzer starts and checks the modified files. In the background, it starts specially so as not to interfere with the programmer. Static analysis takes longer to compile, and it makes no sense to make the programmer sigh heavily while looking at the progress dialog. If everything is written correctly (both for you and for us ;-)), then the programmer will not notice the presence and work of PVS-Studio at all.
However, there is a mistake. And after some time, the analyzer will signal the presence of a suspicious code! It will turn red, and the programmer’s hand itself will reach the desired tab to find out the details:
Indeed, in the message you can read that the condition is always false:
As a result, the error will be fixed almost immediately after writing.
I invite you to try the new PVS-Studio mode. Download the trial version as usual here . If after installation VS2010 starts to slow down, then reduce the appetites of PVS-Studio. By default, the analyzer uses all available processor cores during verification. Accordingly, in the settings you can specify how many cores can be used.
I foresee several questions at once. Therefore, I prepared in advance at the end a small FAQ.
1) Why is the new mode supported only in VS2010?
Only in Visual Studio 2010 there was an API that allows you to find out which files have been changed and which depend on them. In other words, a mechanism has appeared that allows you to select files for verification. A little more about this here [ 1 ].
If you really like the new mode of operation, then we will think about how to implement it for users of Visual Studio 2005/2008. However, I want to note that the transition to Visual Studio 2010 is much easier than it might seem. You can choose the old compiler, but use the capabilities of the new environment. This is done using the Platform Toolset [ 2 ].
2) Why not do a check at all on the fly and emphasize errors immediately, as, for example, does IntelliSense?
Firstly, such tight integration with Visual Studio is not available to us. To get it you need to be more friendly with Microsoft.
Secondly, nothing will work out anyway. Static analysis is quite complicated and in order to identify some errors, it is necessary to collect information on a fairly large piece of code. Since the code will be edited, it will almost always be impossible to parse. I think every programmer saw the C / C ++ compiler go crazy due to an accidentally superfluous bracket or incorrect template. C / C ++ is not a language that can be analyzed quite on the fly.
3) And what if I do not distinguish between green and red?
I agree with the remark. Perhaps in the next version we will not only change the color, but also write text on the tab.
4) Why is it not working here like this?
This is a new opportunity and there is a high probability of some errors. We will be grateful if you tell us about the shortcomings and wishes you noticed. Feedback page .
5) If I do Rebuild, then the analyzer will then check the entire project?
Not. It makes no sense for too long. The analyzer ignores the decision and project reassignment commands. If you want to check a solution or project, then, as before, the corresponding commands are available in the analyzer.
Additional resources:
- Pavel Eremeev. Using the PVS-Studio analyzer during the incremental build process in Microsoft Visual Studio 2010. http://www.viva64.com/en/b/0101/
- MSDN How to: Modify the Target Framework and Platform Toolset. http://www.viva64.com/go.php?url=689