Number of Recursions
Number of call graph cycles over one or more functions
Description
The metric provides a quantitative estimate of the number of recursion cycles in your project. The metric is the sum of:
Number of direct recursions (self recursive functions or functions calling themselves).
Number of strongly connected components formed by the indirect recursion cycles in your project. If you consider the recursion cycles as a directed graph, the graph is strongly connected if there is a path between all pairs of vertices.
To compute the number of strongly connected components:
Draw the recursion cycles in your code.
For instance, the recursion cycles in this example are shown below.
volatile int checkStatus; void func1() { if(checkStatus) { func2(); } else { func3(); } } func2() { func1(); } func3() { func1(); }
Identify the number of strongly connected components formed by the recursion cycles.
In the preceding example, there is one strongly connected component. You can move from any vertex to another vertex by following the paths in the graph.
The event list below the metric shows one of the recursion cycles in the strongly connected component.
Polyspace® computes this metric using a syntactical call hierarchy, which is different than the call hierarchy computed by Polyspace. Because this metric is calculated using a syntactical hierarchy, it considers calls in unreachable branches and does not consider calls through function pointers.
The recommended upper limit for this metric is 0. To avoid the possibility of exceeding available stack space, do not use recursions in your code. Recursions can tend to exhaust stack space easily. See examples of stack size growth with recursions described for this CERT-C rule that forbids recursions.
To detect use of recursions, check for violations of one of MISRA C:2012 Rule 17.2
,MISRA C™: 2004 Rule 16.2, MISRA C++:2008 Rule 7-5-4
, JSF® Rule 119, or Number of
Recursions Exceeds Threshold
. Note that:
The rule checkers report each function that calls itself, directly or indirectly. Even if several functions are involved in one recursion cycle, each function is individually reported.
The rule checkers consider explicit function calls only. For instance, in C++ code, the rule checkers ignore implicit calls to constructors during object creation. However, the metrics computation considers both implicit and explicit calls.
To enforce limits on metrics, see:
Examples
Metric Information
Group: Project |
Acronym: AP_CG_CYCLE |
HIS Metric: Yes |