From 589493227bea37357a1abbd69568ac8bb64cea37 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 24 Dec 2009 00:40:03 +0000 Subject: [PATCH] Teach GRExprEngine to handle the initialization of the condition variable of a SwitchStmt. llvm-svn: 92102 --- .../Analysis/PathSensitive/GRExprEngine.h | 8 ++++---- clang/lib/Analysis/GRExprEngine.cpp | 18 +++++++++++------- clang/test/Analysis/misc-ps-region-store.cpp | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/Analysis/PathSensitive/GRExprEngine.h b/clang/include/clang/Analysis/PathSensitive/GRExprEngine.h index 6aa4be345688..3f5e16532ed4 100644 --- a/clang/include/clang/Analysis/PathSensitive/GRExprEngine.h +++ b/clang/include/clang/Analysis/PathSensitive/GRExprEngine.h @@ -293,10 +293,10 @@ protected: void VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R, ExplodedNode* Pred, ExplodedNodeSet& Dst); - /// VisitIfStmtCondInit - Transfer function for handling the initialization - /// of a condition variable in an IfStmt. - void VisitIfStmtCondInit(IfStmt *IS, ExplodedNode *Pred, - ExplodedNodeSet& Dst); + /// VisitCondInit - Transfer function for handling the initialization + /// of a condition variable in an IfStmt, SwitchStmt, etc. + void VisitCondInit(VarDecl *VD, Stmt *S, ExplodedNode *Pred, + ExplodedNodeSet& Dst); void VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred, ExplodedNodeSet& Dst); diff --git a/clang/lib/Analysis/GRExprEngine.cpp b/clang/lib/Analysis/GRExprEngine.cpp index ceefea81f5c1..d7486f85134c 100644 --- a/clang/lib/Analysis/GRExprEngine.cpp +++ b/clang/lib/Analysis/GRExprEngine.cpp @@ -665,7 +665,7 @@ void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) { case Stmt::IfStmtClass: // This case isn't for branch processing, but for handling the // initialization of a condition variable. - VisitIfStmtCondInit(cast(S), Pred, Dst); + VisitCondInit(cast(S)->getConditionVariable(), S, Pred, Dst); break; case Stmt::InitListExprClass: @@ -733,6 +733,12 @@ void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) { case Stmt::StringLiteralClass: VisitLValue(cast(S), Pred, Dst); break; + + case Stmt::SwitchStmtClass: + // This case isn't for branch processing, but for handling the + // initialization of a condition variable. + VisitCondInit(cast(S)->getConditionVariable(), S, Pred, Dst); + break; case Stmt::UnaryOperatorClass: { UnaryOperator *U = cast(S); @@ -2230,12 +2236,10 @@ void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred, } } -void GRExprEngine::VisitIfStmtCondInit(IfStmt *IS, ExplodedNode *Pred, - ExplodedNodeSet& Dst) { - - VarDecl* VD = IS->getConditionVariable(); - Expr* InitEx = VD->getInit(); +void GRExprEngine::VisitCondInit(VarDecl *VD, Stmt *S, + ExplodedNode *Pred, ExplodedNodeSet& Dst) { + Expr* InitEx = VD->getInit(); ExplodedNodeSet Tmp; Visit(InitEx, Pred, Tmp); @@ -2255,7 +2259,7 @@ void GRExprEngine::VisitIfStmtCondInit(IfStmt *IS, ExplodedNode *Pred, Builder->getCurrentBlockCount()); } - EvalBind(Dst, IS, IS, N, state, + EvalBind(Dst, S, S, N, state, loc::MemRegionVal(state->getRegion(VD, LC)), InitVal, true); } } diff --git a/clang/test/Analysis/misc-ps-region-store.cpp b/clang/test/Analysis/misc-ps-region-store.cpp index 7b911a5c82de..e9d6d938710f 100644 --- a/clang/test/Analysis/misc-ps-region-store.cpp +++ b/clang/test/Analysis/misc-ps-region-store.cpp @@ -41,3 +41,21 @@ int test_init_in_condition() { } return 0; } + +int test_init_in_condition_switch() { + switch (int x = test_init_in_condition_aux()) { // no-warning + case 1: + return 0; + case 2: + if (x == 2) + return 0; + else { + // Unreachable. + int *p = 0; + *p = 0xDEADBEEF; // no-warning + } + default: + break; + } + return 0; +}