[cfe-commits] r90424 - in /cfe/trunk: examples/wpa/clang-wpa.cpp include/clang/Analysis/CallGraph.h include/clang/Index/CallGraph.h lib/Analysis/CallGraph.cpp lib/Index/CallGraph.cpp

Daniel Dunbar daniel at zuster.org
Wed Dec 2 23:20:04 PST 2009


Author: ddunbar
Date: Thu Dec  3 01:20:04 2009
New Revision: 90424

URL: http://llvm.org/viewvc/llvm-project?rev=90424&view=rev
Log:
Fix layering violation by moving Analysis/CallGraph to Index

Added:
    cfe/trunk/include/clang/Index/CallGraph.h
      - copied, changed from r90422, cfe/trunk/include/clang/Analysis/CallGraph.h
    cfe/trunk/lib/Index/CallGraph.cpp
      - copied, changed from r90422, cfe/trunk/lib/Analysis/CallGraph.cpp
Removed:
    cfe/trunk/include/clang/Analysis/CallGraph.h
    cfe/trunk/lib/Analysis/CallGraph.cpp
Modified:
    cfe/trunk/examples/wpa/clang-wpa.cpp

Modified: cfe/trunk/examples/wpa/clang-wpa.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/wpa/clang-wpa.cpp?rev=90424&r1=90423&r2=90424&view=diff

==============================================================================
--- cfe/trunk/examples/wpa/clang-wpa.cpp (original)
+++ cfe/trunk/examples/wpa/clang-wpa.cpp Thu Dec  3 01:20:04 2009
@@ -12,11 +12,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/Analysis/CallGraph.h"
-#include "clang/Frontend/ASTUnit.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Frontend/ASTUnit.h"
 #include "clang/Frontend/CompilerInstance.h"
+#include "clang/Index/CallGraph.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;

Removed: cfe/trunk/include/clang/Analysis/CallGraph.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/CallGraph.h?rev=90423&view=auto

==============================================================================
--- cfe/trunk/include/clang/Analysis/CallGraph.h (original)
+++ cfe/trunk/include/clang/Analysis/CallGraph.h (removed)
@@ -1,146 +0,0 @@
-//== CallGraph.cpp - Call graph building ------------------------*- C++ -*--==//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-//  This file defined the CallGraph and CallGraphNode classes.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_CLANG_ANALYSIS_CALLGRAPH
-#define LLVM_CLANG_ANALYSIS_CALLGRAPH
-
-#include "clang/Index/ASTLocation.h"
-#include "clang/Index/Entity.h"
-#include "clang/Index/Program.h"
-#include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/GraphTraits.h"
-#include "llvm/ADT/STLExtras.h"
-#include <vector>
-#include <map>
-
-namespace clang {
-
-class CallGraphNode {
-  idx::Entity F;
-  typedef std::pair<idx::ASTLocation, CallGraphNode*> CallRecord;
-  std::vector<CallRecord> CalledFunctions;
-
-public:
-  CallGraphNode(idx::Entity f) : F(f) {}
-
-  typedef std::vector<CallRecord>::iterator iterator;
-  typedef std::vector<CallRecord>::const_iterator const_iterator;
-
-  iterator begin() { return CalledFunctions.begin(); }
-  iterator end()   { return CalledFunctions.end(); }
-  const_iterator begin() const { return CalledFunctions.begin(); }
-  const_iterator end()   const { return CalledFunctions.end();   }
-
-  void addCallee(idx::ASTLocation L, CallGraphNode *Node) {
-    CalledFunctions.push_back(std::make_pair(L, Node));
-  }
-
-  bool hasCallee() const { return begin() != end(); }
-
-  std::string getName() const { return F.getPrintableName(); }
-
-  Decl *getDecl(ASTContext &Ctx) const { return F.getDecl(Ctx); }
-};
-
-class CallGraph {
-  /// Program manages all Entities.
-  idx::Program Prog;
-
-  typedef std::map<idx::Entity, CallGraphNode *> FunctionMapTy;
-
-  /// FunctionMap owns all CallGraphNodes.
-  FunctionMapTy FunctionMap;
-
-  /// CallerCtx maps a caller to its ASTContext.
-  llvm::DenseMap<CallGraphNode *, ASTContext *> CallerCtx;
-
-  /// Root node is the 'main' function or 0.
-  CallGraphNode *Root;
-
-  /// ExternalCallingNode has edges to all external functions.
-  CallGraphNode *ExternalCallingNode;
-
-public:
-  CallGraph();
-  ~CallGraph();
-
-  typedef FunctionMapTy::iterator iterator;
-  typedef FunctionMapTy::const_iterator const_iterator;
-
-  iterator begin() { return FunctionMap.begin(); }
-  iterator end()   { return FunctionMap.end();   }
-  const_iterator begin() const { return FunctionMap.begin(); }
-  const_iterator end()   const { return FunctionMap.end();   }
-
-  CallGraphNode *getRoot() { return Root; }
-
-  CallGraphNode *getExternalCallingNode() { return ExternalCallingNode; }
-
-  void addTU(ASTContext &AST);
-
-  idx::Program &getProgram() { return Prog; }
-
-  CallGraphNode *getOrInsertFunction(idx::Entity F);
-
-  Decl *getDecl(CallGraphNode *Node);
-
-  void print(llvm::raw_ostream &os);
-  void dump();
-
-  void ViewCallGraph() const;
-};
-
-} // end clang namespace
-
-namespace llvm {
-
-template <> struct GraphTraits<clang::CallGraph> {
-  typedef clang::CallGraph GraphType;
-  typedef clang::CallGraphNode NodeType;
-
-  typedef std::pair<clang::idx::ASTLocation, NodeType*> CGNPairTy;
-  typedef std::pointer_to_unary_function<CGNPairTy, NodeType*> CGNDerefFun;
-
-  typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
-
-  static NodeType *getEntryNode(GraphType *CG) {
-    return CG->getExternalCallingNode();
-  }
-
-  static ChildIteratorType child_begin(NodeType *N) {
-    return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
-  }
-  static ChildIteratorType child_end(NodeType *N) {
-    return map_iterator(N->end(), CGNDerefFun(CGNDeref));
-  }
-
-  typedef std::pair<clang::idx::Entity, NodeType*> PairTy;
-  typedef std::pointer_to_unary_function<PairTy, NodeType*> DerefFun;
-
-  typedef mapped_iterator<GraphType::const_iterator, DerefFun> nodes_iterator;
-
-  static nodes_iterator nodes_begin(const GraphType &CG) {
-    return map_iterator(CG.begin(), DerefFun(CGDeref));
-  }
-  static nodes_iterator nodes_end(const GraphType &CG) {
-    return map_iterator(CG.end(), DerefFun(CGDeref));
-  }
-
-  static NodeType *CGNDeref(CGNPairTy P) { return P.second; }
-
-  static NodeType *CGDeref(PairTy P) { return P.second; }
-};
-
-} // end llvm namespace
-
-#endif

Copied: cfe/trunk/include/clang/Index/CallGraph.h (from r90422, cfe/trunk/include/clang/Analysis/CallGraph.h)
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/CallGraph.h?p2=cfe/trunk/include/clang/Index/CallGraph.h&p1=cfe/trunk/include/clang/Analysis/CallGraph.h&r1=90422&r2=90424&rev=90424&view=diff

==============================================================================
    (empty)

Removed: cfe/trunk/lib/Analysis/CallGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CallGraph.cpp?rev=90423&view=auto

==============================================================================
--- cfe/trunk/lib/Analysis/CallGraph.cpp (original)
+++ cfe/trunk/lib/Analysis/CallGraph.cpp (removed)
@@ -1,150 +0,0 @@
-//== CallGraph.cpp - Call graph building ------------------------*- C++ -*--==//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-//  This file defined the CallGraph and CGBuilder classes.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang/Analysis/CallGraph.h"
-
-#include "clang/AST/ASTContext.h"
-#include "clang/AST/StmtVisitor.h"
-
-#include "llvm/Support/GraphWriter.h"
-
-using namespace clang;
-using namespace idx;
-
-namespace {
-class CGBuilder : public StmtVisitor<CGBuilder> {
-
-  CallGraph &G;
-  FunctionDecl *FD;
-
-  Entity CallerEnt;
-
-  CallGraphNode *CallerNode;
-
-public:
-  CGBuilder(CallGraph &g, FunctionDecl *fd, Entity E, CallGraphNode *N)
-    : G(g), FD(fd), CallerEnt(E), CallerNode(N) {}
-
-  void VisitStmt(Stmt *S) { VisitChildren(S); }
-
-  void VisitCallExpr(CallExpr *CE);
-
-  void VisitChildren(Stmt *S) {
-    for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I != E;++I)
-      if (*I)
-        static_cast<CGBuilder*>(this)->Visit(*I);
-  }
-};
-}
-
-void CGBuilder::VisitCallExpr(CallExpr *CE) {
-  if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) {
-    Entity Ent = Entity::get(CalleeDecl, G.getProgram());
-    CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent);
-    CallerNode->addCallee(ASTLocation(FD, CE), CalleeNode);
-  }
-}
-
-CallGraph::CallGraph() : Root(0) {
-  ExternalCallingNode = getOrInsertFunction(Entity());
-}
-
-CallGraph::~CallGraph() {
-  if (!FunctionMap.empty()) {
-    for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
-        I != E; ++I)
-      delete I->second;
-    FunctionMap.clear();
-  }
-}
-
-void CallGraph::addTU(ASTContext& Ctx) {
-  DeclContext *DC = Ctx.getTranslationUnitDecl();
-  for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
-       I != E; ++I) {
-
-    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
-      if (FD->isThisDeclarationADefinition()) {
-        // Set caller's ASTContext.
-        Entity Ent = Entity::get(FD, Prog);
-        CallGraphNode *Node = getOrInsertFunction(Ent);
-        CallerCtx[Node] = &Ctx;
-
-        // If this function has external linkage, anything could call it.
-        if (FD->isGlobal())
-          ExternalCallingNode->addCallee(idx::ASTLocation(), Node);
-
-        // Set root node to 'main' function.
-        if (FD->getNameAsString() == "main")
-          Root = Node;
-
-        CGBuilder builder(*this, FD, Ent, Node);
-        builder.Visit(FD->getBody());
-      }
-    }
-  }
-}
-
-CallGraphNode *CallGraph::getOrInsertFunction(Entity F) {
-  CallGraphNode *&Node = FunctionMap[F];
-  if (Node)
-    return Node;
-
-  return Node = new CallGraphNode(F);
-}
-
-Decl *CallGraph::getDecl(CallGraphNode *Node) {
-  // Get the function's context.
-  ASTContext *Ctx = CallerCtx[Node];
-
-  return Node->getDecl(*Ctx);
-}
-
-void CallGraph::print(llvm::raw_ostream &os) {
-  for (iterator I = begin(), E = end(); I != E; ++I) {
-    if (I->second->hasCallee()) {
-      os << "function: " << I->first.getPrintableName()
-         << " calls:\n";
-      for (CallGraphNode::iterator CI = I->second->begin(),
-             CE = I->second->end(); CI != CE; ++CI) {
-        os << "    " << CI->second->getName();
-      }
-      os << '\n';
-    }
-  }
-}
-
-void CallGraph::dump() {
-  print(llvm::errs());
-}
-
-void CallGraph::ViewCallGraph() const {
-  llvm::ViewGraph(*this, "CallGraph");
-}
-
-namespace llvm {
-
-template <>
-struct DOTGraphTraits<CallGraph> : public DefaultDOTGraphTraits {
-
-  DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
-
-  static std::string getNodeLabel(const CallGraphNode *Node,
-                                  const CallGraph &CG) {
-    return Node->getName();
-
-  }
-
-};
-
-}

Copied: cfe/trunk/lib/Index/CallGraph.cpp (from r90422, cfe/trunk/lib/Analysis/CallGraph.cpp)
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/CallGraph.cpp?p2=cfe/trunk/lib/Index/CallGraph.cpp&p1=cfe/trunk/lib/Analysis/CallGraph.cpp&r1=90422&r2=90424&rev=90424&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CallGraph.cpp (original)
+++ cfe/trunk/lib/Index/CallGraph.cpp Thu Dec  3 01:20:04 2009
@@ -11,7 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/Analysis/CallGraph.h"
+#include "clang/Index/CallGraph.h"
 
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/StmtVisitor.h"





More information about the cfe-commits mailing list