Analyzing C/C++ code

Purpose

The C++ analyzer is intended to determine dependencies between C/C++ source files based on header file includes. The element hierarchy is based on the directory structure of the source code. Namespaces and types are not taken into account.

Potential ambiguity

It is important to understand that this analyzer does not have any knowledge on how to build the code and therefore does not known the include paths used during the build. It simply uses all source code directories and their nested directories as include paths.

This technique works well when source filenames are unique. It this is not the case it can be ambiguous which file is intended.

How this ambiguity is resolved can be configured:

When building VC++ projects using Visual Studio selecting the Visual Studio analyzers gives more reliable results, because the actual include paths used during building are used.

Merging source and header files

A C++ module consists in most cases of a header and a source file. In many cases externally visible header files are located in a different directory the private header files or source files. For proper analysis it is better to merge them to a single location in the DSM. Three strategies are possible:

Pre requisites

Performing an analysis

Command line usage

Use the following command to run a analysis:

"C:\Program Files\DsmSuite\Analyzers\C++\DsmSuite.Analyzer.Cpp.Exe" AnalyzerSettings.xml

Settings

The following settings are defined:

Setting Description
LogLevel Log level as described above
Input.RootDirectory Root of source code directory used to determine root of names in DSM
Input.SourceDirectories List of directories where source code can be found
Input.IncludeDirectories List of additional directories where include files can be found. Think of system and third party include files.
Input.IgnorePaths List of directories in the source code tree which need to be excluded
Analysis.ResolveMethod How to resolve ambiguity when filenames are not unique
Transformation.IgnoredNames Names in input data which will be ignore. Defines as regular expression.
Transformation.AddTransitiveIncludes Add transitive includes.
Transformation.ModuleMergeStrategy Strategy to merge source and related header file into a single module.
Transformation.ModuleMergeRules Rules to merge header and source file directories.
Output.Filename Filename with dsi extension to which results will be written
Output.Compress Compress output file

AnalyzerSettings.xml example

Example for analyzing C++ in a D:\MyProject source directory.

<?xml version="1.0" encoding="utf-8"?>
<AnalyzerSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <LogLevel>Error</LogLevel>
  <Input>
    <RootDirectory>C:\</RootDirectory>
    <SourceDirectories>
      <string>C:\</string>
    </SourceDirectories>
    <ExternalIncludeDirectories>
      <string>C:\</string>
    </ExternalIncludeDirectories>
    <IgnorePaths>
      <string>C:\</string>
    </IgnorePaths>
  </Input>
  <Analysis>
    <ResolveMethod>AddBestMatch</ResolveMethod>
  </Analysis>
  <Transformation>
    <IgnoredNames />
    <AddTransitiveIncludes>false</AddTransitiveIncludes>
    <HeaderSourceFileMergeStrategy>None</HeaderSourceFileMergeStrategy>
    <MergeHeaderAndSourceFileDirectoryRules>
      <TransformationMergeRule>
        <From>inc.</From>
        <To>src.</To>
      </TransformationMergeRule>
    </MergeHeaderAndSourceFileDirectoryRules>
  </Transformation>
  <Output>
    <Filename>Output.dsi</Filename>
    <Compress>true</Compress>
  </Output>
</AnalyzerSettings>

Additional Logging

The following additional logging files defined.

When logging is enabled the following types of logging are provided of the analysis:

Log file Description
filesNotFound.log Source files not found in the file system
includePathsNotFound.log Include paths not found in the file system
pathsNotResolved.log Relative include files which could not be resolved to an absolute path
includeFilesAmbigious.log Relative include files which can be resolved to multiple absolute include files
includeFilesNotFound.log Absolute include files which could not be found in the file system

back