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" |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame^] | 19 | #include "clang/Sema/CodeCompleteConsumer.h" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclVisitor.h" |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 23 | #include "clang/Basic/FileManager.h" |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/ASTUnit.h" |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame^] | 26 | #include "llvm/ADT/StringExtras.h" |
| 27 | #include "llvm/ADT/StringSwitch.h" |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 28 | #include "llvm/Config/config.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MemoryBuffer.h" |
| 31 | #include "llvm/System/Path.h" |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 32 | #include "llvm/System/Program.h" |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 34 | |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 35 | #include <cstdio> |
Ted Kremenek | 49358d8 | 2009-10-19 21:17:25 +0000 | [diff] [blame] | 36 | #include <vector> |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame^] | 37 | #include <sstream> |
Ted Kremenek | 49358d8 | 2009-10-19 21:17:25 +0000 | [diff] [blame] | 38 | |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 39 | #ifdef LLVM_ON_WIN32 |
| 40 | #define WIN32_LEAN_AND_MEAN |
| 41 | #include <windows.h> |
| 42 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 43 | #include <dlfcn.h> |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame] | 44 | #endif |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 45 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 46 | using namespace clang; |
| 47 | using namespace idx; |
| 48 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 49 | namespace { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 50 | static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) |
| 51 | { |
| 52 | NamedDecl *D = DRE->getDecl(); |
| 53 | if (isa<VarDecl>(D)) |
| 54 | return CXCursor_VarRef; |
| 55 | else if (isa<FunctionDecl>(D)) |
| 56 | return CXCursor_FunctionRef; |
| 57 | else if (isa<EnumConstantDecl>(D)) |
| 58 | return CXCursor_EnumConstantRef; |
| 59 | else |
| 60 | return CXCursor_NotImplemented; |
| 61 | } |
| 62 | |
| 63 | #if 0 |
| 64 | // Will be useful one day. |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 65 | class CRefVisitor : public StmtVisitor<CRefVisitor> { |
| 66 | CXDecl CDecl; |
| 67 | CXDeclIterator Callback; |
| 68 | CXClientData CData; |
| 69 | |
| 70 | void Call(enum CXCursorKind CK, Stmt *SRef) { |
| 71 | CXCursor C = { CK, CDecl, SRef }; |
| 72 | Callback(CDecl, C, CData); |
| 73 | } |
| 74 | |
| 75 | public: |
| 76 | CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : |
| 77 | CDecl(C), Callback(cback), CData(D) {} |
| 78 | |
| 79 | void VisitStmt(Stmt *S) { |
| 80 | for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end(); |
| 81 | C != CEnd; ++C) |
| 82 | Visit(*C); |
| 83 | } |
| 84 | void VisitDeclRefExpr(DeclRefExpr *Node) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 85 | Call(TranslateDeclRefExpr(Node), Node); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 86 | } |
| 87 | void VisitMemberExpr(MemberExpr *Node) { |
| 88 | Call(CXCursor_MemberRef, Node); |
| 89 | } |
| 90 | void VisitObjCMessageExpr(ObjCMessageExpr *Node) { |
| 91 | Call(CXCursor_ObjCSelectorRef, Node); |
| 92 | } |
| 93 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
| 94 | Call(CXCursor_ObjCIvarRef, Node); |
| 95 | } |
| 96 | }; |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 97 | #endif |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 98 | |
| 99 | /// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted |
| 100 | /// warnings and errors. |
| 101 | class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient { |
| 102 | public: |
| 103 | virtual ~IgnoreDiagnosticsClient() {} |
| 104 | virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {} |
| 105 | }; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 106 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 107 | // Translation Unit Visitor. |
| 108 | class TUVisitor : public DeclVisitor<TUVisitor> { |
| 109 | CXTranslationUnit TUnit; |
| 110 | CXTranslationUnitIterator Callback; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 111 | CXClientData CData; |
| 112 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 113 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 114 | // to the visitor. Declarations with a PCH level greater than this value will |
| 115 | // be suppressed. |
| 116 | unsigned MaxPCHLevel; |
| 117 | |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 118 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 119 | // Filter any declarations that have a PCH level greater than what we allow. |
| 120 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 121 | return; |
| 122 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 123 | // Filter any implicit declarations (since the source info will be bogus). |
| 124 | if (ND->isImplicit()) |
| 125 | return; |
| 126 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 127 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 128 | Callback(TUnit, C, CData); |
| 129 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 130 | public: |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 131 | TUVisitor(CXTranslationUnit CTU, |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 132 | CXTranslationUnitIterator cback, CXClientData D, |
| 133 | unsigned MaxPCHLevel) : |
| 134 | TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 135 | |
| 136 | void VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 137 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 138 | } |
| 139 | void VisitDeclContext(DeclContext *DC) { |
| 140 | for (DeclContext::decl_iterator |
| 141 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 142 | Visit(*I); |
| 143 | } |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 144 | void VisitTypedefDecl(TypedefDecl *ND) { |
| 145 | Call(CXCursor_TypedefDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 146 | } |
| 147 | void VisitTagDecl(TagDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 148 | switch (ND->getTagKind()) { |
| 149 | case TagDecl::TK_struct: |
| 150 | Call(CXCursor_StructDecl, ND); |
| 151 | break; |
| 152 | case TagDecl::TK_class: |
| 153 | Call(CXCursor_ClassDecl, ND); |
| 154 | break; |
| 155 | case TagDecl::TK_union: |
| 156 | Call(CXCursor_UnionDecl, ND); |
| 157 | break; |
| 158 | case TagDecl::TK_enum: |
| 159 | Call(CXCursor_EnumDecl, ND); |
| 160 | break; |
| 161 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 162 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 163 | void VisitVarDecl(VarDecl *ND) { |
| 164 | Call(CXCursor_VarDecl, ND); |
| 165 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 166 | void VisitFunctionDecl(FunctionDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 167 | Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn |
| 168 | : CXCursor_FunctionDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 169 | } |
| 170 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 171 | Call(CXCursor_ObjCInterfaceDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 172 | } |
| 173 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 174 | Call(CXCursor_ObjCCategoryDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 175 | } |
| 176 | void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 177 | Call(CXCursor_ObjCProtocolDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 178 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 179 | void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) { |
| 180 | Call(CXCursor_ObjCClassDefn, ND); |
| 181 | } |
| 182 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) { |
| 183 | Call(CXCursor_ObjCCategoryDefn, ND); |
| 184 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 187 | // Declaration visitor. |
| 188 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 189 | CXDecl CDecl; |
| 190 | CXDeclIterator Callback; |
| 191 | CXClientData CData; |
| 192 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 193 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 194 | // to the visitor. Declarations with a PCH level greater than this value will |
| 195 | // be suppressed. |
| 196 | unsigned MaxPCHLevel; |
| 197 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 198 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 199 | // Disable the callback when the context is equal to the visiting decl. |
| 200 | if (CDecl == ND && !clang_isReference(CK)) |
| 201 | return; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 202 | |
| 203 | // Filter any declarations that have a PCH level greater than what we allow. |
| 204 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 205 | return; |
| 206 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 207 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 208 | Callback(CDecl, C, CData); |
| 209 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 210 | public: |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 211 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D, |
| 212 | unsigned MaxPCHLevel) : |
| 213 | CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 214 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 215 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 216 | // Issue callbacks for the containing class. |
| 217 | Call(CXCursor_ObjCClassRef, ND); |
| 218 | // FIXME: Issue callbacks for protocol refs. |
| 219 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 220 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 221 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 222 | // Issue callbacks for super class. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 223 | if (D->getSuperClass()) |
| 224 | Call(CXCursor_ObjCSuperClassRef, D); |
| 225 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 226 | for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 227 | E = D->protocol_end(); I != E; ++I) |
| 228 | Call(CXCursor_ObjCProtocolRef, *I); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 229 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 230 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 231 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 232 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 233 | E = PID->protocol_end(); I != E; ++I) |
| 234 | Call(CXCursor_ObjCProtocolRef, *I); |
| 235 | |
| 236 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 237 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 238 | void VisitTagDecl(TagDecl *D) { |
| 239 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 240 | } |
| 241 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 242 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 243 | } |
| 244 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 245 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 246 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 247 | void VisitDeclContext(DeclContext *DC) { |
| 248 | for (DeclContext::decl_iterator |
| 249 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 250 | Visit(*I); |
| 251 | } |
| 252 | void VisitEnumConstantDecl(EnumConstantDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 253 | Call(CXCursor_EnumConstantDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 254 | } |
| 255 | void VisitFieldDecl(FieldDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 256 | Call(CXCursor_FieldDecl, ND); |
| 257 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 258 | void VisitVarDecl(VarDecl *ND) { |
| 259 | Call(CXCursor_VarDecl, ND); |
| 260 | } |
| 261 | void VisitParmVarDecl(ParmVarDecl *ND) { |
| 262 | Call(CXCursor_ParmDecl, ND); |
| 263 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 264 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 265 | Call(CXCursor_ObjCPropertyDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 266 | } |
| 267 | void VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 268 | Call(CXCursor_ObjCIvarDecl, ND); |
| 269 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 270 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 271 | if (ND->isThisDeclarationADefinition()) { |
| 272 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 273 | #if 0 |
| 274 | // Not currently needed. |
| 275 | CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody()); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 276 | CRefVisitor RVisit(CDecl, Callback, CData); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 277 | RVisit.Visit(Body); |
| 278 | #endif |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 281 | void VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 282 | if (ND->getBody()) { |
| 283 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 284 | : CXCursor_ObjCClassMethodDefn, ND); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 285 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 286 | } else |
| 287 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 288 | : CXCursor_ObjCClassMethodDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 289 | } |
| 290 | }; |
| 291 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 292 | class CIndexer : public Indexer { |
| 293 | public: |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 294 | explicit CIndexer(Program *prog) : Indexer(*prog), |
| 295 | OnlyLocalDecls(false), |
| 296 | DisplayDiagnostics(false) {} |
Ted Kremenek | dff7689 | 2009-10-17 06:21:47 +0000 | [diff] [blame] | 297 | |
| 298 | virtual ~CIndexer() { delete &getProgram(); } |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 299 | |
| 300 | /// \brief Whether we only want to see "local" declarations (that did not |
| 301 | /// come from a previous precompiled header). If false, we want to see all |
| 302 | /// declarations. |
| 303 | bool getOnlyLocalDecls() const { return OnlyLocalDecls; } |
| 304 | void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } |
Benjamin Kramer | 9670762 | 2009-10-18 11:10:55 +0000 | [diff] [blame] | 305 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 306 | void setDisplayDiagnostics(bool Display = true) { |
| 307 | DisplayDiagnostics = Display; |
| 308 | } |
| 309 | bool getDisplayDiagnostics() const { return DisplayDiagnostics; } |
| 310 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 311 | /// \brief Get the path of the clang binary. |
Benjamin Kramer | c5a9e95 | 2009-10-19 10:20:24 +0000 | [diff] [blame] | 312 | const llvm::sys::Path& getClangPath(); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 313 | private: |
| 314 | bool OnlyLocalDecls; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 315 | bool DisplayDiagnostics; |
| 316 | |
Benjamin Kramer | c5a9e95 | 2009-10-19 10:20:24 +0000 | [diff] [blame] | 317 | llvm::sys::Path ClangPath; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 318 | }; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 319 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 320 | const llvm::sys::Path& CIndexer::getClangPath() { |
| 321 | // Did we already compute the path? |
| 322 | if (!ClangPath.empty()) |
| 323 | return ClangPath; |
| 324 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 325 | // Find the location where this library lives (libCIndex.dylib). |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 326 | #ifdef LLVM_ON_WIN32 |
| 327 | MEMORY_BASIC_INFORMATION mbi; |
| 328 | char path[MAX_PATH]; |
Benjamin Kramer | edcd828 | 2009-10-18 16:20:58 +0000 | [diff] [blame] | 329 | VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi, |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 330 | sizeof(mbi)); |
| 331 | GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); |
| 332 | |
| 333 | llvm::sys::Path CIndexPath(path); |
| 334 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 335 | // This silly cast below avoids a C++ warning. |
| 336 | Dl_info info; |
| 337 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
| 338 | assert(0 && "Call to dladdr() failed"); |
| 339 | |
| 340 | llvm::sys::Path CIndexPath(info.dli_fname); |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame] | 341 | #endif |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 342 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 343 | // We now have the CIndex directory, locate clang relative to it. |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 344 | CIndexPath.eraseComponent(); |
| 345 | CIndexPath.eraseComponent(); |
| 346 | CIndexPath.appendComponent("bin"); |
| 347 | CIndexPath.appendComponent("clang"); |
| 348 | |
| 349 | // Cache our result. |
| 350 | ClangPath = CIndexPath; |
| 351 | return ClangPath; |
| 352 | } |
| 353 | |
| 354 | } |
| 355 | |
| 356 | extern "C" { |
| 357 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 358 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 359 | int displayDiagnostics) |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 360 | { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 361 | CIndexer *CIdxr = new CIndexer(new Program()); |
| 362 | if (excludeDeclarationsFromPCH) |
| 363 | CIdxr->setOnlyLocalDecls(); |
| 364 | if (displayDiagnostics) |
| 365 | CIdxr->setDisplayDiagnostics(); |
| 366 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 369 | void clang_disposeIndex(CXIndex CIdx) |
| 370 | { |
| 371 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 372 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 375 | // FIXME: need to pass back error info. |
| 376 | CXTranslationUnit clang_createTranslationUnit( |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 377 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 378 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 379 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 380 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 381 | std::string astName(ast_filename); |
| 382 | std::string ErrMsg; |
| 383 | |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 384 | CXTranslationUnit TU = |
| 385 | ASTUnit::LoadFromPCHFile(astName, &ErrMsg, |
| 386 | CXXIdx->getDisplayDiagnostics() ? |
| 387 | NULL : new IgnoreDiagnosticsClient(), |
| 388 | CXXIdx->getOnlyLocalDecls(), |
| 389 | /* UseBumpAllocator = */ true); |
| 390 | |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 391 | if (!ErrMsg.empty()) |
| 392 | llvm::errs() << "clang_createTranslationUnit: " << ErrMsg << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 393 | |
| 394 | return TU; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 397 | CXTranslationUnit clang_createTranslationUnitFromSourceFile( |
| 398 | CXIndex CIdx, |
| 399 | const char *source_filename, |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 400 | int num_command_line_args, const char **command_line_args) { |
| 401 | assert(CIdx && "Passed null CXIndex"); |
| 402 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 403 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 404 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 405 | std::vector<const char *> argv; |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 406 | |
| 407 | // First add the complete path to the 'clang' executable. |
| 408 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 409 | argv.push_back(ClangPath.c_str()); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 410 | |
| 411 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 412 | argv.push_back("-emit-ast"); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 413 | |
| 414 | // The 'source_filename' argument is optional. If the caller does not |
| 415 | // specify it then it is assumed that the source file is specified |
| 416 | // in the actual argument list. |
| 417 | if (source_filename) |
| 418 | argv.push_back(source_filename); |
| 419 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 420 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 421 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 422 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 423 | argv.push_back(tmpnam(astTmpFile)); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 424 | |
| 425 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 426 | for (int i = 0; i < num_command_line_args; ++i) |
| 427 | if (const char *arg = command_line_args[i]) { |
| 428 | if (strcmp(arg, "-o") == 0) { |
| 429 | ++i; // Also skip the matching argument. |
| 430 | continue; |
| 431 | } |
| 432 | if (strcmp(arg, "-emit-ast") == 0 || |
| 433 | strcmp(arg, "-c") == 0 || |
| 434 | strcmp(arg, "-fsyntax-only") == 0) { |
| 435 | continue; |
| 436 | } |
| 437 | |
| 438 | // Keep the argument. |
| 439 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 440 | } |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 441 | |
| 442 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 443 | argv.push_back(NULL); |
| 444 | |
Ted Kremenek | feb15e3 | 2009-10-26 22:14:08 +0000 | [diff] [blame] | 445 | // Invoke 'clang'. |
| 446 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 447 | // on Unix or NUL (Windows). |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 448 | std::string ErrMsg; |
Ted Kremenek | c46e463 | 2009-10-19 22:27:32 +0000 | [diff] [blame] | 449 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL }; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 450 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 451 | /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL, |
| 452 | /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); |
| 453 | |
Ted Kremenek | 8ee1f3f | 2009-10-22 22:19:00 +0000 | [diff] [blame] | 454 | if (!ErrMsg.empty()) { |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 455 | llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg |
| 456 | << '\n' << "Arguments: \n"; |
| 457 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 458 | I!=E; ++I) { |
| 459 | if (*I) |
| 460 | llvm::errs() << ' ' << *I << '\n'; |
| 461 | } |
| 462 | llvm::errs() << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 463 | } |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 464 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 465 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 466 | ASTUnit *ATU = static_cast<ASTUnit *>( |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 467 | clang_createTranslationUnit(CIdx, astTmpFile)); |
| 468 | if (ATU) |
| 469 | ATU->unlinkTemporaryFile(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 470 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 473 | void clang_disposeTranslationUnit( |
| 474 | CXTranslationUnit CTUnit) |
| 475 | { |
| 476 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 477 | delete static_cast<ASTUnit *>(CTUnit); |
| 478 | } |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 479 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 480 | const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) |
| 481 | { |
| 482 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 483 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 484 | return CXXUnit->getOriginalSourceFileName().c_str(); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 485 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 486 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 487 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
| 488 | CXTranslationUnitIterator callback, |
| 489 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 490 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 491 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 492 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 493 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 494 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 495 | TUVisitor DVisit(CTUnit, callback, CData, |
| 496 | CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 497 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 500 | void clang_loadDeclaration(CXDecl Dcl, |
| 501 | CXDeclIterator callback, |
| 502 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 503 | { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 504 | assert(Dcl && "Passed null CXDecl"); |
| 505 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 506 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 507 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 508 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 511 | // Some notes on CXEntity: |
| 512 | // |
| 513 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 514 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 515 | // entity for 2 different types). For example: |
| 516 | // |
| 517 | // module1.m: @interface Foo @end Foo *x; |
| 518 | // module2.m: void Foo(int); |
| 519 | // |
| 520 | // - Since the unique name spans translation units, static data/functions |
| 521 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 522 | // As a result, there will be no entity for the following: |
| 523 | // |
| 524 | // module.m: static void Foo() { } |
| 525 | // |
| 526 | |
| 527 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 528 | const char *clang_getDeclarationName(CXEntity) |
| 529 | { |
| 530 | return ""; |
| 531 | } |
| 532 | const char *clang_getURI(CXEntity) |
| 533 | { |
| 534 | return ""; |
| 535 | } |
| 536 | |
| 537 | CXEntity clang_getEntity(const char *URI) |
| 538 | { |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | // |
| 543 | // CXDecl Operations. |
| 544 | // |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 545 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 546 | { |
| 547 | return 0; |
| 548 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 549 | const char *clang_getDeclSpelling(CXDecl AnonDecl) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 550 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 551 | assert(AnonDecl && "Passed null CXDecl"); |
| 552 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 553 | |
| 554 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 555 | return OMD->getSelector().getAsString().c_str(); |
Steve Naroff | f9f6196 | 2009-10-29 18:55:50 +0000 | [diff] [blame] | 556 | } |
| 557 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 558 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 559 | // and returns different names. NamedDecl returns the class name and |
| 560 | // ObjCCategoryImplDecl returns the category name. |
| 561 | return CIMP->getIdentifier()->getNameStart(); |
Steve Naroff | f9f6196 | 2009-10-29 18:55:50 +0000 | [diff] [blame] | 562 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 563 | if (ND->getIdentifier()) |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 564 | return ND->getIdentifier()->getNameStart(); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 565 | else |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 566 | return ""; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 567 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 568 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 569 | unsigned clang_getDeclLine(CXDecl AnonDecl) |
| 570 | { |
| 571 | assert(AnonDecl && "Passed null CXDecl"); |
| 572 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 573 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 574 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 575 | } |
| 576 | |
| 577 | unsigned clang_getDeclColumn(CXDecl AnonDecl) |
| 578 | { |
| 579 | assert(AnonDecl && "Passed null CXDecl"); |
| 580 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 581 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 582 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 585 | const char *clang_getDeclSource(CXDecl AnonDecl) |
| 586 | { |
| 587 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 588 | FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl)); |
| 589 | assert (FEnt && "Cannot find FileEntry for Decl"); |
| 590 | return clang_getFileName(FEnt); |
| 591 | } |
| 592 | |
| 593 | static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr, |
| 594 | SourceLocation SLoc) |
| 595 | { |
| 596 | FileID FID; |
| 597 | if (SLoc.isFileID()) |
| 598 | FID = SMgr.getFileID(SLoc); |
| 599 | else |
| 600 | FID = SMgr.getDecomposedSpellingLoc(SLoc).first; |
| 601 | return SMgr.getFileEntryForID(FID); |
| 602 | } |
| 603 | |
| 604 | CXFile clang_getDeclSourceFile(CXDecl AnonDecl) |
| 605 | { |
| 606 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 607 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 608 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 609 | return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation()); |
| 610 | } |
| 611 | |
| 612 | const char *clang_getFileName(CXFile SFile) { |
| 613 | assert(SFile && "Passed null CXFile"); |
| 614 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 615 | return FEnt->getName(); |
| 616 | } |
| 617 | |
| 618 | time_t clang_getFileTime(CXFile SFile) { |
| 619 | assert(SFile && "Passed null CXFile"); |
| 620 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 621 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 624 | const char *clang_getCursorSpelling(CXCursor C) |
| 625 | { |
| 626 | assert(C.decl && "CXCursor has null decl"); |
| 627 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 628 | |
| 629 | if (clang_isReference(C.kind)) { |
| 630 | switch (C.kind) { |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 631 | case CXCursor_ObjCSuperClassRef: { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 632 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 633 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 634 | return OID->getSuperClass()->getIdentifier()->getNameStart(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 635 | } |
| 636 | case CXCursor_ObjCClassRef: { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 637 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 638 | return OID->getIdentifier()->getNameStart(); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 639 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 640 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 641 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 642 | return OID->getClassInterface()->getIdentifier()->getNameStart(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 643 | } |
| 644 | case CXCursor_ObjCProtocolRef: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 645 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 646 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 647 | return OID->getIdentifier()->getNameStart(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 648 | } |
| 649 | case CXCursor_ObjCSelectorRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 650 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 651 | static_cast<Stmt *>(C.stmt)); |
| 652 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 653 | return OME->getSelector().getAsString().c_str(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 654 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 655 | case CXCursor_VarRef: |
| 656 | case CXCursor_FunctionRef: |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 657 | case CXCursor_EnumConstantRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 658 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 659 | static_cast<Stmt *>(C.stmt)); |
| 660 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 661 | return DRE->getDecl()->getIdentifier()->getNameStart(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 662 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 663 | default: |
| 664 | return "<not implemented>"; |
| 665 | } |
| 666 | } |
| 667 | return clang_getDeclSpelling(C.decl); |
| 668 | } |
| 669 | |
| 670 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 671 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 672 | switch (Kind) { |
| 673 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 674 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 675 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 676 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 677 | case CXCursor_StructDecl: return "StructDecl"; |
| 678 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 679 | case CXCursor_ClassDecl: return "ClassDecl"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 680 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 681 | case CXCursor_VarDecl: return "VarDecl"; |
| 682 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 683 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 684 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 685 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 686 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 687 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 688 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 689 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 690 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 691 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 692 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 693 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 694 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 695 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 696 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 697 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 698 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
| 699 | |
| 700 | case CXCursor_VarRef: return "VarRef"; |
| 701 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 702 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 703 | case CXCursor_MemberRef: return "MemberRef"; |
| 704 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 705 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 706 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 707 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 708 | default: return "<not implemented>"; |
| 709 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 710 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 711 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 712 | static enum CXCursorKind TranslateKind(Decl *D) { |
| 713 | switch (D->getKind()) { |
| 714 | case Decl::Function: return CXCursor_FunctionDecl; |
| 715 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 716 | case Decl::Enum: return CXCursor_EnumDecl; |
| 717 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 718 | case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class |
| 719 | case Decl::Field: return CXCursor_FieldDecl; |
| 720 | case Decl::Var: return CXCursor_VarDecl; |
| 721 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 722 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 723 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 724 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 725 | case Decl::ObjCMethod: { |
| 726 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D); |
| 727 | if (MD->isInstanceMethod()) |
| 728 | return CXCursor_ObjCInstanceMethodDecl; |
| 729 | return CXCursor_ObjCClassMethodDecl; |
| 730 | } |
| 731 | default: break; |
| 732 | } |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 733 | return CXCursor_NotImplemented; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 734 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 735 | // |
| 736 | // CXCursor Operations. |
| 737 | // |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 738 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 739 | unsigned line, unsigned column) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 740 | { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 741 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 742 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 743 | |
| 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 | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 754 | ASTLocation LastLoc = CXXUnit->getLastASTLocation(); |
| 755 | |
| 756 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc, |
| 757 | &LastLoc); |
| 758 | if (ALoc.isValid()) |
| 759 | CXXUnit->setLastASTLocation(ALoc); |
| 760 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 761 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 762 | if (ALoc.isNamedRef()) |
| 763 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 764 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 765 | if (Dcl) { |
| 766 | if (Stm) { |
| 767 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) { |
| 768 | CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm }; |
| 769 | return C; |
| 770 | } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) { |
| 771 | CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp }; |
| 772 | return C; |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 773 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 774 | // Fall through...treat as a decl, not a ref. |
| 775 | } |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 776 | if (ALoc.isNamedRef()) { |
| 777 | if (isa<ObjCInterfaceDecl>(Dcl)) { |
| 778 | CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() }; |
| 779 | return C; |
| 780 | } |
| 781 | if (isa<ObjCProtocolDecl>(Dcl)) { |
| 782 | CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() }; |
| 783 | return C; |
| 784 | } |
| 785 | } |
| 786 | CXCursor C = { TranslateKind(Dcl), Dcl, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 787 | return C; |
| 788 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 789 | CXCursor C = { CXCursor_NoDeclFound, 0, 0 }; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 790 | return C; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 793 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) |
| 794 | { |
| 795 | assert(AnonDecl && "Passed null CXDecl"); |
| 796 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 797 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 798 | CXCursor C = { TranslateKind(ND), ND, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 799 | return C; |
| 800 | } |
| 801 | |
| 802 | unsigned clang_isInvalid(enum CXCursorKind K) |
| 803 | { |
| 804 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 805 | } |
| 806 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 807 | unsigned clang_isDeclaration(enum CXCursorKind K) |
| 808 | { |
| 809 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 810 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 811 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 812 | unsigned clang_isReference(enum CXCursorKind K) |
| 813 | { |
| 814 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 815 | } |
| 816 | |
| 817 | unsigned clang_isDefinition(enum CXCursorKind K) |
| 818 | { |
| 819 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 820 | } |
| 821 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 822 | CXCursorKind clang_getCursorKind(CXCursor C) |
| 823 | { |
| 824 | return C.kind; |
| 825 | } |
| 826 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 827 | static Decl *getDeclFromExpr(Stmt *E) { |
| 828 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 829 | return RefExpr->getDecl(); |
| 830 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 831 | return ME->getMemberDecl(); |
| 832 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 833 | return RE->getDecl(); |
| 834 | |
| 835 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 836 | return getDeclFromExpr(CE->getCallee()); |
| 837 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 838 | return getDeclFromExpr(CE->getSubExpr()); |
| 839 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 840 | return OME->getMethodDecl(); |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 845 | CXDecl clang_getCursorDecl(CXCursor C) |
| 846 | { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 847 | if (clang_isDeclaration(C.kind)) |
| 848 | return C.decl; |
| 849 | |
| 850 | if (clang_isReference(C.kind)) { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 851 | if (C.stmt) { |
Steve Naroff | f9adf8f | 2009-10-05 17:58:19 +0000 | [diff] [blame] | 852 | if (C.kind == CXCursor_ObjCClassRef || |
| 853 | C.kind == CXCursor_ObjCProtocolRef) |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 854 | return static_cast<Stmt *>(C.stmt); |
| 855 | else |
| 856 | return getDeclFromExpr(static_cast<Stmt *>(C.stmt)); |
| 857 | } else |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 858 | return C.decl; |
| 859 | } |
| 860 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 863 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 864 | static SourceLocation getLocationFromCursor(CXCursor C, |
| 865 | SourceManager &SourceMgr, |
| 866 | NamedDecl *ND) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 867 | if (clang_isReference(C.kind)) { |
| 868 | switch (C.kind) { |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 869 | case CXCursor_ObjCClassRef: { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 870 | if (isa<ObjCInterfaceDecl>(ND)) { |
| 871 | // FIXME: This is a hack (storing the parent decl in the stmt slot). |
| 872 | NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt); |
| 873 | return parentDecl->getLocation(); |
| 874 | } |
| 875 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 876 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 877 | return OID->getClassInterface()->getLocation(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 878 | } |
| 879 | case CXCursor_ObjCSuperClassRef: { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 880 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 881 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 882 | return OID->getSuperClassLoc(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 883 | } |
| 884 | case CXCursor_ObjCProtocolRef: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 885 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 886 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 887 | return OID->getLocation(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 888 | } |
| 889 | case CXCursor_ObjCSelectorRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 890 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 891 | static_cast<Stmt *>(C.stmt)); |
| 892 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 893 | return OME->getLeftLoc(); /* FIXME: should be a range */ |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 894 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 895 | case CXCursor_VarRef: |
| 896 | case CXCursor_FunctionRef: |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 897 | case CXCursor_EnumConstantRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 898 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 899 | static_cast<Stmt *>(C.stmt)); |
| 900 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 901 | return DRE->getLocation(); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 902 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 903 | default: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 904 | return SourceLocation(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 905 | } |
| 906 | } else { // We have a declaration or a definition. |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 907 | SourceLocation SLoc; |
| 908 | switch (ND->getKind()) { |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 909 | case Decl::ObjCInterface: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 910 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 911 | break; |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 912 | } |
| 913 | case Decl::ObjCProtocol: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 914 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 915 | break; |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 916 | } |
| 917 | default: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 918 | SLoc = ND->getLocation(); |
| 919 | break; |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 920 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 921 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 922 | if (SLoc.isInvalid()) |
| 923 | return SourceLocation(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 924 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 925 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 928 | unsigned clang_getCursorLine(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.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 936 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 937 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 938 | unsigned clang_getCursorColumn(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 939 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 940 | assert(C.decl && "CXCursor has null decl"); |
| 941 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 942 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 943 | |
| 944 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 945 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 946 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 947 | const char *clang_getCursorSource(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 948 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 949 | assert(C.decl && "CXCursor has null decl"); |
| 950 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 951 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 952 | |
| 953 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 954 | if (SLoc.isFileID()) |
| 955 | return SourceMgr.getBufferName(SLoc); |
| 956 | |
| 957 | // Retrieve the file in which the macro was instantiated, then provide that |
| 958 | // buffer name. |
| 959 | // FIXME: Do we want to give specific macro-instantiation information? |
| 960 | const llvm::MemoryBuffer *Buffer |
| 961 | = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first); |
| 962 | if (!Buffer) |
| 963 | return 0; |
| 964 | |
| 965 | return Buffer->getBufferIdentifier(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 968 | CXFile clang_getCursorSourceFile(CXCursor C) |
| 969 | { |
| 970 | assert(C.decl && "CXCursor has null decl"); |
| 971 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 972 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 973 | |
| 974 | return (void *)getFileEntryFromSourceLocation(SourceMgr, |
| 975 | getLocationFromCursor(C,SourceMgr, ND)); |
| 976 | } |
| 977 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 978 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 979 | const char **startBuf, |
| 980 | const char **endBuf, |
| 981 | unsigned *startLine, |
| 982 | unsigned *startColumn, |
| 983 | unsigned *endLine, |
| 984 | unsigned *endColumn) |
| 985 | { |
| 986 | assert(C.decl && "CXCursor has null decl"); |
| 987 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 988 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 989 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 990 | |
| 991 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 992 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 993 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 994 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 995 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 996 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 997 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 998 | } |
| 999 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame^] | 1000 | enum CXCompletionChunkKind |
| 1001 | clang_getCompletionChunkKind(CXCompletionString completion_string, |
| 1002 | unsigned chunk_number) { |
| 1003 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1004 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1005 | return CXCompletionChunk_Text; |
| 1006 | |
| 1007 | switch ((*CCStr)[chunk_number].Kind) { |
| 1008 | case CodeCompletionString::CK_TypedText: |
| 1009 | return CXCompletionChunk_TypedText; |
| 1010 | case CodeCompletionString::CK_Text: |
| 1011 | return CXCompletionChunk_Text; |
| 1012 | case CodeCompletionString::CK_Optional: |
| 1013 | return CXCompletionChunk_Optional; |
| 1014 | case CodeCompletionString::CK_Placeholder: |
| 1015 | return CXCompletionChunk_Placeholder; |
| 1016 | case CodeCompletionString::CK_Informative: |
| 1017 | return CXCompletionChunk_Informative; |
| 1018 | case CodeCompletionString::CK_CurrentParameter: |
| 1019 | return CXCompletionChunk_CurrentParameter; |
| 1020 | case CodeCompletionString::CK_LeftParen: |
| 1021 | return CXCompletionChunk_LeftParen; |
| 1022 | case CodeCompletionString::CK_RightParen: |
| 1023 | return CXCompletionChunk_RightParen; |
| 1024 | case CodeCompletionString::CK_LeftBracket: |
| 1025 | return CXCompletionChunk_LeftBracket; |
| 1026 | case CodeCompletionString::CK_RightBracket: |
| 1027 | return CXCompletionChunk_RightBracket; |
| 1028 | case CodeCompletionString::CK_LeftBrace: |
| 1029 | return CXCompletionChunk_LeftBrace; |
| 1030 | case CodeCompletionString::CK_RightBrace: |
| 1031 | return CXCompletionChunk_RightBrace; |
| 1032 | case CodeCompletionString::CK_LeftAngle: |
| 1033 | return CXCompletionChunk_LeftAngle; |
| 1034 | case CodeCompletionString::CK_RightAngle: |
| 1035 | return CXCompletionChunk_RightAngle; |
| 1036 | case CodeCompletionString::CK_Comma: |
| 1037 | return CXCompletionChunk_Comma; |
| 1038 | } |
| 1039 | |
| 1040 | // Should be unreachable, but let's be careful. |
| 1041 | return CXCompletionChunk_Text; |
| 1042 | } |
| 1043 | |
| 1044 | const char *clang_getCompletionChunkText(CXCompletionString completion_string, |
| 1045 | unsigned chunk_number) { |
| 1046 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1047 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1048 | return 0; |
| 1049 | |
| 1050 | switch ((*CCStr)[chunk_number].Kind) { |
| 1051 | case CodeCompletionString::CK_TypedText: |
| 1052 | case CodeCompletionString::CK_Text: |
| 1053 | case CodeCompletionString::CK_Placeholder: |
| 1054 | case CodeCompletionString::CK_CurrentParameter: |
| 1055 | case CodeCompletionString::CK_Informative: |
| 1056 | case CodeCompletionString::CK_LeftParen: |
| 1057 | case CodeCompletionString::CK_RightParen: |
| 1058 | case CodeCompletionString::CK_LeftBracket: |
| 1059 | case CodeCompletionString::CK_RightBracket: |
| 1060 | case CodeCompletionString::CK_LeftBrace: |
| 1061 | case CodeCompletionString::CK_RightBrace: |
| 1062 | case CodeCompletionString::CK_LeftAngle: |
| 1063 | case CodeCompletionString::CK_RightAngle: |
| 1064 | case CodeCompletionString::CK_Comma: |
| 1065 | return (*CCStr)[chunk_number].Text; |
| 1066 | |
| 1067 | case CodeCompletionString::CK_Optional: |
| 1068 | // Note: treated as an empty text block. |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | // Should be unreachable, but let's be careful. |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | CXCompletionString |
| 1077 | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
| 1078 | unsigned chunk_number) { |
| 1079 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1080 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1081 | return 0; |
| 1082 | |
| 1083 | switch ((*CCStr)[chunk_number].Kind) { |
| 1084 | case CodeCompletionString::CK_TypedText: |
| 1085 | case CodeCompletionString::CK_Text: |
| 1086 | case CodeCompletionString::CK_Placeholder: |
| 1087 | case CodeCompletionString::CK_CurrentParameter: |
| 1088 | case CodeCompletionString::CK_Informative: |
| 1089 | case CodeCompletionString::CK_LeftParen: |
| 1090 | case CodeCompletionString::CK_RightParen: |
| 1091 | case CodeCompletionString::CK_LeftBracket: |
| 1092 | case CodeCompletionString::CK_RightBracket: |
| 1093 | case CodeCompletionString::CK_LeftBrace: |
| 1094 | case CodeCompletionString::CK_RightBrace: |
| 1095 | case CodeCompletionString::CK_LeftAngle: |
| 1096 | case CodeCompletionString::CK_RightAngle: |
| 1097 | case CodeCompletionString::CK_Comma: |
| 1098 | return 0; |
| 1099 | |
| 1100 | case CodeCompletionString::CK_Optional: |
| 1101 | // Note: treated as an empty text block. |
| 1102 | return (*CCStr)[chunk_number].Optional; |
| 1103 | } |
| 1104 | |
| 1105 | // Should be unreachable, but let's be careful. |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { |
| 1110 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1111 | return CCStr? CCStr->size() : 0; |
| 1112 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1113 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame^] | 1114 | static CXCursorKind parseResultKind(llvm::StringRef Str) { |
| 1115 | return llvm::StringSwitch<CXCursorKind>(Str) |
| 1116 | .Case("Typedef", CXCursor_TypedefDecl) |
| 1117 | .Case("Struct", CXCursor_StructDecl) |
| 1118 | .Case("Union", CXCursor_UnionDecl) |
| 1119 | .Case("Class", CXCursor_ClassDecl) |
| 1120 | .Case("Field", CXCursor_FieldDecl) |
| 1121 | .Case("EnumConstant", CXCursor_EnumConstantDecl) |
| 1122 | .Case("Function", CXCursor_FunctionDecl) |
| 1123 | // FIXME: Hacks here to make C++ member functions look like C functions |
| 1124 | .Case("CXXMethod", CXCursor_FunctionDecl) |
| 1125 | .Case("CXXConstructor", CXCursor_FunctionDecl) |
| 1126 | .Case("CXXDestructor", CXCursor_FunctionDecl) |
| 1127 | .Case("CXXConversion", CXCursor_FunctionDecl) |
| 1128 | .Case("Var", CXCursor_VarDecl) |
| 1129 | .Case("ParmVar", CXCursor_ParmDecl) |
| 1130 | .Case("ObjCInterface", CXCursor_ObjCInterfaceDecl) |
| 1131 | .Case("ObjCCategory", CXCursor_ObjCCategoryDecl) |
| 1132 | .Case("ObjCProtocol", CXCursor_ObjCProtocolDecl) |
| 1133 | .Case("ObjCProperty", CXCursor_ObjCPropertyDecl) |
| 1134 | .Case("ObjCIvar", CXCursor_ObjCIvarDecl) |
| 1135 | .Case("ObjCInstanceMethod", CXCursor_ObjCInstanceMethodDecl) |
| 1136 | .Case("ObjCClassMethod", CXCursor_ObjCClassMethodDecl) |
| 1137 | .Default(CXCursor_NotImplemented); |
| 1138 | } |
| 1139 | |
| 1140 | void clang_codeComplete(CXIndex CIdx, |
| 1141 | const char *source_filename, |
| 1142 | int num_command_line_args, |
| 1143 | const char **command_line_args, |
| 1144 | const char *complete_filename, |
| 1145 | unsigned complete_line, |
| 1146 | unsigned complete_column, |
| 1147 | CXCompletionIterator completion_iterator, |
| 1148 | CXClientData client_data) { |
| 1149 | // The indexer, which is mainly used to determine where diagnostics go. |
| 1150 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 1151 | |
| 1152 | // Build up the arguments for invoking 'clang'. |
| 1153 | std::vector<const char *> argv; |
| 1154 | |
| 1155 | // First add the complete path to the 'clang' executable. |
| 1156 | llvm::sys::Path ClangPath = CXXIdx->getClangPath(); |
| 1157 | argv.push_back(ClangPath.c_str()); |
| 1158 | |
| 1159 | // Add the '-fsyntax-only' argument so that we only perform a basic |
| 1160 | // syntax check of the code. |
| 1161 | argv.push_back("-fsyntax-only"); |
| 1162 | |
| 1163 | // Add the appropriate '-code-completion-at=file:line:column' argument |
| 1164 | // to perform code completion, with an "-Xclang" preceding it. |
| 1165 | std::string code_complete_at; |
| 1166 | code_complete_at += "-code-completion-at="; |
| 1167 | code_complete_at += complete_filename; |
| 1168 | code_complete_at += ":"; |
| 1169 | code_complete_at += llvm::utostr(complete_line); |
| 1170 | code_complete_at += ":"; |
| 1171 | code_complete_at += llvm::utostr(complete_column); |
| 1172 | argv.push_back("-Xclang"); |
| 1173 | argv.push_back(code_complete_at.c_str()); |
| 1174 | argv.push_back("-Xclang"); |
| 1175 | argv.push_back("-code-completion-printer=cindex"); |
| 1176 | |
| 1177 | // Add the source file name (FIXME: later, we'll want to build temporary |
| 1178 | // file from the buffer, or just feed the source text via standard input). |
| 1179 | argv.push_back(source_filename); |
| 1180 | |
| 1181 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 1182 | for (int i = 0; i < num_command_line_args; ++i) |
| 1183 | if (const char *arg = command_line_args[i]) { |
| 1184 | if (strcmp(arg, "-o") == 0) { |
| 1185 | ++i; // Also skip the matching argument. |
| 1186 | continue; |
| 1187 | } |
| 1188 | if (strcmp(arg, "-emit-ast") == 0 || |
| 1189 | strcmp(arg, "-c") == 0 || |
| 1190 | strcmp(arg, "-fsyntax-only") == 0) { |
| 1191 | continue; |
| 1192 | } |
| 1193 | |
| 1194 | // Keep the argument. |
| 1195 | argv.push_back(arg); |
| 1196 | } |
| 1197 | |
| 1198 | // Add the null terminator. |
| 1199 | argv.push_back(NULL); |
| 1200 | |
| 1201 | // Generate a temporary name for the AST file. |
| 1202 | char tmpFile[L_tmpnam]; |
| 1203 | char *tmpFileName = tmpnam(tmpFile); |
| 1204 | llvm::sys::Path ResultsFile(tmpFileName); |
| 1205 | |
| 1206 | // Invoke 'clang'. |
| 1207 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 1208 | // on Unix or NUL (Windows). |
| 1209 | std::string ErrMsg; |
| 1210 | const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, &DevNull, 0 }; |
| 1211 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 1212 | /* redirects */ &Redirects[0], |
| 1213 | /* secondsToWait */ 0, |
| 1214 | /* memoryLimits */ 0, &ErrMsg); |
| 1215 | |
| 1216 | if (!ErrMsg.empty()) { |
| 1217 | llvm::errs() << "clang_codeComplete: " << ErrMsg |
| 1218 | << '\n' << "Arguments: \n"; |
| 1219 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
| 1220 | I!=E; ++I) { |
| 1221 | if (*I) |
| 1222 | llvm::errs() << ' ' << *I << '\n'; |
| 1223 | } |
| 1224 | llvm::errs() << '\n'; |
| 1225 | } |
| 1226 | |
| 1227 | // Parse the resulting source file to find code-completion results. |
| 1228 | using llvm::MemoryBuffer; |
| 1229 | using llvm::StringRef; |
| 1230 | if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { |
| 1231 | StringRef Buffer = F->getBuffer(); |
| 1232 | do { |
| 1233 | StringRef::size_type CompletionIdx = Buffer.find("COMPLETION:"); |
| 1234 | StringRef::size_type OverloadIdx = Buffer.find("OVERLOAD:"); |
| 1235 | if (CompletionIdx == StringRef::npos && OverloadIdx == StringRef::npos) |
| 1236 | break; |
| 1237 | |
| 1238 | if (OverloadIdx < CompletionIdx) { |
| 1239 | // Parse an overload result. |
| 1240 | Buffer = Buffer.substr(OverloadIdx); |
| 1241 | |
| 1242 | // Skip past the OVERLOAD: |
| 1243 | Buffer = Buffer.substr(Buffer.find(':') + 1); |
| 1244 | |
| 1245 | // Find the entire completion string. |
| 1246 | StringRef::size_type EOL = Buffer.find_first_of("\n\r"); |
| 1247 | if (EOL == StringRef::npos) |
| 1248 | continue; |
| 1249 | |
| 1250 | StringRef Line = Buffer.substr(0, EOL); |
| 1251 | Buffer = Buffer.substr(EOL + 1); |
| 1252 | CodeCompletionString *CCStr = CodeCompletionString::Deserialize(Line); |
| 1253 | if (!CCStr || CCStr->empty()) |
| 1254 | continue; |
| 1255 | |
| 1256 | // Vend the code-completion result to the caller. |
| 1257 | CXCompletionResult Result; |
| 1258 | Result.CursorKind = CXCursor_NotImplemented; |
| 1259 | Result.CompletionString = CCStr; |
| 1260 | if (completion_iterator) |
| 1261 | completion_iterator(&Result, client_data); |
| 1262 | delete CCStr; |
| 1263 | |
| 1264 | continue; |
| 1265 | } |
| 1266 | |
| 1267 | // Parse a completion result. |
| 1268 | Buffer = Buffer.substr(CompletionIdx); |
| 1269 | |
| 1270 | // Skip past the COMPLETION: |
| 1271 | Buffer = Buffer.substr(Buffer.find(':') + 1); |
| 1272 | |
| 1273 | // Get the rank |
| 1274 | unsigned Rank = 0; |
| 1275 | StringRef::size_type AfterRank = Buffer.find(':'); |
| 1276 | Buffer.substr(0, AfterRank).getAsInteger(10, Rank); |
| 1277 | Buffer = Buffer.substr(AfterRank + 1); |
| 1278 | |
| 1279 | // Get the kind of result. |
| 1280 | StringRef::size_type AfterKind = Buffer.find(':'); |
| 1281 | StringRef Kind = Buffer.substr(0, AfterKind); |
| 1282 | Buffer = Buffer.substr(AfterKind + 1); |
| 1283 | |
| 1284 | // Skip over any whitespace. |
| 1285 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t")); |
| 1286 | |
| 1287 | // Find the entire completion string. |
| 1288 | StringRef::size_type EOL = Buffer.find_first_of("\n\r"); |
| 1289 | if (EOL == StringRef::npos) |
| 1290 | continue; |
| 1291 | |
| 1292 | StringRef Line = Buffer.substr(0, EOL); |
| 1293 | Buffer = Buffer.substr(EOL + 1); |
| 1294 | CodeCompletionString *CCStr = CodeCompletionString::Deserialize(Line); |
| 1295 | if (!CCStr || CCStr->empty()) |
| 1296 | continue; |
| 1297 | |
| 1298 | // Vend the code-completion result to the caller. |
| 1299 | CXCompletionResult Result; |
| 1300 | Result.CursorKind = parseResultKind(Kind); |
| 1301 | Result.CompletionString = CCStr; |
| 1302 | if (completion_iterator) |
| 1303 | completion_iterator(&Result, client_data); |
| 1304 | delete CCStr; |
| 1305 | } while (true); |
| 1306 | delete F; |
| 1307 | } |
| 1308 | |
| 1309 | ResultsFile.eraseFromDisk(); |
| 1310 | } |
| 1311 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1312 | } // end extern "C" |