Contenido principal

Check Coding Rule Compliance of Code Generated from MATLAB Code

R2026b

After generating C/C++ code from MATLAB® code, you can verify that the generated code complies with industry coding standards. Polyspace® Bug Finder™ supports checking for violations of standards including:

  • For C code: MISRA C:2023, MISRA C:2012, CERT C, ISO/IEC TS 17961, and CWE

  • For C++ code: MISRA C++:2023, CERT C++, and AUTOSAR C++14

Prerequisites

To follow this example:

  • You must have an Embedded Coder® or MATLAB Coder™ license.

  • You must have a Polyspace Bug Finder license.

  • You must have a checkers activation file that specifies the coding standards and rules to check. To create this file, open the Polyspace checkers selection interface, select the desired standards and rules, and save the configuration.

Run Coding Rules Analysis on Generated Code

Generate C code from the MATLAB function averaging_filter() as a static library. In the MATLAB Command Window, enter:

% Generate code
matlabFileName = fullfile(polyspaceroot, 'polyspace',...
    'examples','doc_cxx','matlab_coder','averaging_filter.m');
codegenFolder = fullfile(pwd, 'codegenFolder');
codegen(matlabFileName, '-config:lib', '-c', '-args', ...
    {zeros(1,100,'double')}, '-d', codegenFolder);

Associate a polyspace.ModelLinkOptions object with the generated code. The -codegenfolder option automatically discovers the source files, include paths, and compiler settings from the code generation output.

psprjCfg = polyspace.ModelLinkOptions('-codegenfolder', codegenFolder);

Enable the coding rules you want using a checkers activation file. To create the checkers activation file, open the Checkers Selection window. At the system command line, enter:

polyspace-checkers-selection
After selecting the coding rules and defects, save your selection as checkers_activation_file.xml in an accessible location. Then, specify the path to this file in CheckersSelectionFile:
psprjCfg.CodingRulesCodeMetrics.CheckersSelectionFile = 'path/to/checkers_activation_file.xml';

Optionally, configure the analysis to generate a report and specify a folder for the analysis results.

psprjCfg.MergedReporting.EnableReportGeneration = true;
psprjCfg.MergedReporting.ReportOutputFormat = 'PDF';
psprjCfg.ResultsDir = 'newResfolder';

Create a polyspace.Project object. Associate the Configuration property of this object to the options that you previously specified.

proj = polyspace.Project;
proj.Configuration = psprjCfg;

Run the analysis and open the results.

bfStatus = proj.run('bugFinder');
proj.Results.getResults('readable');

See Also

Topics