Performing Code Analysis from the Command Line

You can perform code analysis from the command line using MSBuild .

Enterprise Developer enables you to perform code analysis using the MSBuild utility when a project has finished building. The MSBuild command line takes a number of switches and parameters that enable you to tailor its behavior.

There are two ways you can configure and run code analysis from the command line:

  • Run MSBuild and perform code analysis using the settings specified in the project's properties.
  • Run MSBuild with command-line options for code analysis that differ from the ones set at project level.

    For example, running MSBuild with /p:RunMicroFocusCodeAnalysisAfterBuild=true specifies that code analysis is to be executed after the build regardless of whether code analysis is enabled in the project's properties.

    Running MSBuild with /p:ActiveRulesets="Coding Standards" specifies the rules from the Coding Standards rule sets enabled in the IDE are to be used.

    Using these command line parameters, you can create and execute code analysis jobs at the command line without having to change the way your projects are built using the IDE.

To build a project and perform code analysis using MSBuild from the command line:

  1. If you want to use the project's settings for code analysis from the command line, open your project in the IDE and configure it for code analysis.
  2. Start a Visual Studio command prompt and execute an MSBuild command to build your project:
    msbuild [path]MyProject.cblproj [Code-analysis-parameters]

    If you specified any parameters for executing code analysis, MSBuild runs code analysis after the build completes. The output from code analysis is displayed in the command line window after the messages from the build.

Syntax for the MSBuild command

To perform code analysis from the command line, you run the MSBuild command and specify parameters for code analysis:

msbuild [path]MyProject.cblproj [Code-analysis-parameters]

If no Code-analysis-parameters are specified, MSBuild uses the code analysis settings specified in the IDE and in the project's properties.

MSBuild supports various switches and parameters at the command line. The switch to use for code analysis is /p and you can use the following syntax to specify various properties with it:

msbuild [path]MyProject.cblproj /p:property1=value1;property2=value2;...

Or:

msbuild [path]MyProject.cblproj /p:property1=value1 /p:property2=value2...

For example:

msbuild Myproject.cblproj /p:CodeAnalysisResultsAsErrors=true;RunMicroFocusCodeAnalysisAfterBuild=true;ActiveRulesets="Coding Standards"

Code analysis properties for MSBuild

Enterprise Developer provides the following properties for code analysis to use with the MSBuild /p switch:

RunMicroFocusCodeAnalysisAfterBuild=[false|true]
Specifies whether to run code analysis at the end of a build performed using MSBuild from the command line.

If this property is not specified, MSBuild uses the code analysis properties set in the project's properties. If code analysis is not enabled for the project, MSBuild only performs a build but does not analyze the project.

When this property is specified, any code analysis properties set in the IDE are ignored. This enables you to run command-line specific code analysis jobs that differ from the way code analysis is configured when running builds from within the IDE. The supported options are:

true
MSBuild performs code analysis at the end of the build regardless of whether code analysis is enabled in the project's properties.
false
MSBuild only builds the project without performing code analysis regardless of whether code analysis is enabled in the project's properties.
ActiveRulesets=[Ruleset1;Ruleset2;…]
Specifies which rule sets of the ones installed with or imported into the product should be used when running code analysis from the command line. This only executes the rules enabled for a rule set in the IDE.

For example:

/p:ActiveRulesets="Coding Standards;Within Entire Program"
CodeAnalysisResultsAsErrors=[false|true]
Specifies whether code analysis should produce warnings or errors. The default value is false.

You can use Microsoft's own MSBuild switches and properties when performing code analysis. For example, you can use the /maxcpucount switch:

/maxcpucount:n
Setting n to more than 1 enables building and analyzing of n number of projects in parallel. The results of analyzing each project are displayed with the results from the other projects in the command line window.
Note: To optimize the build process, the maximum number of parallel project builds is automatically set to the number of CPUs of your computer, the maximum being 32.

Some of the Microsoft properties you can use with the /p switch are:

TreatWarningsAsErrors=[false|true]
When this is set to true, any issues reported during code analysis cause the execution to fail. A message "Build Failed" is then displayed.
ContinueOnError=value
Where value can be either WarnAndContinue, ErrorAndContinue or ErrorAndStop.

For example, setting this to ErrorAndStop means that any errors encountered during code analysis (such as failures to analyze due to some internal error) or the analysis results reported as errors (see TreatWarningsAsErrors) cause the execution to stop.

For more details about using MSBuild from the command line, see the Visual Studio help.

Results from a code analysis job

The results of running code analysis from the command line are shown in the command prompt window alongside all the build messages.

The messages have the following format:

[Filename and full path] [LocationInFile (start line, column, end line, column)] : [ErrorMessage (error/warning + output code)] :  [RuleSetName] :  [Rule] :  [Code snippet the location refers to]

For example:

C:\Tutorials\Projects\MyProject\MyProgram.cbl (14,8,14,33): warning MFCA0001 : Within Entire Program : Dead Statements : display "Some Message"