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