Troubleshoot Polyspace Analysis from MATLAB
When you run a Polyspace® analysis on your C/C++ code, if one or more of your files fail to compile, the analysis continues with the remaining files. You can choose to stop the analysis on compilation errors.
proj = polyspace.Project; proj.Configuration.EnvironmentSettings.StopWithCompileError = true;
However, it is more convenient to let the analysis complete and capture all compilation errors.
The compilation errors are displayed in the analysis log that appears on the
MATLAB® command window. The analysis log also contains the options used and the
various stages of analysis. The lines that indicate errors begin with the
Error:
string. Find these lines and extract them to a log file
for easier scanning. Produce a warning to indicate that compilation errors
occurred.
Prerequisites
Before you run Polyspace from MATLAB, you must link your Polyspace and MATLAB installations. See Integrate Polyspace with MATLAB and Simulink.
Capture Polyspace Analysis Errors in Error Log
The function runPolyspace
defined later captures the output
from the command window using the evalc
function and stores lines
starting with Error:
in a file error.log
. You
can call runPolyspace
with paths to your source and include
folders.
For instance, you can call the function with paths to demo source files in the
subfolder polyspace/examples/cxx/Bug_Finder_Example/sources
of
the MATLAB installation folder.
sourcePath = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources'); includePath = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'sources'); [status, resultsSummary] = runPolyspace(sourcePath, includePath);
The function is defined as follows.
function [status, resultsSummary] = runPolyspace(sourcePath, libPath) % runPolyspace takes two string arguments: source and include folder. % The files in the source folder are analyzed for defects. % If one or more files fail to compile, the errors are saved in a log. % A warning on the screen indicates that compilation errors occurred. proj = polyspace.Project; % Specify sources proj.Configuration.Sources = {fullfile(sourcePath,'*')}; % Specify compiler and paths to libraries proj.Configuration.TargetCompiler.Compiler = 'gnu4.9'; proj.Configuration.EnvironmentSettings.IncludeFolders = {fullfile(libPath,'*')}; % Run analysis runMode = 'bugFinder'; [logFileContent,status] = evalc('run(proj, runMode)'); % Open file for writing errors errorFile = fopen('error.log','wt+'); % Check log file for compilation errors numErrors = 0; log = strsplit(logFileContent,'\n'); errorLines = find(contains(log, {'Error:'}, 'IgnoreCase', true)); for ii=1:numel(errorLines) fprintf(errorFile, '%s\n', log{errorLines(ii)}); numErrors = numErrors + 1; end if numErrors warning('%d compilation error(s). See error.log for details.', numErrors); end fclose(errorFile); % Read results resObj = proj.Results; resultsSummary = getSummary(resObj, 'defects'); end
The analysis log is also captured in a file
Polyspace_R20
.
Instead of capturing the output from the command window, you can search this
file.##n
_ProjectName
_date
-time
.log
You can adapt this script for other purposes. For instance, you can capture warnings in addition to errors. The lines with warnings begin with Warning:
. The warnings indicate situations where the analysis proceeds despite an issue. The analysis makes an assumption to work around the issue. If the assumption is incorrect, you can see errors later or in rare cases, incorrect analysis results.