Search code examples
pythonclangllvm-clang

how to catch for/while statement with clang in python


I calculate C/C++ code's cyclomatic complexity with clang in python module.

And, I caught almost statement but, I can't catch several statement that don't

have a brace.

example, if below statement can catch and calculate cyclomatic complexity.

if (i == 1 && k ==2)
{
    cout << 'q' << endl;
}

but, below statement can't catch.

if (i == 1 && k ==2)
    cout << 'q' << endl;

please, tell me how to catch that statement.


Solution

  • The only block I can think of that is not necessarily brace-enclosed (except for goto but we won't go in to that) would be the cases of a switch/case block. In that case you could check for delimiters. Unlike python, you don't have indentation check. So either the block has one line of code or it is brace-enclosed.

    To add extra ugliness, do you currently check for lines like

    if (my_condition_1) ++x;
    else if (my_condition_2) --x;
    

    ?

    Yes, they're ugly, and you'll usually be quickly poo-pooed out of doing one of these. But nevertheless, coding styles like that are possible.

    (All suggestions under the condition that I understand correctly what kind of information you're trying to get. +1 for the clarification request)