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

Matthieu Monrocq matthieu.monrocq at gmail.com
Wed Jan 2 09:36:09 PST 2013


You do realize that if someone used a "return" or a "throw" then you will
miss this ?

-- Matthieu

On Wed, Jan 2, 2013 at 10:15 AM, kevinlynx <kevinlynx at gmail.com> wrote:

> I only want to know if there's a break between two cases (or between a
> case and default). I dump a CastStmt AST, but not found a BreakStmt. That's
> why i asked for help here.
>
>
> ------------------ Original ------------------
> *From: * "Alexander Kornienko"<alexfh at google.com>;
> *Date: * Wed, Jan 2, 2013 10:17 AM
> *To: * "kevinlynx"<kevinlynx at gmail.com>; **
> *Cc: * "cfe-dev"<cfe-dev at cs.uiuc.edu>; **
> *Subject: * Re: [cfe-dev] check whether a `case` statement has a `break`
> statement?
>
> 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
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130102/2da33fa3/attachment.html>


More information about the cfe-dev mailing list