Tracing Generated C/C++ Code toMATLAB源代码
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:
ConfigureMATLAB 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. SeeInteractively 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 theMATLAB Coderapp, setMATLAB source code as commentsto
Yes
.In a code generation configuration object, set
MATLABSourceComments
totrue
.
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:
.
For example, this comment indicates that the codex = r * cos(theta);
appears at line4
in the source filestraightline.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 withoutif
,while
,for
orswitch
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 Cartesianx = 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 theif
statement immediately precedes the code that implements the statement. This comment appears after user comments that precede the generated code. The comments for theelseif
andelse
clauses appear immediately after the code that implements the clause, and before the code generated for statements in the clause.
MATLAB Code
functiony = ifstmt(u,v)%#codegenifu > v y = v + 10;elseifu == v y = u * 2;elsey = 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 thefor
statement header immediately precedes the generated code that implements the header. This comment appears after user comments that precede the generated code.
MATLAB Code
functiony = forstmt(u)%#codegeny = 0;fori = 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 thewhile
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
functiony = subfcn(y) coder.inline('never');whiley < 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 theswitch
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 thecase
andotherwise
clauses appear immediately after the generated code that implements the clause, and before the code generated for statements in the clause.
MATLAB Code
functiony = switchstmt(u)%#codegeny = 0;switchucase1 y = y + 1;case3 y = y + 2;otherwisey = 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
- Interactively Trace Between MATLAB Code and Generated C/C++ Code(Embedded Coder)
- Include Comments in Generated C/C++ Code(Embedded Coder)
- Code Generation Reports