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