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