From: Ted Kremenek Date: Tue, 18 Sep 2007 21:43:18 +0000 (+0000) Subject: More refactoring and code reduction. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=43a1698cfbdd6623737d72bdf6ab9a13cdeadd14;p=clang More refactoring and code reduction. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42109 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Analysis/UninitializedValues.cpp b/Analysis/UninitializedValues.cpp index 09f195e382..63c9e9f2f9 100644 --- a/Analysis/UninitializedValues.cpp +++ b/Analysis/UninitializedValues.cpp @@ -91,6 +91,8 @@ public: bool BlockStmt_VisitExpr(Expr* E); bool VisitDeclStmt(DeclStmt* D); + BlockVarDecl* FindBlockVarDecl(Stmt* S); + static inline bool Initialized() { return true; } static inline bool Uninitialized() { return false; } }; @@ -98,48 +100,47 @@ public: bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { if (BlockVarDecl* VD = dyn_cast(DR->getDecl())) { - assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl."); - if (AD.Observer) - AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD); + if (AD.Observer) AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD); - return V.DeclBV[ AD.VMap[VD] ]; + return V.getBitRef(VD,AD); } - else - return Initialized(); + else return Initialized(); +} + +BlockVarDecl* TransferFuncs::FindBlockVarDecl(Stmt *S) { + for (;;) { + if (ParenExpr* P = dyn_cast(S)) { + S = P->getSubExpr(); + continue; + } + else if (DeclRefExpr* DR = dyn_cast(S)) + if (BlockVarDecl* VD = dyn_cast(DR->getDecl())) + return VD; + + return NULL; + } } bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { - if (CFG::hasImplicitControlFlow(B)) { - assert ( AD.EMap.find(B) != AD.EMap.end() && "Unknown block-level expr."); - return V.ExprBV[ AD.EMap[B] ]; - } - - if (B->isAssignmentOp()) { - // Get the Decl for the LHS, if any - for (Stmt* S = B->getLHS() ;; ) { - if (ParenExpr* P = dyn_cast(S)) - S = P->getSubExpr(); - else if (DeclRefExpr* DR = dyn_cast(S)) - if (BlockVarDecl* VD = dyn_cast(DR->getDecl())) { - assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl."); - - if(InitWithAssigns) { - // Pseudo-hack to prevent cascade of warnings. If the RHS uses - // an uninitialized value, then we are already going to flag a warning - // related to the "cause". Thus, propogating uninitialized doesn't - // make sense, since we are just adding extra messages that don't - // contribute to diagnosing the bug. In InitWithAssigns mode - // we unconditionally set the assigned variable to Initialized to - // prevent Uninitialized propogation. - return V.DeclBV[AD.VMap[VD]] = Initialized(); - } - else - return V.DeclBV[ AD.VMap[VD] ] = Visit(B->getRHS()); - } - break; - } - } + if (CFG::hasImplicitControlFlow(B)) + return V.getBitRef(B,AD); + + if (B->isAssignmentOp()) + // Get the Decl for the LHS (if any). + if (BlockVarDecl* VD = FindBlockVarDecl(B->getLHS())) + if(InitWithAssigns) { + // Pseudo-hack to prevent cascade of warnings. If the RHS uses + // an uninitialized value, then we are already going to flag a warning + // for the RHS, or for the root "source" of the unintialized values. + // Thus, propogating uninitialized doesn't make sense, since we are + // just adding extra messages that don't + // contribute to diagnosing the bug. In InitWithAssigns mode + // we unconditionally set the assigned variable to Initialized to + // prevent Uninitialized propogation. + return V.getBitRef(VD,AD) = Initialized(); + } + else return V.getBitRef(VD,AD) = Visit(B->getRHS()); return VisitStmt(B); } @@ -150,12 +151,8 @@ bool TransferFuncs::VisitDeclStmt(DeclStmt* S) { for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator()) if (BlockVarDecl* VD = dyn_cast(D)) if (Stmt* I = VD->getInit()) { - assert ( AD.EMap.find(cast(I)) != - AD.EMap.end() && "Unknown Expr."); - - assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl."); - x = V.ExprBV[ AD.EMap[cast(I)] ]; - V.DeclBV[ AD.VMap[VD] ] = x; + x = V.getBitRef(cast(I),AD); + V.getBitRef(VD,AD) = x; } return x; @@ -168,27 +165,12 @@ bool TransferFuncs::VisitCallExpr(CallExpr* C) { bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { switch (U->getOpcode()) { - case UnaryOperator::AddrOf: { - // Blast through parentheses and find the decl (if any). Treat it - // as initialized from this point forward. - for (Stmt* S = U->getSubExpr() ;; ) - if (ParenExpr* P = dyn_cast(S)) - S = P->getSubExpr(); - else if (DeclRefExpr* DR = dyn_cast(S)) { - if (BlockVarDecl* VD = dyn_cast(DR->getDecl())) { - assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl."); - V.DeclBV[ AD.VMap[VD] ] = Initialized(); - } - break; - } - else { - // Evaluate the transfer function for subexpressions, even - // if we cannot reason more deeply about the &-expression. - return Visit(U->getSubExpr()); - } - - return Initialized(); - } + case UnaryOperator::AddrOf: + // For "&x", treat "x" as now being initialized. + if (BlockVarDecl* VD = FindBlockVarDecl(U->getSubExpr())) + V.getBitRef(VD,AD) = Initialized(); + else + return Visit(U->getSubExpr()); default: return Visit(U->getSubExpr()); @@ -202,15 +184,14 @@ bool TransferFuncs::VisitStmt(Stmt* S) { // evaluating some subexpressions may result in propogating "Uninitialized" // or "Initialized" to variables referenced in the other subexpressions. for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) - if (Visit(*I) == Uninitialized()) - x = Uninitialized(); + if (Visit(*I) == Uninitialized()) x = Uninitialized(); return x; } bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) { - assert ( AD.EMap.find(E) != AD.EMap.end() ); - return V.ExprBV[ AD.EMap[E] ] = Visit(E); + assert (AD.isTracked(E)); + return V.getBitRef(E,AD) = Visit(E); } } // end anonymous namespace @@ -233,9 +214,7 @@ namespace { struct Merge { void operator()(UninitializedValues::ValTy& Dst, UninitializedValues::ValTy& Src) { - assert (Dst.DeclBV.size() == Src.DeclBV.size() && "BV sizes do not match."); - assert (Dst.ExprBV.size() == Src.ExprBV.size() && "BV sizes do not match."); - + assert (Src.sizesEqual(Dst) && "BV sizes do not match."); Dst.DeclBV |= Src.DeclBV; Dst.ExprBV |= Src.ExprBV; } @@ -262,8 +241,9 @@ public: UninitializedValues::AnalysisDataTy& AD, DeclRefExpr* DR, BlockVarDecl* VD) { - assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl."); - if (V.DeclBV[ AD.VMap[VD] ] == TransferFuncs::Uninitialized()) + assert ( AD.isTracked(VD) && "Unknown VarDecl."); + + if (V.getBitRef(VD,AD) == TransferFuncs::Uninitialized()) if (AlreadyWarned.insert(VD)) Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val); } diff --git a/include/clang/Analysis/UninitializedValues.h b/include/clang/Analysis/UninitializedValues.h index 6fcee2dbf4..f27cdc46cd 100644 --- a/include/clang/Analysis/UninitializedValues.h +++ b/include/clang/Analysis/UninitializedValues.h @@ -23,7 +23,8 @@ namespace clang { class BlockVarDecl; class Expr; class DeclRefExpr; - + class VarDecl; + /// UninitializedValues_ValueTypes - Utility class to wrap type declarations /// for dataflow values and dataflow analysis state for the /// Unitialized Values analysis. @@ -45,6 +46,14 @@ public: ObserverTy* Observer; AnalysisDataTy() : NumDecls(0), NumBlockExprs(0), Observer(NULL) {} + + bool isTracked(const BlockVarDecl* VD) { + return VMap.find(VD) != VMap.end(); + } + + bool isTracked(const Expr* E) { + return EMap.find(E) != EMap.end(); + } }; //===--------------------------------------------------------------------===// @@ -69,7 +78,24 @@ public: void copyValues(ValTy& RHS) { DeclBV = RHS.DeclBV; ExprBV = RHS.ExprBV; - } + } + + llvm::BitVector::reference getBitRef(const BlockVarDecl* VD, + AnalysisDataTy& AD) { + assert (AD.isTracked(VD) && "BlockVarDecl not tracked."); + return DeclBV[AD.VMap[VD]]; + } + + llvm::BitVector::reference getBitRef(const Expr* E, + AnalysisDataTy& AD) { + assert (AD.isTracked(E) && "Expr not tracked."); + return DeclBV[AD.EMap[E]]; + } + + bool sizesEqual(ValTy& RHS) { + return DeclBV.size() == RHS.DeclBV.size() && + ExprBV.size() == RHS.ExprBV.size(); + } }; //===--------------------------------------------------------------------===//