: Clang Static Code Analyzer does not report 'Switch' Statement as a branch condition.

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.


Prashant S. Purohit

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/, or alternately submit a patch to cfe-commits@cs.uiuc.edu for review (and CC me or Anna).

Sorry for the trouble,

* 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.

Hi Jordan,

Thanks for the reply.

As suggested by you, I tried getting the switch statement using check::PreStmt

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;





return 0;


It is not printing “Switch Statement found” on the console for above code.

Kindly suggest if I am missing something.


Prashant S. Purohit

Right; what I meant was that we'd prefer to make this work rather than adding switch statements to checkBranchCondition, but right now neither one works. Sorry for the inconvenience.