Loop depth calculation

I'm not sure if this question is suitable for SO or not. I will remove if it is not.

I need to review some KLOC code. As part of this, I need to find out the depth of the loops and check the status. [Cm. Examples below]

If it is more than five remedies, I need to note this.

My question is: "Is there any tool that could do this for me?" I searched google but got no answer.

Example 1: for {}// Block depth 1

Example 2: for { if () {} }// Block Depth 2

Example 3: for () { for () { if() {} } }// Block Depth 3

+3
source share
1 answer

Program that just counts

//>prog file
//>prog file max_depth

#include <stdio.h>
#include <stdlib.h>

#define DEPTH_MAX 5 //no count top level

int main(int argc, char *argv[]) {
    FILE *fp;
    int depth_max = DEPTH_MAX;
    if(argc > 1){
        if(NULL==(fp = fopen(argv[1], "r"))){
            perror("fopen");
            exit( EXIT_FAILURE);
        }
    } else {
        fp = stdin;
    }
    if(argc == 3)
        depth_max = atoi(argv[2]);
    int ch, level = 0;
    size_t line_no = 1;
    while(EOF!=(ch=fgetc(fp))){
        if(ch == '\n'){
            ++line_no;
        } else if(ch == '{'){
            if(++level > depth_max)
                printf("found at number of line : %zu\n", line_no);
        } else if(ch == '}'){
            --level;
        }
    }
    exit(EXIT_SUCCESS);
    return 0;
}
+2
source

All Articles