[cfe-dev] : Clang Static Code Analyzer does not report 'Switch' Statement as a branch condition.

Prashant Purohit Prashant.Purohit at kpit.com
Fri Dec 6 04:24:55 PST 2013


Hi Jordan,

Thanks for the reply.
As suggested by you, I tried getting the switch statement using check::PreStmt<SwitchStmt>
But it is still failing, please find the below code which I added to get switch statement:

void SimpleChecker::checkPreStmt(const SwitchStmt *SW, CheckerContext &C ) const {
  // Print when a switch statement is found.
  llvm::errs() << "Switch Statement found.\n";
}

The above implementation is failing for a simple 'C' code mentioned below:
int main() {
    int var1 = 1, var2 = 2;
    switch(var1) {
        case 1:
            var1 = var2 + var1;
            break;
        default:
            break;
    }
    return 0;
}
It is not printing "Switch Statement found" on the console for above code.

Kindly suggest if I am missing something.

Thanks,
Prashant S. Purohit

From: Jordan Rose [mailto:jordan_rose at apple.com]
Sent: 03 December 2013 23:02
To: Prashant Purohit
Cc: cfe-dev at cs.uiuc.edu; Anna Zaks
Subject: Re: [cfe-dev] : Clang Static Code Analyzer does not report 'Switch' Statement as a branch condition.

Hi, Prashant. check::BranchCondition is only intended to be used for boolean branches, since the first argument is the conditional expression used for branching.* With a switch statement, that no longer makes sense. That said, you're right that there's currently no callback for a switch statement. The right thing to do here is probably to run pre-statement checks on the terminator statement in the CFG when it's not a logical expression or conditional expression, so that the nomal check::PreStmt<SwitchStmt> would work.

Please file a bug at llvm.org/bugs/<http://llvm.org/bugs/>, or alternately submit a patch to cfe-commits at cs.uiuc.edu<mailto:cfe-commits at cs.uiuc.edu> for review (and CC me or Anna).

Sorry for the trouble,
Jordan

* Implementation detail: it's a Stmt, not an Expr, because we stash the branch info for an Objective-C for-in loop on the loop statement itself. But it's still a boolean value.



On Nov 28, 2013, at 5:04 , Prashant Purohit <Prashant.Purohit at kpit.com<mailto:Prashant.Purohit at kpit.com>> wrote:


Hi all,

I am new to Clang Static Code analyzer internals.

Currently, I am trying to get all the branch conditions in a function using 'check::BranchCondition' call back function.
I am successful in detecting all the branch conditions like if, while, do..while, for and ternary operator condition except switch condition.

I have checked this with already existing checks (e.g.- DumpTraversal) which uses 'check::BranchCondition' call back function, these checks
also do not detect 'switch' statements.

Kindly help me in getting switch statements using 'check::BranchCondition' call back function.

Thanks,
Prashant S. Purohit
This message contains information that may be privileged or confidential and is the property of the KPIT Technologies Ltd. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message. KPIT Technologies Ltd. does not accept any liability for virus infected mails. _______________________________________________
cfe-dev mailing list
cfe-dev at cs.uiuc.edu<mailto:cfe-dev at cs.uiuc.edu>
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

This message contains information that may be privileged or confidential and is the property of the KPIT Technologies Ltd. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message. KPIT Technologies Ltd. does not accept any liability for virus infected mails.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131206/0c3cc887/attachment.html>


More information about the cfe-dev mailing list