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); |
John Thompson | 6a6742a | 2009-11-11 23:11:14 +0000 | [diff] [blame] | 334 | |
| 335 | CIndexPath.eraseComponent(); |
| 336 | CIndexPath.appendComponent("clang"); |
| 337 | CIndexPath.appendSuffix("exe"); |
| 338 | CIndexPath.makeAbsolute(); |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 339 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 340 | // This silly cast below avoids a C++ warning. |
| 341 | Dl_info info; |
| 342 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
| 343 | assert(0 && "Call to dladdr() failed"); |
| 344 | |
| 345 | llvm::sys::Path CIndexPath(info.dli_fname); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 346 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 347 | // We now have the CIndex directory, locate clang relative to it. |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 348 | CIndexPath.eraseComponent(); |
| 349 | CIndexPath.eraseComponent(); |
| 350 | CIndexPath.appendComponent("bin"); |
| 351 | CIndexPath.appendComponent("clang"); |
John Thompson | 6a6742a | 2009-11-11 23:11:14 +0000 | [diff] [blame] | 352 | #endif |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 353 | |
| 354 | // Cache our result. |
| 355 | ClangPath = CIndexPath; |
| 356 | return ClangPath; |
| 357 | } |
| 358 | |
| 359 | } |
| 360 | |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 361 | static SourceLocation getLocationFromCursor(CXCursor C, |
| 362 | SourceManager &SourceMgr, |
| 363 | NamedDecl *ND) { |
| 364 | if (clang_isReference(C.kind)) { |
| 365 | switch (C.kind) { |
| 366 | case CXCursor_ObjCClassRef: { |
| 367 | if (isa<ObjCInterfaceDecl>(ND)) { |
| 368 | // FIXME: This is a hack (storing the parent decl in the stmt slot). |
| 369 | NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt); |
| 370 | return parentDecl->getLocation(); |
| 371 | } |
| 372 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 373 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 374 | return OID->getClassInterface()->getLocation(); |
| 375 | } |
| 376 | case CXCursor_ObjCSuperClassRef: { |
| 377 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 378 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 379 | return OID->getSuperClassLoc(); |
| 380 | } |
| 381 | case CXCursor_ObjCProtocolRef: { |
| 382 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 383 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 384 | return OID->getLocation(); |
| 385 | } |
| 386 | case CXCursor_ObjCSelectorRef: { |
| 387 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 388 | static_cast<Stmt *>(C.stmt)); |
| 389 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 390 | return OME->getLeftLoc(); /* FIXME: should be a range */ |
| 391 | } |
| 392 | case CXCursor_VarRef: |
| 393 | case CXCursor_FunctionRef: |
| 394 | case CXCursor_EnumConstantRef: { |
| 395 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 396 | static_cast<Stmt *>(C.stmt)); |
| 397 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 398 | return DRE->getLocation(); |
| 399 | } |
| 400 | default: |
| 401 | return SourceLocation(); |
| 402 | } |
| 403 | } else { // We have a declaration or a definition. |
| 404 | SourceLocation SLoc; |
| 405 | switch (ND->getKind()) { |
| 406 | case Decl::ObjCInterface: { |
| 407 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 408 | break; |
| 409 | } |
| 410 | case Decl::ObjCProtocol: { |
| 411 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 412 | break; |
| 413 | } |
| 414 | default: { |
| 415 | SLoc = ND->getLocation(); |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | if (SLoc.isInvalid()) |
| 420 | return SourceLocation(); |
| 421 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
| 422 | } |
| 423 | } |
| 424 | |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 425 | static CXString createCXString(const char *String, bool DupString = false) { |
| 426 | CXString Str; |
| 427 | if (DupString) { |
| 428 | Str.Spelling = strdup(String); |
| 429 | Str.MustFreeString = 1; |
| 430 | } else { |
| 431 | Str.Spelling = String; |
| 432 | Str.MustFreeString = 0; |
| 433 | } |
| 434 | return Str; |
| 435 | } |
| 436 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 437 | extern "C" { |
| 438 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 439 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 440 | int displayDiagnostics) |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 441 | { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 442 | CIndexer *CIdxr = new CIndexer(new Program()); |
| 443 | if (excludeDeclarationsFromPCH) |
| 444 | CIdxr->setOnlyLocalDecls(); |
| 445 | if (displayDiagnostics) |
| 446 | CIdxr->setDisplayDiagnostics(); |
| 447 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 450 | void clang_disposeIndex(CXIndex CIdx) |
| 451 | { |
| 452 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 453 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 456 | // FIXME: need to pass back error info. |
| 457 | CXTranslationUnit clang_createTranslationUnit( |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 458 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 459 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 460 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 461 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 462 | std::string astName(ast_filename); |
| 463 | std::string ErrMsg; |
| 464 | |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 465 | CXTranslationUnit TU = |
| 466 | ASTUnit::LoadFromPCHFile(astName, &ErrMsg, |
| 467 | CXXIdx->getDisplayDiagnostics() ? |
| 468 | NULL : new IgnoreDiagnosticsClient(), |
| 469 | CXXIdx->getOnlyLocalDecls(), |
| 470 | /* UseBumpAllocator = */ true); |
| 471 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 472 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 473 | llvm::errs() << "clang_createTranslationUnit: " << ErrMsg << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 474 | |
| 475 | return TU; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 478 | CXTranslationUnit clang_createTranslationUnitFromSourceFile( |
| 479 | CXIndex CIdx, |
| 480 | const char *source_filename, |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 481 | int num_command_line_args, const char **command_line_args) { |
| 482 | assert(CIdx && "Passed null CXIndex"); |
| 483 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 484 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 485 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 486 | std::vector<const char *> argv; |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 487 | |
| 488 | // First add the complete path to the 'clang' executable. |
| 489 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 490 | argv.push_back(ClangPath.c_str()); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 491 | |
| 492 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 493 | argv.push_back("-emit-ast"); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 494 | |
| 495 | // The 'source_filename' argument is optional. If the caller does not |
| 496 | // specify it then it is assumed that the source file is specified |
| 497 | // in the actual argument list. |
| 498 | if (source_filename) |
| 499 | argv.push_back(source_filename); |
| 500 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 501 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 502 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 503 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 504 | argv.push_back(tmpnam(astTmpFile)); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 505 | |
| 506 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 507 | for (int i = 0; i < num_command_line_args; ++i) |
| 508 | if (const char *arg = command_line_args[i]) { |
| 509 | if (strcmp(arg, "-o") == 0) { |
| 510 | ++i; // Also skip the matching argument. |
| 511 | continue; |
| 512 | } |
| 513 | if (strcmp(arg, "-emit-ast") == 0 || |
| 514 | strcmp(arg, "-c") == 0 || |
| 515 | strcmp(arg, "-fsyntax-only") == 0) { |
| 516 | continue; |
| 517 | } |
| 518 | |
| 519 | // Keep the argument. |
| 520 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 521 | } |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 522 | |
| 523 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 524 | argv.push_back(NULL); |
| 525 | |
Ted Kremenek | feb15e3 | 2009-10-26 22:14:08 +0000 | [diff] [blame] | 526 | // Invoke 'clang'. |
| 527 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 528 | // on Unix or NUL (Windows). |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 529 | std::string ErrMsg; |
Ted Kremenek | c46e463 | 2009-10-19 22:27:32 +0000 | [diff] [blame] | 530 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL }; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 531 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 532 | /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL, |
| 533 | /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); |
| 534 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 535 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 536 | llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg |
| 537 | << '\n' << "Arguments: \n"; |
| 538 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 539 | I!=E; ++I) { |
| 540 | if (*I) |
| 541 | llvm::errs() << ' ' << *I << '\n'; |
| 542 | } |
| 543 | llvm::errs() << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 544 | } |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 545 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 546 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 547 | ASTUnit *ATU = static_cast<ASTUnit *>( |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 548 | clang_createTranslationUnit(CIdx, astTmpFile)); |
| 549 | if (ATU) |
| 550 | ATU->unlinkTemporaryFile(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 551 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 554 | void clang_disposeTranslationUnit( |
| 555 | CXTranslationUnit CTUnit) |
| 556 | { |
| 557 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 558 | delete static_cast<ASTUnit *>(CTUnit); |
| 559 | } |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 560 | |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 561 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 562 | { |
| 563 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 564 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 565 | return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 566 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 567 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 568 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
| 569 | CXTranslationUnitIterator callback, |
| 570 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 571 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 572 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 573 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 574 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 575 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 576 | TUVisitor DVisit(CTUnit, callback, CData, |
| 577 | CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 578 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 581 | void clang_loadDeclaration(CXDecl Dcl, |
| 582 | CXDeclIterator callback, |
| 583 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 584 | { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 585 | assert(Dcl && "Passed null CXDecl"); |
| 586 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 587 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 588 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 589 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 592 | // Some notes on CXEntity: |
| 593 | // |
| 594 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 595 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 596 | // entity for 2 different types). For example: |
| 597 | // |
| 598 | // module1.m: @interface Foo @end Foo *x; |
| 599 | // module2.m: void Foo(int); |
| 600 | // |
| 601 | // - Since the unique name spans translation units, static data/functions |
| 602 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 603 | // As a result, there will be no entity for the following: |
| 604 | // |
| 605 | // module.m: static void Foo() { } |
| 606 | // |
| 607 | |
| 608 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 609 | const char *clang_getDeclarationName(CXEntity) |
| 610 | { |
| 611 | return ""; |
| 612 | } |
| 613 | const char *clang_getURI(CXEntity) |
| 614 | { |
| 615 | return ""; |
| 616 | } |
| 617 | |
| 618 | CXEntity clang_getEntity(const char *URI) |
| 619 | { |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | // |
| 624 | // CXDecl Operations. |
| 625 | // |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 626 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 627 | { |
| 628 | return 0; |
| 629 | } |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 630 | CXString clang_getDeclSpelling(CXDecl AnonDecl) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 631 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 632 | assert(AnonDecl && "Passed null CXDecl"); |
| 633 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 634 | |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 635 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
| 636 | return createCXString(OMD->getSelector().getAsString().c_str(), true); |
| 637 | |
| 638 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 639 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 640 | // and returns different names. NamedDecl returns the class name and |
| 641 | // ObjCCategoryImplDecl returns the category name. |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 642 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
| 643 | |
| 644 | if (ND->getIdentifier()) |
| 645 | return createCXString(ND->getIdentifier()->getNameStart()); |
| 646 | |
| 647 | return createCXString(""); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 648 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 649 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 650 | unsigned clang_getDeclLine(CXDecl AnonDecl) |
| 651 | { |
| 652 | assert(AnonDecl && "Passed null CXDecl"); |
| 653 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 654 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 655 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 656 | } |
| 657 | |
| 658 | unsigned clang_getDeclColumn(CXDecl AnonDecl) |
| 659 | { |
| 660 | assert(AnonDecl && "Passed null CXDecl"); |
| 661 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 662 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 663 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 666 | const char *clang_getDeclSource(CXDecl AnonDecl) |
| 667 | { |
| 668 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 669 | FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl)); |
| 670 | assert (FEnt && "Cannot find FileEntry for Decl"); |
| 671 | return clang_getFileName(FEnt); |
| 672 | } |
| 673 | |
| 674 | static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr, |
| 675 | SourceLocation SLoc) |
| 676 | { |
| 677 | FileID FID; |
| 678 | if (SLoc.isFileID()) |
| 679 | FID = SMgr.getFileID(SLoc); |
| 680 | else |
| 681 | FID = SMgr.getDecomposedSpellingLoc(SLoc).first; |
| 682 | return SMgr.getFileEntryForID(FID); |
| 683 | } |
| 684 | |
| 685 | CXFile clang_getDeclSourceFile(CXDecl AnonDecl) |
| 686 | { |
| 687 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 688 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 689 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 690 | return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation()); |
| 691 | } |
| 692 | |
| 693 | const char *clang_getFileName(CXFile SFile) { |
| 694 | assert(SFile && "Passed null CXFile"); |
| 695 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 696 | return FEnt->getName(); |
| 697 | } |
| 698 | |
| 699 | time_t clang_getFileTime(CXFile SFile) { |
| 700 | assert(SFile && "Passed null CXFile"); |
| 701 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 702 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 705 | CXString clang_getCursorSpelling(CXCursor C) |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 706 | { |
| 707 | assert(C.decl && "CXCursor has null decl"); |
| 708 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 709 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 710 | if (clang_isReference(C.kind)) { |
| 711 | switch (C.kind) { |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 712 | case CXCursor_ObjCSuperClassRef: { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 713 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 714 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 715 | return createCXString(OID->getSuperClass()->getIdentifier() |
| 716 | ->getNameStart()); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 717 | } |
| 718 | case CXCursor_ObjCClassRef: { |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 719 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 720 | return createCXString(OID->getIdentifier()->getNameStart()); |
| 721 | |
| 722 | ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND); |
| 723 | assert(OCD && "clang_getCursorLine(): Missing category decl"); |
| 724 | return createCXString(OCD->getClassInterface()->getIdentifier() |
| 725 | ->getNameStart()); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 726 | } |
| 727 | case CXCursor_ObjCProtocolRef: { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 728 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 729 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 730 | return createCXString(OID->getIdentifier()->getNameStart()); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 731 | } |
| 732 | case CXCursor_ObjCSelectorRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 733 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 734 | static_cast<Stmt *>(C.stmt)); |
| 735 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 736 | return createCXString(OME->getSelector().getAsString().c_str(), true); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 737 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 738 | case CXCursor_VarRef: |
| 739 | case CXCursor_FunctionRef: |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 740 | case CXCursor_EnumConstantRef: { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 741 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 742 | static_cast<Stmt *>(C.stmt)); |
| 743 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 744 | return createCXString(DRE->getDecl()->getIdentifier()->getNameStart()); |
Steve Naroff | bade7de | 2009-10-19 13:41:39 +0000 | [diff] [blame] | 745 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 746 | default: |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 747 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | return clang_getDeclSpelling(C.decl); |
| 751 | } |
| 752 | |
| 753 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 754 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 755 | switch (Kind) { |
| 756 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 757 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 758 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 759 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 760 | case CXCursor_StructDecl: return "StructDecl"; |
| 761 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 762 | case CXCursor_ClassDecl: return "ClassDecl"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 763 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 764 | case CXCursor_VarDecl: return "VarDecl"; |
| 765 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 766 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 767 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 768 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 769 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 770 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 771 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 772 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 773 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 774 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 775 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 776 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 777 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 778 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 779 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 780 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 781 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
| 782 | |
| 783 | case CXCursor_VarRef: return "VarRef"; |
| 784 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 785 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 786 | case CXCursor_MemberRef: return "MemberRef"; |
| 787 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 788 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 789 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 790 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 791 | default: return "<not implemented>"; |
| 792 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 793 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 794 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 795 | static enum CXCursorKind TranslateKind(Decl *D) { |
| 796 | switch (D->getKind()) { |
| 797 | case Decl::Function: return CXCursor_FunctionDecl; |
| 798 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 799 | case Decl::Enum: return CXCursor_EnumDecl; |
| 800 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 801 | case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class |
| 802 | case Decl::Field: return CXCursor_FieldDecl; |
| 803 | case Decl::Var: return CXCursor_VarDecl; |
| 804 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 805 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 806 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 807 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 808 | case Decl::ObjCMethod: { |
| 809 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D); |
| 810 | if (MD->isInstanceMethod()) |
| 811 | return CXCursor_ObjCInstanceMethodDecl; |
| 812 | return CXCursor_ObjCClassMethodDecl; |
| 813 | } |
| 814 | default: break; |
| 815 | } |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 816 | return CXCursor_NotImplemented; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 817 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 818 | // |
| 819 | // CXCursor Operations. |
| 820 | // |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 821 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 822 | unsigned line, unsigned column) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 823 | { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 824 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 825 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 826 | |
| 827 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 828 | const FileEntry *File = FMgr.getFile(source_name, |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 829 | source_name+strlen(source_name)); |
| 830 | if (!File) { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 831 | CXCursor C = { CXCursor_InvalidFile, 0, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 832 | return C; |
| 833 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 834 | SourceLocation SLoc = |
| 835 | CXXUnit->getSourceManager().getLocation(File, line, column); |
| 836 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 837 | ASTLocation LastLoc = CXXUnit->getLastASTLocation(); |
| 838 | |
| 839 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc, |
| 840 | &LastLoc); |
| 841 | if (ALoc.isValid()) |
| 842 | CXXUnit->setLastASTLocation(ALoc); |
| 843 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 844 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 845 | if (ALoc.isNamedRef()) |
| 846 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 847 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 848 | if (Dcl) { |
| 849 | if (Stm) { |
| 850 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) { |
| 851 | CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm }; |
| 852 | return C; |
| 853 | } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) { |
| 854 | CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp }; |
| 855 | return C; |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 856 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 857 | // Fall through...treat as a decl, not a ref. |
| 858 | } |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 859 | if (ALoc.isNamedRef()) { |
| 860 | if (isa<ObjCInterfaceDecl>(Dcl)) { |
| 861 | CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() }; |
| 862 | return C; |
| 863 | } |
| 864 | if (isa<ObjCProtocolDecl>(Dcl)) { |
| 865 | CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() }; |
| 866 | return C; |
| 867 | } |
| 868 | } |
| 869 | CXCursor C = { TranslateKind(Dcl), Dcl, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 870 | return C; |
| 871 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 872 | CXCursor C = { CXCursor_NoDeclFound, 0, 0 }; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 873 | return C; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 876 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) |
| 877 | { |
| 878 | assert(AnonDecl && "Passed null CXDecl"); |
| 879 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 880 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 881 | CXCursor C = { TranslateKind(ND), ND, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 882 | return C; |
| 883 | } |
| 884 | |
| 885 | unsigned clang_isInvalid(enum CXCursorKind K) |
| 886 | { |
| 887 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 888 | } |
| 889 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 890 | unsigned clang_isDeclaration(enum CXCursorKind K) |
| 891 | { |
| 892 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 893 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 894 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 895 | unsigned clang_isReference(enum CXCursorKind K) |
| 896 | { |
| 897 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 898 | } |
| 899 | |
| 900 | unsigned clang_isDefinition(enum CXCursorKind K) |
| 901 | { |
| 902 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 903 | } |
| 904 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 905 | CXCursorKind clang_getCursorKind(CXCursor C) |
| 906 | { |
| 907 | return C.kind; |
| 908 | } |
| 909 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 910 | static Decl *getDeclFromExpr(Stmt *E) { |
| 911 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 912 | return RefExpr->getDecl(); |
| 913 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 914 | return ME->getMemberDecl(); |
| 915 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 916 | return RE->getDecl(); |
| 917 | |
| 918 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 919 | return getDeclFromExpr(CE->getCallee()); |
| 920 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 921 | return getDeclFromExpr(CE->getSubExpr()); |
| 922 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 923 | return OME->getMethodDecl(); |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 928 | CXDecl clang_getCursorDecl(CXCursor C) |
| 929 | { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 930 | if (clang_isDeclaration(C.kind)) |
| 931 | return C.decl; |
| 932 | |
| 933 | if (clang_isReference(C.kind)) { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 934 | if (C.stmt) { |
Steve Naroff | f9adf8f | 2009-10-05 17:58:19 +0000 | [diff] [blame] | 935 | if (C.kind == CXCursor_ObjCClassRef || |
| 936 | C.kind == CXCursor_ObjCProtocolRef) |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 937 | return static_cast<Stmt *>(C.stmt); |
| 938 | else |
| 939 | return getDeclFromExpr(static_cast<Stmt *>(C.stmt)); |
| 940 | } else |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 941 | return C.decl; |
| 942 | } |
| 943 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 946 | unsigned clang_getCursorLine(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 947 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 948 | assert(C.decl && "CXCursor has null decl"); |
| 949 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 950 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 951 | |
| 952 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 953 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 954 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 955 | |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 956 | // Access string. |
| 957 | const char *clang_getCString(CXString string) { |
| 958 | return string.Spelling; |
| 959 | } |
| 960 | |
| 961 | // Free CXString. |
| 962 | void clang_disposeString(CXString string) { |
Benjamin Kramer | 858e5de | 2009-11-09 18:24:53 +0000 | [diff] [blame] | 963 | if (string.MustFreeString) |
| 964 | free((void*)string.Spelling); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 967 | unsigned clang_getCursorColumn(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 968 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 969 | assert(C.decl && "CXCursor has null decl"); |
| 970 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 971 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 972 | |
| 973 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 974 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 975 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 976 | const char *clang_getCursorSource(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 977 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 978 | assert(C.decl && "CXCursor has null decl"); |
| 979 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 980 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 981 | |
| 982 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame^] | 983 | |
| 984 | if (SLoc.isFileID()) { |
| 985 | const char *bufferName = SourceMgr.getBufferName(SLoc); |
| 986 | return bufferName[0] == '<' ? NULL : bufferName; |
| 987 | } |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 988 | |
| 989 | // Retrieve the file in which the macro was instantiated, then provide that |
| 990 | // buffer name. |
| 991 | // FIXME: Do we want to give specific macro-instantiation information? |
| 992 | const llvm::MemoryBuffer *Buffer |
| 993 | = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first); |
| 994 | if (!Buffer) |
| 995 | return 0; |
| 996 | |
| 997 | return Buffer->getBufferIdentifier(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 1000 | CXFile clang_getCursorSourceFile(CXCursor C) |
| 1001 | { |
| 1002 | assert(C.decl && "CXCursor has null decl"); |
| 1003 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 1004 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 1005 | |
| 1006 | return (void *)getFileEntryFromSourceLocation(SourceMgr, |
| 1007 | getLocationFromCursor(C,SourceMgr, ND)); |
| 1008 | } |
| 1009 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1010 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 1011 | const char **startBuf, |
| 1012 | const char **endBuf, |
| 1013 | unsigned *startLine, |
| 1014 | unsigned *startColumn, |
| 1015 | unsigned *endLine, |
| 1016 | unsigned *endColumn) |
| 1017 | { |
| 1018 | assert(C.decl && "CXCursor has null decl"); |
| 1019 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 1020 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 1021 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 1022 | |
| 1023 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 1024 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 1025 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 1026 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 1027 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 1028 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 1029 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 1030 | } |
| 1031 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1032 | enum CXCompletionChunkKind |
| 1033 | clang_getCompletionChunkKind(CXCompletionString completion_string, |
| 1034 | unsigned chunk_number) { |
| 1035 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1036 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1037 | return CXCompletionChunk_Text; |
| 1038 | |
| 1039 | switch ((*CCStr)[chunk_number].Kind) { |
| 1040 | case CodeCompletionString::CK_TypedText: |
| 1041 | return CXCompletionChunk_TypedText; |
| 1042 | case CodeCompletionString::CK_Text: |
| 1043 | return CXCompletionChunk_Text; |
| 1044 | case CodeCompletionString::CK_Optional: |
| 1045 | return CXCompletionChunk_Optional; |
| 1046 | case CodeCompletionString::CK_Placeholder: |
| 1047 | return CXCompletionChunk_Placeholder; |
| 1048 | case CodeCompletionString::CK_Informative: |
| 1049 | return CXCompletionChunk_Informative; |
| 1050 | case CodeCompletionString::CK_CurrentParameter: |
| 1051 | return CXCompletionChunk_CurrentParameter; |
| 1052 | case CodeCompletionString::CK_LeftParen: |
| 1053 | return CXCompletionChunk_LeftParen; |
| 1054 | case CodeCompletionString::CK_RightParen: |
| 1055 | return CXCompletionChunk_RightParen; |
| 1056 | case CodeCompletionString::CK_LeftBracket: |
| 1057 | return CXCompletionChunk_LeftBracket; |
| 1058 | case CodeCompletionString::CK_RightBracket: |
| 1059 | return CXCompletionChunk_RightBracket; |
| 1060 | case CodeCompletionString::CK_LeftBrace: |
| 1061 | return CXCompletionChunk_LeftBrace; |
| 1062 | case CodeCompletionString::CK_RightBrace: |
| 1063 | return CXCompletionChunk_RightBrace; |
| 1064 | case CodeCompletionString::CK_LeftAngle: |
| 1065 | return CXCompletionChunk_LeftAngle; |
| 1066 | case CodeCompletionString::CK_RightAngle: |
| 1067 | return CXCompletionChunk_RightAngle; |
| 1068 | case CodeCompletionString::CK_Comma: |
| 1069 | return CXCompletionChunk_Comma; |
| 1070 | } |
| 1071 | |
| 1072 | // Should be unreachable, but let's be careful. |
| 1073 | return CXCompletionChunk_Text; |
| 1074 | } |
| 1075 | |
| 1076 | const char *clang_getCompletionChunkText(CXCompletionString completion_string, |
| 1077 | unsigned chunk_number) { |
| 1078 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1079 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1080 | return 0; |
| 1081 | |
| 1082 | switch ((*CCStr)[chunk_number].Kind) { |
| 1083 | case CodeCompletionString::CK_TypedText: |
| 1084 | case CodeCompletionString::CK_Text: |
| 1085 | case CodeCompletionString::CK_Placeholder: |
| 1086 | case CodeCompletionString::CK_CurrentParameter: |
| 1087 | case CodeCompletionString::CK_Informative: |
| 1088 | case CodeCompletionString::CK_LeftParen: |
| 1089 | case CodeCompletionString::CK_RightParen: |
| 1090 | case CodeCompletionString::CK_LeftBracket: |
| 1091 | case CodeCompletionString::CK_RightBracket: |
| 1092 | case CodeCompletionString::CK_LeftBrace: |
| 1093 | case CodeCompletionString::CK_RightBrace: |
| 1094 | case CodeCompletionString::CK_LeftAngle: |
| 1095 | case CodeCompletionString::CK_RightAngle: |
| 1096 | case CodeCompletionString::CK_Comma: |
| 1097 | return (*CCStr)[chunk_number].Text; |
| 1098 | |
| 1099 | case CodeCompletionString::CK_Optional: |
| 1100 | // Note: treated as an empty text block. |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | // Should be unreachable, but let's be careful. |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | CXCompletionString |
| 1109 | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
| 1110 | unsigned chunk_number) { |
| 1111 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1112 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1113 | return 0; |
| 1114 | |
| 1115 | switch ((*CCStr)[chunk_number].Kind) { |
| 1116 | case CodeCompletionString::CK_TypedText: |
| 1117 | case CodeCompletionString::CK_Text: |
| 1118 | case CodeCompletionString::CK_Placeholder: |
| 1119 | case CodeCompletionString::CK_CurrentParameter: |
| 1120 | case CodeCompletionString::CK_Informative: |
| 1121 | case CodeCompletionString::CK_LeftParen: |
| 1122 | case CodeCompletionString::CK_RightParen: |
| 1123 | case CodeCompletionString::CK_LeftBracket: |
| 1124 | case CodeCompletionString::CK_RightBracket: |
| 1125 | case CodeCompletionString::CK_LeftBrace: |
| 1126 | case CodeCompletionString::CK_RightBrace: |
| 1127 | case CodeCompletionString::CK_LeftAngle: |
| 1128 | case CodeCompletionString::CK_RightAngle: |
| 1129 | case CodeCompletionString::CK_Comma: |
| 1130 | return 0; |
| 1131 | |
| 1132 | case CodeCompletionString::CK_Optional: |
| 1133 | // Note: treated as an empty text block. |
| 1134 | return (*CCStr)[chunk_number].Optional; |
| 1135 | } |
| 1136 | |
| 1137 | // Should be unreachable, but let's be careful. |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { |
| 1142 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1143 | return CCStr? CCStr->size() : 0; |
| 1144 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1146 | static CXCursorKind parseResultKind(llvm::StringRef Str) { |
| 1147 | return llvm::StringSwitch<CXCursorKind>(Str) |
| 1148 | .Case("Typedef", CXCursor_TypedefDecl) |
| 1149 | .Case("Struct", CXCursor_StructDecl) |
| 1150 | .Case("Union", CXCursor_UnionDecl) |
| 1151 | .Case("Class", CXCursor_ClassDecl) |
Douglas Gregor | ff4393c | 2009-11-09 21:35:27 +0000 | [diff] [blame] | 1152 | .Case("Enum", CXCursor_EnumDecl) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1153 | .Case("Field", CXCursor_FieldDecl) |
| 1154 | .Case("EnumConstant", CXCursor_EnumConstantDecl) |
| 1155 | .Case("Function", CXCursor_FunctionDecl) |
| 1156 | // FIXME: Hacks here to make C++ member functions look like C functions |
| 1157 | .Case("CXXMethod", CXCursor_FunctionDecl) |
| 1158 | .Case("CXXConstructor", CXCursor_FunctionDecl) |
| 1159 | .Case("CXXDestructor", CXCursor_FunctionDecl) |
| 1160 | .Case("CXXConversion", CXCursor_FunctionDecl) |
| 1161 | .Case("Var", CXCursor_VarDecl) |
| 1162 | .Case("ParmVar", CXCursor_ParmDecl) |
| 1163 | .Case("ObjCInterface", CXCursor_ObjCInterfaceDecl) |
| 1164 | .Case("ObjCCategory", CXCursor_ObjCCategoryDecl) |
| 1165 | .Case("ObjCProtocol", CXCursor_ObjCProtocolDecl) |
| 1166 | .Case("ObjCProperty", CXCursor_ObjCPropertyDecl) |
| 1167 | .Case("ObjCIvar", CXCursor_ObjCIvarDecl) |
| 1168 | .Case("ObjCInstanceMethod", CXCursor_ObjCInstanceMethodDecl) |
| 1169 | .Case("ObjCClassMethod", CXCursor_ObjCClassMethodDecl) |
| 1170 | .Default(CXCursor_NotImplemented); |
| 1171 | } |
| 1172 | |
| 1173 | void clang_codeComplete(CXIndex CIdx, |
| 1174 | const char *source_filename, |
| 1175 | int num_command_line_args, |
| 1176 | const char **command_line_args, |
| 1177 | const char *complete_filename, |
| 1178 | unsigned complete_line, |
| 1179 | unsigned complete_column, |
| 1180 | CXCompletionIterator completion_iterator, |
| 1181 | CXClientData client_data) { |
| 1182 | // The indexer, which is mainly used to determine where diagnostics go. |
| 1183 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 1184 | |
| 1185 | // Build up the arguments for invoking 'clang'. |
| 1186 | std::vector<const char *> argv; |
| 1187 | |
| 1188 | // First add the complete path to the 'clang' executable. |
| 1189 | llvm::sys::Path ClangPath = CXXIdx->getClangPath(); |
| 1190 | argv.push_back(ClangPath.c_str()); |
| 1191 | |
| 1192 | // Add the '-fsyntax-only' argument so that we only perform a basic |
| 1193 | // syntax check of the code. |
| 1194 | argv.push_back("-fsyntax-only"); |
| 1195 | |
| 1196 | // Add the appropriate '-code-completion-at=file:line:column' argument |
| 1197 | // to perform code completion, with an "-Xclang" preceding it. |
| 1198 | std::string code_complete_at; |
| 1199 | code_complete_at += "-code-completion-at="; |
| 1200 | code_complete_at += complete_filename; |
| 1201 | code_complete_at += ":"; |
| 1202 | code_complete_at += llvm::utostr(complete_line); |
| 1203 | code_complete_at += ":"; |
| 1204 | code_complete_at += llvm::utostr(complete_column); |
| 1205 | argv.push_back("-Xclang"); |
| 1206 | argv.push_back(code_complete_at.c_str()); |
| 1207 | argv.push_back("-Xclang"); |
Daniel Dunbar | 914474c | 2009-11-13 01:02:10 +0000 | [diff] [blame] | 1208 | argv.push_back("-code-completion-debug-printer=0"); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1209 | |
| 1210 | // Add the source file name (FIXME: later, we'll want to build temporary |
| 1211 | // file from the buffer, or just feed the source text via standard input). |
| 1212 | argv.push_back(source_filename); |
| 1213 | |
| 1214 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 1215 | for (int i = 0; i < num_command_line_args; ++i) |
| 1216 | if (const char *arg = command_line_args[i]) { |
| 1217 | if (strcmp(arg, "-o") == 0) { |
| 1218 | ++i; // Also skip the matching argument. |
| 1219 | continue; |
| 1220 | } |
| 1221 | if (strcmp(arg, "-emit-ast") == 0 || |
| 1222 | strcmp(arg, "-c") == 0 || |
| 1223 | strcmp(arg, "-fsyntax-only") == 0) { |
| 1224 | continue; |
| 1225 | } |
| 1226 | |
| 1227 | // Keep the argument. |
| 1228 | argv.push_back(arg); |
| 1229 | } |
| 1230 | |
| 1231 | // Add the null terminator. |
| 1232 | argv.push_back(NULL); |
| 1233 | |
| 1234 | // Generate a temporary name for the AST file. |
| 1235 | char tmpFile[L_tmpnam]; |
| 1236 | char *tmpFileName = tmpnam(tmpFile); |
| 1237 | llvm::sys::Path ResultsFile(tmpFileName); |
| 1238 | |
| 1239 | // Invoke 'clang'. |
| 1240 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 1241 | // on Unix or NUL (Windows). |
| 1242 | std::string ErrMsg; |
| 1243 | const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, &DevNull, 0 }; |
| 1244 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 1245 | /* redirects */ &Redirects[0], |
| 1246 | /* secondsToWait */ 0, |
| 1247 | /* memoryLimits */ 0, &ErrMsg); |
| 1248 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 1249 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1250 | llvm::errs() << "clang_codeComplete: " << ErrMsg |
| 1251 | << '\n' << "Arguments: \n"; |
| 1252 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
| 1253 | I!=E; ++I) { |
| 1254 | if (*I) |
| 1255 | llvm::errs() << ' ' << *I << '\n'; |
| 1256 | } |
| 1257 | llvm::errs() << '\n'; |
| 1258 | } |
| 1259 | |
| 1260 | // Parse the resulting source file to find code-completion results. |
| 1261 | using llvm::MemoryBuffer; |
| 1262 | using llvm::StringRef; |
| 1263 | if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { |
| 1264 | StringRef Buffer = F->getBuffer(); |
| 1265 | do { |
| 1266 | StringRef::size_type CompletionIdx = Buffer.find("COMPLETION:"); |
| 1267 | StringRef::size_type OverloadIdx = Buffer.find("OVERLOAD:"); |
| 1268 | if (CompletionIdx == StringRef::npos && OverloadIdx == StringRef::npos) |
| 1269 | break; |
| 1270 | |
| 1271 | if (OverloadIdx < CompletionIdx) { |
| 1272 | // Parse an overload result. |
| 1273 | Buffer = Buffer.substr(OverloadIdx); |
| 1274 | |
| 1275 | // Skip past the OVERLOAD: |
| 1276 | Buffer = Buffer.substr(Buffer.find(':') + 1); |
| 1277 | |
| 1278 | // Find the entire completion string. |
| 1279 | StringRef::size_type EOL = Buffer.find_first_of("\n\r"); |
| 1280 | if (EOL == StringRef::npos) |
| 1281 | continue; |
| 1282 | |
| 1283 | StringRef Line = Buffer.substr(0, EOL); |
| 1284 | Buffer = Buffer.substr(EOL + 1); |
| 1285 | CodeCompletionString *CCStr = CodeCompletionString::Deserialize(Line); |
| 1286 | if (!CCStr || CCStr->empty()) |
| 1287 | continue; |
| 1288 | |
| 1289 | // Vend the code-completion result to the caller. |
| 1290 | CXCompletionResult Result; |
| 1291 | Result.CursorKind = CXCursor_NotImplemented; |
| 1292 | Result.CompletionString = CCStr; |
| 1293 | if (completion_iterator) |
| 1294 | completion_iterator(&Result, client_data); |
| 1295 | delete CCStr; |
| 1296 | |
| 1297 | continue; |
| 1298 | } |
| 1299 | |
| 1300 | // Parse a completion result. |
| 1301 | Buffer = Buffer.substr(CompletionIdx); |
| 1302 | |
| 1303 | // Skip past the COMPLETION: |
| 1304 | Buffer = Buffer.substr(Buffer.find(':') + 1); |
| 1305 | |
| 1306 | // Get the rank |
| 1307 | unsigned Rank = 0; |
| 1308 | StringRef::size_type AfterRank = Buffer.find(':'); |
| 1309 | Buffer.substr(0, AfterRank).getAsInteger(10, Rank); |
| 1310 | Buffer = Buffer.substr(AfterRank + 1); |
| 1311 | |
| 1312 | // Get the kind of result. |
| 1313 | StringRef::size_type AfterKind = Buffer.find(':'); |
| 1314 | StringRef Kind = Buffer.substr(0, AfterKind); |
| 1315 | Buffer = Buffer.substr(AfterKind + 1); |
| 1316 | |
| 1317 | // Skip over any whitespace. |
| 1318 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t")); |
| 1319 | |
| 1320 | // Find the entire completion string. |
| 1321 | StringRef::size_type EOL = Buffer.find_first_of("\n\r"); |
| 1322 | if (EOL == StringRef::npos) |
| 1323 | continue; |
| 1324 | |
| 1325 | StringRef Line = Buffer.substr(0, EOL); |
| 1326 | Buffer = Buffer.substr(EOL + 1); |
| 1327 | CodeCompletionString *CCStr = CodeCompletionString::Deserialize(Line); |
| 1328 | if (!CCStr || CCStr->empty()) |
| 1329 | continue; |
| 1330 | |
| 1331 | // Vend the code-completion result to the caller. |
| 1332 | CXCompletionResult Result; |
| 1333 | Result.CursorKind = parseResultKind(Kind); |
| 1334 | Result.CompletionString = CCStr; |
| 1335 | if (completion_iterator) |
| 1336 | completion_iterator(&Result, client_data); |
| 1337 | delete CCStr; |
| 1338 | } while (true); |
| 1339 | delete F; |
| 1340 | } |
| 1341 | |
| 1342 | ResultsFile.eraseFromDisk(); |
| 1343 | } |
| 1344 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1345 | } // end extern "C" |