使用 MATLAB 脚本运行 Polyspace 分析
您可以使用 MATLAB® 脚本自动分析 C/C++ 代码。在您的脚本中,您可以指定源文件和分析选项(例如编译器)、运行分析并将分析结果读取到 MATLAB 表中。
例如,使用此脚本对示例文件运行 Polyspace® Bug Finder™ 分析:
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');
另请参阅 polyspace.Project
。
前提条件
在从 MATLAB 中运行 Polyspace 之前,必须将已安装的 Polyspace 与 MATLAB 相关联。请参阅将 Polyspace 与 MATLAB 和 Simulink 集成。
指定多个源文件
您可以指定一个包含所有源文件的文件夹。例如,如果 proj
是一个 polyspace.Project
对象,请输入:
sourceFolder = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources'); proj.Configuration.Sources = {fullfile(sourceFolder,'*')};
您可以指定一个包含所有源文件的文件夹,源文件直接位于文件夹中或位于子文件夹中。例如:
sourceFolder = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources'); proj.Configuration.Sources = {fullfile(sourceFolder,'**')};
如果您不想分析文件夹中的所有文件,可以显式指定要分析的文件。例如:
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};
您可以从分析中显式排除文件。例如:
% 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];
检查 MISRA C:2012 违规
您可以自定义 Polyspace 分析以检查 MISRA C™:2012 违规。
设置选项以检查 MISRA C:2012 规则。禁用查找缺陷的常规 Bug Finder 分析。
如果 proj
是一个 polyspace.Project
对象,要使用所有强制 MISRA C:2012 规则运行 Bug Finder 分析,请输入:
% 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');
检查特定缺陷或编码违规
您可以指定自己的集,而不是一组默认的缺陷或编码规则检查项。
如果 proj
是一个 polyspace.Project
对象,要禁用 MISRA C:2012 规则 8.1 至 8.4,请输入:
% 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;
另请参阅 polyspace.CodingRulesOptions
。
要启用 Bug Finder 缺陷,请使用类 polyspace.DefectsOptions
。编码规则和缺陷类之间的区别之一是编码规则检查项默认为启用的。您需要禁用那些您不需要的检查项。所有缺陷检查项默认为禁用的。您需要启用那些您需要的检查项。
您还可以指定编码标准 XML 文件,以启用来自不同标准的编码规则。在检查编码违规时,您可以参考该文件。例如,要使用子文件夹 polyspace\examples\cxx\Bug_Finder_Example\sources
中随产品提供的模板 XML 文件 StandardsConfiguration.xml
,请输入:
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;
查找未编译的文件
如果一个或多个文件包含编译错误,则将继续分析其余文件。您可以选择停止分析编译错误。
如果 proj
是一个 polyspace.Project
对象,要停止对编译错误的分析,请输入:
proj.Configuration.EnvironmentSettings.StopWithCompileError = true;
但是,在分析完成后从分析日志文件中捕获所有编译错误会更方便。有关详细信息,请参阅Troubleshoot Polyspace Analysis from MATLAB。
在服务器端运行分析
您可以在远程服务器端而不是本地桌面端运行分析。设置与服务器的连接后,您可以在批处理模式下运行分析。有关设置信息,请参阅安装用于将 Polyspace 分析从桌面端提交到远程服务器端的产品。
指定分析必须在服务器端运行。在桌面端指定一个文件夹,分析后将结果下载到该文件夹中。如果 proj
是一个 polyspace.Project
对象,要在服务器端配置分析,请输入:
proj.Configuration.MergedComputingSettings.BatchBugFinder = true;
proj.Configuration.ResultsDir = fullfile(pwd,'results');
指定管理 Polyspace 作业的主节点:
proj.Configuration.Advanced.Additional = '-schedular nodeHost'
正常运行分析。
run(proj, 'bugFinder');
从结果文件夹位置打开结果。
pslinkfun('openresults', '-resultsfolder', proj.Configuration.ResultsDir);
另请参阅
polyspace.Project
| polyspaceCodeProver
| -scheduler