]> granicus.if.org Git - clang/commitdiff
Lot's of little changes to get the C-based indexing API going...
authorSteve Naroff <snaroff@apple.com>
Fri, 28 Aug 2009 15:28:48 +0000 (15:28 +0000)
committerSteve Naroff <snaroff@apple.com>
Fri, 28 Aug 2009 15:28:48 +0000 (15:28 +0000)
Work in progress.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80367 91177308-0d34-0410-b5e6-96231b3b80d8

clang.xcodeproj/project.pbxproj
include/clang-c/Index.h
include/clang/Index/Indexer.h
tools/CIndex/CIndex.cpp
tools/CIndex/CIndex.exports
tools/Makefile
tools/c-index-test/CMakeLists.txt [new file with mode: 0644]
tools/c-index-test/Makefile [new file with mode: 0644]
tools/c-index-test/c-index-test.c [new file with mode: 0644]
tools/index-test/index-test.cpp

index 89fb901d3869b19006c5c2d44fd2697c06280621..705ff04a32db89b7ac9077a0c2edd8ce01aaa2df 100644 (file)
                84AF36A10CB17A3B00C820A5 /* DeclObjC.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 84AF36A00CB17A3B00C820A5 /* DeclObjC.h */; };
                84D9A8880C1A57E100AC7ABC /* AttributeList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */; };
                84D9A88C0C1A581300AC7ABC /* AttributeList.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 84D9A88B0C1A581300AC7ABC /* AttributeList.h */; };
+               9012911D1048068D0083456D /* ASTUnit.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9012911C1048068D0083456D /* ASTUnit.cpp */; };
+               90129121104812F90083456D /* CIndex.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9012911F104812F90083456D /* CIndex.cpp */; };
                906BF4B00F83BA2E001071FA /* ConvertUTF.c in Sources */ = {isa = PBXBuildFile; fileRef = 906BF4AF0F83BA2E001071FA /* ConvertUTF.c */; };
                90FD6D7B103C3D49005F5B73 /* Analyzer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 90FD6D6D103C3D49005F5B73 /* Analyzer.cpp */; };
                90FD6D7C103C3D49005F5B73 /* ASTLocation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 90FD6D6E103C3D49005F5B73 /* ASTLocation.cpp */; };
                84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 2; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = "<group>"; tabWidth = 2; };
                8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
                9012911510470FCE0083456D /* Index.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Index.h; path = "clang-c/Index.h"; sourceTree = "<group>"; };
+               9012911C1048068D0083456D /* ASTUnit.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ASTUnit.cpp; path = lib/Frontend/ASTUnit.cpp; sourceTree = "<group>"; };
+               9012911F104812F90083456D /* CIndex.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CIndex.cpp; path = tools/CIndex/CIndex.cpp; sourceTree = "<group>"; };
+               90129120104812F90083456D /* CIndex.exports */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = CIndex.exports; path = tools/CIndex/CIndex.exports; sourceTree = "<group>"; };
                9063F2210F9E8BDF002F7251 /* ExternalSemaSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ExternalSemaSource.h; path = clang/Sema/ExternalSemaSource.h; sourceTree = "<group>"; };
                9063F2220F9E8BDF002F7251 /* SemaConsumer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SemaConsumer.h; path = clang/Sema/SemaConsumer.h; sourceTree = "<group>"; };
                9063F2280F9E911F002F7251 /* OnDiskHashTable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OnDiskHashTable.h; sourceTree = "<group>"; };
                352246E00F5C6BC000D0D279 /* Frontend */ = {
                        isa = PBXGroup;
                        children = (
+                               9012911C1048068D0083456D /* ASTUnit.cpp */,
                                1A2A54A40FD1DD1C00F4CE45 /* AnalysisConsumer.cpp */,
                                1A2A54A50FD1DD1C00F4CE45 /* ASTConsumers.cpp */,
                                1A2A54A60FD1DD1C00F4CE45 /* Backend.cpp */,
                        name = "clang-c";
                        sourceTree = "<group>";
                };
+               9012911E104812DA0083456D /* CIndex */ = {
+                       isa = PBXGroup;
+                       children = (
+                               9012911F104812F90083456D /* CIndex.cpp */,
+                               90129120104812F90083456D /* CIndex.exports */,
+                       );
+                       name = CIndex;
+                       sourceTree = "<group>";
+               };
                90FD6D5E103C3D03005F5B73 /* Index */ = {
                        isa = PBXGroup;
                        children = (
                DEDFE61F0F7B3AE10035BD10 /* Tools */ = {
                        isa = PBXGroup;
                        children = (
+                               9012911E104812DA0083456D /* CIndex */,
                                90FD6DB4103D9763005F5B73 /* index-test */,
                                DEDFE6200F7B3AE90035BD10 /* clang-cc */,
                                DEDFE6210F7B3AF10035BD10 /* clang */,
                                90FD6D84103C3D49005F5B73 /* ResolveLocation.cpp in Sources */,
                                90FD6D85103C3D49005F5B73 /* SelectorMap.cpp in Sources */,
                                90FD6DB6103D977E005F5B73 /* index-test.cpp in Sources */,
+                               9012911D1048068D0083456D /* ASTUnit.cpp in Sources */,
+                               90129121104812F90083456D /* CIndex.cpp in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
index 52ffb931ee1fa150546119dbe4c3a8b5a08f856d..b96cb6cb143129bbbf34aabd48001310d0bb6c29 100644 (file)
@@ -33,13 +33,13 @@ extern "C" {
    Naming Conventions: To avoid namespace pollution, data types are prefixed 
    with "CX" and functions are prefixed with "clang_".
 */
-typedef void *CXIndex;            // An indexing instance.
+typedef void *CXIndex;            /* An indexing instance. */
 
-typedef void *CXTranslationUnit;  // A translation unit instance.
+typedef void *CXTranslationUnit;  /* A translation unit instance. */
 
-typedef void *CXCursor;  // An opaque cursor into the CXTranslationUnit.
+typedef void *CXCursor;  /* An opaque cursor into the CXTranslationUnit. */
 
-// Cursors represent declarations and references (provides line/column info).
+/* Cursors represent declarations and references (provides line/column info). */
 enum CXCursorKind {  
  CXCursor_Declaration,
  CXCursor_Reference,
@@ -49,9 +49,9 @@ enum CXCursorKind {
  CXCursor_ObjC_SelectorRef
 };
 
-typedef void *CXDecl;    // A specific declaration within a translation unit.
+typedef void *CXDecl;    /* A specific declaration within a translation unit. */
 
-enum CXDeclKind {  // The various kinds of declarations.
+enum CXDeclKind {  /* The various kinds of declarations. */
  CXDecl_any,
  CXDecl_typedef,
  CXDecl_enum,
@@ -73,12 +73,12 @@ enum CXDeclKind {  // The various kinds of declarations.
  CXDecl_ObjC_property_implementation
 };
 
-// A unique token for looking up "visible" CXDecls from a CXTranslationUnit.
+/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
 typedef void *CXEntity;     
 
 CXIndex clang_createIndex();
 
-CXTranslationUnit clang_loadTranslationUnitFromASTFile(
+CXTranslationUnit clang_createTranslationUnit(
   CXIndex, const char *ast_filename
 );
 
@@ -130,33 +130,35 @@ void clang_loadTranslationUnit(
 */
 void clang_loadDeclaration(CXDecl, void (*callback)(CXDecl, CXCursor));
 
-//
-// CXEntity Operations.
-//
+/*
+ * CXEntity Operations.
+ */
 const char *clang_getDeclarationName(CXEntity);
 const char *clang_getURI(CXEntity);
 CXEntity clang_getEntity(const char *URI);
-//
-// CXDecl Operations.
-//
+/*
+ * CXDecl Operations.
+ */
 CXCursor clang_getCursorFromDecl(CXDecl);
 CXEntity clang_getEntityFromDecl(CXDecl);
 enum CXDeclKind clang_getDeclKind(CXDecl);
 const char *clang_getDeclSpelling(CXDecl);
-//
-// CXCursor Operations.
-//
+/*
+ * CXCursor Operations.
+ */
 CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, 
                          unsigned line, unsigned column);
 
-CXCursorKind clang_getCursorKind(CXCursor);
+enum CXCursorKind clang_getCursorKind(CXCursor);
 
 unsigned clang_getCursorLine(CXCursor);
 unsigned clang_getCursorColumn(CXCursor);
 const char *clang_getCursorSource(CXCursor);
 
-// If CXCursorKind == Cursor_Reference, then this will return the referenced declaration.
-// If CXCursorKind == Cursor_Declaration, then this will return the declaration.
+/*
+ * If CXCursorKind == Cursor_Reference, then this will return the referenced declaration.
+ * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
+ */
 CXDecl clang_getCursorDecl(CXCursor);
 
 #ifdef __cplusplus
index 98cce9e44ba4b2667b6a92640ff1ccdb79539717..0fcf31c6443719ef5ece0438f29ddcb10c1fd5fb 100644 (file)
@@ -19,6 +19,7 @@
 #include "clang/Index/GlobalSelector.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/DenseMap.h"
+#include "clang/Basic/FileManager.h"
 #include <map>
 
 namespace clang {
@@ -36,10 +37,13 @@ public:
   typedef std::map<Entity, TUSetTy> MapTy;
   typedef std::map<GlobalSelector, TUSetTy> SelMapTy;
 
-  explicit Indexer(Program &prog) : Prog(prog) { }
+  explicit Indexer(Program &prog, FileManager &FM) : 
+    Prog(prog), FileMgr(FM) { }
 
   Program &getProgram() const { return Prog; }
 
+  FileManager &getFileManager() const { return FileMgr; }
+  
   /// \brief Find all Entities and map them to the given translation unit.
   void IndexAST(TranslationUnit *TU);
 
@@ -50,6 +54,8 @@ public:
 
 private:
   Program &Prog;
+  FileManager &FileMgr;
+  
   MapTy Map;
   CtxTUMapTy CtxTUMap;
   SelMapTy SelMap;
index ec575475ecd503880f2a09e22233a186b0c7e378..dc44fd2b1cc5b5d36dba23255d0b0cd173dd9388 100644 (file)
 
 #include "clang-c/Index.h"
 
+#include "clang/Index/Program.h"
+#include "clang/Index/Indexer.h"
+
+#include "clang/Frontend/ASTUnit.h"
+#include "clang/Basic/FileManager.h"
+
+#include "clang/AST/DeclVisitor.h"
+
+using namespace clang;
+using namespace idx;
+
 extern "C" {
 
 CXIndex clang_createIndex() 
-{ 
-  return 0; 
+{
+  return new Indexer(*new Program(), *new FileManager());
 }
 
-CXTranslationUnit clang_loadTranslationUnitFromASTFile(
-  CXIndex, const char *ast_filename) 
+// FIXME: need to pass back error info.
+CXTranslationUnit clang_createTranslationUnit(
+  CXIndex CIdx, const char *ast_filename) 
 {
-  return 0;
+  assert(CIdx && "Passed null CXIndex");
+  Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
+  std::string astName(ast_filename);
+  std::string ErrMsg;
+  
+  return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg);
 }
 
+class IdxVisitor : public DeclVisitor<IdxVisitor> {
+public:
+  IdxVisitor();
+  
+  void VisitNamedDecl(NamedDecl *ND) {
+    printf("NamedDecl (%s:", ND->getDeclKindName());
+    if (ND->getIdentifier())
+      printf("%s)\n", ND->getIdentifier()->getName());
+    else
+      printf("<no name>)\n");
+  }
+};
+
 void clang_loadTranslationUnit(
-  CXTranslationUnit, void (*callback)(CXTranslationUnit, CXCursor)
-)
-{
+  CXTranslationUnit CTUnit, void (*callback)(CXTranslationUnit, CXCursor))
+{
+  assert(CTUnit && "Passed null CXTranslationUnit");
+  ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
+  ASTContext &Ctx = CXXUnit->getASTContext();
+  
+  IdxVisitor DVisit;
+  DVisit.Visit(Ctx.getTranslationUnitDecl());
 }
 
 void clang_loadDeclaration(CXDecl, void (*callback)(CXDecl, CXCursor))
index 0e6cfa549a5912831b16903f8f60c05a7c7791d5..41b67a2467508706cc109cb3564817f7fc3ce797 100644 (file)
@@ -14,4 +14,4 @@ _clang_getEntityFromDecl
 _clang_getURI
 _clang_loadDeclaration
 _clang_loadTranslationUnit
-_clang_loadTranslationUnitFromASTFile
+_clang_createTranslationUnit
index 3ee73ca2827551c276b3abf42f40609330b9ceaa..d76e95606d47c4f31ead51d6c21c90086973e20f 100644 (file)
@@ -8,6 +8,6 @@
 ##===----------------------------------------------------------------------===##
 
 LEVEL := ../../..
-DIRS := clang-cc driver index-test wpa CIndex 
+DIRS := clang-cc driver index-test wpa CIndex c-index-test
 
 include $(LEVEL)/Makefile.common
diff --git a/tools/c-index-test/CMakeLists.txt b/tools/c-index-test/CMakeLists.txt
new file mode 100644 (file)
index 0000000..abf3cc4
--- /dev/null
@@ -0,0 +1,20 @@
+set(LLVM_NO_RTTI 1)
+
+set( LLVM_USED_LIBS
+  CIndex
+  clangIndex
+  clangFrontend
+  clangSema
+  clangAST
+  clangLex
+  clangBasic
+  )
+
+set( LLVM_LINK_COMPONENTS
+  bitreader
+  mc
+  )
+
+add_clang_executable(c-index-test
+  c-index-test.c
+  )
diff --git a/tools/c-index-test/Makefile b/tools/c-index-test/Makefile
new file mode 100644 (file)
index 0000000..81fee40
--- /dev/null
@@ -0,0 +1,24 @@
+##===- tools/index-test/Makefile ---------------------------*- Makefile -*-===##
+# 
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+# 
+##===----------------------------------------------------------------------===##
+LEVEL = ../../../..
+
+TOOLNAME = c-index-test
+CPPFLAGS += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include
+CXXFLAGS = -fno-rtti
+NO_INSTALL = 1
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+include $(LEVEL)/Makefile.config
+
+LINK_COMPONENTS := bitreader mc
+USEDLIBS = CIndex.a clangIndex.a clangFrontend.a clangSema.a clangAST.a clangLex.a clangBasic.a 
+
+include $(LLVM_SRC_ROOT)/Makefile.rules
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
new file mode 100644 (file)
index 0000000..71bdcc5
--- /dev/null
@@ -0,0 +1,12 @@
+
+#include "clang-c/Index.h"
+
+/*
+ * First sign of life:-)
+ */
+int main(int argc, char **argv) {
+  CXIndex Idx = clang_createIndex();
+  CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
+  clang_loadTranslationUnit(TU, 0);
+  return 1;
+}
index 02971ed782a6876ea5da6090b75efd52d1d4e965..88fd2417ac0d390cdd9a429418013476c1a400c7 100644 (file)
@@ -214,7 +214,7 @@ int main(int argc, char **argv) {
   FileManager FileMgr;
 
   Program Prog;
-  Indexer Idxer(Prog);
+  Indexer Idxer(Prog, FileMgr);
   llvm::SmallVector<TUnit*, 4> TUnits;
   
   // If no input was specified, read from stdin.