このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
MATLAB スクリプトを使用した Polyspace 解析の実行
MATLAB® スクリプトを使用して、C/C++ コードの解析を自動化できます。スクリプトで、ソース ファイルと解析オプション (コンパイラなど) を指定し、解析を実行して、解析結果を MATLAB table に読み取ります。
たとえば、次のスクリプトを使用して、サンプル ファイルに 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 インストールをリンクしなければなりません。MATLAB や Simulink との Polyspace の統合を参照してください。
複数のソース ファイルの指定
すべてのソース ファイルを含むフォルダーを指定できます。たとえば、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 Rule 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
を使用します。コーディング ルールと欠陥クラスの違いの 1 つは、コーディング ルール チェッカーは既定で有効になっていることです。不要なものを無効にします。すべての欠陥チェッカーは既定で無効になっています。不要なものを有効にします。
規約の異なるコーディング ルールを有効にする、コーディング規約の 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;
コンパイルされないファイルの検索
1 つ以上のファイルにコンパイル エラーがある場合、解析は残りのファイルで続行されます。コンパイル エラーで解析を停止するようにできます。
proj
が polyspace.Project
オブジェクトである場合、コンパイル エラーで解析を停止するには、次のように入力します。
proj.Configuration.EnvironmentSettings.StopWithCompileError = true;
ただし、解析を完了して、解析ログ ファイルからすべてのコンパイル エラーを見つけるほうが便利です。詳細は、Polyspace 解析の MATLAB からのトラブルシューティングを参照してください。
サーバーでの解析の実行
解析をローカル デスクトップではなくリモート サーバーで実行できます。サーバーへの接続を設定したら、バッチ モードで解析を実行できます。設定の詳細は、Install Products for Submitting Polyspace Analysis from Desktops to Remote Serverを参照してください。
解析をサーバーで実行しなければならないことを指定します。解析後に結果をダウンロードするデスクトップ上のフォルダーを指定します。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
| polyspaceBugFinder
| -scheduler