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