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