Run Polyspace Analysis by Using MATLAB Scripts
You can automate the analysis of your C/C++ code by using MATLAB® scripts. In your script, you specify your source files and analysis options such as compiler, run an analysis, and read the analysis results to MATLAB tables.
For instance, use this script to run a Polyspace® Bug Finder™ analysis on a sample file:
proj = polyspace.Project % Specify sources and includes sourceFile = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources', 'numerical.c'); includeFolder = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources'); % Configure analysis proj.Configuration.Sources = {sourceFile}; proj.Configuration.TargetCompiler.Compiler = 'gnu4.9'; proj.Configuration.EnvironmentSettings.IncludeFolders = {includeFolder}; proj.Configuration.ResultsDir = fullfile(pwd,'results'); % Run analysis bfStatus = run(proj, 'bugFinder'); % Read results resObj = proj.Results; bfSummary = getSummary(resObj, 'defects');
See also polyspace.Project
.
Prerequisites
Before you run Polyspace from MATLAB, you must link your Polyspace and MATLAB installations. See Integrate Polyspace with MATLAB and Simulink.
Specify Multiple Source Files
You can specify a folder containing all your source files. For instance, if
proj
is a polyspace.Project
, object,
enter:
sourceFolder = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources'); proj.Configuration.Sources = {fullfile(sourceFolder,'*')};
You can specify a folder that contains all your source files both directly and in subfolders. For instance:
sourceFolder = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources'); proj.Configuration.Sources = {fullfile(sourceFolder,'**')};
If you do not want to analyze all files in a folder, you can explicitly specify which files to analyze. For instance:
sourceFolder = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources'); file1 = fullfile(sourceFolder,'numerical.c'); file2 = fullfile(sourceFolder,'staticmemory.c'); proj.Configuration.Sources = {file1, file2};
You can explicitly exclude files from analysis. For instance:
% Specify source folder. sourceFolder = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources'); proj.Configuration.Sources = {fullfile(sourceFolder,'**')}; % Specify files to exclude. file1 = fullfile(sourceFolder,'security.c'); file2 = fullfile(sourceFolder,'tainteddata.c'); proj.Configuration.InputsStubbing.DoNotGenerateResultsFor = ['custom=' file1 ... ',' file2];
Check for MISRA C:2012 Violations
You can customize the Polyspace analysis to check for MISRA C™:2012 rule violations.
Set options for checking MISRA C:2012 rules. Disable the regular Bug Finder analysis, which looks for defects.
If proj
is a polyspace.Project
object, to
run a Bug Finder analysis with all mandatory MISRA C:2012 rules,
enter:
% Enable MISRA C checking proj.Configuration.CodingRulesCodeMetrics.EnableMisraC3 = true; proj.Configuration.CodingRulesCodeMetrics.MisraC3Subset = 'mandatory'; % Disable defect checking proj.Configuration.BugFinderAnalysis.EnableCheckers = false; % Run analysis bfStatus = run(proj, 'bugFinder'); % Read summary of results resObj = proj.Results; misraSummary = getSummary(resObj, 'misraC2012');
Check for Specific Defects or Coding Rule Violations
Instead of the default set of defect or coding rule checkers, you can specify your own set.
If proj
is a polyspace.Project
object, to
disable MISRA C:2012 rules 8.1 to 8.4,
enter:
% Disable rules misraRules = polyspace.CodingRulesOptions('misraC2012'); misraRules.Section_8_Declarations_and_definitions.rule_8_1 = false; misraRules.Section_8_Declarations_and_definitions.rule_8_2 = false; misraRules.Section_8_Declarations_and_definitions.rule_8_3 = false; misraRules.Section_8_Declarations_and_definitions.rule_8_4 = false; % Configure analysis proj.Configuration.CodingRulesCodeMetrics.EnableMisraC3 = true; proj.Configuration.CodingRulesCodeMetrics.MisraC3Subset = misraRules;
See also polyspace.CodingRulesOptions
.
To enable Bug Finder defects, use the class polyspace.DefectsOptions
. One
difference between coding rules and defects class is that coding rule checkers are
enabled by default. You disable the ones that you do not want. All defect checkers
are disabled by default. You enable the ones that you want.
You can also specify a coding standard XML file that enables coding rules from
different standards. When checking for coding rule violations, you can refer to the
file. For instance, to use the template XML file
StandardsConfiguration.xml
provided with the product in the
subfolder polyspace\examples\cxx\Bug_Finder_Example\sources
,
enter:
pathToTemplate = fullfile(polyspaceroot,'polyspace','examples',... 'cxx','Bug_Finder_Example','sources','StandardsConfiguration.xml'); proj.Configuration.CodingRulesCodeMetrics.EnableMisraC3 = true; proj.Configuration.CodingRulesCodeMetrics.MisraC3Subset = 'from-file'; proj.Configuration.CodingRulesCodeMetrics.EnableCheckersSelectionByFile = true; proj.Configuration.CodingRulesCodeMetrics.CheckersSelectionByFile = pathToTemplate;
Find Files That Do Not Compile
If one or more of your files contain a compilation error, the analysis continues with the remaining files. You can choose to stop analysis on compilation errors.
If proj
is a polyspace.Project
object, to
stop analysis on compilation errors,
enter:
proj.Configuration.EnvironmentSettings.StopWithCompileError = true;
However, it is more convenient to let the analysis complete and capture all compilation errors from the analysis log file. For more information, see Troubleshoot Polyspace Analysis from MATLAB.
Run Analysis on Server
You can run an analysis on a remote server instead of your local desktop. Once you have set up connection to a server, you can run the analysis in batch mode. For setup information, see Install Products for Submitting Polyspace Analysis from Desktops to Remote Server.
Specify that the analysis must run on a server. Specify a folder on your desktop
where results are downloaded after analysis. If proj
is a
polyspace.Project
object, to configure analysis on a server,
enter:
proj.Configuration.MergedComputingSettings.BatchBugFinder = true;
proj.Configuration.ResultsDir = fullfile(pwd,'results');
Specify the head node that manages the Polyspace jobs:
proj.Configuration.Advanced.Additional = '-schedular nodeHost'
Run analysis as usual.
run(proj, 'bugFinder');
Open the results from the results folder location.
pslinkfun('openresults', '-resultsfolder', proj.Configuration.ResultsDir);
See Also
polyspace.Project
| polyspaceBugFinder
| -scheduler