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" |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/ASTUnit.h" |
| 22 | #include <cstdio> |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace idx; |
| 25 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | // Translation Unit Visitor. |
| 29 | class TUVisitor : public DeclVisitor<TUVisitor> { |
| 30 | CXTranslationUnit TUnit; |
| 31 | CXTranslationUnitIterator Callback; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 32 | CXClientData CData; |
| 33 | |
| 34 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
| 35 | CXCursor C = { CK, ND }; |
| 36 | Callback(TUnit, C, CData); |
| 37 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 38 | public: |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 39 | TUVisitor(CXTranslationUnit CTU, |
| 40 | CXTranslationUnitIterator cback, CXClientData D) : |
| 41 | TUnit(CTU), Callback(cback), CData(D) {} |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 42 | |
| 43 | void VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 44 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 45 | } |
| 46 | void VisitDeclContext(DeclContext *DC) { |
| 47 | for (DeclContext::decl_iterator |
| 48 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 49 | Visit(*I); |
| 50 | } |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 51 | void VisitTypedefDecl(TypedefDecl *ND) { |
| 52 | Call(CXCursor_TypedefDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 53 | } |
| 54 | void VisitTagDecl(TagDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 55 | switch (ND->getTagKind()) { |
| 56 | case TagDecl::TK_struct: |
| 57 | Call(CXCursor_StructDecl, ND); |
| 58 | break; |
| 59 | case TagDecl::TK_class: |
| 60 | Call(CXCursor_ClassDecl, ND); |
| 61 | break; |
| 62 | case TagDecl::TK_union: |
| 63 | Call(CXCursor_UnionDecl, ND); |
| 64 | break; |
| 65 | case TagDecl::TK_enum: |
| 66 | Call(CXCursor_EnumDecl, ND); |
| 67 | break; |
| 68 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 69 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 70 | void VisitVarDecl(VarDecl *ND) { |
| 71 | Call(CXCursor_VarDecl, ND); |
| 72 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 73 | void VisitFunctionDecl(FunctionDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 74 | Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn |
| 75 | : CXCursor_FunctionDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 76 | } |
| 77 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 78 | Call(CXCursor_ObjCInterfaceDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 79 | } |
| 80 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 81 | Call(CXCursor_ObjCCategoryDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 82 | } |
| 83 | void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 84 | Call(CXCursor_ObjCProtocolDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 85 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 86 | void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) { |
| 87 | Call(CXCursor_ObjCClassDefn, ND); |
| 88 | } |
| 89 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) { |
| 90 | Call(CXCursor_ObjCCategoryDefn, ND); |
| 91 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 94 | // Declaration visitor. |
| 95 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 96 | CXDecl CDecl; |
| 97 | CXDeclIterator Callback; |
| 98 | CXClientData CData; |
| 99 | |
| 100 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 101 | // Disable the callback when the context is equal to the visiting decl. |
| 102 | if (CDecl == ND && !clang_isReference(CK)) |
| 103 | return; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 104 | CXCursor C = { CK, ND }; |
| 105 | Callback(CDecl, C, CData); |
| 106 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 107 | public: |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 108 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : |
| 109 | CDecl(C), Callback(cback), CData(D) {} |
| 110 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 111 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 112 | // Issue callbacks for the containing class. |
| 113 | Call(CXCursor_ObjCClassRef, ND); |
| 114 | // FIXME: Issue callbacks for protocol refs. |
| 115 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 116 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 117 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 118 | // Issue callbacks for super class. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 119 | if (D->getSuperClass()) |
| 120 | Call(CXCursor_ObjCSuperClassRef, D); |
| 121 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 122 | // FIXME: Issue callbacks for protocol refs. |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 123 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 124 | } |
| 125 | void VisitTagDecl(TagDecl *D) { |
| 126 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 127 | } |
| 128 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 129 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 130 | } |
| 131 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 132 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 133 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 134 | void VisitDeclContext(DeclContext *DC) { |
| 135 | for (DeclContext::decl_iterator |
| 136 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 137 | Visit(*I); |
| 138 | } |
| 139 | void VisitEnumConstantDecl(EnumConstantDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 140 | Call(CXCursor_EnumConstantDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 141 | } |
| 142 | void VisitFieldDecl(FieldDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 143 | Call(CXCursor_FieldDecl, ND); |
| 144 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 145 | void VisitVarDecl(VarDecl *ND) { |
| 146 | Call(CXCursor_VarDecl, ND); |
| 147 | } |
| 148 | void VisitParmVarDecl(ParmVarDecl *ND) { |
| 149 | Call(CXCursor_ParmDecl, ND); |
| 150 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 151 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 152 | Call(CXCursor_ObjCPropertyDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 153 | } |
| 154 | void VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 155 | Call(CXCursor_ObjCIvarDecl, ND); |
| 156 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 157 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 158 | if (ND->isThisDeclarationADefinition()) { |
| 159 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 160 | } |
| 161 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 162 | void VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 163 | if (ND->getBody()) { |
| 164 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 165 | : CXCursor_ObjCClassMethodDefn, ND); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 166 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 167 | } else |
| 168 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 169 | : CXCursor_ObjCClassMethodDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 170 | } |
| 171 | }; |
| 172 | |
| 173 | } |
| 174 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 175 | extern "C" { |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 176 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 177 | CXIndex clang_createIndex() |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 178 | { |
| 179 | return new Indexer(*new Program(), *new FileManager()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 182 | // FIXME: need to pass back error info. |
| 183 | CXTranslationUnit clang_createTranslationUnit( |
| 184 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 185 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 186 | assert(CIdx && "Passed null CXIndex"); |
| 187 | Indexer *CXXIdx = static_cast<Indexer *>(CIdx); |
| 188 | std::string astName(ast_filename); |
| 189 | std::string ErrMsg; |
| 190 | |
| 191 | return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 194 | const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) |
| 195 | { |
| 196 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 197 | //ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 198 | //return CXXUnit->getOriginalSourceFileName().c_str(); |
| 199 | return "<unimplemented>"; |
| 200 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 201 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 202 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
| 203 | CXTranslationUnitIterator callback, |
| 204 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 205 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 206 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 207 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 208 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 209 | |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 210 | TUVisitor DVisit(CTUnit, callback, CData); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 211 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 214 | void clang_loadDeclaration(CXDecl Dcl, |
| 215 | CXDeclIterator callback, |
| 216 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 217 | { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 218 | assert(Dcl && "Passed null CXDecl"); |
| 219 | |
| 220 | CDeclVisitor DVisit(Dcl, callback, CData); |
| 221 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 224 | // Some notes on CXEntity: |
| 225 | // |
| 226 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 227 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 228 | // entity for 2 different types). For example: |
| 229 | // |
| 230 | // module1.m: @interface Foo @end Foo *x; |
| 231 | // module2.m: void Foo(int); |
| 232 | // |
| 233 | // - Since the unique name spans translation units, static data/functions |
| 234 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 235 | // As a result, there will be no entity for the following: |
| 236 | // |
| 237 | // module.m: static void Foo() { } |
| 238 | // |
| 239 | |
| 240 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 241 | const char *clang_getDeclarationName(CXEntity) |
| 242 | { |
| 243 | return ""; |
| 244 | } |
| 245 | const char *clang_getURI(CXEntity) |
| 246 | { |
| 247 | return ""; |
| 248 | } |
| 249 | |
| 250 | CXEntity clang_getEntity(const char *URI) |
| 251 | { |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | // |
| 256 | // CXDecl Operations. |
| 257 | // |
| 258 | CXCursor clang_getCursorFromDecl(CXDecl) |
| 259 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 260 | return CXCursor(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 261 | } |
| 262 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 263 | { |
| 264 | return 0; |
| 265 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 266 | const char *clang_getDeclSpelling(CXDecl AnonDecl) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 267 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 268 | assert(AnonDecl && "Passed null CXDecl"); |
| 269 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 270 | |
| 271 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 272 | return OMD->getSelector().getAsString().c_str(); |
| 273 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 274 | if (ND->getIdentifier()) |
| 275 | return ND->getIdentifier()->getName(); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 276 | else |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 277 | return ""; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 278 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 279 | |
| 280 | const char *clang_getCursorSpelling(CXCursor C) |
| 281 | { |
| 282 | assert(C.decl && "CXCursor has null decl"); |
| 283 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 284 | |
| 285 | if (clang_isReference(C.kind)) { |
| 286 | switch (C.kind) { |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 287 | case CXCursor_ObjCSuperClassRef: |
| 288 | { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 289 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 290 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 291 | return OID->getSuperClass()->getIdentifier()->getName(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 292 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 293 | case CXCursor_ObjCClassRef: |
| 294 | { |
| 295 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 296 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 297 | return OID->getClassInterface()->getIdentifier()->getName(); |
| 298 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 299 | default: |
| 300 | return "<not implemented>"; |
| 301 | } |
| 302 | } |
| 303 | return clang_getDeclSpelling(C.decl); |
| 304 | } |
| 305 | |
| 306 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 307 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 308 | switch (Kind) { |
| 309 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 310 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 311 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 312 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 313 | case CXCursor_StructDecl: return "StructDecl"; |
| 314 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 315 | case CXCursor_ClassDecl: return "ClassDecl"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 316 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 317 | case CXCursor_VarDecl: return "VarDecl"; |
| 318 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 319 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 320 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 321 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 322 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 323 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 324 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 325 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 326 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 327 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 328 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 329 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 330 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 331 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame^] | 332 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 333 | default: return "<not implemented>"; |
| 334 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 335 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 336 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 337 | // |
| 338 | // CXCursor Operations. |
| 339 | // |
| 340 | CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, |
| 341 | unsigned line, unsigned column) |
| 342 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 343 | return CXCursor(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | CXCursorKind clang_getCursorKind(CXCursor) |
| 347 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 348 | return CXCursor_Invalid; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 351 | unsigned clang_isDeclaration(enum CXCursorKind K) |
| 352 | { |
| 353 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 354 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 355 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 356 | unsigned clang_isReference(enum CXCursorKind K) |
| 357 | { |
| 358 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 359 | } |
| 360 | |
| 361 | unsigned clang_isDefinition(enum CXCursorKind K) |
| 362 | { |
| 363 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 364 | } |
| 365 | |
| 366 | static SourceLocation getLocationFromCursor(CXCursor C, |
| 367 | SourceManager &SourceMgr, |
| 368 | NamedDecl *ND) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 369 | if (clang_isReference(C.kind)) { |
| 370 | switch (C.kind) { |
| 371 | case CXCursor_ObjCSuperClassRef: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 372 | { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 373 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 374 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 375 | return OID->getSuperClassLoc(); |
| 376 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 377 | default: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 378 | return SourceLocation(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 379 | } |
| 380 | } else { // We have a declaration or a definition. |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 381 | SourceLocation SLoc = ND->getLocation(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 382 | if (SLoc.isInvalid()) |
| 383 | return SourceLocation(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 384 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 385 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 388 | unsigned clang_getCursorLine(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 389 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 390 | assert(C.decl && "CXCursor has null decl"); |
| 391 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 392 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 393 | |
| 394 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 395 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 396 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 397 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 398 | unsigned clang_getCursorColumn(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 399 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 400 | assert(C.decl && "CXCursor has null decl"); |
| 401 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 402 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 403 | |
| 404 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 405 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 406 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 407 | const char *clang_getCursorSource(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 408 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 409 | assert(C.decl && "CXCursor has null decl"); |
| 410 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 411 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 412 | |
| 413 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 414 | return SourceMgr.getBufferName(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | // If CXCursorKind == Cursor_Reference, then this will return the referenced declaration. |
| 418 | // If CXCursorKind == Cursor_Declaration, then this will return the declaration. |
| 419 | CXDecl clang_getCursorDecl(CXCursor) |
| 420 | { |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | } // end extern "C" |