Main Content

Tracing Generated C/C++ Code to MATLAB Source Code

Tracing the generated C/C++ code to the original MATLAB® source code helps you to:

  • Understand how the generated code implements your algorithm.

  • Evaluate the quality of the generated code.

You can trace by using one of these methods:

  • Configure MATLAB Coder™ to generate code that includes the MATLAB source code as comments. In the comments, a traceability tag immediately precedes each line of source code. The traceability tag provides details about the location of the source code. If you have Embedded Coder®, in the code generation report, the traceability tags link to the corresponding MATLAB source code.

  • With Embedded Coder, produce a code generation report that includes interactive traceability. Interactive tracing in the report helps you to visualize the mapping between the MATLAB source code and the generated C/C++ code. See Interactively Trace Between MATLAB Code and Generated C/C++ Code (Embedded Coder).

Generate Traceability Tags

To produce traceability tags in the generated code, enable generation of MATLAB source code as comments.

  • In the MATLAB Coder app, set MATLAB source code as comments to Yes.

  • In a code generation configuration object, set MATLABSourceComments to true.

Format of Traceability Tags

In the generated code, traceability tags appear immediately before the MATLAB source code in the comment. The format of the tag is:
<filename>:<line number>.

For example, this comment indicates that the code x = r * cos(theta); appears at line 4 in the source file straightline.m.

/* 'straightline:4' x = r * cos(theta); */

Location of Comments in Generated Code

The generated comments containing the source code and traceability tag appear in the generated code as follows.

Straight-Line Source Code

In straight-line source code without if, while, for or switch statements, the comment containing the source code precedes the generated code that implements the source code statement. This comment appears after user comments that precede the generated code.

For example, in the following code, the user comment, /* Convert polar to Cartesian */, appears before the generated comment containing the first line of source code, together with its traceability tag,
/* 'straightline:4' x = r * cos(theta); */.

MATLAB Code

function [x, y] = straightline(r,theta)
%#codegen
% Convert polar to Cartesian
x = r * cos(theta);
y = r * sin(theta);

Commented C Code

void straightline(double r, double theta, double *x, double *y)
{
  /*  Convert polar to Cartesian */
  /* 'straightline:4' x = r * cos(theta); */
  *x = r * cos(theta);

  /* 'straightline:5' y = r * sin(theta); */
  *y = r * sin(theta);
}

If Statements

The comment for the if statement immediately precedes the code that implements the statement. This comment appears after user comments that precede the generated code. The comments for the elseif and else clauses appear immediately after the code that implements the clause, and before the code generated for statements in the clause.

MATLAB Code

function y = ifstmt(u,v) 
%#codegen
if u > v
    y = v + 10;
elseif u == v
    y = u * 2;
else
    y = v - 10;
end

Commented C Code

double ifstmt(double u, double v)
{
  double y;

  /* 'ifstmt:3' if u > v */
  if (u > v) {
    /* 'ifstmt:4' y = v + 10; */
    y = v + 10.0;
  } else if (u == v) {
    /* 'ifstmt:5' elseif u == v */
    /* 'ifstmt:6' y = u * 2; */
    y = u * 2.0;
  } else {
    /* 'ifstmt:7' else */
    /* 'ifstmt:8' y = v - 10; */
    y = v - 10.0;
  }

  return y;
}

For Statements

The comment for the for statement header immediately precedes the generated code that implements the header. This comment appears after user comments that precede the generated code.

MATLAB Code

function y = forstmt(u) 
%#codegen
y = 0;
for i = 1:u
    y = y + 1;
end

Commented C Code

double forstmt(double u)
{
  double y;
  int i;

  /* 'forstmt:3' y = 0; */
  y = 0.0;

  /* 'forstmt:4' for i = 1:u */
  for (i = 0; i < (int)u; i++) {
    /* 'forstmt:5' y = y + 1; */
    y++;
  }

  return y;
}

While Statements

The comment for the while statement header immediately precedes the generated code that implements the statement header. This comment appears after user comments that precede the generated code.

MATLAB Code

function y = subfcn(y) 
coder.inline('never');
while y < 100
    y = y + 1;
end

Commented C Code

void subfcn(double *y)
{
  /* 'subfcn:2' coder.inline('never'); */
  /* 'subfcn:3' while y < 100 */
  while (*y < 100.0) {
    /* 'subfcn:4' y = y + 1; */
    (*y)++;
  }
}

Switch Statements

The comment for the switch statement header immediately precedes the generated code that implements the statement header. This comment appears after user comments that precede the generated code. The comments for the case and otherwise clauses appear immediately after the generated code that implements the clause, and before the code generated for statements in the clause.

MATLAB Code

function y = switchstmt(u)
%#codegen
y = 0;
switch u
    case 1
        y = y + 1;
    case 3
        y = y + 2;
    otherwise
        y = y - 1;
end

Commented C Code

double switchstmt(double u)
{
  double y;

  /* 'switchstmt:3' y = 0; */
  /* 'switchstmt:4' switch u */
  switch ((int)u) {
   case 1:
    /* 'switchstmt:5' case 1 */
    /* 'switchstmt:6' y = y + 1; */
    y = 1.0;
    break;

   case 3:
    /* 'switchstmt:7' case 3 */
    /* 'switchstmt:8' y = y + 2; */
    y = 2.0;
    break;

   default:
    /* 'switchstmt:9' otherwise */
    /* 'switchstmt:10' y = y - 1; */
    y = -1.0;
    break;
  }

  return y;
}

Traceability Tag Limitations

  • You cannot include MATLAB source code as comments for:

    • MathWorks® toolbox functions

    • P-code

  • The appearance or location of comments can vary:

    • Even if the implementation code is eliminated, for example, due to constant folding, comments can still appear in the generated code.

    • If a complete function or code block is eliminated, comments can be eliminated from the generated code.

    • For certain optimizations, the comments can be separated from the generated code.

    • Even if you do not choose to include source code comments in the generated code, the generated code includes legally required comments from the MATLAB source code.

Related Topics