3

I am not sure whether this question is suited for SO or not. I will delete if it is not.

I have to review a few KLOC of code. As a part of it I have to find out the depth of loops and condition checking. [Please see the examples below]

If it is more than five means I need to note it down.

My question is "Is there any tool that could do this for me?" I did searching the google, but did not get an answer.

Ex 1 : for {} // Block depth 1

Ex 2 : for { if () {} } // Block depth 2

Ex 3 : for () { for () { if() {} } } //Block depth 3

NeonGlow
  • 1,659
  • 4
  • 18
  • 36
  • 1
    You could check some of the tools from http://stackoverflow.com/questions/60394/calculate-code-metrics – Andreas Fester Feb 17 '14 at 11:37
  • Recursion. Hitting a "{" makes it go deeper in recursive function, hitting "}" returns with a necessary value(appends " // Block depth %d",depthVal). If you have forgotten to enclose with "}", function returns error at the end of file and says "}" missing somewhere. But not works for '"{"' or '"}"' Writing this shouldnt take too long just for depth values. – huseyin tugrul buyukisik Feb 17 '14 at 11:40
  • @Andreas : Many thanks. This will definitely help. – NeonGlow Feb 17 '14 at 11:40
  • An `if` should not count in my world, it's not adding "depth" since it's just doing a simple test. A `for` loop inside another `for` can be very costly, but an `if` runs in constant time. Strange criterion. – unwind Feb 17 '14 at 11:45
  • @unwind : I was asked to do this. But I feels like you are right here. :) – NeonGlow Feb 17 '14 at 11:48
  • @Andreas : I tried one tool SourceMonitor which I got from the link you suggested. It could tell me the max depth of each file. – NeonGlow Feb 17 '14 at 11:54
  • 1
    If the code is properly indented then you can just get the indentation level. Note that only the first non-blank line after a line that ends with `{` or `;` should be counted and if you don't consider comments you may get false positives (and miss some real positives). – Klas Lindbäck Feb 17 '14 at 12:14
  • 2
    @unwind The measurement is probably there to find functions that are hard to read and should be subdivided into smaller functions. See http://programmers.stackexchange.com/questions/52685/if-you-need-more-than-3-levels-of-indentation-youre-screwed – Klas Lindbäck Feb 17 '14 at 12:26

1 Answers1

2

Program that simply counting

//>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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70