check whether a `case` statement has a `break` statement?

In my parser, I get a SwitchStmt, and I can traverse all case statements of it. I want to check whether a case statement has a break statement. But i can not figure it out. Here is my codes:

for (SwitchCase *c = stmt->getSwitchCaseList(); c != NULL; c = c->getNextSwitchCase()) {
if (isa(c)) {
CaseStmt *caseStmt = cast(c)
// I do not know how to check CaseStmt has a BreakStmt
}
}

Thanks.

BTW, I’m writing a static c code analyzer recently, this tool will check a number of c coding style rules for c codes. And because I’m a newbie to clang, so there will be more newbie questions coming, i’m sorry if i bother you guys.

For questions like this, it is usually easiest to just dump the AST
created for a snippet of sample code. See
<http://clang.llvm.org/docs/IntroductionToTheClangAST.html> for
information about how to do that.

-- Sean Silva

I guess you’re trying to do something similar to -Wimplicit-fallthrough diagnostics. If this is the case, take a look at http://llvm.org/svn/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp, DiagnoseSwitchLabelsFallthrough function. In short: you can use CFG (control-flow graph) to reason about execution paths, and, for example, detect if there’s an execution path between two case labels.

If you need something simpler, and just detect if there’s any ‘break’ somewhere between two case labels (I’m not sure I understand why you would need this, though), you can just use RecursiveASTVisitor and handle SwitchStmt, SwitchCase (which can be either CaseStmt or DefaultStmt) and BreakStmt.

Unfortunately, C does not work the way you think it does; a switch statement
simply opens a new statement scope, and case statements can be embedded
at arbitrary positions within those statements. This really is a control-flow
analysis, and you need to use the CFG.

John.

Make sure that you have a version of Duff's device in your test matrix for this.

  http://en.wikipedia.org/wiki/Duff’s_device

-- Marshall

Marshall Clow Idio Software <mailto:mclow.lists@gmail.com>

A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
        -- Yu Suzuki