主要内容

polyspace.Project

对 C 和 C++ 代码运行 Polyspace 分析并读取结果

说明

使用此 MATLAB® 对象对 C 和 C++ 源文件运行 Polyspace® 分析。要指定源文件并自定义分析选项,请使用 Configuration 属性。要运行分析,请使用 run 方法。要在分析后读取结果,请使用 Results 属性。

注意

在从 MATLAB 中运行 Polyspace 之前,必须将已安装的 Polyspace 与 MATLAB 相关联。请参阅将 Polyspace 与 MATLAB 和 Simulink 集成将 Polyspace Server 产品与 MATLAB 集成

创建对象

proj = polyspace.Project 创建一个对象,您可以使用该对象配置并运行 Polyspace 分析,然后读取分析结果。

属性

全部展开

用于运行 Polyspace 分析的选项,以 polyspace.Options 对象形式实现。该对象具有与分析选项对应的属性。有关这些属性的详细信息,请参阅 polyspace.Project.Configuration Properties

您可以保留默认选项,也可以通过以下方式之一更改选项:

  • 将源代码语言设置为 'C'、'CPP' 或 'C-CPP'(默认值)。根据对象的语言设置,一些分析选项可能不可用。

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

  • 直接修改属性。

    proj = polyspace.Project;
    proj.Configuration.TargetCompiler.Compiler = 'gnu4.9';
  • 从另一个 polyspace.Project 对象中获取选项。

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

    要在多个工程中使用通用分析选项,请遵循此方法。例如,您希望重用所有选项,并仅更改源文件。

  • 从在 Polyspace 桌面端产品的用户界面中创建的工程(.psprj 文件)获取选项。

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

    要确定最佳选项集,请在用户界面中设置选项,然后将它们导入到 polyspace.Project 对象中。在用户界面中,您可以获取选项的工具提示帮助。

  • 从 Simulink® 模型中获取选项(仅适用于 Polyspace 桌面端产品)。在获取选项之前,请先从该模型生成代码。

    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);
    

    使用这些选项分析从该模型生成的代码。

Polyspace 分析结果。创建 polyspace.Project 对象时,此属性初始为空。仅在您执行对象的 run 方法后,此属性才会被填充。根据 run 方法的参量('bugFinder''codeProver'),此属性将以 polyspace.BugFinderResults 对象或 polyspace.CodeProverResults 对象形式实现。

要读取结果,请使用 polyspace.BugFinderResultspolyspace.CodeProverResults 对象的以下方法:

  • getSummary:将结果的汇总格式获取到 MATLAB 表中。

    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');

    有关详细信息,请参阅 getSummary

  • getResults:将完整结果或更易读的格式获取到 MATLAB 表中。

    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');

    有关详细信息,请参阅 getResults

对象函数

run运行 Polyspace 分析

示例

全部折叠

对示例文件 numerical.c 运行 Polyspace Bug Finder™ 分析。配置以下选项:

  • 指定 GCC 4.9 为编译器。

  • 将结果保存在当前工作文件夹的 results 子文件夹中。

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');

对示例文件 single_file_analysis.c 运行 Polyspace Code Prover™ 分析。配置以下选项:

  • 指定 GCC 4.9 为编译器。

  • 将结果保存在当前工作文件夹的 results 子文件夹中。

  • 指定如果在源代码中不存在 main 函数,则必须生成该函数。

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');

对示例文件 single_file_analysis.c 运行 Polyspace Bug Finder 分析。配置以下选项:

  • 指定 GCC 4.9 为编译器。

  • 将结果保存在当前工作文件夹的 results 子文件夹中。

  • 启用 MISRA C™:2012 规则检查。仅检查强制规则。

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');

版本历史记录

在 R2017b 中推出