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