From: Jordan Rose Date: Wed, 20 Mar 2013 00:35:31 +0000 (+0000) Subject: [analyzer] Break cycles (optionally) when trimming an ExplodedGraph. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=34e19a1d1d241c9b4d1030d5e4db530fee3e5a37;p=platform%2Fupstream%2Fllvm.git [analyzer] Break cycles (optionally) when trimming an ExplodedGraph. Having a trimmed graph with no cycles (a DAG) is much more convenient for trying to find shortest paths, which is exactly what BugReporter needs to do. Part of the performance work for . llvm-svn: 177468 --- diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h index 5211916..208c12b 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h @@ -363,12 +363,16 @@ public: /// /// \param Nodes The nodes which must appear in the final graph. Presumably /// these are end-of-path nodes (i.e. they have no successors). + /// \param BreakCycles Whether or not the trimmed graph should make an effort + /// to eliminate cycles. Note that this may result in some + /// unnecessary nodes being included in the final graph + /// (i.e. nodes that would have only appeared in a cycle). /// \param[out] ForwardMap A optional map from nodes in this graph to nodes in /// the returned graph. /// \param[out] InverseMap An optional map from nodes in the returned graph to /// nodes in this graph. /// \returns The trimmed graph - ExplodedGraph *trim(ArrayRef Nodes, + ExplodedGraph *trim(ArrayRef Nodes, bool BreakCycles = false, InterExplodedGraphMap *ForwardMap = 0, InterExplodedGraphMap *InverseMap = 0) const; diff --git a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp index cc67343..51fdab5 100644 --- a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp +++ b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp @@ -1895,7 +1895,8 @@ public: ArrayRef Nodes) { // The trimmed graph is created in the body of the constructor to ensure // that the DenseMaps have been initialized already. - G.reset(OriginalGraph->trim(Nodes, &ForwardMap, &InverseMap)); + G.reset(OriginalGraph->trim(Nodes, /*BreakCycles=*/true, + &ForwardMap, &InverseMap)); } void createBestReportGraph(ArrayRef Nodes, diff --git a/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp b/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp index ca466d8..f9d4345 100644 --- a/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp @@ -331,8 +331,22 @@ ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L, return V; } +static bool isDescendent(const ExplodedNode *Parent, const ExplodedNode *Child){ + SmallVector WL; + WL.push_back(Parent); + + while (!WL.empty()) { + const ExplodedNode *N = WL.pop_back_val(); + if (N == Child) + return true; + WL.append(N->succ_begin(), N->succ_end()); + } + + return false; +} + ExplodedGraph * -ExplodedGraph::trim(ArrayRef Sinks, +ExplodedGraph::trim(ArrayRef Sinks, bool BreakCycles, InterExplodedGraphMap *ForwardMap, InterExplodedGraphMap *InverseMap) const{ @@ -429,7 +443,8 @@ ExplodedGraph::trim(ArrayRef Sinks, I != E; ++I) { Pass2Ty::iterator PI = Pass2.find(*I); if (PI != Pass2.end()) { - const_cast(PI->second)->addPredecessor(NewN, *G); + if (!BreakCycles || !isDescendent(PI->second, NewN)) + const_cast(PI->second)->addPredecessor(NewN, *G); continue; }