C/C++ analysis with Kiuwan, part II

Following our series of posts on analyzing C/C++ with Kiuwan, today we are going to see how to analyze a Microsoft Windows application and learn how to configure the Kiuwan Local Analyzer to get the most out of it.

In the first post, we saw the basics to run static analyses on C and C++ applications with Kiuwan, both locally and in the cloud, which are a bit different from other programming languages. Let’s focus now on how to analyze a Microsoft Windows application:

Sample application

From ‘Help – Samples’ option, in Visual Studio 13 Community edition, you can download the source code of our sample application.

Database Query From MFC

Technologies    SQL Server, Visual Studio 2012

Topics                User Interface, Data Access, Database Synchronization, Database, Database Connectivity

Platforms           Desktop, Data

Analizing the code

First of all, start ‘Kiuwan Local Analyzer’ with the {KiuwanLocalAnalyzer_install_dir}\kiuwan.cmd script.

Then, create a new application and set the source code directory.

vs-0

 

In the ‘exclude patterns’ option, add the ‘**/Generated Files/**’ path. Visual Studio generates temporal files in this directory and we need to exclude them from the analysis because we don’t want to analyze files we haven’t coded.

Click ‘Continue’, select ‘C++’, and analyze…

vs-1

 

Once the analysis has finished, you can see the results on Kiuwan:

vs-2

 

Everything seems just ok, but if you look closely, only four files were analyzed out of the eight files detected by Kiuwan Local Analyzer.

If we go to the log screen to find out what happened, we can see what files were left out:
vs3

Solving parser problems

Looking in the log directory for this analysis ({KiuwanLocalAnalyzer_install_dir}\temp\Database Query From MFC.1444389776635-0\results), we can find the file cpp.unresolved.headers.log.

The content of this file are the header files that Kiuwan couldn’t find during the analysis. Most probably because they are not in our project folders:

<SDKDDKVer.h>

<afxwin.h>

<afxext.h>

<afxdisp.h>

<afxdtctl.h>

<afxcmn.h>

<afxcontrolbars.h>

“afxwin.h”

<afxdb.h>

“afxdialogex.h”

In fact, these are Windows and Visual Studio header files our application uses, but are located in external directories. As we can see in the previous post, the resolution of these files is necessary to get a complete and reliable analysis.

With the last version of Kiuwan Local Analyzer, you can autoconfigure the header files paths and macro definitions using the log file that Visual Studio generates on the compilation phase.

To get the default Visual Studio header files paths, run the following commands from a DOS console:

> cd C:\_dev\vs\Database Query From MFC

> call “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat”

> echo %INCLUDE% > include.txt

(update the source code and Visual Studio directories with those of your installation).

 

To get Visual Studio log file we need to compile the application from the command line:

> cd C:\_dev\vs\Database Query From MFC

> call “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat”

> msbuild.exe “Database Query From MFC.sln” /nologo /target:Rebuild /p:Configuration=Release /p:Platform=Win32 > msbuild.log

Now, we can use the generated ‘include.txt’ and ‘msbuild.log’ files to configure Kiuwan Local analyzer.

Before clicking on ‘Analyze’, open the configuration section:

vs-1a

 

Copy the content of ‘include.txt’ file in ‘Header directories’ option. You need to replace the semicolon separator by a comma separator before the copy.

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE,C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE,C:\Program Files (x86)\Windows Kits\8.1\include\shared,C:\Program Files (x86)\Windows Kits\8.1\include\um,C:\Program Files (x86)\Windows Kits\8.1\include\winrt

You should also set the ‘Use all know macros?’ to true.

vs-9

 

Now, in this same screen, click ‘Analyze build logfile – Details’ button, to load the msbuild output logfile:

vs-9avs-6vs-7

 

After these steps, run the analysis again and check the new results on Kiuwan:

vs10

 

Now, all the eight C++ files of this sample application were correctly analyzed.

Now that you know how to configure Kiuwan Local Analyzer to properly analyze your C/C++ applications, you can try it yourself by downloading it from Kiuwan website, after you log in or register for free on Kiuwan.

Stay tuned, there are more posts on C/C++ analysis coming soon!