Main Content

polyspace.CodingRulesOptions Class

Namespace: polyspace

Create custom list of coding rules to check

Description

Create a custom list of coding rules to check in a Polyspace® analysis.

Note

Before you run Polyspace from MATLAB®, you must link your Polyspace and MATLAB installations. See Integrate Polyspace with MATLAB and Simulink or Integrate Polyspace Server Products with MATLAB.

Construction

ruleList = polyspace.CodingRulesOptions(RuleSet) creates the coding rules object ruleList for the RuleSet coding rule set. Set the active rules in the coding rules object.

Input Arguments

expand all

Standard coding rule set specified as one of the coding rule acronyms.

Example: 'misraCpp'

Data Types: char

Properties

For each coding rule set, an object is created with all supported rules divided into sections. By default, all rules are on. To turn off a rule, set the rule to false. For example:

misraRules = polyspace.CodingRulesOptions('misraC');
misraRules.Section_20_Standard_libraries.rule_20_1 = false;

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

Customize the coding rules that are checked in a Polyspace analysis. Since all rules are enabled by default, you can create a custom subset by disabling some rules.

Create two objects: a polyspace.CodingRulesOptions object for setting coding rules and a polyspace.Project object for running the Polyspace analysis.

misraRules = polyspace.CodingRulesOptions('misraC2012');
proj = polyspace.Project;

Customize the coding rule list by turning off rules 2.1-2.7.

misraRules.Section_2_Unused_code.rule_2_1 = false;
misraRules.Section_2_Unused_code.rule_2_2 = false;
misraRules.Section_2_Unused_code.rule_2_3 = false;
misraRules.Section_2_Unused_code.rule_2_4 = false;
misraRules.Section_2_Unused_code.rule_2_5 = false;
misraRules.Section_2_Unused_code.rule_2_6 = false;
misraRules.Section_2_Unused_code.rule_2_7 = false;

Add the customized list of coding rules to the Configuration property of the polyspace.Project object.

proj.Configuration.CodingRulesCodeMetrics.MisraC3Subset = misraRules;
proj.Configuration.CodingRulesCodeMetrics.EnableMisraC3 = true;
proj.Configuration.CodingRulesCodeMetrics.EnableCheckersSelectionByFile = true;
You have to enable checkers selection by file because the Polyspace run uses an XML file underneath to enable the coding rule checkers. The XML file is saved in a .settings subfolder of the results folder.

You can now use the polyspace.Project object to run the analysis. For instance, you can enter:

proj.Configuration.Sources = {fullfile(polyspaceroot, 'polyspace', ... 
    'examples', 'cxx', 'Bug_Finder_Example', 'sources', 'numerical.c')};
run(proj, 'bugfinder');

Suppose that you want to specify a subset of MISRA C™: 2012 rules for the analysis. Instead of enumerating rules that you want disabled, you can specify the rules that you want to keep enabled. You can also specify the rule numbers only without the MISRA C: 2012 sections containing the rules.

Specify the rule numbers in a cell array to the createRulesObject function defined as follows.

function rulesObject = createRulesObject(rulesToEnable)

%% This function takes a cell array of MISRA C:2012 rules and returns
%% a polyspace.CodingRulesOptions object with the rules enabled.
%% Example input argument: {'2.7', '3.1'}

    rulesObject = polyspace.CodingRulesOptions('misraC2012');
    
    % Coding Standards documents have many sections. Loop over all
    % sections.
    ruleSections = properties(rulesObject);
    for i=1:length(ruleSections)
         sectionName = ruleSections{i};
         rulesInSection = properties(rulesObject.(sectionName));
         
         % Loop over all rules in a section, enable or disable rule based
         % on input
         for j=1:length(rulesInSection)
             ruleNumberAsProperty = rulesInSection{j};
             ruleNumber = strrep(strrep(ruleNumberAsProperty,'rule_',''),'_','.');
             if(any(strcmp(rulesToEnable,ruleNumber)))
                 rulesObject.(sectionName).(ruleNumberAsProperty)=1;
             else
                 rulesObject.(sectionName).(ruleNumberAsProperty)=0;
             end
         end
    end
end

For instance, to enable rules 1.1 and 2.2, enter:

createRulesObject({'1.1','2.2'})

Version History

Introduced in R2016b