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 | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 22 | #include "clang/Basic/FileManager.h" |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/ASTUnit.h" |
| 25 | #include <cstdio> |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace idx; |
| 28 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 31 | static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) |
| 32 | { |
| 33 | NamedDecl *D = DRE->getDecl(); |
| 34 | if (isa<VarDecl>(D)) |
| 35 | return CXCursor_VarRef; |
| 36 | else if (isa<FunctionDecl>(D)) |
| 37 | return CXCursor_FunctionRef; |
| 38 | else if (isa<EnumConstantDecl>(D)) |
| 39 | return CXCursor_EnumConstantRef; |
| 40 | else |
| 41 | return CXCursor_NotImplemented; |
| 42 | } |
| 43 | |
| 44 | #if 0 |
| 45 | // Will be useful one day. |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 46 | class CRefVisitor : public StmtVisitor<CRefVisitor> { |
| 47 | CXDecl CDecl; |
| 48 | CXDeclIterator Callback; |
| 49 | CXClientData CData; |
| 50 | |
| 51 | void Call(enum CXCursorKind CK, Stmt *SRef) { |
| 52 | CXCursor C = { CK, CDecl, SRef }; |
| 53 | Callback(CDecl, C, CData); |
| 54 | } |
| 55 | |
| 56 | public: |
| 57 | CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : |
| 58 | CDecl(C), Callback(cback), CData(D) {} |
| 59 | |
| 60 | void VisitStmt(Stmt *S) { |
| 61 | for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end(); |
| 62 | C != CEnd; ++C) |
| 63 | Visit(*C); |
| 64 | } |
| 65 | void VisitDeclRefExpr(DeclRefExpr *Node) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 66 | Call(TranslateDeclRefExpr(Node), Node); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 67 | } |
| 68 | void VisitMemberExpr(MemberExpr *Node) { |
| 69 | Call(CXCursor_MemberRef, Node); |
| 70 | } |
| 71 | void VisitObjCMessageExpr(ObjCMessageExpr *Node) { |
| 72 | Call(CXCursor_ObjCSelectorRef, Node); |
| 73 | } |
| 74 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
| 75 | Call(CXCursor_ObjCIvarRef, Node); |
| 76 | } |
| 77 | }; |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 78 | #endif |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 79 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 80 | // Translation Unit Visitor. |
| 81 | class TUVisitor : public DeclVisitor<TUVisitor> { |
| 82 | CXTranslationUnit TUnit; |
| 83 | CXTranslationUnitIterator Callback; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 84 | CXClientData CData; |
| 85 | |
| 86 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 87 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 88 | Callback(TUnit, C, CData); |
| 89 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 90 | public: |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 91 | TUVisitor(CXTranslationUnit CTU, |
| 92 | CXTranslationUnitIterator cback, CXClientData D) : |
| 93 | TUnit(CTU), Callback(cback), CData(D) {} |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 94 | |
| 95 | void VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 96 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 97 | } |
| 98 | void VisitDeclContext(DeclContext *DC) { |
| 99 | for (DeclContext::decl_iterator |
| 100 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 101 | Visit(*I); |
| 102 | } |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 103 | void VisitTypedefDecl(TypedefDecl *ND) { |
| 104 | Call(CXCursor_TypedefDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 105 | } |
| 106 | void VisitTagDecl(TagDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 107 | switch (ND->getTagKind()) { |
| 108 | case TagDecl::TK_struct: |
| 109 | Call(CXCursor_StructDecl, ND); |
| 110 | break; |
| 111 | case TagDecl::TK_class: |
| 112 | Call(CXCursor_ClassDecl, ND); |
| 113 | break; |
| 114 | case TagDecl::TK_union: |
| 115 | Call(CXCursor_UnionDecl, ND); |
| 116 | break; |
| 117 | case TagDecl::TK_enum: |
| 118 | Call(CXCursor_EnumDecl, ND); |
| 119 | break; |
| 120 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 121 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 122 | void VisitVarDecl(VarDecl *ND) { |
| 123 | Call(CXCursor_VarDecl, ND); |
| 124 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 125 | void VisitFunctionDecl(FunctionDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 126 | Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn |
| 127 | : CXCursor_FunctionDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 128 | } |
| 129 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 130 | Call(CXCursor_ObjCInterfaceDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 131 | } |
| 132 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 133 | Call(CXCursor_ObjCCategoryDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 134 | } |
| 135 | void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 136 | Call(CXCursor_ObjCProtocolDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 137 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 138 | void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) { |
| 139 | Call(CXCursor_ObjCClassDefn, ND); |
| 140 | } |
| 141 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) { |
| 142 | Call(CXCursor_ObjCCategoryDefn, ND); |
| 143 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 146 | // Declaration visitor. |
| 147 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 148 | CXDecl CDecl; |
| 149 | CXDeclIterator Callback; |
| 150 | CXClientData CData; |
| 151 | |
| 152 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 153 | // Disable the callback when the context is equal to the visiting decl. |
| 154 | if (CDecl == ND && !clang_isReference(CK)) |
| 155 | return; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 156 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 157 | Callback(CDecl, C, CData); |
| 158 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 159 | public: |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 160 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : |
| 161 | CDecl(C), Callback(cback), CData(D) {} |
| 162 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 163 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 164 | // Issue callbacks for the containing class. |
| 165 | Call(CXCursor_ObjCClassRef, ND); |
| 166 | // FIXME: Issue callbacks for protocol refs. |
| 167 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 168 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 169 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 170 | // Issue callbacks for super class. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 171 | if (D->getSuperClass()) |
| 172 | Call(CXCursor_ObjCSuperClassRef, D); |
| 173 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 174 | for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 175 | E = D->protocol_end(); I != E; ++I) |
| 176 | Call(CXCursor_ObjCProtocolRef, *I); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 177 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 178 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 179 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 180 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 181 | E = PID->protocol_end(); I != E; ++I) |
| 182 | Call(CXCursor_ObjCProtocolRef, *I); |
| 183 | |
| 184 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 185 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 186 | void VisitTagDecl(TagDecl *D) { |
| 187 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 188 | } |
| 189 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 190 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 191 | } |
| 192 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 193 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 194 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 195 | void VisitDeclContext(DeclContext *DC) { |
| 196 | for (DeclContext::decl_iterator |
| 197 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 198 | Visit(*I); |
| 199 | } |
| 200 | void VisitEnumConstantDecl(EnumConstantDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 201 | Call(CXCursor_EnumConstantDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 202 | } |
| 203 | void VisitFieldDecl(FieldDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 204 | Call(CXCursor_FieldDecl, ND); |
| 205 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 206 | void VisitVarDecl(VarDecl *ND) { |
| 207 | Call(CXCursor_VarDecl, ND); |
| 208 | } |
| 209 | void VisitParmVarDecl(ParmVarDecl *ND) { |
| 210 | Call(CXCursor_ParmDecl, ND); |
| 211 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 212 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 213 | Call(CXCursor_ObjCPropertyDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 214 | } |
| 215 | void VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 216 | Call(CXCursor_ObjCIvarDecl, ND); |
| 217 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 218 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 219 | if (ND->isThisDeclarationADefinition()) { |
| 220 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 221 | #if 0 |
| 222 | // Not currently needed. |
| 223 | CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody()); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 224 | CRefVisitor RVisit(CDecl, Callback, CData); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 225 | RVisit.Visit(Body); |
| 226 | #endif |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 229 | void VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 230 | if (ND->getBody()) { |
| 231 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 232 | : CXCursor_ObjCClassMethodDefn, ND); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 233 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 234 | } else |
| 235 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 236 | : CXCursor_ObjCClassMethodDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 237 | } |
| 238 | }; |
| 239 | |
| 240 | } |
| 241 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 242 | extern "C" { |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 243 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 244 | CXIndex clang_createIndex() |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 245 | { |
Daniel Dunbar | a390759 | 2009-09-21 03:03:22 +0000 | [diff] [blame] | 246 | // FIXME: Program is leaked. |
| 247 | return new Indexer(*new Program()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 250 | void clang_disposeIndex(CXIndex CIdx) |
| 251 | { |
| 252 | assert(CIdx && "Passed null CXIndex"); |
| 253 | delete static_cast<Indexer *>(CIdx); |
| 254 | } |
| 255 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 256 | // FIXME: need to pass back error info. |
| 257 | CXTranslationUnit clang_createTranslationUnit( |
| 258 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 259 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 260 | assert(CIdx && "Passed null CXIndex"); |
| 261 | Indexer *CXXIdx = static_cast<Indexer *>(CIdx); |
| 262 | std::string astName(ast_filename); |
| 263 | std::string ErrMsg; |
| 264 | |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 265 | return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(), |
| 266 | CXXIdx->getFileManager(), &ErrMsg); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 269 | void clang_disposeTranslationUnit( |
| 270 | CXTranslationUnit CTUnit) |
| 271 | { |
| 272 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 273 | delete static_cast<ASTUnit *>(CTUnit); |
| 274 | } |
| 275 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 276 | const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) |
| 277 | { |
| 278 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 279 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 280 | return CXXUnit->getOriginalSourceFileName().c_str(); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 281 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 282 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 283 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
| 284 | CXTranslationUnitIterator callback, |
| 285 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 286 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 287 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 288 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 289 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 290 | |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 291 | TUVisitor DVisit(CTUnit, callback, CData); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 292 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 295 | void clang_loadDeclaration(CXDecl Dcl, |
| 296 | CXDeclIterator callback, |
| 297 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 298 | { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 299 | assert(Dcl && "Passed null CXDecl"); |
| 300 | |
| 301 | CDeclVisitor DVisit(Dcl, callback, CData); |
| 302 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 305 | // Some notes on CXEntity: |
| 306 | // |
| 307 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 308 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 309 | // entity for 2 different types). For example: |
| 310 | // |
| 311 | // module1.m: @interface Foo @end Foo *x; |
| 312 | // module2.m: void Foo(int); |
| 313 | // |
| 314 | // - Since the unique name spans translation units, static data/functions |
| 315 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 316 | // As a result, there will be no entity for the following: |
| 317 | // |
| 318 | // module.m: static void Foo() { } |
| 319 | // |
| 320 | |
| 321 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 322 | const char *clang_getDeclarationName(CXEntity) |
| 323 | { |
| 324 | return ""; |
| 325 | } |
| 326 | const char *clang_getURI(CXEntity) |
| 327 | { |
| 328 | return ""; |
| 329 | } |
| 330 | |
| 331 | CXEntity clang_getEntity(const char *URI) |
| 332 | { |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | // |
| 337 | // CXDecl Operations. |
| 338 | // |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 339 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 340 | { |
| 341 | return 0; |
| 342 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 343 | const char *clang_getDeclSpelling(CXDecl AnonDecl) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 344 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 345 | assert(AnonDecl && "Passed null CXDecl"); |
| 346 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 347 | |
| 348 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 349 | return OMD->getSelector().getAsString().c_str(); |
| 350 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 351 | if (ND->getIdentifier()) |
| 352 | return ND->getIdentifier()->getName(); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 353 | else |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 354 | return ""; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 355 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 356 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 357 | unsigned clang_getDeclLine(CXDecl AnonDecl) |
| 358 | { |
| 359 | assert(AnonDecl && "Passed null CXDecl"); |
| 360 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 361 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 362 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 363 | } |
| 364 | |
| 365 | unsigned clang_getDeclColumn(CXDecl AnonDecl) |
| 366 | { |
| 367 | assert(AnonDecl && "Passed null CXDecl"); |
| 368 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 369 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 370 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 373 | const char *clang_getDeclSource(CXDecl AnonDecl) |
| 374 | { |
| 375 | assert(AnonDecl && "Passed null CXDecl"); |
| 376 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 377 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 378 | return SourceMgr.getBufferName(ND->getLocation()); |
| 379 | } |
| 380 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 381 | const char *clang_getCursorSpelling(CXCursor C) |
| 382 | { |
| 383 | assert(C.decl && "CXCursor has null decl"); |
| 384 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 385 | |
| 386 | if (clang_isReference(C.kind)) { |
| 387 | switch (C.kind) { |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 388 | case CXCursor_ObjCSuperClassRef: |
| 389 | { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 390 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 391 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 392 | return OID->getSuperClass()->getIdentifier()->getName(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 393 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 394 | case CXCursor_ObjCClassRef: |
| 395 | { |
| 396 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 397 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 398 | return OID->getClassInterface()->getIdentifier()->getName(); |
| 399 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 400 | case CXCursor_ObjCProtocolRef: |
| 401 | { |
| 402 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 403 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 404 | return OID->getIdentifier()->getName(); |
| 405 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 406 | case CXCursor_ObjCSelectorRef: |
| 407 | { |
| 408 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 409 | static_cast<Stmt *>(C.stmt)); |
| 410 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 411 | return OME->getSelector().getAsString().c_str(); |
| 412 | } |
| 413 | case CXCursor_VarRef: |
| 414 | case CXCursor_FunctionRef: |
| 415 | case CXCursor_EnumConstantRef: |
| 416 | { |
| 417 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 418 | static_cast<Stmt *>(C.stmt)); |
| 419 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 420 | return DRE->getDecl()->getIdentifier()->getName(); |
| 421 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 422 | default: |
| 423 | return "<not implemented>"; |
| 424 | } |
| 425 | } |
| 426 | return clang_getDeclSpelling(C.decl); |
| 427 | } |
| 428 | |
| 429 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 430 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 431 | switch (Kind) { |
| 432 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 433 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 434 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 435 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 436 | case CXCursor_StructDecl: return "StructDecl"; |
| 437 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 438 | case CXCursor_ClassDecl: return "ClassDecl"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 439 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 440 | case CXCursor_VarDecl: return "VarDecl"; |
| 441 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 442 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 443 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 444 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 445 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 446 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 447 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 448 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 449 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 450 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 451 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 452 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 453 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 454 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 455 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 456 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 457 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
| 458 | |
| 459 | case CXCursor_VarRef: return "VarRef"; |
| 460 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 461 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 462 | case CXCursor_MemberRef: return "MemberRef"; |
| 463 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 464 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 465 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 466 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 467 | default: return "<not implemented>"; |
| 468 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 469 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 470 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 471 | static enum CXCursorKind TranslateKind(Decl *D) { |
| 472 | switch (D->getKind()) { |
| 473 | case Decl::Function: return CXCursor_FunctionDecl; |
| 474 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 475 | case Decl::Enum: return CXCursor_EnumDecl; |
| 476 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 477 | case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class |
| 478 | case Decl::Field: return CXCursor_FieldDecl; |
| 479 | case Decl::Var: return CXCursor_VarDecl; |
| 480 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 481 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 482 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 483 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 484 | case Decl::ObjCMethod: { |
| 485 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D); |
| 486 | if (MD->isInstanceMethod()) |
| 487 | return CXCursor_ObjCInstanceMethodDecl; |
| 488 | return CXCursor_ObjCClassMethodDecl; |
| 489 | } |
| 490 | default: break; |
| 491 | } |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 492 | return CXCursor_NotImplemented; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 493 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 494 | // |
| 495 | // CXCursor Operations. |
| 496 | // |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 497 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 498 | unsigned line, unsigned column) |
| 499 | { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 500 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 501 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 502 | |
| 503 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 504 | const FileEntry *File = FMgr.getFile(source_name, |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 505 | source_name+strlen(source_name)); |
| 506 | if (!File) { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 507 | CXCursor C = { CXCursor_InvalidFile, 0, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 508 | return C; |
| 509 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 510 | SourceLocation SLoc = |
| 511 | CXXUnit->getSourceManager().getLocation(File, line, column); |
| 512 | |
| 513 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc); |
| 514 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame^] | 515 | Decl *Dcl = ALoc.getParentDecl(); |
| 516 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 517 | if (Dcl) { |
| 518 | if (Stm) { |
| 519 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) { |
| 520 | CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm }; |
| 521 | return C; |
| 522 | } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) { |
| 523 | CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp }; |
| 524 | return C; |
| 525 | } |
| 526 | // Fall through...treat as a decl, not a ref. |
| 527 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 528 | CXCursor C = { TranslateKind(Dcl), Dcl, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 529 | return C; |
| 530 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 531 | CXCursor C = { CXCursor_NoDeclFound, 0, 0 }; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 532 | return C; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 535 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) |
| 536 | { |
| 537 | assert(AnonDecl && "Passed null CXDecl"); |
| 538 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 539 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 540 | CXCursor C = { TranslateKind(ND), ND, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 541 | return C; |
| 542 | } |
| 543 | |
| 544 | unsigned clang_isInvalid(enum CXCursorKind K) |
| 545 | { |
| 546 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 547 | } |
| 548 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 549 | unsigned clang_isDeclaration(enum CXCursorKind K) |
| 550 | { |
| 551 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 552 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 553 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 554 | unsigned clang_isReference(enum CXCursorKind K) |
| 555 | { |
| 556 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 557 | } |
| 558 | |
| 559 | unsigned clang_isDefinition(enum CXCursorKind K) |
| 560 | { |
| 561 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 562 | } |
| 563 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 564 | CXCursorKind clang_getCursorKind(CXCursor C) |
| 565 | { |
| 566 | return C.kind; |
| 567 | } |
| 568 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 569 | static Decl *getDeclFromExpr(Stmt *E) { |
| 570 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 571 | return RefExpr->getDecl(); |
| 572 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 573 | return ME->getMemberDecl(); |
| 574 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 575 | return RE->getDecl(); |
| 576 | |
| 577 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 578 | return getDeclFromExpr(CE->getCallee()); |
| 579 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 580 | return getDeclFromExpr(CE->getSubExpr()); |
| 581 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 582 | return OME->getMethodDecl(); |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 587 | CXDecl clang_getCursorDecl(CXCursor C) |
| 588 | { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 589 | if (clang_isDeclaration(C.kind)) |
| 590 | return C.decl; |
| 591 | |
| 592 | if (clang_isReference(C.kind)) { |
| 593 | if (C.stmt) |
| 594 | return getDeclFromExpr(static_cast<Stmt *>(C.stmt)); |
| 595 | else |
| 596 | return C.decl; |
| 597 | } |
| 598 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 601 | static SourceLocation getLocationFromCursor(CXCursor C, |
| 602 | SourceManager &SourceMgr, |
| 603 | NamedDecl *ND) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 604 | if (clang_isReference(C.kind)) { |
| 605 | switch (C.kind) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 606 | case CXCursor_ObjCSuperClassRef: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 607 | { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 608 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 609 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 610 | return OID->getSuperClassLoc(); |
| 611 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 612 | case CXCursor_ObjCProtocolRef: |
| 613 | { |
| 614 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 615 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 616 | return OID->getLocation(); |
| 617 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 618 | case CXCursor_ObjCSelectorRef: |
| 619 | { |
| 620 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 621 | static_cast<Stmt *>(C.stmt)); |
| 622 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 623 | return OME->getLeftLoc(); /* FIXME: should be a range */ |
| 624 | } |
| 625 | case CXCursor_VarRef: |
| 626 | case CXCursor_FunctionRef: |
| 627 | case CXCursor_EnumConstantRef: |
| 628 | { |
| 629 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 630 | static_cast<Stmt *>(C.stmt)); |
| 631 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 632 | return DRE->getLocation(); |
| 633 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 634 | default: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 635 | return SourceLocation(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 636 | } |
| 637 | } else { // We have a declaration or a definition. |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 638 | SourceLocation SLoc; |
| 639 | switch (ND->getKind()) { |
| 640 | case Decl::ObjCInterface: |
| 641 | { |
| 642 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 643 | break; |
| 644 | } |
| 645 | case Decl::ObjCProtocol: |
| 646 | { |
| 647 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 648 | break; |
| 649 | } |
| 650 | default: |
| 651 | { |
| 652 | SLoc = ND->getLocation(); |
| 653 | break; |
| 654 | } |
| 655 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 656 | if (SLoc.isInvalid()) |
| 657 | return SourceLocation(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 658 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 659 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 662 | unsigned clang_getCursorLine(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 663 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 664 | assert(C.decl && "CXCursor has null decl"); |
| 665 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 666 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 667 | |
| 668 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 669 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 670 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 671 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 672 | unsigned clang_getCursorColumn(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 673 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 674 | assert(C.decl && "CXCursor has null decl"); |
| 675 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 676 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 677 | |
| 678 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 679 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 680 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 681 | const char *clang_getCursorSource(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 682 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 683 | assert(C.decl && "CXCursor has null decl"); |
| 684 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 685 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 686 | |
| 687 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 688 | return SourceMgr.getBufferName(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 691 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 692 | const char **startBuf, |
| 693 | const char **endBuf, |
| 694 | unsigned *startLine, |
| 695 | unsigned *startColumn, |
| 696 | unsigned *endLine, |
| 697 | unsigned *endColumn) |
| 698 | { |
| 699 | assert(C.decl && "CXCursor has null decl"); |
| 700 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 701 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 702 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 703 | |
| 704 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 705 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 706 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 707 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 708 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 709 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 710 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 711 | } |
| 712 | |
| 713 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 714 | } // end extern "C" |