[cfe-dev] check whether a `case` statement has a `break` statement?

Alexander Kornienko alexfh at google.com
Tue Jan 1 18:17:41 PST 2013


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.

On Tue, Jan 1, 2013 at 3:30 PM, kevinlynx <kevinlynx at gmail.com> wrote:

> 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<CaseStmt>(c)) {
>                 CaseStmt *caseStmt = cast<CaseStmt>(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.
>
>

-- 
Regards,
Alexander Kornienko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130102/2e9bea6a/attachment.html>


More information about the cfe-dev mailing list