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" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 15 | #include "clang/Index/Program.h" |
| 16 | #include "clang/Index/Indexer.h" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclVisitor.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 18 | #include "clang/Basic/FileManager.h" |
| 19 | #include "clang/Frontend/ASTUnit.h" |
| 20 | #include <cstdio> |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace idx; |
| 23 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 24 | namespace { |
| 25 | |
| 26 | // Translation Unit Visitor. |
| 27 | class TUVisitor : public DeclVisitor<TUVisitor> { |
| 28 | CXTranslationUnit TUnit; |
| 29 | CXTranslationUnitIterator Callback; |
| 30 | public: |
| 31 | TUVisitor(CXTranslationUnit CTU, CXTranslationUnitIterator cback) : |
| 32 | TUnit(CTU), Callback(cback) {} |
| 33 | |
| 34 | void VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 35 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 36 | } |
| 37 | void VisitDeclContext(DeclContext *DC) { |
| 38 | for (DeclContext::decl_iterator |
| 39 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 40 | Visit(*I); |
| 41 | } |
| 42 | void VisitTypedefDecl(TypedefDecl *ND) { |
| 43 | CXCursor C = { CXCursor_TypedefDecl, ND }; |
| 44 | Callback(TUnit, C); |
| 45 | } |
| 46 | void VisitTagDecl(TagDecl *ND) { |
| 47 | CXCursor C = { ND->isEnum() ? CXCursor_EnumDecl : CXCursor_RecordDecl, ND }; |
| 48 | Callback(TUnit, C); |
| 49 | } |
| 50 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 51 | CXCursor C = { CXCursor_FunctionDecl, ND }; |
| 52 | Callback(TUnit, C); |
| 53 | } |
| 54 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { |
| 55 | CXCursor C = { CXCursor_ObjCInterfaceDecl, ND }; |
| 56 | Callback(TUnit, C); |
| 57 | } |
| 58 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 59 | CXCursor C = { CXCursor_ObjCCategoryDecl, ND }; |
| 60 | Callback(TUnit, C); |
| 61 | } |
| 62 | void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { |
| 63 | CXCursor C = { CXCursor_ObjCProtocolDecl, ND }; |
| 64 | Callback(TUnit, C); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | // Top-level declaration visitor. |
| 69 | class TLDeclVisitor : public DeclVisitor<TLDeclVisitor> { |
| 70 | public: |
| 71 | void VisitDeclContext(DeclContext *DC) { |
| 72 | for (DeclContext::decl_iterator |
| 73 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 74 | Visit(*I); |
| 75 | } |
| 76 | void VisitEnumConstantDecl(EnumConstantDecl *ND) { |
| 77 | } |
| 78 | void VisitFieldDecl(FieldDecl *ND) { |
| 79 | } |
| 80 | void VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | } |
| 85 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 86 | extern "C" { |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 87 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 88 | CXIndex clang_createIndex() |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 89 | { |
| 90 | return new Indexer(*new Program(), *new FileManager()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 93 | // FIXME: need to pass back error info. |
| 94 | CXTranslationUnit clang_createTranslationUnit( |
| 95 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 96 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 97 | assert(CIdx && "Passed null CXIndex"); |
| 98 | Indexer *CXXIdx = static_cast<Indexer *>(CIdx); |
| 99 | std::string astName(ast_filename); |
| 100 | std::string ErrMsg; |
| 101 | |
| 102 | return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 105 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 106 | void clang_loadTranslationUnit( |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 107 | CXTranslationUnit CTUnit, CXTranslationUnitIterator callback) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 108 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 109 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 110 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 111 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 112 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 113 | TUVisitor DVisit(CTUnit, callback); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 114 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 117 | void clang_loadDeclaration(CXDecl, CXDeclIterator) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 118 | { |
| 119 | } |
| 120 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 121 | // Some notes on CXEntity: |
| 122 | // |
| 123 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 124 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 125 | // entity for 2 different types). For example: |
| 126 | // |
| 127 | // module1.m: @interface Foo @end Foo *x; |
| 128 | // module2.m: void Foo(int); |
| 129 | // |
| 130 | // - Since the unique name spans translation units, static data/functions |
| 131 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 132 | // As a result, there will be no entity for the following: |
| 133 | // |
| 134 | // module.m: static void Foo() { } |
| 135 | // |
| 136 | |
| 137 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 138 | const char *clang_getDeclarationName(CXEntity) |
| 139 | { |
| 140 | return ""; |
| 141 | } |
| 142 | const char *clang_getURI(CXEntity) |
| 143 | { |
| 144 | return ""; |
| 145 | } |
| 146 | |
| 147 | CXEntity clang_getEntity(const char *URI) |
| 148 | { |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | // |
| 153 | // CXDecl Operations. |
| 154 | // |
| 155 | CXCursor clang_getCursorFromDecl(CXDecl) |
| 156 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 157 | return CXCursor(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 158 | } |
| 159 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 160 | { |
| 161 | return 0; |
| 162 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 163 | const char *clang_getDeclSpelling(CXDecl AnonDecl) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 164 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 165 | assert(AnonDecl && "Passed null CXDecl"); |
| 166 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 167 | if (ND->getIdentifier()) |
| 168 | return ND->getIdentifier()->getName(); |
| 169 | else |
| 170 | return ""; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 171 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 172 | const char *clang_getKindSpelling(enum CXCursorKind Kind) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 173 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 174 | switch (Kind) { |
| 175 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 176 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 177 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 178 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
| 179 | case CXCursor_RecordDecl: return "RecordDecl"; |
| 180 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 181 | case CXCursor_VarDecl: return "VarDecl"; |
| 182 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 183 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 184 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 185 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 186 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 187 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
| 188 | case CXCursor_ObjCMethodDecl: return "ObjCMethodDecl"; |
| 189 | default: return "<not implemented>"; |
| 190 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 191 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 192 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 193 | // |
| 194 | // CXCursor Operations. |
| 195 | // |
| 196 | CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, |
| 197 | unsigned line, unsigned column) |
| 198 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 199 | return CXCursor(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | CXCursorKind clang_getCursorKind(CXCursor) |
| 203 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 204 | return CXCursor_Invalid; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame^] | 207 | unsigned clang_isDeclaration(enum CXCursorKind K) |
| 208 | { |
| 209 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 210 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 211 | unsigned clang_getCursorLine(CXCursor) |
| 212 | { |
| 213 | return 0; |
| 214 | } |
| 215 | unsigned clang_getCursorColumn(CXCursor) |
| 216 | { |
| 217 | return 0; |
| 218 | } |
| 219 | const char *clang_getCursorSource(CXCursor) |
| 220 | { |
| 221 | return ""; |
| 222 | } |
| 223 | |
| 224 | // If CXCursorKind == Cursor_Reference, then this will return the referenced declaration. |
| 225 | // If CXCursorKind == Cursor_Declaration, then this will return the declaration. |
| 226 | CXDecl clang_getCursorDecl(CXCursor) |
| 227 | { |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | } // end extern "C" |