メインコンテンツ

himl_0004: MATLAB Code Analyzer recommendations for code generation

R2026b

Use MATLAB Code Analyzer with %#codegen directive and follow code generation recommendations

Usage: High-Integrity System Modeling

Guideline ID: himl_0004

Rules

himl_0004: MATLAB Code Analyzer recommendations for code generation
A

To activate MATLAB® Code Analyzer messages for code generations, use the %#codegen directive in external MATLAB functions.

Rationale

In external MATLAB functions, the %#codegen directive activates MATLAB Code Analyzer messages for code generation.

Verification

Check MATLAB Code Analyzer messages (Simulink Check)

Example — Correct

Activate MATLAB Code Analyzer messages for code generations:

%#codegen
  function y = function(u)
    y = inc_u(u));
  end
  function yy = inc_u(uu)
    yy = uu + 1;
  end

Example — Incorrect

External MATLAB file used in Simulink® with missing %#codegen directive:

function y = function(u)
    % nested functions can't be used for code generation
    function yy = inc_u(uu)
      yy = uu + 1;
    end
    y = inc_u(u));
  end

B

Review the MATLAB Code Analyzer messages. Either:

  • Implement the recommendations or

  • Justify not following the recommendations with %#ok<message-ID(S)> directives in the MATLAB function. Do not use %#ok without specific message-IDs.

Rationale

  • Following MATLAB Code Analyzer recommendations helps to:

    • Generate efficient code.

    • Follow best code generation practices

    • Avoid using MATLAB features not supported for code generation.

    • Avoid code patterns which potentially influence safety.

  • Not following MATLAB Code Analyzer recommendations are justified with message id (e.g. %#ok<NOPRT>.

    In the MATLAB function, using %#ok without a message ID justifies the full line, potentially hiding issues.

Verification

Check MATLAB Code Analyzer messages (Simulink Check)

Example — Correct

  • Justify missing ; and value assigned might be unused:

    y = 2*u %#ok<NOPRT,NAGSU> output for debugging
    ...
    y = 3*u;

  • If output is not desired and assigned value is unused, remove the line y = 2*u ...:

    y = 3*u;

Example — Incorrect

  • All messages in line are justified by using %#ok without a message ID:

    % missing ';' and the value might be unused
      y = 2*u %#ok
      …
      y = 3*u;
    

  • No justification:

    % missing justification for missing ';' and unnecessary '[..]'
    y= [2*u]

Tips

  • The MATLAB Code Analyzer messages provide identifies potential errors, problems, and opportunities for improvement in the code.

Industry Standards

  • IEC 61508-3, Table A.3 (3) 'Language subset'
    IEC 61508-3, Table A.4 (3) 'Defensive programming'
    IEC 61508-3, Table A.4 (5) 'Design and coding standards'

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1 (1b) 'Use of language subsets'
    ISO 26262-6, Table 1 (1d) 'Use of defensive implementation techniques'
    ISO 26262-6, Table 1 (1e) 'Use of well-trusted design principles'
    ISO 26262-6, Table 1 (1f) 'Use of unambiguous graphical representation'
    ISO 26262-6, Table 1 (1g) 'Use of style guides'
    ISO 26262-6, Table 1 (1h) 'Use of naming conventions'

  • EN 50128, Table A.4 (11) 'Language Subset'
    EN 50128, Table A.3 (1) 'Defensive Programming'
    EN 50128, Table A.12 (1) 'Coding Standard'
    EN 50128, Table A.12 (2) 'Coding Style Guide'

  • EN 50657, Table A.4 (11) 'Language Subset'
    EN 50657, Table A.3 (1) 'Defensive Programming'
    EN 50657, Table A.12 (1) 'Coding Standard'
    EN 50657, Table A.12 (2) 'Coding Style Guide'

  • EN 50716, Table A.3 (1) 'Defensive Programming'
    EN 50716, Table A.12 (1) 'Coding Standard'
    EN 50716, Table A.12 (2) 'Coding Style Guide'

  • DO-331, Section MB.6.3.1.b 'Accuracy and consistency'
    DO-331, Section MB.6.3.2.b 'Accuracy and consistency'

Version History

Introduced in R2014a

expand all