Ted Kremenek | b60d87c | 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 | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 15 | #include "clang/Index/Program.h" |
| 16 | #include "clang/Index/Indexer.h" |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 17 | #include "clang/Index/ASTLocation.h" |
| 18 | #include "clang/Index/Utils.h" |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclVisitor.h" |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Benjamin Kramer | 8b83f5d | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileManager.h" |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | 8b83f5d | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/ASTUnit.h" |
| 24 | #include <cstdio> |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace idx; |
| 27 | |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | // Translation Unit Visitor. |
| 31 | class TUVisitor : public DeclVisitor<TUVisitor> { |
| 32 | CXTranslationUnit TUnit; |
| 33 | CXTranslationUnitIterator Callback; |
Steve Naroff | 69b10fd | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 34 | CXClientData CData; |
| 35 | |
| 36 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
| 37 | CXCursor C = { CK, ND }; |
| 38 | Callback(TUnit, C, CData); |
| 39 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 40 | public: |
Steve Naroff | 69b10fd | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 41 | TUVisitor(CXTranslationUnit CTU, |
| 42 | CXTranslationUnitIterator cback, CXClientData D) : |
| 43 | TUnit(CTU), Callback(cback), CData(D) {} |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 44 | |
| 45 | void VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 46 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 47 | } |
| 48 | void VisitDeclContext(DeclContext *DC) { |
| 49 | for (DeclContext::decl_iterator |
| 50 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 51 | Visit(*I); |
| 52 | } |
Steve Naroff | 69b10fd | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 53 | void VisitTypedefDecl(TypedefDecl *ND) { |
| 54 | Call(CXCursor_TypedefDecl, ND); |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 55 | } |
| 56 | void VisitTagDecl(TagDecl *ND) { |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 57 | switch (ND->getTagKind()) { |
| 58 | case TagDecl::TK_struct: |
| 59 | Call(CXCursor_StructDecl, ND); |
| 60 | break; |
| 61 | case TagDecl::TK_class: |
| 62 | Call(CXCursor_ClassDecl, ND); |
| 63 | break; |
| 64 | case TagDecl::TK_union: |
| 65 | Call(CXCursor_UnionDecl, ND); |
| 66 | break; |
| 67 | case TagDecl::TK_enum: |
| 68 | Call(CXCursor_EnumDecl, ND); |
| 69 | break; |
| 70 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 71 | } |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 72 | void VisitVarDecl(VarDecl *ND) { |
| 73 | Call(CXCursor_VarDecl, ND); |
| 74 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 75 | void VisitFunctionDecl(FunctionDecl *ND) { |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 76 | Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn |
| 77 | : CXCursor_FunctionDecl, ND); |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 78 | } |
| 79 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { |
Steve Naroff | 69b10fd | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 80 | Call(CXCursor_ObjCInterfaceDecl, ND); |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 81 | } |
| 82 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Steve Naroff | 69b10fd | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 83 | Call(CXCursor_ObjCCategoryDecl, ND); |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 84 | } |
| 85 | void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { |
Steve Naroff | 69b10fd | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 86 | Call(CXCursor_ObjCProtocolDecl, ND); |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 87 | } |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 88 | void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) { |
| 89 | Call(CXCursor_ObjCClassDefn, ND); |
| 90 | } |
| 91 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) { |
| 92 | Call(CXCursor_ObjCCategoryDefn, ND); |
| 93 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 96 | // Declaration visitor. |
| 97 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 98 | CXDecl CDecl; |
| 99 | CXDeclIterator Callback; |
| 100 | CXClientData CData; |
| 101 | |
| 102 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 103 | // Disable the callback when the context is equal to the visiting decl. |
| 104 | if (CDecl == ND && !clang_isReference(CK)) |
| 105 | return; |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 106 | CXCursor C = { CK, ND }; |
| 107 | Callback(CDecl, C, CData); |
| 108 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 109 | public: |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 110 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : |
| 111 | CDecl(C), Callback(cback), CData(D) {} |
| 112 | |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 113 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 114 | // Issue callbacks for the containing class. |
| 115 | Call(CXCursor_ObjCClassRef, ND); |
| 116 | // FIXME: Issue callbacks for protocol refs. |
| 117 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 118 | } |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 119 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 120 | // Issue callbacks for super class. |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 121 | if (D->getSuperClass()) |
| 122 | Call(CXCursor_ObjCSuperClassRef, D); |
| 123 | |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 124 | for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 125 | E = D->protocol_end(); I != E; ++I) |
| 126 | Call(CXCursor_ObjCProtocolRef, *I); |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 127 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 128 | } |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 129 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 130 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 131 | E = PID->protocol_end(); I != E; ++I) |
| 132 | Call(CXCursor_ObjCProtocolRef, *I); |
| 133 | |
| 134 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 135 | } |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 136 | void VisitTagDecl(TagDecl *D) { |
| 137 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 138 | } |
| 139 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 140 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 141 | } |
| 142 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 143 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 144 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 145 | void VisitDeclContext(DeclContext *DC) { |
| 146 | for (DeclContext::decl_iterator |
| 147 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 148 | Visit(*I); |
| 149 | } |
| 150 | void VisitEnumConstantDecl(EnumConstantDecl *ND) { |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 151 | Call(CXCursor_EnumConstantDecl, ND); |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 152 | } |
| 153 | void VisitFieldDecl(FieldDecl *ND) { |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 154 | Call(CXCursor_FieldDecl, ND); |
| 155 | } |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 156 | void VisitVarDecl(VarDecl *ND) { |
| 157 | Call(CXCursor_VarDecl, ND); |
| 158 | } |
| 159 | void VisitParmVarDecl(ParmVarDecl *ND) { |
| 160 | Call(CXCursor_ParmDecl, ND); |
| 161 | } |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 162 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 163 | Call(CXCursor_ObjCPropertyDecl, ND); |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 164 | } |
| 165 | void VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 166 | Call(CXCursor_ObjCIvarDecl, ND); |
| 167 | } |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 168 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 169 | if (ND->isThisDeclarationADefinition()) { |
| 170 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 171 | } |
| 172 | } |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 173 | void VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 174 | if (ND->getBody()) { |
| 175 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 176 | : CXCursor_ObjCClassMethodDefn, ND); |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 177 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 178 | } else |
| 179 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 180 | : CXCursor_ObjCClassMethodDecl, ND); |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 181 | } |
| 182 | }; |
| 183 | |
| 184 | } |
| 185 | |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 186 | extern "C" { |
Ted Kremenek | b60d87c | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 187 | |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 188 | CXIndex clang_createIndex() |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 189 | { |
| 190 | return new Indexer(*new Program(), *new FileManager()); |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Steve Naroff | 3aa2d73 | 2009-09-17 18:33:27 +0000 | [diff] [blame^] | 193 | void clang_disposeIndex(CXIndex CIdx) |
| 194 | { |
| 195 | assert(CIdx && "Passed null CXIndex"); |
| 196 | delete static_cast<Indexer *>(CIdx); |
| 197 | } |
| 198 | |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 199 | // FIXME: need to pass back error info. |
| 200 | CXTranslationUnit clang_createTranslationUnit( |
| 201 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 202 | { |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 203 | assert(CIdx && "Passed null CXIndex"); |
| 204 | Indexer *CXXIdx = static_cast<Indexer *>(CIdx); |
| 205 | std::string astName(ast_filename); |
| 206 | std::string ErrMsg; |
| 207 | |
| 208 | return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg); |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Steve Naroff | 3aa2d73 | 2009-09-17 18:33:27 +0000 | [diff] [blame^] | 211 | void clang_disposeTranslationUnit( |
| 212 | CXTranslationUnit CTUnit) |
| 213 | { |
| 214 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 215 | delete static_cast<ASTUnit *>(CTUnit); |
| 216 | } |
| 217 | |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 218 | const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) |
| 219 | { |
| 220 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | c0683b9 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 221 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 222 | return CXXUnit->getOriginalSourceFileName().c_str(); |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 223 | } |
Daniel Dunbar | e58bd8b | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 224 | |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 225 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
| 226 | CXTranslationUnitIterator callback, |
| 227 | CXClientData CData) |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 228 | { |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 229 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 230 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 231 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 232 | |
Steve Naroff | 69b10fd | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 233 | TUVisitor DVisit(CTUnit, callback, CData); |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 234 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 237 | void clang_loadDeclaration(CXDecl Dcl, |
| 238 | CXDeclIterator callback, |
| 239 | CXClientData CData) |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 240 | { |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 241 | assert(Dcl && "Passed null CXDecl"); |
| 242 | |
| 243 | CDeclVisitor DVisit(Dcl, callback, CData); |
| 244 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Steve Naroff | 8721959 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 247 | // Some notes on CXEntity: |
| 248 | // |
| 249 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 250 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 251 | // entity for 2 different types). For example: |
| 252 | // |
| 253 | // module1.m: @interface Foo @end Foo *x; |
| 254 | // module2.m: void Foo(int); |
| 255 | // |
| 256 | // - Since the unique name spans translation units, static data/functions |
| 257 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 258 | // As a result, there will be no entity for the following: |
| 259 | // |
| 260 | // module.m: static void Foo() { } |
| 261 | // |
| 262 | |
| 263 | |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 264 | const char *clang_getDeclarationName(CXEntity) |
| 265 | { |
| 266 | return ""; |
| 267 | } |
| 268 | const char *clang_getURI(CXEntity) |
| 269 | { |
| 270 | return ""; |
| 271 | } |
| 272 | |
| 273 | CXEntity clang_getEntity(const char *URI) |
| 274 | { |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | // |
| 279 | // CXDecl Operations. |
| 280 | // |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 281 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 282 | { |
| 283 | return 0; |
| 284 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 285 | const char *clang_getDeclSpelling(CXDecl AnonDecl) |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 286 | { |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 287 | assert(AnonDecl && "Passed null CXDecl"); |
| 288 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 289 | |
| 290 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 291 | return OMD->getSelector().getAsString().c_str(); |
| 292 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 293 | if (ND->getIdentifier()) |
| 294 | return ND->getIdentifier()->getName(); |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 295 | else |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 296 | return ""; |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 297 | } |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 298 | |
| 299 | const char *clang_getCursorSpelling(CXCursor C) |
| 300 | { |
| 301 | assert(C.decl && "CXCursor has null decl"); |
| 302 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 303 | |
| 304 | if (clang_isReference(C.kind)) { |
| 305 | switch (C.kind) { |
Steve Naroff | b92c73a | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 306 | case CXCursor_ObjCSuperClassRef: |
| 307 | { |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 308 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 309 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 310 | return OID->getSuperClass()->getIdentifier()->getName(); |
Steve Naroff | b92c73a | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 311 | } |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 312 | case CXCursor_ObjCClassRef: |
| 313 | { |
| 314 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 315 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 316 | return OID->getClassInterface()->getIdentifier()->getName(); |
| 317 | } |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 318 | case CXCursor_ObjCProtocolRef: |
| 319 | { |
| 320 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 321 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 322 | return OID->getIdentifier()->getName(); |
| 323 | } |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 324 | default: |
| 325 | return "<not implemented>"; |
| 326 | } |
| 327 | } |
| 328 | return clang_getDeclSpelling(C.decl); |
| 329 | } |
| 330 | |
| 331 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 332 | { |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 333 | switch (Kind) { |
| 334 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 335 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 336 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 337 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 338 | case CXCursor_StructDecl: return "StructDecl"; |
| 339 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 340 | case CXCursor_ClassDecl: return "ClassDecl"; |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 341 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 342 | case CXCursor_VarDecl: return "VarDecl"; |
| 343 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 344 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 345 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 346 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 347 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 348 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Steve Naroff | 3645f5a | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 349 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 350 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 351 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 352 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 353 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 354 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 355 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 356 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 357 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 358 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
Steve Naroff | 54f22fb | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 359 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 360 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 361 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 362 | default: return "<not implemented>"; |
| 363 | } |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 364 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 365 | |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 366 | static enum CXCursorKind TranslateKind(Decl *D) { |
| 367 | switch (D->getKind()) { |
| 368 | case Decl::Function: return CXCursor_FunctionDecl; |
| 369 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 370 | case Decl::Enum: return CXCursor_EnumDecl; |
| 371 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 372 | case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class |
| 373 | case Decl::Field: return CXCursor_FieldDecl; |
| 374 | case Decl::Var: return CXCursor_VarDecl; |
| 375 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 376 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
| 377 | case Decl::ObjCMethod: { |
| 378 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D); |
| 379 | if (MD->isInstanceMethod()) |
| 380 | return CXCursor_ObjCInstanceMethodDecl; |
| 381 | return CXCursor_ObjCClassMethodDecl; |
| 382 | } |
| 383 | default: break; |
| 384 | } |
Steve Naroff | 54f22fb | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 385 | return CXCursor_NotImplemented; |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 386 | } |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 387 | // |
| 388 | // CXCursor Operations. |
| 389 | // |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 390 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 391 | unsigned line, unsigned column) |
| 392 | { |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 393 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 394 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 395 | |
| 396 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 397 | const FileEntry *File = FMgr.getFile(source_name, |
Steve Naroff | 54f22fb | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 398 | source_name+strlen(source_name)); |
| 399 | if (!File) { |
| 400 | CXCursor C = { CXCursor_InvalidFile, 0 }; |
| 401 | return C; |
| 402 | } |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 403 | SourceLocation SLoc = |
| 404 | CXXUnit->getSourceManager().getLocation(File, line, column); |
| 405 | |
| 406 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc); |
| 407 | |
| 408 | Decl *Dcl = ALoc.getDecl(); |
Steve Naroff | 54f22fb | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 409 | if (Dcl) { |
| 410 | CXCursor C = { TranslateKind(Dcl), Dcl }; |
| 411 | return C; |
| 412 | } |
| 413 | CXCursor C = { CXCursor_NoDeclFound, 0 }; |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 414 | return C; |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Steve Naroff | 54f22fb | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 417 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) |
| 418 | { |
| 419 | assert(AnonDecl && "Passed null CXDecl"); |
| 420 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 421 | |
| 422 | CXCursor C = { TranslateKind(ND), ND }; |
| 423 | return C; |
| 424 | } |
| 425 | |
| 426 | unsigned clang_isInvalid(enum CXCursorKind K) |
| 427 | { |
| 428 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 429 | } |
| 430 | |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 431 | unsigned clang_isDeclaration(enum CXCursorKind K) |
| 432 | { |
| 433 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 434 | } |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 435 | |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 436 | unsigned clang_isReference(enum CXCursorKind K) |
| 437 | { |
| 438 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 439 | } |
| 440 | |
| 441 | unsigned clang_isDefinition(enum CXCursorKind K) |
| 442 | { |
| 443 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 444 | } |
| 445 | |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 446 | CXCursorKind clang_getCursorKind(CXCursor C) |
| 447 | { |
| 448 | return C.kind; |
| 449 | } |
| 450 | |
| 451 | CXDecl clang_getCursorDecl(CXCursor C) |
| 452 | { |
| 453 | return C.decl; |
| 454 | } |
| 455 | |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 456 | static SourceLocation getLocationFromCursor(CXCursor C, |
| 457 | SourceManager &SourceMgr, |
| 458 | NamedDecl *ND) { |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 459 | if (clang_isReference(C.kind)) { |
| 460 | switch (C.kind) { |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 461 | case CXCursor_ObjCSuperClassRef: |
Steve Naroff | b92c73a | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 462 | { |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 463 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 464 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Steve Naroff | b92c73a | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 465 | return OID->getSuperClassLoc(); |
| 466 | } |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 467 | case CXCursor_ObjCProtocolRef: |
| 468 | { |
| 469 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 470 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 471 | return OID->getLocation(); |
| 472 | } |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 473 | default: |
Steve Naroff | b92c73a | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 474 | return SourceLocation(); |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 475 | } |
| 476 | } else { // We have a declaration or a definition. |
Steve Naroff | ef9618b | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 477 | SourceLocation SLoc; |
| 478 | switch (ND->getKind()) { |
| 479 | case Decl::ObjCInterface: |
| 480 | { |
| 481 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 482 | break; |
| 483 | } |
| 484 | case Decl::ObjCProtocol: |
| 485 | { |
| 486 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 487 | break; |
| 488 | } |
| 489 | default: |
| 490 | { |
| 491 | SLoc = ND->getLocation(); |
| 492 | break; |
| 493 | } |
| 494 | } |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 495 | if (SLoc.isInvalid()) |
| 496 | return SourceLocation(); |
Steve Naroff | b92c73a | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 497 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 498 | } |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 501 | unsigned clang_getCursorLine(CXCursor C) |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 502 | { |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 503 | assert(C.decl && "CXCursor has null decl"); |
| 504 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 505 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 506 | |
| 507 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 508 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 509 | } |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 510 | |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 511 | unsigned clang_getCursorColumn(CXCursor C) |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 512 | { |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 513 | assert(C.decl && "CXCursor has null decl"); |
| 514 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 515 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 516 | |
| 517 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 518 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 519 | } |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 520 | const char *clang_getCursorSource(CXCursor C) |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 521 | { |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 522 | assert(C.decl && "CXCursor has null decl"); |
| 523 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 524 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 80a766b | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 525 | |
| 526 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 527 | return SourceMgr.getBufferName(SLoc); |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Steve Naroff | d5e8e86 | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 530 | } // end extern "C" |