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" |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 25 | #include "llvm/Config/config.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
| 28 | #include "llvm/System/Path.h" |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 29 | #include "llvm/System/Program.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 30 | |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 31 | #include <cstdio> |
Ted Kremenek | 49358d8 | 2009-10-19 21:17:25 +0000 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 34 | #ifdef LLVM_ON_WIN32 |
| 35 | #define WIN32_LEAN_AND_MEAN |
| 36 | #include <windows.h> |
| 37 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 38 | #include <dlfcn.h> |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame] | 39 | #endif |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 40 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 41 | using namespace clang; |
| 42 | using namespace idx; |
| 43 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 44 | namespace { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 45 | static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) |
| 46 | { |
| 47 | NamedDecl *D = DRE->getDecl(); |
| 48 | if (isa<VarDecl>(D)) |
| 49 | return CXCursor_VarRef; |
| 50 | else if (isa<FunctionDecl>(D)) |
| 51 | return CXCursor_FunctionRef; |
| 52 | else if (isa<EnumConstantDecl>(D)) |
| 53 | return CXCursor_EnumConstantRef; |
| 54 | else |
| 55 | return CXCursor_NotImplemented; |
| 56 | } |
| 57 | |
| 58 | #if 0 |
| 59 | // Will be useful one day. |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 60 | class CRefVisitor : public StmtVisitor<CRefVisitor> { |
| 61 | CXDecl CDecl; |
| 62 | CXDeclIterator Callback; |
| 63 | CXClientData CData; |
| 64 | |
| 65 | void Call(enum CXCursorKind CK, Stmt *SRef) { |
| 66 | CXCursor C = { CK, CDecl, SRef }; |
| 67 | Callback(CDecl, C, CData); |
| 68 | } |
| 69 | |
| 70 | public: |
| 71 | CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : |
| 72 | CDecl(C), Callback(cback), CData(D) {} |
| 73 | |
| 74 | void VisitStmt(Stmt *S) { |
| 75 | for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end(); |
| 76 | C != CEnd; ++C) |
| 77 | Visit(*C); |
| 78 | } |
| 79 | void VisitDeclRefExpr(DeclRefExpr *Node) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 80 | Call(TranslateDeclRefExpr(Node), Node); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 81 | } |
| 82 | void VisitMemberExpr(MemberExpr *Node) { |
| 83 | Call(CXCursor_MemberRef, Node); |
| 84 | } |
| 85 | void VisitObjCMessageExpr(ObjCMessageExpr *Node) { |
| 86 | Call(CXCursor_ObjCSelectorRef, Node); |
| 87 | } |
| 88 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
| 89 | Call(CXCursor_ObjCIvarRef, Node); |
| 90 | } |
| 91 | }; |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 92 | #endif |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 93 | |
| 94 | /// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted |
| 95 | /// warnings and errors. |
| 96 | class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient { |
| 97 | public: |
| 98 | virtual ~IgnoreDiagnosticsClient() {} |
| 99 | virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {} |
| 100 | }; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 101 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 102 | // Translation Unit Visitor. |
| 103 | class TUVisitor : public DeclVisitor<TUVisitor> { |
| 104 | CXTranslationUnit TUnit; |
| 105 | CXTranslationUnitIterator Callback; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 106 | CXClientData CData; |
| 107 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 108 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 109 | // to the visitor. Declarations with a PCH level greater than this value will |
| 110 | // be suppressed. |
| 111 | unsigned MaxPCHLevel; |
| 112 | |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 113 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 114 | // Filter any declarations that have a PCH level greater than what we allow. |
| 115 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 116 | return; |
| 117 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 118 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 119 | Callback(TUnit, C, CData); |
| 120 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 121 | public: |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 122 | TUVisitor(CXTranslationUnit CTU, |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 123 | CXTranslationUnitIterator cback, CXClientData D, |
| 124 | unsigned MaxPCHLevel) : |
| 125 | TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 126 | |
| 127 | void VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 128 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 129 | } |
| 130 | void VisitDeclContext(DeclContext *DC) { |
| 131 | for (DeclContext::decl_iterator |
| 132 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 133 | Visit(*I); |
| 134 | } |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 135 | void VisitTypedefDecl(TypedefDecl *ND) { |
| 136 | Call(CXCursor_TypedefDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 137 | } |
| 138 | void VisitTagDecl(TagDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 139 | switch (ND->getTagKind()) { |
| 140 | case TagDecl::TK_struct: |
| 141 | Call(CXCursor_StructDecl, ND); |
| 142 | break; |
| 143 | case TagDecl::TK_class: |
| 144 | Call(CXCursor_ClassDecl, ND); |
| 145 | break; |
| 146 | case TagDecl::TK_union: |
| 147 | Call(CXCursor_UnionDecl, ND); |
| 148 | break; |
| 149 | case TagDecl::TK_enum: |
| 150 | Call(CXCursor_EnumDecl, ND); |
| 151 | break; |
| 152 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 153 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 154 | void VisitVarDecl(VarDecl *ND) { |
| 155 | Call(CXCursor_VarDecl, ND); |
| 156 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 157 | void VisitFunctionDecl(FunctionDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 158 | Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn |
| 159 | : CXCursor_FunctionDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 160 | } |
| 161 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 162 | Call(CXCursor_ObjCInterfaceDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 163 | } |
| 164 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 165 | Call(CXCursor_ObjCCategoryDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 166 | } |
| 167 | void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 168 | Call(CXCursor_ObjCProtocolDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 169 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 170 | void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) { |
| 171 | Call(CXCursor_ObjCClassDefn, ND); |
| 172 | } |
| 173 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) { |
| 174 | Call(CXCursor_ObjCCategoryDefn, ND); |
| 175 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 178 | // Declaration visitor. |
| 179 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 180 | CXDecl CDecl; |
| 181 | CXDeclIterator Callback; |
| 182 | CXClientData CData; |
| 183 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 184 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 185 | // to the visitor. Declarations with a PCH level greater than this value will |
| 186 | // be suppressed. |
| 187 | unsigned MaxPCHLevel; |
| 188 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 189 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 190 | // Disable the callback when the context is equal to the visiting decl. |
| 191 | if (CDecl == ND && !clang_isReference(CK)) |
| 192 | return; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 193 | |
| 194 | // Filter any declarations that have a PCH level greater than what we allow. |
| 195 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 196 | return; |
| 197 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 198 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 199 | Callback(CDecl, C, CData); |
| 200 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 201 | public: |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 202 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D, |
| 203 | unsigned MaxPCHLevel) : |
| 204 | CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 205 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 206 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 207 | // Issue callbacks for the containing class. |
| 208 | Call(CXCursor_ObjCClassRef, ND); |
| 209 | // FIXME: Issue callbacks for protocol refs. |
| 210 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 211 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 212 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 213 | // Issue callbacks for super class. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 214 | if (D->getSuperClass()) |
| 215 | Call(CXCursor_ObjCSuperClassRef, D); |
| 216 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 217 | for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 218 | E = D->protocol_end(); I != E; ++I) |
| 219 | Call(CXCursor_ObjCProtocolRef, *I); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 220 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 221 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 222 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 223 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 224 | E = PID->protocol_end(); I != E; ++I) |
| 225 | Call(CXCursor_ObjCProtocolRef, *I); |
| 226 | |
| 227 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 228 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 229 | void VisitTagDecl(TagDecl *D) { |
| 230 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 231 | } |
| 232 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 233 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 234 | } |
| 235 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 236 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 237 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 238 | void VisitDeclContext(DeclContext *DC) { |
| 239 | for (DeclContext::decl_iterator |
| 240 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 241 | Visit(*I); |
| 242 | } |
| 243 | void VisitEnumConstantDecl(EnumConstantDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 244 | Call(CXCursor_EnumConstantDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 245 | } |
| 246 | void VisitFieldDecl(FieldDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 247 | Call(CXCursor_FieldDecl, ND); |
| 248 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 249 | void VisitVarDecl(VarDecl *ND) { |
| 250 | Call(CXCursor_VarDecl, ND); |
| 251 | } |
| 252 | void VisitParmVarDecl(ParmVarDecl *ND) { |
| 253 | Call(CXCursor_ParmDecl, ND); |
| 254 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 255 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 256 | Call(CXCursor_ObjCPropertyDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 257 | } |
| 258 | void VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 259 | Call(CXCursor_ObjCIvarDecl, ND); |
| 260 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 261 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 262 | if (ND->isThisDeclarationADefinition()) { |
| 263 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 264 | #if 0 |
| 265 | // Not currently needed. |
| 266 | CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody()); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 267 | CRefVisitor RVisit(CDecl, Callback, CData); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 268 | RVisit.Visit(Body); |
| 269 | #endif |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 272 | void VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 273 | if (ND->getBody()) { |
| 274 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 275 | : CXCursor_ObjCClassMethodDefn, ND); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 276 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 277 | } else |
| 278 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 279 | : CXCursor_ObjCClassMethodDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 280 | } |
| 281 | }; |
| 282 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 283 | class CIndexer : public Indexer { |
| 284 | public: |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 285 | explicit CIndexer(Program *prog) : Indexer(*prog), |
| 286 | OnlyLocalDecls(false), |
| 287 | DisplayDiagnostics(false) {} |
Ted Kremenek | dff7689 | 2009-10-17 06:21:47 +0000 | [diff] [blame] | 288 | |
| 289 | virtual ~CIndexer() { delete &getProgram(); } |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 290 | |
| 291 | /// \brief Whether we only want to see "local" declarations (that did not |
| 292 | /// come from a previous precompiled header). If false, we want to see all |
| 293 | /// declarations. |
| 294 | bool getOnlyLocalDecls() const { return OnlyLocalDecls; } |
| 295 | void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } |
Benjamin Kramer | 9670762 | 2009-10-18 11:10:55 +0000 | [diff] [blame] | 296 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 297 | void setDisplayDiagnostics(bool Display = true) { |
| 298 | DisplayDiagnostics = Display; |
| 299 | } |
| 300 | bool getDisplayDiagnostics() const { return DisplayDiagnostics; } |
| 301 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 302 | /// \brief Get the path of the clang binary. |
Benjamin Kramer | c5a9e95 | 2009-10-19 10:20:24 +0000 | [diff] [blame] | 303 | const llvm::sys::Path& getClangPath(); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 304 | private: |
| 305 | bool OnlyLocalDecls; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 306 | bool DisplayDiagnostics; |
| 307 | |
Benjamin Kramer | c5a9e95 | 2009-10-19 10:20:24 +0000 | [diff] [blame] | 308 | llvm::sys::Path ClangPath; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 309 | }; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 310 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 311 | const llvm::sys::Path& CIndexer::getClangPath() { |
| 312 | // Did we already compute the path? |
| 313 | if (!ClangPath.empty()) |
| 314 | return ClangPath; |
| 315 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 316 | // Find the location where this library lives (libCIndex.dylib). |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 317 | #ifdef LLVM_ON_WIN32 |
| 318 | MEMORY_BASIC_INFORMATION mbi; |
| 319 | char path[MAX_PATH]; |
Benjamin Kramer | edcd828 | 2009-10-18 16:20:58 +0000 | [diff] [blame] | 320 | VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi, |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 321 | sizeof(mbi)); |
| 322 | GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); |
| 323 | |
| 324 | llvm::sys::Path CIndexPath(path); |
| 325 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 326 | // This silly cast below avoids a C++ warning. |
| 327 | Dl_info info; |
| 328 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
| 329 | assert(0 && "Call to dladdr() failed"); |
| 330 | |
| 331 | llvm::sys::Path CIndexPath(info.dli_fname); |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame] | 332 | #endif |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 333 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 334 | // We now have the CIndex directory, locate clang relative to it. |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 335 | CIndexPath.eraseComponent(); |
| 336 | CIndexPath.eraseComponent(); |
| 337 | CIndexPath.appendComponent("bin"); |
| 338 | CIndexPath.appendComponent("clang"); |
| 339 | |
| 340 | // Cache our result. |
| 341 | ClangPath = CIndexPath; |
| 342 | return ClangPath; |
| 343 | } |
| 344 | |
| 345 | } |
| 346 | |
| 347 | extern "C" { |
| 348 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 349 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 350 | int displayDiagnostics) |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 351 | { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 352 | CIndexer *CIdxr = new CIndexer(new Program()); |
| 353 | if (excludeDeclarationsFromPCH) |
| 354 | CIdxr->setOnlyLocalDecls(); |
| 355 | if (displayDiagnostics) |
| 356 | CIdxr->setDisplayDiagnostics(); |
| 357 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 360 | void clang_disposeIndex(CXIndex CIdx) |
| 361 | { |
| 362 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 363 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 366 | // FIXME: need to pass back error info. |
| 367 | CXTranslationUnit clang_createTranslationUnit( |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 368 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 369 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 370 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 371 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 372 | std::string astName(ast_filename); |
| 373 | std::string ErrMsg; |
| 374 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 375 | return ASTUnit::LoadFromPCHFile(astName, &ErrMsg, |
| 376 | CXXIdx->getDisplayDiagnostics() ? |
| 377 | NULL : new IgnoreDiagnosticsClient(), |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 378 | CXXIdx->getOnlyLocalDecls(), |
| 379 | /* UseBumpAllocator = */ true); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 382 | CXTranslationUnit clang_createTranslationUnitFromSourceFile( |
| 383 | CXIndex CIdx, |
| 384 | const char *source_filename, |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 385 | int num_command_line_args, const char **command_line_args) { |
| 386 | assert(CIdx && "Passed null CXIndex"); |
| 387 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 388 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame^] | 389 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 390 | std::vector<const char *> argv; |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame^] | 391 | |
| 392 | // First add the complete path to the 'clang' executable. |
| 393 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 394 | argv.push_back(ClangPath.c_str()); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame^] | 395 | |
| 396 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 397 | argv.push_back("-emit-ast"); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame^] | 398 | |
| 399 | // The 'source_filename' argument is optional. If the caller does not |
| 400 | // specify it then it is assumed that the source file is specified |
| 401 | // in the actual argument list. |
| 402 | if (source_filename) |
| 403 | argv.push_back(source_filename); |
| 404 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 405 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame^] | 406 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 407 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 408 | argv.push_back(tmpnam(astTmpFile)); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame^] | 409 | |
| 410 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 411 | for (int i = 0; i < num_command_line_args; ++i) |
| 412 | if (const char *arg = command_line_args[i]) { |
| 413 | if (strcmp(arg, "-o") == 0) { |
| 414 | ++i; // Also skip the matching argument. |
| 415 | continue; |
| 416 | } |
| 417 | if (strcmp(arg, "-emit-ast") == 0 || |
| 418 | strcmp(arg, "-c") == 0 || |
| 419 | strcmp(arg, "-fsyntax-only") == 0) { |
| 420 | continue; |
| 421 | } |
| 422 | |
| 423 | // Keep the argument. |
| 424 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 425 | } |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame^] | 426 | |
| 427 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 428 | argv.push_back(NULL); |
| 429 | |
Ted Kremenek | c46e463 | 2009-10-19 22:27:32 +0000 | [diff] [blame] | 430 | #ifndef LLVM_ON_WIN32 |
| 431 | llvm::sys::Path DevNull("/dev/null"); |
| 432 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL }; |
| 433 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], NULL, |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 434 | !CXXIdx->getDisplayDiagnostics() ? |
| 435 | &Redirects[0] : NULL); |
Ted Kremenek | 1374598 | 2009-10-19 22:15:09 +0000 | [diff] [blame] | 436 | #else |
| 437 | // FIXME: I don't know what is the equivalent '/dev/null' redirect for |
| 438 | // Windows for this API. |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 439 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0]); |
Ted Kremenek | 1374598 | 2009-10-19 22:15:09 +0000 | [diff] [blame] | 440 | #endif |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 441 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 442 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 443 | ASTUnit *ATU = static_cast<ASTUnit *>( |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 444 | clang_createTranslationUnit(CIdx, astTmpFile)); |
| 445 | if (ATU) |
| 446 | ATU->unlinkTemporaryFile(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 447 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 450 | void clang_disposeTranslationUnit( |
| 451 | CXTranslationUnit CTUnit) |
| 452 | { |
| 453 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 454 | delete static_cast<ASTUnit *>(CTUnit); |
| 455 | } |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 456 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 457 | const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) |
| 458 | { |
| 459 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 460 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 461 | return CXXUnit->getOriginalSourceFileName().c_str(); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 462 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 463 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 464 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
| 465 | CXTranslationUnitIterator callback, |
| 466 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 467 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 468 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 469 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 470 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 471 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 472 | TUVisitor DVisit(CTUnit, callback, CData, |
| 473 | CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 474 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 477 | void clang_loadDeclaration(CXDecl Dcl, |
| 478 | CXDeclIterator callback, |
| 479 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 480 | { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 481 | assert(Dcl && "Passed null CXDecl"); |
| 482 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 483 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 484 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 485 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 488 | // Some notes on CXEntity: |
| 489 | // |
| 490 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 491 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 492 | // entity for 2 different types). For example: |
| 493 | // |
| 494 | // module1.m: @interface Foo @end Foo *x; |
| 495 | // module2.m: void Foo(int); |
| 496 | // |
| 497 | // - Since the unique name spans translation units, static data/functions |
| 498 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 499 | // As a result, there will be no entity for the following: |
| 500 | // |
| 501 | // module.m: static void Foo() { } |
| 502 | // |
| 503 | |
| 504 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 505 | const char *clang_getDeclarationName(CXEntity) |
| 506 | { |
| 507 | return ""; |
| 508 | } |
| 509 | const char *clang_getURI(CXEntity) |
| 510 | { |
| 511 | return ""; |
| 512 | } |
| 513 | |
| 514 | CXEntity clang_getEntity(const char *URI) |
| 515 | { |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | // |
| 520 | // CXDecl Operations. |
| 521 | // |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 522 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 523 | { |
| 524 | return 0; |
| 525 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 526 | const char *clang_getDeclSpelling(CXDecl AnonDecl) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 527 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 528 | assert(AnonDecl && "Passed null CXDecl"); |
| 529 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 530 | |
| 531 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 532 | return OMD->getSelector().getAsString().c_str(); |
| 533 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 534 | if (ND->getIdentifier()) |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 535 | return ND->getIdentifier()->getNameStart(); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 536 | else |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 537 | return ""; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 538 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 539 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 540 | unsigned clang_getDeclLine(CXDecl AnonDecl) |
| 541 | { |
| 542 | assert(AnonDecl && "Passed null CXDecl"); |
| 543 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 544 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 545 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 546 | } |
| 547 | |
| 548 | unsigned clang_getDeclColumn(CXDecl AnonDecl) |
| 549 | { |
| 550 | assert(AnonDecl && "Passed null CXDecl"); |
| 551 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 552 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 553 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 556 | const char *clang_getDeclSource(CXDecl AnonDecl) |
| 557 | { |
| 558 | assert(AnonDecl && "Passed null CXDecl"); |
| 559 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 560 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 561 | return SourceMgr.getBufferName(ND->getLocation()); |
| 562 | } |
| 563 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 564 | const char *clang_getCursorSpelling(CXCursor C) |
| 565 | { |
| 566 | assert(C.decl && "CXCursor has null decl"); |
| 567 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 568 | |
| 569 | if (clang_isReference(C.kind)) { |
| 570 | switch (C.kind) { |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 571 | case CXCursor_ObjCSuperClassRef: { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 572 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 573 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 574 | return OID->getSuperClass()->getIdentifier()->getNameStart(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 575 | } |
| 576 | case CXCursor_ObjCClassRef: { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 577 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 578 | return OID->getIdentifier()->getNameStart(); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 579 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 580 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 581 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 582 | return OID->getClassInterface()->getIdentifier()->getNameStart(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 583 | } |
| 584 | case CXCursor_ObjCProtocolRef: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 585 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 586 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 587 | return OID->getIdentifier()->getNameStart(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 588 | } |
| 589 | case CXCursor_ObjCSelectorRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 590 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 591 | static_cast<Stmt *>(C.stmt)); |
| 592 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 593 | return OME->getSelector().getAsString().c_str(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 594 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 595 | case CXCursor_VarRef: |
| 596 | case CXCursor_FunctionRef: |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 597 | case CXCursor_EnumConstantRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 598 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 599 | static_cast<Stmt *>(C.stmt)); |
| 600 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 601 | return DRE->getDecl()->getIdentifier()->getNameStart(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 602 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 603 | default: |
| 604 | return "<not implemented>"; |
| 605 | } |
| 606 | } |
| 607 | return clang_getDeclSpelling(C.decl); |
| 608 | } |
| 609 | |
| 610 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 611 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 612 | switch (Kind) { |
| 613 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 614 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 615 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 616 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 617 | case CXCursor_StructDecl: return "StructDecl"; |
| 618 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 619 | case CXCursor_ClassDecl: return "ClassDecl"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 620 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 621 | case CXCursor_VarDecl: return "VarDecl"; |
| 622 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 623 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 624 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 625 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 626 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 627 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 628 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 629 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 630 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 631 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 632 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 633 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 634 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 635 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 636 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 637 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 638 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
| 639 | |
| 640 | case CXCursor_VarRef: return "VarRef"; |
| 641 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 642 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 643 | case CXCursor_MemberRef: return "MemberRef"; |
| 644 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 645 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 646 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 647 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 648 | default: return "<not implemented>"; |
| 649 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 650 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 651 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 652 | static enum CXCursorKind TranslateKind(Decl *D) { |
| 653 | switch (D->getKind()) { |
| 654 | case Decl::Function: return CXCursor_FunctionDecl; |
| 655 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 656 | case Decl::Enum: return CXCursor_EnumDecl; |
| 657 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 658 | case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class |
| 659 | case Decl::Field: return CXCursor_FieldDecl; |
| 660 | case Decl::Var: return CXCursor_VarDecl; |
| 661 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 662 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 663 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 664 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 665 | case Decl::ObjCMethod: { |
| 666 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D); |
| 667 | if (MD->isInstanceMethod()) |
| 668 | return CXCursor_ObjCInstanceMethodDecl; |
| 669 | return CXCursor_ObjCClassMethodDecl; |
| 670 | } |
| 671 | default: break; |
| 672 | } |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 673 | return CXCursor_NotImplemented; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 674 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 675 | // |
| 676 | // CXCursor Operations. |
| 677 | // |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 678 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Steve Naroff | 6a6de8b | 2009-10-21 13:56:23 +0000 | [diff] [blame] | 679 | unsigned line, unsigned column, |
| 680 | CXDecl RelativeToDecl) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 681 | { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 682 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 683 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 684 | |
| 685 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 686 | const FileEntry *File = FMgr.getFile(source_name, |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 687 | source_name+strlen(source_name)); |
| 688 | if (!File) { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 689 | CXCursor C = { CXCursor_InvalidFile, 0, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 690 | return C; |
| 691 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 692 | SourceLocation SLoc = |
| 693 | CXXUnit->getSourceManager().getLocation(File, line, column); |
| 694 | |
Steve Naroff | 6a6de8b | 2009-10-21 13:56:23 +0000 | [diff] [blame] | 695 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc, |
| 696 | static_cast<NamedDecl *>(RelativeToDecl)); |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 697 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 698 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 699 | if (ALoc.isNamedRef()) |
| 700 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 701 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 702 | if (Dcl) { |
| 703 | if (Stm) { |
| 704 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) { |
| 705 | CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm }; |
| 706 | return C; |
| 707 | } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) { |
| 708 | CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp }; |
| 709 | return C; |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 710 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 711 | // Fall through...treat as a decl, not a ref. |
| 712 | } |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 713 | if (ALoc.isNamedRef()) { |
| 714 | if (isa<ObjCInterfaceDecl>(Dcl)) { |
| 715 | CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() }; |
| 716 | return C; |
| 717 | } |
| 718 | if (isa<ObjCProtocolDecl>(Dcl)) { |
| 719 | CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() }; |
| 720 | return C; |
| 721 | } |
| 722 | } |
| 723 | CXCursor C = { TranslateKind(Dcl), Dcl, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 724 | return C; |
| 725 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 726 | CXCursor C = { CXCursor_NoDeclFound, 0, 0 }; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 727 | return C; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 730 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) |
| 731 | { |
| 732 | assert(AnonDecl && "Passed null CXDecl"); |
| 733 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 734 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 735 | CXCursor C = { TranslateKind(ND), ND, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 736 | return C; |
| 737 | } |
| 738 | |
| 739 | unsigned clang_isInvalid(enum CXCursorKind K) |
| 740 | { |
| 741 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 742 | } |
| 743 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 744 | unsigned clang_isDeclaration(enum CXCursorKind K) |
| 745 | { |
| 746 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 747 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 748 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 749 | unsigned clang_isReference(enum CXCursorKind K) |
| 750 | { |
| 751 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 752 | } |
| 753 | |
| 754 | unsigned clang_isDefinition(enum CXCursorKind K) |
| 755 | { |
| 756 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 757 | } |
| 758 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 759 | CXCursorKind clang_getCursorKind(CXCursor C) |
| 760 | { |
| 761 | return C.kind; |
| 762 | } |
| 763 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 764 | static Decl *getDeclFromExpr(Stmt *E) { |
| 765 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 766 | return RefExpr->getDecl(); |
| 767 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 768 | return ME->getMemberDecl(); |
| 769 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 770 | return RE->getDecl(); |
| 771 | |
| 772 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 773 | return getDeclFromExpr(CE->getCallee()); |
| 774 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 775 | return getDeclFromExpr(CE->getSubExpr()); |
| 776 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 777 | return OME->getMethodDecl(); |
| 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 782 | CXDecl clang_getCursorDecl(CXCursor C) |
| 783 | { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 784 | if (clang_isDeclaration(C.kind)) |
| 785 | return C.decl; |
| 786 | |
| 787 | if (clang_isReference(C.kind)) { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 788 | if (C.stmt) { |
Steve Naroff | f9adf8f | 2009-10-05 17:58:19 +0000 | [diff] [blame] | 789 | if (C.kind == CXCursor_ObjCClassRef || |
| 790 | C.kind == CXCursor_ObjCProtocolRef) |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 791 | return static_cast<Stmt *>(C.stmt); |
| 792 | else |
| 793 | return getDeclFromExpr(static_cast<Stmt *>(C.stmt)); |
| 794 | } else |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 795 | return C.decl; |
| 796 | } |
| 797 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 800 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 801 | static SourceLocation getLocationFromCursor(CXCursor C, |
| 802 | SourceManager &SourceMgr, |
| 803 | NamedDecl *ND) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 804 | if (clang_isReference(C.kind)) { |
| 805 | switch (C.kind) { |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 806 | case CXCursor_ObjCClassRef: { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 807 | if (isa<ObjCInterfaceDecl>(ND)) { |
| 808 | // FIXME: This is a hack (storing the parent decl in the stmt slot). |
| 809 | NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt); |
| 810 | return parentDecl->getLocation(); |
| 811 | } |
| 812 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 813 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 814 | return OID->getClassInterface()->getLocation(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 815 | } |
| 816 | case CXCursor_ObjCSuperClassRef: { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 817 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 818 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 819 | return OID->getSuperClassLoc(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 820 | } |
| 821 | case CXCursor_ObjCProtocolRef: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 822 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 823 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 824 | return OID->getLocation(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 825 | } |
| 826 | case CXCursor_ObjCSelectorRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 827 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 828 | static_cast<Stmt *>(C.stmt)); |
| 829 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 830 | return OME->getLeftLoc(); /* FIXME: should be a range */ |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 831 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 832 | case CXCursor_VarRef: |
| 833 | case CXCursor_FunctionRef: |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 834 | case CXCursor_EnumConstantRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 835 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 836 | static_cast<Stmt *>(C.stmt)); |
| 837 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 838 | return DRE->getLocation(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 839 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 840 | default: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 841 | return SourceLocation(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 842 | } |
| 843 | } else { // We have a declaration or a definition. |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 844 | SourceLocation SLoc; |
| 845 | switch (ND->getKind()) { |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 846 | case Decl::ObjCInterface: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 847 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 848 | break; |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 849 | } |
| 850 | case Decl::ObjCProtocol: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 851 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 852 | break; |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 853 | } |
| 854 | default: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 855 | SLoc = ND->getLocation(); |
| 856 | break; |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 857 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 858 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 859 | if (SLoc.isInvalid()) |
| 860 | return SourceLocation(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 861 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 862 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 865 | unsigned clang_getCursorLine(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 866 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 867 | assert(C.decl && "CXCursor has null decl"); |
| 868 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 869 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 870 | |
| 871 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 872 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 873 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 874 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 875 | unsigned clang_getCursorColumn(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 876 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 877 | assert(C.decl && "CXCursor has null decl"); |
| 878 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 879 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 880 | |
| 881 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 882 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 883 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 884 | const char *clang_getCursorSource(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 885 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 886 | assert(C.decl && "CXCursor has null decl"); |
| 887 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 888 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 889 | |
| 890 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 891 | if (SLoc.isFileID()) |
| 892 | return SourceMgr.getBufferName(SLoc); |
| 893 | |
| 894 | // Retrieve the file in which the macro was instantiated, then provide that |
| 895 | // buffer name. |
| 896 | // FIXME: Do we want to give specific macro-instantiation information? |
| 897 | const llvm::MemoryBuffer *Buffer |
| 898 | = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first); |
| 899 | if (!Buffer) |
| 900 | return 0; |
| 901 | |
| 902 | return Buffer->getBufferIdentifier(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 905 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 906 | const char **startBuf, |
| 907 | const char **endBuf, |
| 908 | unsigned *startLine, |
| 909 | unsigned *startColumn, |
| 910 | unsigned *endLine, |
| 911 | unsigned *endColumn) |
| 912 | { |
| 913 | assert(C.decl && "CXCursor has null decl"); |
| 914 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 915 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 916 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 917 | |
| 918 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 919 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 920 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 921 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 922 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 923 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 924 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 925 | } |
| 926 | |
| 927 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 928 | } // end extern "C" |