Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 1 | //===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Clang-C Source Indexing library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang-c/Index.h" |
| 15 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame^] | 16 | #include "clang/Index/Program.h" |
| 17 | #include "clang/Index/Indexer.h" |
| 18 | |
| 19 | #include "clang/Frontend/ASTUnit.h" |
| 20 | #include "clang/Basic/FileManager.h" |
| 21 | |
| 22 | #include "clang/AST/DeclVisitor.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | using namespace idx; |
| 26 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 27 | extern "C" { |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 28 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 29 | CXIndex clang_createIndex() |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame^] | 30 | { |
| 31 | return new Indexer(*new Program(), *new FileManager()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame^] | 34 | // FIXME: need to pass back error info. |
| 35 | CXTranslationUnit clang_createTranslationUnit( |
| 36 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 37 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame^] | 38 | assert(CIdx && "Passed null CXIndex"); |
| 39 | Indexer *CXXIdx = static_cast<Indexer *>(CIdx); |
| 40 | std::string astName(ast_filename); |
| 41 | std::string ErrMsg; |
| 42 | |
| 43 | return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame^] | 46 | class IdxVisitor : public DeclVisitor<IdxVisitor> { |
| 47 | public: |
| 48 | IdxVisitor(); |
| 49 | |
| 50 | void VisitNamedDecl(NamedDecl *ND) { |
| 51 | printf("NamedDecl (%s:", ND->getDeclKindName()); |
| 52 | if (ND->getIdentifier()) |
| 53 | printf("%s)\n", ND->getIdentifier()->getName()); |
| 54 | else |
| 55 | printf("<no name>)\n"); |
| 56 | } |
| 57 | }; |
| 58 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 59 | void clang_loadTranslationUnit( |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame^] | 60 | CXTranslationUnit CTUnit, void (*callback)(CXTranslationUnit, CXCursor)) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 61 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame^] | 62 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 63 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 64 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 65 | |
| 66 | IdxVisitor DVisit; |
| 67 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void clang_loadDeclaration(CXDecl, void (*callback)(CXDecl, CXCursor)) |
| 71 | { |
| 72 | } |
| 73 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 74 | // Some notes on CXEntity: |
| 75 | // |
| 76 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 77 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 78 | // entity for 2 different types). For example: |
| 79 | // |
| 80 | // module1.m: @interface Foo @end Foo *x; |
| 81 | // module2.m: void Foo(int); |
| 82 | // |
| 83 | // - Since the unique name spans translation units, static data/functions |
| 84 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 85 | // As a result, there will be no entity for the following: |
| 86 | // |
| 87 | // module.m: static void Foo() { } |
| 88 | // |
| 89 | |
| 90 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 91 | const char *clang_getDeclarationName(CXEntity) |
| 92 | { |
| 93 | return ""; |
| 94 | } |
| 95 | const char *clang_getURI(CXEntity) |
| 96 | { |
| 97 | return ""; |
| 98 | } |
| 99 | |
| 100 | CXEntity clang_getEntity(const char *URI) |
| 101 | { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | // |
| 106 | // CXDecl Operations. |
| 107 | // |
| 108 | CXCursor clang_getCursorFromDecl(CXDecl) |
| 109 | { |
| 110 | return 0; |
| 111 | } |
| 112 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 113 | { |
| 114 | return 0; |
| 115 | } |
| 116 | enum CXDeclKind clang_getDeclKind(CXDecl) |
| 117 | { |
| 118 | return CXDecl_any; |
| 119 | } |
| 120 | const char *clang_getDeclSpelling(CXDecl) |
| 121 | { |
| 122 | return ""; |
| 123 | } |
| 124 | // |
| 125 | // CXCursor Operations. |
| 126 | // |
| 127 | CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, |
| 128 | unsigned line, unsigned column) |
| 129 | { |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | CXCursorKind clang_getCursorKind(CXCursor) |
| 134 | { |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 135 | return CXCursor_Declaration; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | unsigned clang_getCursorLine(CXCursor) |
| 139 | { |
| 140 | return 0; |
| 141 | } |
| 142 | unsigned clang_getCursorColumn(CXCursor) |
| 143 | { |
| 144 | return 0; |
| 145 | } |
| 146 | const char *clang_getCursorSource(CXCursor) |
| 147 | { |
| 148 | return ""; |
| 149 | } |
| 150 | |
| 151 | // If CXCursorKind == Cursor_Reference, then this will return the referenced declaration. |
| 152 | // If CXCursorKind == Cursor_Declaration, then this will return the declaration. |
| 153 | CXDecl clang_getCursorDecl(CXCursor) |
| 154 | { |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | } // end extern "C" |