Main Content

polyspace.Project

Run Polyspace analysis on C and C++ code and read results

Description

Run a Polyspace® analysis on C and C++ source files by using this MATLAB® object. To specify source files and customize analysis options, use the Configuration property. To run the analysis, use the run method. To read results after analysis, use the Results property.

Note

Before you run Polyspace from MATLAB, you must link your Polyspace and MATLAB installations. See Integrate Polyspace with MATLAB and Simulink or Integrate Polyspace Server Products with MATLAB.

Creation

proj = polyspace.Project creates an object that you can use to configure and run a Polyspace analysis, and then read the analysis results.

Properties

expand all

Options for running Polyspace analysis, implemented as a polyspace.Options object. The object has properties corresponding to the analysis options. For more information on those properties, see polyspace.Project.Configuration Properties.

You can retain the default options or change them in one of these ways:

  • Set the source code language to 'C', 'CPP', or 'C-CPP' (default). Some analysis options might not be available depending on the language setting of the object.

    proj=polyspace.Project;
    proj.Configuration=polyspace.Options('C');

  • Modify the properties directly.

    proj = polyspace.Project;
    proj.Configuration.TargetCompiler.Compiler = 'gnu4.9';
  • Obtain the options from another polyspace.Project object.

    proj1 = polyspace.Project;
    proj1.Configuration.TargetCompiler.Compiler = 'gnu4.9';
    
    proj2 = proj1;
    

    To use common analysis options across multiple projects, follow this approach. For instance, you want to reuse all options and change only the source files.

  • Obtain the options from a project created in the user interface of the Polyspace desktop products (.psprj file).

    proj = polyspace.Project;
    projectLocation = fullfile(polyspaceroot, 'polyspace', ... 
        'examples', 'cxx', 'Bug_Finder_Example', 'Bug_Finder_Example.psprj')
    proj.Configuration = polyspace.loadProject(projectLocation);

    To determine the optimal set of options, set your options in the user interface and then import them to a polyspace.Project object. In the user interface, you can get tooltip help on options.

  • Obtain the options from a Simulink® model (applies only to Polyspace desktop products). Before obtaining the options, generate code from the model.

    modelName = 'model';
    load_system(modelName);
    
    % Set parameters for Embedded Coder target
    set_param(modelName, 'SystemTargetFile', 'ert.tlc');
    set_param(modelName,'Solver','FixedStepDiscrete');
    set_param(modelName,'SupportContinuousTime','on');
    set_param(modelName,'LaunchReport','off');
    set_param(modelName,'InitFltsAndDblsToZero','on');
    % Generate code
    slbuild(modelName);
    
    % Obtain configuration from model
    proj = polyspace.Project;
    proj.Configuration = polyspace.ModelLinkOptions(modelName);
    

    Use the options to analyze the code generated from the model.

Results of Polyspace analysis. When you create a polyspace.Project object, this property is initially empty. The property is populated only after you execute the run method of the object. Depending on the argument to the run method, 'bugFinder' or 'codeProver', the property is implemented as a polyspace.BugFinderResults object or polyspace.CodeProverResults (Polyspace Code Prover) object.

To read the results, use these methods of the polyspace.BugFinderResults or polyspace.CodeProverResults object:

  • getSummary: Obtain a summarized format of the results into a MATLAB table.

    proj = polyspace.Project;
    proj.Configuration.Sources = {fullfile(polyspaceroot, 'polyspace', 'examples',...
        'cxx', 'Code_Prover_Example', 'sources', 'single_file_analysis.c')};
    proj.Configuration.ResultsDir = fullfile(pwd,'results');
    
    run(proj, 'bugFinder');
    
    resObj = proj.Results;
    resTable = getSummary(resObj, 'defects');

    For more information, see getSummary.

  • getResults: Obtain the full results or a more readable format into a MATLAB table.

    proj = polyspace.Project;
    proj.Configuration.Sources = {fullfile(polyspaceroot, 'polyspace', 'examples',...
        'cxx', 'Code_Prover_Example', 'sources', 'single_file_analysis.c')};
    proj.Configuration.ResultsDir = fullfile(pwd,'results');
    
    run(proj, 'bugFinder');
    
    resObj = proj.Results;
    resTable = getResults(resObj, 'readable');

    For more information, see getResults.

Object Functions

runRun a Polyspace analysis

Examples

collapse all

Run a Polyspace Bug Finder™ analysis on the example file numerical.c. Configure these options:

  • Specify GCC 4.9 as your compiler.

  • Save the results in a results subfolder of the current working folder.

proj = polyspace.Project

% Configure analysis
proj.Configuration.Sources = {fullfile(polyspaceroot, 'polyspace', ... 
    'examples', 'cxx', 'Bug_Finder_Example', 'sources', 'numerical.c')};
proj.Configuration.TargetCompiler.Compiler = 'gnu4.9';
proj.Configuration.ResultsDir = fullfile(pwd,'results');

% Run analysis
bfStatus = run(proj, 'bugFinder');

% Read results
resObj = proj.Results;
bfSummary = getSummary(resObj, 'defects');

Run a Polyspace Code Prover™ analysis on the example file single_file_analysis.c. Configure these options:

  • Specify GCC 4.9 as your compiler.

  • Save the results in a results subfolder of the current working folder.

  • Specify that a main function must be generated, if the function does not exist in the source code.

proj = polyspace.Project

% Configure analysis
proj.Configuration.Sources = {fullfile(polyspaceroot, 'polyspace', 'examples',...
    'cxx', 'Code_Prover_Example', 'sources', 'single_file_analysis.c')};
proj.Configuration.TargetCompiler.Compiler = 'gnu4.9';
proj.Configuration.ResultsDir = fullfile(pwd,'results');
proj.Configuration.CodeProverVerification.MainGenerator = true;


% Run analysis
cpStatus = run(proj, 'codeProver');

% Read results
resObj = proj.Results;
cpSummary = getSummary(resObj, 'runtime');

Run a Polyspace Bug Finder analysis on the example file single_file_analysis.c. Configure these options:

  • Specify GCC 4.9 as your compiler.

  • Save the results in a results subfolder of the current working folder.

  • Enable checking of MISRA C™:2012 rules. Check for the mandatory rules only.

proj = polyspace.Project

% Configure analysis
proj.Configuration.Sources = {fullfile(polyspaceroot, 'polyspace', ... 
    'examples', 'cxx', 'Bug_Finder_Example', 'sources', 'numerical.c')};
proj.Configuration.TargetCompiler.Compiler = 'gnu4.9';
proj.Configuration.ResultsDir = fullfile(pwd,'results');
proj.Configuration.CodingRulesCodeMetrics.EnableMisraC3 = true;
proj.Configuration.CodingRulesCodeMetrics.MisraC3Subset = 'mandatory';

% Run analysis
bfStatus = run(proj, 'bugFinder');

% Read results
resObj = proj.Results;
defectsSummary = getSummary(resObj, 'defects');
misraSummary = getSummary(resObj, 'codingStandards');

Version History

Introduced in R2017b