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

Anna Zaks ganna at apple.com
Fri Mar 2 14:54:36 PST 2012


Author: zaks
Date: Fri Mar  2 16:54:36 2012
New Revision: 151950

URL: http://llvm.org/viewvc/llvm-project?rev=151950&view=rev
Log:
[analyzer] Rename clang::CallGraph into clang::idx::CallGraph + rename
the corresponding files to avoid confusion.

This is a preparation to adding an AST-based call graph to Analysis. The
existing call graph works with indexer entries. We might be able to
refactor it to use the AST based graph in the future.

(Minimal testing here as the only example that uses the API has been
completely broken, does not compile.)

Added:
    cfe/trunk/include/clang/Index/GlobalCallGraph.h
      - copied, changed from r151947, cfe/trunk/include/clang/Index/CallGraph.h
    cfe/trunk/lib/Index/GlobalCallGraph.cpp
      - copied, changed from r151947, cfe/trunk/lib/Index/CallGraph.cpp
Removed:
    cfe/trunk/include/clang/Index/CallGraph.h
    cfe/trunk/lib/Index/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=151950&r1=151949&r2=151950&view=diff
==============================================================================
--- cfe/trunk/examples/wpa/clang-wpa.cpp (original)
+++ cfe/trunk/examples/wpa/clang-wpa.cpp Fri Mar  2 16:54:36 2012
@@ -22,7 +22,7 @@
 #include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Frontend/CompilerInstance.h"
-#include "clang/Index/CallGraph.h"
+#include "clang/Index/GlobalCallGraph.h"
 #include "clang/Index/Indexer.h"
 #include "clang/Index/TranslationUnit.h"
 #include "clang/Index/DeclReferenceMap.h"
@@ -104,8 +104,8 @@
   }
 
   if (ViewCallGraph) {
-    OwningPtr<CallGraph> CG;
-    CG.reset(new CallGraph(Prog));
+    OwningPtr<clang::idx::CallGraph> CG;
+    CG.reset(new clang::idx::CallGraph(Prog));
 
     for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i)
       CG->addTU(ASTUnits[i]->getASTContext());

Removed: cfe/trunk/include/clang/Index/CallGraph.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/CallGraph.h?rev=151949&view=auto
==============================================================================
--- cfe/trunk/include/clang/Index/CallGraph.h (original)
+++ cfe/trunk/include/clang/Index/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(idx::Program &P);
-  ~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(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/GlobalCallGraph.h (from r151947, cfe/trunk/include/clang/Index/CallGraph.h)
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/GlobalCallGraph.h?p2=cfe/trunk/include/clang/Index/GlobalCallGraph.h&p1=cfe/trunk/include/clang/Index/CallGraph.h&r1=151947&r2=151950&rev=151950&view=diff
==============================================================================
--- cfe/trunk/include/clang/Index/CallGraph.h (original)
+++ cfe/trunk/include/clang/Index/GlobalCallGraph.h Fri Mar  2 16:54:36 2012
@@ -1,4 +1,4 @@
-//== CallGraph.cpp - Call graph building ------------------------*- C++ -*--==//
+//== GlobalCallGraph.h - Call graph building --------------------*- C++ -*--==//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -11,8 +11,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_ANALYSIS_CALLGRAPH
-#define LLVM_CLANG_ANALYSIS_CALLGRAPH
+#ifndef LLVM_CLANG_INDEX_CALLGRAPH
+#define LLVM_CLANG_INDEX_CALLGRAPH
 
 #include "clang/Index/ASTLocation.h"
 #include "clang/Index/Entity.h"
@@ -23,15 +23,18 @@
 #include <vector>
 #include <map>
 
+using namespace clang;
+
 namespace clang {
+namespace idx {
 
 class CallGraphNode {
-  idx::Entity F;
-  typedef std::pair<idx::ASTLocation, CallGraphNode*> CallRecord;
+  Entity F;
+  typedef std::pair<ASTLocation, CallGraphNode*> CallRecord;
   std::vector<CallRecord> CalledFunctions;
 
 public:
-  CallGraphNode(idx::Entity f) : F(f) {}
+  CallGraphNode(Entity f) : F(f) {}
 
   typedef std::vector<CallRecord>::iterator iterator;
   typedef std::vector<CallRecord>::const_iterator const_iterator;
@@ -41,7 +44,7 @@
   const_iterator begin() const { return CalledFunctions.begin(); }
   const_iterator end()   const { return CalledFunctions.end();   }
 
-  void addCallee(idx::ASTLocation L, CallGraphNode *Node) {
+  void addCallee(ASTLocation L, CallGraphNode *Node) {
     CalledFunctions.push_back(std::make_pair(L, Node));
   }
 
@@ -54,9 +57,9 @@
 
 class CallGraph {
   /// Program manages all Entities.
-  idx::Program &Prog;
+  Program &Prog;
 
-  typedef std::map<idx::Entity, CallGraphNode *> FunctionMapTy;
+  typedef std::map<Entity, CallGraphNode *> FunctionMapTy;
 
   /// FunctionMap owns all CallGraphNodes.
   FunctionMapTy FunctionMap;
@@ -71,7 +74,7 @@
   CallGraphNode *ExternalCallingNode;
 
 public:
-  CallGraph(idx::Program &P);
+  CallGraph(Program &P);
   ~CallGraph();
 
   typedef FunctionMapTy::iterator iterator;
@@ -88,7 +91,7 @@
 
   void addTU(ASTContext &AST);
 
-  idx::Program &getProgram() { return Prog; }
+  Program &getProgram() { return Prog; }
 
   CallGraphNode *getOrInsertFunction(idx::Entity F);
 
@@ -100,13 +103,13 @@
   void ViewCallGraph() const;
 };
 
-} // end clang namespace
+}} // end clang idx namespace
 
 namespace llvm {
 
-template <> struct GraphTraits<clang::CallGraph> {
-  typedef clang::CallGraph GraphType;
-  typedef clang::CallGraphNode NodeType;
+template <> struct GraphTraits<clang::idx::CallGraph> {
+  typedef clang::idx::CallGraph GraphType;
+  typedef clang::idx::CallGraphNode NodeType;
 
   typedef std::pair<clang::idx::ASTLocation, NodeType*> CGNPairTy;
   typedef std::pointer_to_unary_function<CGNPairTy, NodeType*> CGNDerefFun;

Removed: cfe/trunk/lib/Index/CallGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/CallGraph.cpp?rev=151949&view=auto
==============================================================================
--- cfe/trunk/lib/Index/CallGraph.cpp (original)
+++ cfe/trunk/lib/Index/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/Index/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_range I = S->children(); I; ++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(Program &P) : Prog(P), 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->doesThisDeclarationHaveABody()) {
-        // 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(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/GlobalCallGraph.cpp (from r151947, cfe/trunk/lib/Index/CallGraph.cpp)
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/GlobalCallGraph.cpp?p2=cfe/trunk/lib/Index/GlobalCallGraph.cpp&p1=cfe/trunk/lib/Index/CallGraph.cpp&r1=151947&r2=151950&rev=151950&view=diff
==============================================================================
--- cfe/trunk/lib/Index/CallGraph.cpp (original)
+++ cfe/trunk/lib/Index/GlobalCallGraph.cpp Fri Mar  2 16:54:36 2012
@@ -1,4 +1,4 @@
-//== CallGraph.cpp - Call graph building ------------------------*- C++ -*--==//
+//== GlobalCallGraph.cpp - Call graph building ------------------*- C++ -*--==//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -11,15 +11,17 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/Index/CallGraph.h"
+#include "clang/Index/GlobalCallGraph.h"
 
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/StmtVisitor.h"
 
 #include "llvm/Support/GraphWriter.h"
 
-using namespace clang;
-using namespace idx;
+using namespace clang::idx;
+using clang::FunctionDecl;
+using clang::DeclContext;
+using clang::ASTContext;
 
 namespace {
 class CGBuilder : public StmtVisitor<CGBuilder> {





More information about the cfe-commits mailing list