From bb518991ce4298d8662235fc8cb13813f011c18d Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Sat, 18 May 2013 02:26:50 +0000 Subject: [PATCH] Revert "[analyzer; alternate edges] improve support for edges with PseudoObjectExprs." Ted and I spent a long time discussing this today and found out that neither the existing code nor the new code was doing what either of us thought it was, which is never good. The good news is we found a much simpler way to fix the motivating test case (an ObjCSubscriptExpr). This reverts r182083, but pieces of it will come back in subsequent commits. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182185 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/ParentMap.h | 3 +- include/clang/Analysis/AnalysisContext.h | 8 -- lib/AST/ParentMap.cpp | 30 +++--- lib/Analysis/AnalysisDeclContext.cpp | 30 ++---- lib/StaticAnalyzer/Core/BugReporter.cpp | 111 +---------------------- 5 files changed, 26 insertions(+), 156 deletions(-) diff --git a/include/clang/AST/ParentMap.h b/include/clang/AST/ParentMap.h index 9ec7753012..62eae02c15 100644 --- a/include/clang/AST/ParentMap.h +++ b/include/clang/AST/ParentMap.h @@ -20,9 +20,8 @@ class Expr; class ParentMap { void* Impl; - bool IsSemantic; public: - ParentMap(Stmt* ASTRoot, bool isSemantic = false); + ParentMap(Stmt* ASTRoot); ~ParentMap(); /// \brief Adds and/or updates the parent/child-relations of the complete diff --git a/include/clang/Analysis/AnalysisContext.h b/include/clang/Analysis/AnalysisContext.h index 1e6d70591c..46d7d07e09 100644 --- a/include/clang/Analysis/AnalysisContext.h +++ b/include/clang/Analysis/AnalysisContext.h @@ -82,7 +82,6 @@ class AnalysisDeclContext { bool builtCFG, builtCompleteCFG; OwningPtr PM; - OwningPtr SemanticPM; OwningPtr PCA; OwningPtr CFA; @@ -166,9 +165,6 @@ public: bool isCFGBuilt() const { return builtCFG; } ParentMap &getParentMap(); - - ParentMap &getSemanticParentMap(); - PseudoConstantAnalysis *getPseudoConstantAnalysis(); typedef const VarDecl * const * referenced_decls_iterator; @@ -249,10 +245,6 @@ public: return getAnalysisDeclContext()->getParentMap(); } - ParentMap &getSemanticParentMap() const { - return getAnalysisDeclContext()->getSemanticParentMap(); - } - const ImplicitParamDecl *getSelfDecl() const { return Ctx->getSelfDecl(); } diff --git a/lib/AST/ParentMap.cpp b/lib/AST/ParentMap.cpp index 735926d81d..113592860b 100644 --- a/lib/AST/ParentMap.cpp +++ b/lib/AST/ParentMap.cpp @@ -25,7 +25,7 @@ enum OpaqueValueMode { OV_Opaque }; -static void BuildParentMap(MapTy& M, Stmt* S, bool isSemantic, +static void BuildParentMap(MapTy& M, Stmt* S, OpaqueValueMode OVMode = OV_Transparent) { switch (S->getStmtClass()) { @@ -33,17 +33,14 @@ static void BuildParentMap(MapTy& M, Stmt* S, bool isSemantic, assert(OVMode == OV_Transparent && "Should not appear alongside OVEs"); PseudoObjectExpr *POE = cast(S); - if (!isSemantic) { - M[POE->getSyntacticForm()] = S; - BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent); - } + M[POE->getSyntacticForm()] = S; + BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent); for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(), E = POE->semantics_end(); I != E; ++I) { M[*I] = S; - BuildParentMap(M, *I, isSemantic, - isSemantic ? OV_Transparent : OV_Opaque); + BuildParentMap(M, *I, OV_Opaque); } break; } @@ -52,16 +49,16 @@ static void BuildParentMap(MapTy& M, Stmt* S, bool isSemantic, BinaryConditionalOperator *BCO = cast(S); M[BCO->getCommon()] = S; - BuildParentMap(M, BCO->getCommon(), isSemantic, OV_Transparent); + BuildParentMap(M, BCO->getCommon(), OV_Transparent); M[BCO->getCond()] = S; - BuildParentMap(M, BCO->getCond(), isSemantic, OV_Opaque); + BuildParentMap(M, BCO->getCond(), OV_Opaque); M[BCO->getTrueExpr()] = S; - BuildParentMap(M, BCO->getTrueExpr(), isSemantic, OV_Opaque); + BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque); M[BCO->getFalseExpr()] = S; - BuildParentMap(M, BCO->getFalseExpr(), isSemantic, OV_Transparent); + BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent); break; } @@ -69,25 +66,24 @@ static void BuildParentMap(MapTy& M, Stmt* S, bool isSemantic, if (OVMode == OV_Transparent) { OpaqueValueExpr *OVE = cast(S); M[OVE->getSourceExpr()] = S; - BuildParentMap(M, OVE->getSourceExpr(), isSemantic, OV_Transparent); + BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent); } break; default: for (Stmt::child_range I = S->children(); I; ++I) { if (*I) { M[*I] = S; - BuildParentMap(M, *I, isSemantic, OVMode); + BuildParentMap(M, *I, OVMode); } } break; } } -ParentMap::ParentMap(Stmt* S, bool isSemantic) : Impl(0), - IsSemantic(isSemantic) { +ParentMap::ParentMap(Stmt* S) : Impl(0) { if (S) { MapTy *M = new MapTy(); - BuildParentMap(*M, S, isSemantic); + BuildParentMap(*M, S); Impl = M; } } @@ -98,7 +94,7 @@ ParentMap::~ParentMap() { void ParentMap::addStmt(Stmt* S) { if (S) { - BuildParentMap(*(MapTy*) Impl, S, IsSemantic); + BuildParentMap(*(MapTy*) Impl, S); } } diff --git a/lib/Analysis/AnalysisDeclContext.cpp b/lib/Analysis/AnalysisDeclContext.cpp index 316892ea91..5ff7842407 100644 --- a/lib/Analysis/AnalysisDeclContext.cpp +++ b/lib/Analysis/AnalysisDeclContext.cpp @@ -212,34 +212,20 @@ void AnalysisDeclContext::dumpCFG(bool ShowColors) { getCFG()->dump(getASTContext().getLangOpts(), ShowColors); } -static ParentMap *constructParentMap(bool isSemantic, - Stmt *Body, - const Decl *D) { - ParentMap *PM = new ParentMap(Body, isSemantic); - if (const CXXConstructorDecl *C = dyn_cast(D)) { - for (CXXConstructorDecl::init_const_iterator I = C->init_begin(), - E = C->init_end(); - I != E; ++I) { - PM->addStmt((*I)->getInit()); - } - } - return PM; -} - ParentMap &AnalysisDeclContext::getParentMap() { if (!PM) { - PM.reset(constructParentMap(false, getBody(), getDecl())); + PM.reset(new ParentMap(getBody())); + if (const CXXConstructorDecl *C = dyn_cast(getDecl())) { + for (CXXConstructorDecl::init_const_iterator I = C->init_begin(), + E = C->init_end(); + I != E; ++I) { + PM->addStmt((*I)->getInit()); + } + } } return *PM; } -ParentMap &AnalysisDeclContext::getSemanticParentMap() { - if (!SemanticPM) { - SemanticPM.reset(constructParentMap(true, getBody(), getDecl())); - } - return *SemanticPM; -} - PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() { if (!PCA) PCA.reset(new PseudoConstantAnalysis(getBody())); diff --git a/lib/StaticAnalyzer/Core/BugReporter.cpp b/lib/StaticAnalyzer/Core/BugReporter.cpp index ab3464507d..519555396d 100644 --- a/lib/StaticAnalyzer/Core/BugReporter.cpp +++ b/lib/StaticAnalyzer/Core/BugReporter.cpp @@ -1873,78 +1873,9 @@ static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) { typedef llvm::DenseSet OptimizedCallsSet; -typedef llvm::DenseMap > - PseudoObjectExprMap; - -/// Return the PseudoObjectExpr that contains this statement (if any). -static const PseudoObjectExpr * -getContainingPseudoObjectExpr(PseudoObjectExprMap &PEM, - ParentMap &PM, - const Stmt *S) { - if (!S) - return 0; - - Optional &Entry = PEM[S]; - if (!Entry.hasValue()) { - const Stmt *Parent = PM.getParentIgnoreParens(S); - if (const PseudoObjectExpr *PE = dyn_cast_or_null(Parent)) - Entry = PE; - else - Entry = getContainingPseudoObjectExpr(PEM, PM, Parent); - } - return Entry.getValue(); -} - -#if 0 -static void printPath(PathPieces &path, ParentMap &PM) { - unsigned index = 0; - for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ++I ) { - llvm::errs() << "[" << index++ << "]\n"; - if (isa(*I)) { - llvm::errs() << " CALL\n"; - continue; - } - if (isa(*I)) { - llvm::errs() << " EVENT\n"; - continue; - } - if (const PathDiagnosticControlFlowPiece *CP = dyn_cast(*I)) { - llvm::errs() << " CONTROL\n"; - const Stmt *s1Start = getLocStmt(CP->getStartLocation()); - const Stmt *s1End = getLocStmt(CP->getEndLocation()); - if (s1Start) { - s1Start->dump(); - llvm::errs() << "PARENT: \n"; - const Stmt *Parent = getStmtParent(s1Start, PM); - if (Parent) { - Parent->dump(); - } - } - else { - llvm::errs() << "NULL\n"; - } - llvm::errs() << " --------- ===== ----- \n"; - if (s1End) { - s1End->dump(); - llvm::errs() << "PARENT: \n"; - const Stmt *Parent = getStmtParent(s1End, PM); - if (Parent) { - Parent->dump(); - } - } - else { - llvm::errs() << "NULL\n"; - } - } - } -} -#endif - static bool optimizeEdges(PathPieces &path, SourceManager &SM, OptimizedCallsSet &OCS, - LocationContextMap &LCM, - PseudoObjectExprMap &PEM) { + LocationContextMap &LCM) { bool hasChanges = false; const LocationContext *LC = LCM[&path]; assert(LC); @@ -1959,7 +1890,7 @@ static bool optimizeEdges(PathPieces &path, SourceManager &SM, // Record the fact that a call has been optimized so we only do the // effort once. if (!OCS.count(CallI)) { - while (optimizeEdges(CallI->path, SM, OCS, LCM, PEM)) {} + while (optimizeEdges(CallI->path, SM, OCS, LCM)) {} OCS.insert(CallI); } ++I; @@ -1975,7 +1906,7 @@ static bool optimizeEdges(PathPieces &path, SourceManager &SM, continue; } - ParentMap &PM = LC->getSemanticParentMap(); + ParentMap &PM = LC->getParentMap(); const Stmt *s1Start = getLocStmt(PieceI->getStartLocation()); const Stmt *s1End = getLocStmt(PieceI->getEndLocation()); const Stmt *level1 = getStmtParent(s1Start, PM); @@ -2002,39 +1933,6 @@ static bool optimizeEdges(PathPieces &path, SourceManager &SM, } } - // Prune out edges for pseudo object expressions. - // - // Case 1: incoming into a pseudo expr. - // - // An edge into a subexpression of a pseudo object expression - // should be replaced with an edge to the pseudo object expression - // itself. - const PseudoObjectExpr *PE = getContainingPseudoObjectExpr(PEM, PM, s1End); - if (PE) { - PathDiagnosticLocation L(PE, SM, LC); - PieceI->setEndLocation(L); - // Do not increment the iterator. It is possible we will match again. - hasChanges = true; - continue; - } - - // Prune out edges for pseudo object expressions. - // - // Case 2: outgoing from a pseudo expr. - // - // An edge into a subexpression of a pseudo object expression - // should be replaced with an edge to the pseudo object expression - // itself. - PE = getContainingPseudoObjectExpr(PEM, PM, s1Start); - if (PE) { - PathDiagnosticLocation L(PE, SM, LC); - PieceI->setStartLocation(L); - // Do not increment the iterator. It is possible we will match again. - hasChanges = true; - continue; - } - - // Pattern match on two edges after this point. PathPieces::iterator NextI = I; ++NextI; if (NextI == E) break; @@ -2884,8 +2782,7 @@ bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD, // to an aesthetically pleasing subset that conveys the // necessary information. OptimizedCallsSet OCS; - PseudoObjectExprMap PEM; - while (optimizeEdges(PD.getMutablePieces(), SM, OCS, LCM, PEM)) {} + while (optimizeEdges(PD.getMutablePieces(), SM, OCS, LCM)) {} // Adjust edges into loop conditions to make them more uniform // and aesthetically pleasing. -- 2.40.0