From: Ted Kremenek Date: Wed, 23 Apr 2008 23:02:12 +0000 (+0000) Subject: When building PathDiagnostics for bug reports, generate a trimmed ExplodedGraph with... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a43a1eb6e7c773b79898541794bf819601719493;p=clang When building PathDiagnostics for bug reports, generate a trimmed ExplodedGraph with a single path that BugReport objects can safely walk and introspect. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@50194 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Analysis/PathSensitive/ExplodedGraph.h b/include/clang/Analysis/PathSensitive/ExplodedGraph.h index 0d8f31fb82..39a064d4c6 100644 --- a/include/clang/Analysis/PathSensitive/ExplodedGraph.h +++ b/include/clang/Analysis/PathSensitive/ExplodedGraph.h @@ -167,6 +167,10 @@ public: Profile(ID, getLocation(), getState()); } + void addPredecessor(ExplodedNode* V) { + ExplodedNodeImpl::addPredecessor(V); + } + // Iterators over successor and predecessor vertices. typedef ExplodedNode** succ_iterator; typedef const ExplodedNode** const_succ_iterator; @@ -270,7 +274,8 @@ public: llvm::BumpPtrAllocator& getAllocator() { return Allocator; } CFG& getCFG() { return cfg; } ASTContext& getContext() { return Ctx; } - + + Decl& getCodeDecl() { return CodeDecl; } const Decl& getCodeDecl() const { return CodeDecl; } const FunctionDecl* getFunctionDecl() const { diff --git a/lib/Analysis/BugReporter.cpp b/lib/Analysis/BugReporter.cpp index 68b6ad76be..d137cff3e7 100644 --- a/lib/Analysis/BugReporter.cpp +++ b/lib/Analysis/BugReporter.cpp @@ -139,35 +139,66 @@ PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode* N, return NULL; } -void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, - BugReport& R) { - - ExplodedNode* N = R.getEndNode(); - - if (!N) - return; +static std::pair*, ExplodedNode*> +MakeReportGraph(ExplodedGraph* G, ExplodedNode* N) { - llvm::OwningPtr > GTrim(getGraph().Trim(&N, &N+1)); - - // Find the sink in the trimmed graph. - // FIXME: Should we eventually have a sink iterator? + llvm::OwningPtr > GTrim(G->Trim(&N, &N+1)); + + // Find the sink node in the trimmed graph. - ExplodedNode* NewN = 0; + N = NULL; for (ExplodedGraph::node_iterator I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) { if (I->isSink()) { - NewN = &*I; + N = &*I; break; } } - assert (NewN); - assert (NewN->getLocation() == N->getLocation()); - - N = NewN; + assert(N); + + // Create a new graph with a single path. + + G = new ExplodedGraph(GTrim->getCFG(), GTrim->getCodeDecl(), + GTrim->getContext()); + + + ExplodedNode* Last = 0; + + while (N) { + ExplodedNode* NewN = + G->getNode(N->getLocation(), N->getState()); + + if (Last) Last->addPredecessor(NewN); + Last = NewN; + N = N->pred_empty() ? 0 : *(N->pred_begin()); + } + + return std::make_pair(G, Last); +} + +void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, + BugReport& R) { + + ExplodedNode* N = R.getEndNode(); + + if (!N) return; + + // Construct a new graph that contains only a single path from the error + // node to a root. + + const std::pair*,ExplodedNode*> + GPair = MakeReportGraph(&getGraph(), N); + + llvm::OwningPtr > ReportGraph(GPair.first); + assert(GPair.second->getLocation() == N->getLocation()); + N = GPair.second; + + // Start building the path diagnostic... + if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N)) PD.push_back(Piece); else @@ -350,7 +381,8 @@ void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, } } else - if (PathDiagnosticPiece* piece = R.VisitNode(N, NextNode, *GTrim, *this)) + if (PathDiagnosticPiece* piece = R.VisitNode(N, NextNode, + *ReportGraph, *this)) PD.push_front(piece); } }