[cfe-commits] r107642 - in /cfe/trunk: examples/wpa/clang-wpa.cpp include/clang/Index/Entity.h include/clang/Index/Indexer.h lib/Index/Entity.cpp lib/Index/EntityImpl.h lib/Index/Indexer.cpp

Zhongxing Xu xuzhongxing at gmail.com
Mon Jul 5 22:55:13 PDT 2010


Author: zhongxingxu
Date: Tue Jul  6 00:55:13 2010
New Revision: 107642

URL: http://llvm.org/viewvc/llvm-project?rev=107642&view=rev
Log:
Collect function definitions in the Indexer when indexing through the ASTs.
Add an API to get an Entity associated with a name in the global namespace.

Modified:
    cfe/trunk/examples/wpa/clang-wpa.cpp
    cfe/trunk/include/clang/Index/Entity.h
    cfe/trunk/include/clang/Index/Indexer.h
    cfe/trunk/lib/Index/Entity.cpp
    cfe/trunk/lib/Index/EntityImpl.h
    cfe/trunk/lib/Index/Indexer.cpp

Modified: cfe/trunk/examples/wpa/clang-wpa.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/wpa/clang-wpa.cpp?rev=107642&r1=107641&r2=107642&view=diff
==============================================================================
--- cfe/trunk/examples/wpa/clang-wpa.cpp (original)
+++ cfe/trunk/examples/wpa/clang-wpa.cpp Tue Jul  6 00:55:13 2010
@@ -105,5 +105,12 @@
     Idxer.IndexAST(&TU);
   }
 
+  Entity Ent = Entity::get(AnalyzeFunction, Prog);
+  FunctionDecl *FD;
+  TranslationUnit *TU;
+  llvm::tie(FD, TU) = Idxer.getDefinitionFor(Ent);
+
+  if (!FD)
+    return 0;
   return 0;
 }

Modified: cfe/trunk/include/clang/Index/Entity.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/Entity.h?rev=107642&r1=107641&r2=107642&view=diff
==============================================================================
--- cfe/trunk/include/clang/Index/Entity.h (original)
+++ cfe/trunk/include/clang/Index/Entity.h Tue Jul  6 00:55:13 2010
@@ -17,6 +17,7 @@
 
 #include "llvm/ADT/PointerUnion.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/StringRef.h"
 #include <string>
 
 namespace clang {
@@ -71,6 +72,9 @@
   /// \returns invalid Entity if an Entity cannot refer to this Decl.
   static Entity get(Decl *D, Program &Prog);
 
+  /// \brief Get an Entity associated with a name in the global namespace.
+  static Entity get(llvm::StringRef Name, Program &Prog);
+
   /// \brief true if the Entity is not visible outside the trasnlation unit.
   bool isInternalToTU() const {
     assert(isValid() && "This Entity is not valid!");

Modified: cfe/trunk/include/clang/Index/Indexer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/Indexer.h?rev=107642&r1=107641&r2=107642&view=diff
==============================================================================
--- cfe/trunk/include/clang/Index/Indexer.h (original)
+++ cfe/trunk/include/clang/Index/Indexer.h Tue Jul  6 00:55:13 2010
@@ -23,6 +23,7 @@
 
 namespace clang {
   class ASTContext;
+  class FunctionDecl;
 
 namespace idx {
   class Program;
@@ -35,6 +36,7 @@
   typedef llvm::DenseMap<ASTContext *, TranslationUnit *> CtxTUMapTy;
   typedef std::map<Entity, TUSetTy> MapTy;
   typedef std::map<GlobalSelector, TUSetTy> SelMapTy;
+  typedef std::map<Entity, std::pair<FunctionDecl*,TranslationUnit*> > DefMapTy;
 
   explicit Indexer(Program &prog) :
     Prog(prog) { }
@@ -49,10 +51,15 @@
   virtual void GetTranslationUnitsFor(GlobalSelector Sel,
                                       TranslationUnitHandler &Handler);
 
+  std::pair<FunctionDecl*, TranslationUnit*> getDefinitionFor(Entity Ent);
+
 private:
   Program &Prog;
 
   MapTy Map;
+  // Map a function Entity to the its definition.
+  DefMapTy DefMap;
+
   CtxTUMapTy CtxTUMap;
   SelMapTy SelMap;
 };

Modified: cfe/trunk/lib/Index/Entity.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/Entity.cpp?rev=107642&r1=107641&r2=107642&view=diff
==============================================================================
--- cfe/trunk/lib/Index/Entity.cpp (original)
+++ cfe/trunk/lib/Index/Entity.cpp Tue Jul  6 00:55:13 2010
@@ -42,6 +42,13 @@
   EntityGetter(Program &prog, ProgramImpl &progImpl)
     : Prog(prog), ProgImpl(progImpl) { }
 
+  // Get an Entity.
+  Entity getEntity(Entity Parent, DeclarationName Name, 
+                   unsigned IdNS, bool isObjCInstanceMethod);
+
+  // Get an Entity associated with the name in the global namespace.
+  Entity getGlobalEntity(llvm::StringRef Name);
+
   Entity VisitNamedDecl(NamedDecl *D);
   Entity VisitVarDecl(VarDecl *D);
   Entity VisitFieldDecl(FieldDecl *D);
@@ -52,6 +59,31 @@
 }
 }
 
+Entity EntityGetter::getEntity(Entity Parent, DeclarationName Name, 
+                               unsigned IdNS, bool isObjCInstanceMethod) {
+  llvm::FoldingSetNodeID ID;
+  EntityImpl::Profile(ID, Parent, Name, IdNS, isObjCInstanceMethod);
+
+  ProgramImpl::EntitySetTy &Entities = ProgImpl.getEntities();
+  void *InsertPos = 0;
+  if (EntityImpl *Ent = Entities.FindNodeOrInsertPos(ID, InsertPos))
+    return Entity(Ent);
+
+  void *Buf = ProgImpl.Allocate(sizeof(EntityImpl));
+  EntityImpl *New =
+      new (Buf) EntityImpl(Parent, Name, IdNS, isObjCInstanceMethod);
+  Entities.InsertNode(New, InsertPos);
+
+  return Entity(New);
+}
+
+Entity EntityGetter::getGlobalEntity(llvm::StringRef Name) {
+  IdentifierInfo *II = &ProgImpl.getIdents().get(Name);
+  DeclarationName GlobName(II);
+  unsigned IdNS = Decl::IDNS_Ordinary;
+  return getEntity(Entity(), GlobName, IdNS, false);
+}
+
 Entity EntityGetter::VisitNamedDecl(NamedDecl *D) {
   Entity Parent;
   if (!D->getDeclContext()->isTranslationUnit()) {
@@ -93,21 +125,7 @@
 
   ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
   bool isObjCInstanceMethod = MD && MD->isInstanceMethod();
-
-  llvm::FoldingSetNodeID ID;
-  EntityImpl::Profile(ID, Parent, GlobName, IdNS, isObjCInstanceMethod);
-
-  ProgramImpl::EntitySetTy &Entities = ProgImpl.getEntities();
-  void *InsertPos = 0;
-  if (EntityImpl *Ent = Entities.FindNodeOrInsertPos(ID, InsertPos))
-    return Entity(Ent);
-
-  void *Buf = ProgImpl.Allocate(sizeof(EntityImpl));
-  EntityImpl *New =
-      new (Buf) EntityImpl(Parent, GlobName, IdNS, isObjCInstanceMethod);
-  Entities.InsertNode(New, InsertPos);
-
-  return Entity(New);
+  return getEntity(Parent, GlobName, IdNS, isObjCInstanceMethod);
 }
 
 Entity EntityGetter::VisitVarDecl(VarDecl *D) {
@@ -190,6 +208,12 @@
   return EntityGetter(Prog, ProgImpl).Visit(D);
 }
 
+/// \brief Get an Entity associated with a global name.
+Entity EntityImpl::get(llvm::StringRef Name, Program &Prog, 
+                       ProgramImpl &ProgImpl) {
+  return EntityGetter(Prog, ProgImpl).getGlobalEntity(Name);
+}
+
 std::string EntityImpl::getPrintableName() {
   return Name.getAsString();
 }
@@ -235,6 +259,11 @@
   return EntityImpl::get(D, Prog, ProgImpl);
 }
 
+Entity Entity::get(llvm::StringRef Name, Program &Prog) {
+  ProgramImpl &ProgImpl = *static_cast<ProgramImpl*>(Prog.Impl);
+  return EntityImpl::get(Name, Prog, ProgImpl);
+}
+
 unsigned
 llvm::DenseMapInfo<Entity>::getHashValue(Entity E) {
   return DenseMapInfo<void*>::getHashValue(E.getAsOpaquePtr());

Modified: cfe/trunk/lib/Index/EntityImpl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/EntityImpl.h?rev=107642&r1=107641&r2=107642&view=diff
==============================================================================
--- cfe/trunk/lib/Index/EntityImpl.h (original)
+++ cfe/trunk/lib/Index/EntityImpl.h Tue Jul  6 00:55:13 2010
@@ -47,6 +47,7 @@
   /// \brief Get an Entity associated with the given Decl.
   /// \returns Null if an Entity cannot refer to this Decl.
   static Entity get(Decl *D, Program &Prog, ProgramImpl &ProgImpl);
+  static Entity get(llvm::StringRef Name, Program &Prog, ProgramImpl &ProgImpl);
 
   std::string getPrintableName();
 

Modified: cfe/trunk/lib/Index/Indexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/Indexer.cpp?rev=107642&r1=107641&r2=107642&view=diff
==============================================================================
--- cfe/trunk/lib/Index/Indexer.cpp (original)
+++ cfe/trunk/lib/Index/Indexer.cpp Tue Jul  6 00:55:13 2010
@@ -25,14 +25,22 @@
 class EntityIndexer : public EntityHandler {
   TranslationUnit *TU;
   Indexer::MapTy ⤅
+  Indexer::DefMapTy &DefMap;
 
 public:
-  EntityIndexer(TranslationUnit *tu, Indexer::MapTy &map) : TU(tu), Map(map) { }
+  EntityIndexer(TranslationUnit *tu, Indexer::MapTy &map, 
+                Indexer::DefMapTy &defmap) 
+    : TU(tu), Map(map), DefMap(defmap) { }
 
   virtual void Handle(Entity Ent) {
     if (Ent.isInternalToTU())
       return;
     Map[Ent].insert(TU);
+
+    Decl *D = Ent.getDecl(TU->getASTContext());
+    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
+      if (FD->isThisDeclarationADefinition())
+        DefMap[Ent] = std::make_pair(FD, TU);
   }
 };
 
@@ -62,7 +70,7 @@
   assert(TU && "Passed null TranslationUnit");
   ASTContext &Ctx = TU->getASTContext();
   CtxTUMap[&Ctx] = TU;
-  EntityIndexer Idx(TU, Map);
+  EntityIndexer Idx(TU, Map, DefMap);
   Prog.FindEntities(Ctx, Idx);
 
   SelectorIndexer SelIdx(Prog, TU, SelMap);
@@ -102,3 +110,12 @@
   for (TUSetTy::iterator I = Set.begin(), E = Set.end(); I != E; ++I)
     Handler.Handle(*I);
 }
+
+std::pair<FunctionDecl *, TranslationUnit *> 
+Indexer::getDefinitionFor(Entity Ent) {
+  DefMapTy::iterator I = DefMap.find(Ent);
+  if (I == DefMap.end())
+    return std::make_pair((FunctionDecl *)0, (TranslationUnit *)0);
+  else
+    return I->second;
+}





More information about the cfe-commits mailing list