I want to find all the case statement having no break statement. I using clang-query to build my matcher. My matcher is failing in some of the test cases.
I wrote simple matcher as
match caseStmt(unless(has(breakStmt())))
it works with follwing test case
#include<stdlib.h>
int main(){
int x;
switch(x){
  case 1:
     break;
  case 2:
  default:
     x++;
}
return 0;
}
int main() 
{ 
    int x = 1, y = 2; 
    // Outer Switch 
    switch (x) { 
    // If x == 1 
    case 1: 
        // Nested Switch 
        switch (y) { 
        // If y == 2 
        case 2: 
            //break; 
        // If y == 3 
        case 3: 
            break; 
        } 
        break; 
    // If x == 4 
    case 4: 
        break; 
    // If x == 5 
    case 5: 
        break; 
    default: 
        break; 
    } 
    return 0; 
} 
 does not work well with following
does not work well with following 
#include <iostream> 
using namespace std; 
int main() 
{ 
    int x = 1, y = 2; 
    // Outer Switch 
    switch (x) { 
    // If x == 1 
    case 1: 
        // Nested Switch 
        switch (y) { 
        // If y == 2 
        case 2: 
            cout << "Choice is 2"; 
            //break; 
        // If y == 3 
        case 3: 
            cout << "Choice is 3"; 
            break; 
        } 
        //break; 
    // If x == 4 
    case 4: 
        cout << "Choice is 4"; 
        break; 
    // If x == 5 
    case 5: 
        cout << "Choice is 5"; 
        break; 
    default: 
        cout << "Choice is other than 1, 2 3, 4, or 5"; 
        break; 
    } 
    return 0; 
} 
 In above case it shows case statement that are having break statement along with case statement with no break statement.
In above case it shows case statement that are having break statement along with case statement with no break statement.
what wrong i am doing ? please help :) I am following this http://releases.llvm.org/8.0.0/tools/clang/docs/LibASTMatchersTutorial.html

 
                        
Unfortunately this is not going to work :-(
caseis technically alabel, andlabelhas only one statement as its child. If you print out AST you'll see thatcaseandbreakstatements will be at the same level:Here you can see that
CallExpris a child ofCaseStmtwhileBreakStmtis not.NOTE: to make example a bit easier I replaced
std::cout << "..."withfoo().You'll have to write a much more complex matcher that fetches for
casesthat don't havebreakstatements between them and the followingcases.I hope this is still helpful.