blob: 4681b939cea29ce975aa3dfc342b4eee5b7c2422 [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001//===- 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 Dunbar0d7dd222009-11-30 20:42:43 +00007//
Ted Kremenekd2fa5662009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the Clang-C Source Indexing library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang-c/Index.h"
Steve Naroff50398192009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroff9efa7672009-09-04 15:44:05 +000017#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Utils.h"
Douglas Gregor0c8296d2009-11-07 00:00:49 +000019#include "clang/Sema/CodeCompleteConsumer.h"
Steve Naroff50398192009-08-28 15:28:48 +000020#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000021#include "clang/AST/StmtVisitor.h"
Steve Naroffc857ea42009-09-02 13:28:54 +000022#include "clang/AST/Decl.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000023#include "clang/Basic/FileManager.h"
Steve Naroff2d4d6292009-08-31 14:26:51 +000024#include "clang/Basic/SourceManager.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000025#include "clang/Frontend/ASTUnit.h"
Douglas Gregor0c8296d2009-11-07 00:00:49 +000026#include "llvm/ADT/StringExtras.h"
Benjamin Kramer20d75812009-10-18 16:13:48 +000027#include "llvm/Config/config.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000028#include "llvm/Support/Compiler.h"
Douglas Gregor02465752009-10-16 21:24:31 +000029#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/System/Path.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000031#include "llvm/System/Program.h"
Ted Kremenek379afec2009-10-22 03:24:01 +000032#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000033
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000034#include <cstdio>
Ted Kremenek49358d82009-10-19 21:17:25 +000035#include <vector>
Douglas Gregor0c8296d2009-11-07 00:00:49 +000036#include <sstream>
Ted Kremenek49358d82009-10-19 21:17:25 +000037
Benjamin Kramer20d75812009-10-18 16:13:48 +000038#ifdef LLVM_ON_WIN32
39#define WIN32_LEAN_AND_MEAN
40#include <windows.h>
41#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +000042#include <dlfcn.h>
Daniel Dunbara47dd192009-10-17 23:53:11 +000043#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +000044
Steve Naroff50398192009-08-28 15:28:48 +000045using namespace clang;
46using namespace idx;
47
Steve Naroff89922f82009-08-31 00:59:03 +000048namespace {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000049static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
Steve Naroff4ade6d62009-09-23 17:52:52 +000050{
51 NamedDecl *D = DRE->getDecl();
52 if (isa<VarDecl>(D))
53 return CXCursor_VarRef;
54 else if (isa<FunctionDecl>(D))
55 return CXCursor_FunctionRef;
56 else if (isa<EnumConstantDecl>(D))
57 return CXCursor_EnumConstantRef;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000058 else
Steve Naroff4ade6d62009-09-23 17:52:52 +000059 return CXCursor_NotImplemented;
60}
61
62#if 0
63// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000064class CRefVisitor : public StmtVisitor<CRefVisitor> {
65 CXDecl CDecl;
66 CXDeclIterator Callback;
67 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000068
Steve Narofffb570422009-09-22 19:25:29 +000069 void Call(enum CXCursorKind CK, Stmt *SRef) {
70 CXCursor C = { CK, CDecl, SRef };
71 Callback(CDecl, C, CData);
72 }
73
74public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000075 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Narofffb570422009-09-22 19:25:29 +000076 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000077
Steve Narofffb570422009-09-22 19:25:29 +000078 void VisitStmt(Stmt *S) {
79 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
80 C != CEnd; ++C)
81 Visit(*C);
82 }
83 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000084 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000085 }
86 void VisitMemberExpr(MemberExpr *Node) {
87 Call(CXCursor_MemberRef, Node);
88 }
89 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
90 Call(CXCursor_ObjCSelectorRef, Node);
91 }
92 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
93 Call(CXCursor_ObjCIvarRef, Node);
94 }
95};
Steve Naroff4ade6d62009-09-23 17:52:52 +000096#endif
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000097
Ted Kremenekfc062212009-10-19 21:44:57 +000098/// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted
99/// warnings and errors.
100class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient {
101public:
102 virtual ~IgnoreDiagnosticsClient() {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000103 virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {}
Ted Kremenekfc062212009-10-19 21:44:57 +0000104};
Steve Narofffb570422009-09-22 19:25:29 +0000105
Steve Naroff89922f82009-08-31 00:59:03 +0000106// Translation Unit Visitor.
107class TUVisitor : public DeclVisitor<TUVisitor> {
108 CXTranslationUnit TUnit;
109 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000110 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000111
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000112 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
113 // to the visitor. Declarations with a PCH level greater than this value will
114 // be suppressed.
115 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000116
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000117 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000118 // Filter any declarations that have a PCH level greater than what we allow.
119 if (ND->getPCHLevel() > MaxPCHLevel)
120 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000121
Steve Narofff96b5242009-10-28 20:44:47 +0000122 // Filter any implicit declarations (since the source info will be bogus).
123 if (ND->isImplicit())
124 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000125
Steve Narofffb570422009-09-22 19:25:29 +0000126 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000127 Callback(TUnit, C, CData);
128 }
Steve Naroff89922f82009-08-31 00:59:03 +0000129public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000130 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000131 CXTranslationUnitIterator cback, CXClientData D,
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000132 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000133 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000134
Steve Naroff89922f82009-08-31 00:59:03 +0000135 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
136 VisitDeclContext(dyn_cast<DeclContext>(D));
137 }
138 void VisitDeclContext(DeclContext *DC) {
139 for (DeclContext::decl_iterator
140 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
141 Visit(*I);
142 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000143
144 void VisitFunctionDecl(FunctionDecl *ND) {
145 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
146 : CXCursor_FunctionDecl, ND);
147 }
148 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
149 Call(CXCursor_ObjCCategoryDecl, ND);
150 }
151 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
152 Call(CXCursor_ObjCCategoryDefn, ND);
153 }
154 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
155 Call(CXCursor_ObjCClassDefn, ND);
156 }
157 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
158 Call(CXCursor_ObjCInterfaceDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000159 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000160 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
161 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000162 }
163 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000164 switch (ND->getTagKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000165 case TagDecl::TK_struct:
166 Call(CXCursor_StructDecl, ND);
167 break;
168 case TagDecl::TK_class:
169 Call(CXCursor_ClassDecl, ND);
170 break;
171 case TagDecl::TK_union:
172 Call(CXCursor_UnionDecl, ND);
173 break;
174 case TagDecl::TK_enum:
175 Call(CXCursor_EnumDecl, ND);
176 break;
Steve Naroffc857ea42009-09-02 13:28:54 +0000177 }
Steve Naroff89922f82009-08-31 00:59:03 +0000178 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000179 void VisitTypedefDecl(TypedefDecl *ND) {
180 Call(CXCursor_TypedefDecl, ND);
181 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000182 void VisitVarDecl(VarDecl *ND) {
183 Call(CXCursor_VarDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000184 }
Steve Naroff89922f82009-08-31 00:59:03 +0000185};
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000186
Steve Naroff89922f82009-08-31 00:59:03 +0000187
Steve Naroffc857ea42009-09-02 13:28:54 +0000188// Declaration visitor.
189class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
190 CXDecl CDecl;
191 CXDeclIterator Callback;
192 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000193
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000194 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
195 // to the visitor. Declarations with a PCH level greater than this value will
196 // be suppressed.
197 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000198
Steve Naroffc857ea42009-09-02 13:28:54 +0000199 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000200 // Disable the callback when the context is equal to the visiting decl.
201 if (CDecl == ND && !clang_isReference(CK))
202 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000203
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000204 // Filter any declarations that have a PCH level greater than what we allow.
205 if (ND->getPCHLevel() > MaxPCHLevel)
206 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000207
Steve Narofffb570422009-09-22 19:25:29 +0000208 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000209 Callback(CDecl, C, CData);
210 }
Steve Naroff89922f82009-08-31 00:59:03 +0000211public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000212 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
213 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000214 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000215
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000216 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
217 // Issue callbacks for the containing class.
218 Call(CXCursor_ObjCClassRef, ND);
219 // FIXME: Issue callbacks for protocol refs.
220 VisitDeclContext(dyn_cast<DeclContext>(ND));
221 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000222 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000223 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000224 if (D->getSuperClass())
225 Call(CXCursor_ObjCSuperClassRef, D);
226
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000227 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000228 E = D->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000229 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000230 VisitDeclContext(dyn_cast<DeclContext>(D));
231 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000232 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000233 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000234 E = PID->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000235 Call(CXCursor_ObjCProtocolRef, *I);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000236
Steve Naroff9efa7672009-09-04 15:44:05 +0000237 VisitDeclContext(dyn_cast<DeclContext>(PID));
238 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000239 void VisitTagDecl(TagDecl *D) {
240 VisitDeclContext(dyn_cast<DeclContext>(D));
241 }
242 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
243 VisitDeclContext(dyn_cast<DeclContext>(D));
244 }
245 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
246 VisitDeclContext(dyn_cast<DeclContext>(D));
247 }
Steve Naroff89922f82009-08-31 00:59:03 +0000248 void VisitDeclContext(DeclContext *DC) {
249 for (DeclContext::decl_iterator
250 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
251 Visit(*I);
252 }
253 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000254 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000255 }
256 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000257 Call(CXCursor_FieldDecl, ND);
258 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000259 void VisitVarDecl(VarDecl *ND) {
260 Call(CXCursor_VarDecl, ND);
261 }
262 void VisitParmVarDecl(ParmVarDecl *ND) {
263 Call(CXCursor_ParmDecl, ND);
264 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000265 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
266 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000267 }
268 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000269 Call(CXCursor_ObjCIvarDecl, ND);
270 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000271 void VisitFunctionDecl(FunctionDecl *ND) {
272 if (ND->isThisDeclarationADefinition()) {
273 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000274#if 0
275 // Not currently needed.
276 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000277 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000278 RVisit.Visit(Body);
279#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000280 }
281 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000282 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
283 if (ND->getBody()) {
284 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
285 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000286 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000287 } else
288 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
289 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000290 }
291};
292
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000293class CIndexer : public Indexer {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000294public:
295 explicit CIndexer(Program *prog) : Indexer(*prog),
296 OnlyLocalDecls(false),
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000297 DisplayDiagnostics(false) {}
Ted Kremenekdff76892009-10-17 06:21:47 +0000298
299 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000300
301 /// \brief Whether we only want to see "local" declarations (that did not
302 /// come from a previous precompiled header). If false, we want to see all
303 /// declarations.
304 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
305 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
Benjamin Kramer96707622009-10-18 11:10:55 +0000306
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000307 void setDisplayDiagnostics(bool Display = true) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000308 DisplayDiagnostics = Display;
309 }
310 bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000311
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000312 /// \brief Get the path of the clang binary.
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000313 const llvm::sys::Path& getClangPath();
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000314private:
315 bool OnlyLocalDecls;
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000316 bool DisplayDiagnostics;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000317
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000318 llvm::sys::Path ClangPath;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000319};
Steve Naroff89922f82009-08-31 00:59:03 +0000320
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000321const llvm::sys::Path& CIndexer::getClangPath() {
322 // Did we already compute the path?
323 if (!ClangPath.empty())
324 return ClangPath;
325
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000326 // Find the location where this library lives (libCIndex.dylib).
Benjamin Kramer20d75812009-10-18 16:13:48 +0000327#ifdef LLVM_ON_WIN32
328 MEMORY_BASIC_INFORMATION mbi;
329 char path[MAX_PATH];
Benjamin Krameredcd8282009-10-18 16:20:58 +0000330 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
Benjamin Kramer20d75812009-10-18 16:13:48 +0000331 sizeof(mbi));
332 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
333
334 llvm::sys::Path CIndexPath(path);
John Thompson6a6742a2009-11-11 23:11:14 +0000335
336 CIndexPath.eraseComponent();
337 CIndexPath.appendComponent("clang");
338 CIndexPath.appendSuffix("exe");
339 CIndexPath.makeAbsolute();
Benjamin Kramer20d75812009-10-18 16:13:48 +0000340#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000341 // This silly cast below avoids a C++ warning.
342 Dl_info info;
343 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
344 assert(0 && "Call to dladdr() failed");
345
346 llvm::sys::Path CIndexPath(info.dli_fname);
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000347
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000348 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000349 CIndexPath.eraseComponent();
350 CIndexPath.eraseComponent();
351 CIndexPath.appendComponent("bin");
352 CIndexPath.appendComponent("clang");
John Thompson6a6742a2009-11-11 23:11:14 +0000353#endif
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000354
355 // Cache our result.
356 ClangPath = CIndexPath;
357 return ClangPath;
358}
359
360}
361
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000362static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000363 SourceManager &SourceMgr,
364 NamedDecl *ND) {
365 if (clang_isReference(C.kind)) {
366 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000367 case CXCursor_ObjCClassRef: {
368 if (isa<ObjCInterfaceDecl>(ND)) {
369 // FIXME: This is a hack (storing the parent decl in the stmt slot).
370 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
371 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000372 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000373 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
374 assert(OID && "clang_getCursorLine(): Missing category decl");
375 return OID->getClassInterface()->getLocation();
376 }
377 case CXCursor_ObjCSuperClassRef: {
378 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
379 assert(OID && "clang_getCursorLine(): Missing interface decl");
380 return OID->getSuperClassLoc();
381 }
382 case CXCursor_ObjCProtocolRef: {
383 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
384 assert(OID && "clang_getCursorLine(): Missing protocol decl");
385 return OID->getLocation();
386 }
387 case CXCursor_ObjCSelectorRef: {
388 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
389 static_cast<Stmt *>(C.stmt));
390 assert(OME && "clang_getCursorLine(): Missing message expr");
391 return OME->getLeftLoc(); /* FIXME: should be a range */
392 }
393 case CXCursor_VarRef:
394 case CXCursor_FunctionRef:
395 case CXCursor_EnumConstantRef: {
396 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
397 static_cast<Stmt *>(C.stmt));
398 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
399 return DRE->getLocation();
400 }
401 default:
402 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000403 }
404 } else { // We have a declaration or a definition.
405 SourceLocation SLoc;
406 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000407 case Decl::ObjCInterface: {
408 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
409 break;
410 }
411 case Decl::ObjCProtocol: {
412 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
413 break;
414 }
415 default: {
416 SLoc = ND->getLocation();
417 break;
418 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000419 }
420 if (SLoc.isInvalid())
421 return SourceLocation();
422 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
423 }
424}
425
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000426static CXString createCXString(const char *String, bool DupString = false) {
427 CXString Str;
428 if (DupString) {
429 Str.Spelling = strdup(String);
430 Str.MustFreeString = 1;
431 } else {
432 Str.Spelling = String;
433 Str.MustFreeString = 0;
434 }
435 return Str;
436}
437
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000438extern "C" {
439
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000440CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000441 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000442 CIndexer *CIdxr = new CIndexer(new Program());
443 if (excludeDeclarationsFromPCH)
444 CIdxr->setOnlyLocalDecls();
445 if (displayDiagnostics)
446 CIdxr->setDisplayDiagnostics();
447 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000448}
449
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000450void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000451 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000452 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000453}
454
Steve Naroff50398192009-08-28 15:28:48 +0000455// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000456CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
457 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000458 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000459 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000460 std::string astName(ast_filename);
461 std::string ErrMsg;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000462
Ted Kremenek379afec2009-10-22 03:24:01 +0000463 CXTranslationUnit TU =
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000464 ASTUnit::LoadFromPCHFile(astName, &ErrMsg,
Daniel Dunbaracca7252009-11-30 20:42:49 +0000465 CXXIdx->getDisplayDiagnostics() ?
466 NULL : new IgnoreDiagnosticsClient(),
467 CXXIdx->getOnlyLocalDecls(),
468 /* UseBumpAllocator = */ true);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000469
Ted Kremenek0854d702009-11-10 19:18:52 +0000470 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty())
Ted Kremenek779e5f42009-10-26 22:08:39 +0000471 llvm::errs() << "clang_createTranslationUnit: " << ErrMsg << '\n';
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000472
Ted Kremenek379afec2009-10-22 03:24:01 +0000473 return TU;
Steve Naroff600866c2009-08-27 19:51:58 +0000474}
475
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000476CXTranslationUnit
477clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
478 const char *source_filename,
479 int num_command_line_args,
480 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000481 assert(CIdx && "Passed null CXIndex");
482 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
483
Ted Kremenek139ba862009-10-22 00:03:57 +0000484 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000485 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000486
Ted Kremenek139ba862009-10-22 00:03:57 +0000487 // First add the complete path to the 'clang' executable.
488 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000489 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000490
Ted Kremenek139ba862009-10-22 00:03:57 +0000491 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000492 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000493
Ted Kremenek139ba862009-10-22 00:03:57 +0000494 // The 'source_filename' argument is optional. If the caller does not
495 // specify it then it is assumed that the source file is specified
496 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000497 if (source_filename)
498 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000499
Steve Naroff37b5ac22009-10-15 20:50:09 +0000500 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000501 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000502 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000503 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000504
505 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
506 for (int i = 0; i < num_command_line_args; ++i)
507 if (const char *arg = command_line_args[i]) {
508 if (strcmp(arg, "-o") == 0) {
509 ++i; // Also skip the matching argument.
510 continue;
511 }
512 if (strcmp(arg, "-emit-ast") == 0 ||
513 strcmp(arg, "-c") == 0 ||
514 strcmp(arg, "-fsyntax-only") == 0) {
515 continue;
516 }
517
518 // Keep the argument.
519 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000520 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000521
Ted Kremenek139ba862009-10-22 00:03:57 +0000522 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000523 argv.push_back(NULL);
524
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000525 // Invoke 'clang'.
526 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
527 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000528 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000529 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000530 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
531 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
532 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000533
Ted Kremenek0854d702009-11-10 19:18:52 +0000534 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000535 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000536 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000537 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000538 I!=E; ++I) {
539 if (*I)
540 llvm::errs() << ' ' << *I << '\n';
541 }
542 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000543 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000544
Steve Naroff37b5ac22009-10-15 20:50:09 +0000545 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000546 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000547 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000548 if (ATU)
549 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000550 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000551}
552
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000553void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000554 assert(CTUnit && "Passed null CXTranslationUnit");
555 delete static_cast<ASTUnit *>(CTUnit);
556}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000557
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000558CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000559 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000560 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000561 return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000562}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000563
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000564void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000565 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000566 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000567 assert(CTUnit && "Passed null CXTranslationUnit");
568 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
569 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000570
571 TUVisitor DVisit(CTUnit, callback, CData,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000572 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000573 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000574}
575
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000576void clang_loadDeclaration(CXDecl Dcl,
577 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000578 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000579 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000580
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000581 CDeclVisitor DVisit(Dcl, callback, CData,
582 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000583 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000584}
585
Steve Naroff7e8f8182009-08-28 12:07:44 +0000586// Some notes on CXEntity:
587//
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000588// - Since the 'ordinary' namespace includes functions, data, typedefs,
589// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
Steve Naroff7e8f8182009-08-28 12:07:44 +0000590// entity for 2 different types). For example:
591//
592// module1.m: @interface Foo @end Foo *x;
593// module2.m: void Foo(int);
594//
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000595// - Since the unique name spans translation units, static data/functions
Steve Naroff7e8f8182009-08-28 12:07:44 +0000596// within a CXTranslationUnit are *not* currently represented by entities.
597// As a result, there will be no entity for the following:
598//
599// module.m: static void Foo() { }
600//
601
602
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000603const char *clang_getDeclarationName(CXEntity) {
Steve Naroff600866c2009-08-27 19:51:58 +0000604 return "";
605}
606
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000607const char *clang_getURI(CXEntity) {
608 return "";
609}
610
611CXEntity clang_getEntity(const char *URI) {
Steve Naroff600866c2009-08-27 19:51:58 +0000612 return 0;
613}
614
615//
616// CXDecl Operations.
617//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000618
619CXEntity clang_getEntityFromDecl(CXDecl) {
Steve Naroff600866c2009-08-27 19:51:58 +0000620 return 0;
621}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000622
623CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000624 assert(AnonDecl && "Passed null CXDecl");
625 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000626
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000627 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
628 return createCXString(OMD->getSelector().getAsString().c_str(), true);
629
630 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000631 // No, this isn't the same as the code below. getIdentifier() is non-virtual
632 // and returns different names. NamedDecl returns the class name and
633 // ObjCCategoryImplDecl returns the category name.
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000634 return createCXString(CIMP->getIdentifier()->getNameStart());
635
636 if (ND->getIdentifier())
637 return createCXString(ND->getIdentifier()->getNameStart());
638
639 return createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000640}
Steve Narofff334b4e2009-09-02 18:26:48 +0000641
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000642unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000643 assert(AnonDecl && "Passed null CXDecl");
644 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
645 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
646 return SourceMgr.getSpellingLineNumber(ND->getLocation());
647}
648
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000649unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000650 assert(AnonDecl && "Passed null CXDecl");
651 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
652 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000653 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000654}
655
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000656const char *clang_getDeclSource(CXDecl AnonDecl) {
Steve Naroffee9405e2009-09-25 21:45:39 +0000657 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff88145032009-10-27 14:35:18 +0000658 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
659 assert (FEnt && "Cannot find FileEntry for Decl");
660 return clang_getFileName(FEnt);
661}
662
663static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000664 SourceLocation SLoc) {
Steve Naroff88145032009-10-27 14:35:18 +0000665 FileID FID;
666 if (SLoc.isFileID())
667 FID = SMgr.getFileID(SLoc);
668 else
669 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
670 return SMgr.getFileEntryForID(FID);
671}
672
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000673CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000674 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000675 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
676 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000677 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
678}
679
680const char *clang_getFileName(CXFile SFile) {
681 assert(SFile && "Passed null CXFile");
682 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
683 return FEnt->getName();
684}
685
686time_t clang_getFileTime(CXFile SFile) {
687 assert(SFile && "Passed null CXFile");
688 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
689 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000690}
691
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000692CXString clang_getCursorSpelling(CXCursor C) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000693 assert(C.decl && "CXCursor has null decl");
694 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000695
Steve Narofff334b4e2009-09-02 18:26:48 +0000696 if (clang_isReference(C.kind)) {
697 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000698 case CXCursor_ObjCSuperClassRef: {
699 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
700 assert(OID && "clang_getCursorLine(): Missing interface decl");
701 return createCXString(OID->getSuperClass()->getIdentifier()
702 ->getNameStart());
703 }
704 case CXCursor_ObjCClassRef: {
705 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000706 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000707
708 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
709 assert(OCD && "clang_getCursorLine(): Missing category decl");
710 return createCXString(OCD->getClassInterface()->getIdentifier()
711 ->getNameStart());
712 }
713 case CXCursor_ObjCProtocolRef: {
714 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
715 assert(OID && "clang_getCursorLine(): Missing protocol decl");
716 return createCXString(OID->getIdentifier()->getNameStart());
717 }
718 case CXCursor_ObjCSelectorRef: {
719 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
720 static_cast<Stmt *>(C.stmt));
721 assert(OME && "clang_getCursorLine(): Missing message expr");
722 return createCXString(OME->getSelector().getAsString().c_str(), true);
723 }
724 case CXCursor_VarRef:
725 case CXCursor_FunctionRef:
726 case CXCursor_EnumConstantRef: {
727 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
728 static_cast<Stmt *>(C.stmt));
729 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
730 return createCXString(DRE->getDecl()->getIdentifier()->getNameStart());
731 }
732 default:
733 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000734 }
735 }
736 return clang_getDeclSpelling(C.decl);
737}
738
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000739const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000740 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000741 case CXCursor_FunctionDecl: return "FunctionDecl";
742 case CXCursor_TypedefDecl: return "TypedefDecl";
743 case CXCursor_EnumDecl: return "EnumDecl";
744 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
745 case CXCursor_StructDecl: return "StructDecl";
746 case CXCursor_UnionDecl: return "UnionDecl";
747 case CXCursor_ClassDecl: return "ClassDecl";
748 case CXCursor_FieldDecl: return "FieldDecl";
749 case CXCursor_VarDecl: return "VarDecl";
750 case CXCursor_ParmDecl: return "ParmDecl";
751 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
752 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
753 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
754 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
755 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
756 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
757 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
758 case CXCursor_FunctionDefn: return "FunctionDefn";
759 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
760 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
761 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
762 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
763 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
764 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
765 case CXCursor_ObjCClassRef: return "ObjCClassRef";
766 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000767
Daniel Dunbaracca7252009-11-30 20:42:49 +0000768 case CXCursor_VarRef: return "VarRef";
769 case CXCursor_FunctionRef: return "FunctionRef";
770 case CXCursor_EnumConstantRef: return "EnumConstantRef";
771 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000772
Daniel Dunbaracca7252009-11-30 20:42:49 +0000773 case CXCursor_InvalidFile: return "InvalidFile";
774 case CXCursor_NoDeclFound: return "NoDeclFound";
775 case CXCursor_NotImplemented: return "NotImplemented";
776 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000777 }
Steve Naroff600866c2009-08-27 19:51:58 +0000778}
Steve Naroff89922f82009-08-31 00:59:03 +0000779
Steve Naroff9efa7672009-09-04 15:44:05 +0000780static enum CXCursorKind TranslateKind(Decl *D) {
781 switch (D->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000782 case Decl::Function: return CXCursor_FunctionDecl;
783 case Decl::Typedef: return CXCursor_TypedefDecl;
784 case Decl::Enum: return CXCursor_EnumDecl;
785 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
786 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
787 case Decl::Field: return CXCursor_FieldDecl;
788 case Decl::Var: return CXCursor_VarDecl;
789 case Decl::ParmVar: return CXCursor_ParmDecl;
790 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
791 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
792 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
793 case Decl::ObjCMethod: {
794 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
795 if (MD->isInstanceMethod())
796 return CXCursor_ObjCInstanceMethodDecl;
797 return CXCursor_ObjCClassMethodDecl;
798 }
799 default: break;
Steve Naroff9efa7672009-09-04 15:44:05 +0000800 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000801 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000802}
Steve Naroff600866c2009-08-27 19:51:58 +0000803//
804// CXCursor Operations.
805//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000806
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000807CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000808 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000809 assert(CTUnit && "Passed null CXTranslationUnit");
810 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000811
Steve Naroff9efa7672009-09-04 15:44:05 +0000812 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000813 const FileEntry *File = FMgr.getFile(source_name,
814 source_name+strlen(source_name));
Steve Naroff77128dd2009-09-15 20:25:34 +0000815 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000816 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000817 return C;
818 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000819 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000820 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000821
Steve Narofff96b5242009-10-28 20:44:47 +0000822 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
823
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000824 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000825 &LastLoc);
826 if (ALoc.isValid())
827 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000828
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000829 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000830 if (ALoc.isNamedRef())
831 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000832 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000833 if (Dcl) {
834 if (Stm) {
835 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
836 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
837 return C;
838 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
839 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
840 return C;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000841 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000842 // Fall through...treat as a decl, not a ref.
843 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000844 if (ALoc.isNamedRef()) {
845 if (isa<ObjCInterfaceDecl>(Dcl)) {
846 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
847 return C;
848 }
849 if (isa<ObjCProtocolDecl>(Dcl)) {
850 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
851 return C;
852 }
853 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000854 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000855 return C;
856 }
Steve Narofffb570422009-09-22 19:25:29 +0000857 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000858 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000859}
860
Ted Kremenek73885552009-11-17 19:28:59 +0000861CXCursor clang_getNullCursor(void) {
862 CXCursor C;
863 C.kind = CXCursor_InvalidFile;
864 C.decl = NULL;
865 C.stmt = NULL;
866 return C;
867}
868
869unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
870 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt;
871}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000872
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000873CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000874 assert(AnonDecl && "Passed null CXDecl");
875 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000876
Steve Narofffb570422009-09-22 19:25:29 +0000877 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000878 return C;
879}
880
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000881unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000882 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
883}
884
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000885unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000886 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
887}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000888
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000889unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000890 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
891}
892
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000893unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000894 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
895}
896
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000897CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000898 return C.kind;
899}
900
Steve Naroff699a07d2009-09-25 21:32:34 +0000901static Decl *getDeclFromExpr(Stmt *E) {
902 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
903 return RefExpr->getDecl();
904 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
905 return ME->getMemberDecl();
906 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
907 return RE->getDecl();
908
909 if (CallExpr *CE = dyn_cast<CallExpr>(E))
910 return getDeclFromExpr(CE->getCallee());
911 if (CastExpr *CE = dyn_cast<CastExpr>(E))
912 return getDeclFromExpr(CE->getSubExpr());
913 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
914 return OME->getMethodDecl();
915
916 return 0;
917}
918
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000919CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000920 if (clang_isDeclaration(C.kind))
921 return C.decl;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000922
Steve Naroff699a07d2009-09-25 21:32:34 +0000923 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000924 if (C.stmt) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000925 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +0000926 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000927 return static_cast<Stmt *>(C.stmt);
928 else
929 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
930 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000931 return C.decl;
932 }
933 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000934}
935
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000936unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000937 assert(C.decl && "CXCursor has null decl");
938 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000939 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000940
Steve Narofff334b4e2009-09-02 18:26:48 +0000941 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000942 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000943}
Steve Narofff334b4e2009-09-02 18:26:48 +0000944
Steve Naroffef0cef62009-11-09 17:45:52 +0000945const char *clang_getCString(CXString string) {
946 return string.Spelling;
947}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000948
Steve Naroffef0cef62009-11-09 17:45:52 +0000949void clang_disposeString(CXString string) {
Benjamin Kramer858e5de2009-11-09 18:24:53 +0000950 if (string.MustFreeString)
951 free((void*)string.Spelling);
Steve Naroffef0cef62009-11-09 17:45:52 +0000952}
953
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000954unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000955 assert(C.decl && "CXCursor has null decl");
956 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000957 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000958
Steve Narofff334b4e2009-09-02 18:26:48 +0000959 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000960 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000961}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000962
963const char *clang_getCursorSource(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000964 assert(C.decl && "CXCursor has null decl");
965 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000966 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000967
Steve Narofff334b4e2009-09-02 18:26:48 +0000968 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000969
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000970 if (SLoc.isFileID()) {
971 const char *bufferName = SourceMgr.getBufferName(SLoc);
972 return bufferName[0] == '<' ? NULL : bufferName;
973 }
Douglas Gregor02465752009-10-16 21:24:31 +0000974
975 // Retrieve the file in which the macro was instantiated, then provide that
976 // buffer name.
977 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000978 const llvm::MemoryBuffer *Buffer
Douglas Gregor02465752009-10-16 21:24:31 +0000979 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
980 if (!Buffer)
981 return 0;
982
983 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000984}
985
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000986CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff88145032009-10-27 14:35:18 +0000987 assert(C.decl && "CXCursor has null decl");
988 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
989 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000990
Steve Naroff88145032009-10-27 14:35:18 +0000991 return (void *)getFileEntryFromSourceLocation(SourceMgr,
Daniel Dunbaracca7252009-11-30 20:42:49 +0000992 getLocationFromCursor(C,SourceMgr, ND));
Steve Naroff88145032009-10-27 14:35:18 +0000993}
994
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000995void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000996 const char **startBuf,
997 const char **endBuf,
998 unsigned *startLine,
999 unsigned *startColumn,
1000 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001001 unsigned *endColumn) {
Steve Naroff4ade6d62009-09-23 17:52:52 +00001002 assert(C.decl && "CXCursor has null decl");
1003 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1004 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1005 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001006
Steve Naroff4ade6d62009-09-23 17:52:52 +00001007 SourceManager &SM = FD->getASTContext().getSourceManager();
1008 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1009 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1010 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1011 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1012 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1013 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1014}
1015
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001016enum CXCompletionChunkKind
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001017clang_getCompletionChunkKind(CXCompletionString completion_string,
1018 unsigned chunk_number) {
1019 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
1020 if (!CCStr || chunk_number >= CCStr->size())
1021 return CXCompletionChunk_Text;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001022
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001023 switch ((*CCStr)[chunk_number].Kind) {
1024 case CodeCompletionString::CK_TypedText:
1025 return CXCompletionChunk_TypedText;
1026 case CodeCompletionString::CK_Text:
1027 return CXCompletionChunk_Text;
1028 case CodeCompletionString::CK_Optional:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001029 return CXCompletionChunk_Optional;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001030 case CodeCompletionString::CK_Placeholder:
1031 return CXCompletionChunk_Placeholder;
1032 case CodeCompletionString::CK_Informative:
1033 return CXCompletionChunk_Informative;
1034 case CodeCompletionString::CK_CurrentParameter:
1035 return CXCompletionChunk_CurrentParameter;
1036 case CodeCompletionString::CK_LeftParen:
1037 return CXCompletionChunk_LeftParen;
1038 case CodeCompletionString::CK_RightParen:
1039 return CXCompletionChunk_RightParen;
1040 case CodeCompletionString::CK_LeftBracket:
1041 return CXCompletionChunk_LeftBracket;
1042 case CodeCompletionString::CK_RightBracket:
1043 return CXCompletionChunk_RightBracket;
1044 case CodeCompletionString::CK_LeftBrace:
1045 return CXCompletionChunk_LeftBrace;
1046 case CodeCompletionString::CK_RightBrace:
1047 return CXCompletionChunk_RightBrace;
1048 case CodeCompletionString::CK_LeftAngle:
1049 return CXCompletionChunk_LeftAngle;
1050 case CodeCompletionString::CK_RightAngle:
1051 return CXCompletionChunk_RightAngle;
1052 case CodeCompletionString::CK_Comma:
1053 return CXCompletionChunk_Comma;
1054 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001055
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001056 // Should be unreachable, but let's be careful.
1057 return CXCompletionChunk_Text;
1058}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001059
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001060const char *clang_getCompletionChunkText(CXCompletionString completion_string,
1061 unsigned chunk_number) {
1062 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
1063 if (!CCStr || chunk_number >= CCStr->size())
1064 return 0;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001065
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001066 switch ((*CCStr)[chunk_number].Kind) {
1067 case CodeCompletionString::CK_TypedText:
1068 case CodeCompletionString::CK_Text:
1069 case CodeCompletionString::CK_Placeholder:
1070 case CodeCompletionString::CK_CurrentParameter:
1071 case CodeCompletionString::CK_Informative:
1072 case CodeCompletionString::CK_LeftParen:
1073 case CodeCompletionString::CK_RightParen:
1074 case CodeCompletionString::CK_LeftBracket:
1075 case CodeCompletionString::CK_RightBracket:
1076 case CodeCompletionString::CK_LeftBrace:
1077 case CodeCompletionString::CK_RightBrace:
1078 case CodeCompletionString::CK_LeftAngle:
1079 case CodeCompletionString::CK_RightAngle:
1080 case CodeCompletionString::CK_Comma:
1081 return (*CCStr)[chunk_number].Text;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001082
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001083 case CodeCompletionString::CK_Optional:
1084 // Note: treated as an empty text block.
1085 return 0;
1086 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001087
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001088 // Should be unreachable, but let's be careful.
1089 return 0;
1090}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001091
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001092CXCompletionString
1093clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
1094 unsigned chunk_number) {
1095 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
1096 if (!CCStr || chunk_number >= CCStr->size())
1097 return 0;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001098
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001099 switch ((*CCStr)[chunk_number].Kind) {
1100 case CodeCompletionString::CK_TypedText:
1101 case CodeCompletionString::CK_Text:
1102 case CodeCompletionString::CK_Placeholder:
1103 case CodeCompletionString::CK_CurrentParameter:
1104 case CodeCompletionString::CK_Informative:
1105 case CodeCompletionString::CK_LeftParen:
1106 case CodeCompletionString::CK_RightParen:
1107 case CodeCompletionString::CK_LeftBracket:
1108 case CodeCompletionString::CK_RightBracket:
1109 case CodeCompletionString::CK_LeftBrace:
1110 case CodeCompletionString::CK_RightBrace:
1111 case CodeCompletionString::CK_LeftAngle:
1112 case CodeCompletionString::CK_RightAngle:
1113 case CodeCompletionString::CK_Comma:
1114 return 0;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001115
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001116 case CodeCompletionString::CK_Optional:
1117 // Note: treated as an empty text block.
1118 return (*CCStr)[chunk_number].Optional;
1119 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001120
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001121 // Should be unreachable, but let's be careful.
1122 return 0;
1123}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001124
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001125unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) {
1126 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
1127 return CCStr? CCStr->size() : 0;
1128}
Steve Naroff4ade6d62009-09-23 17:52:52 +00001129
Douglas Gregor2b4074f2009-12-01 05:55:20 +00001130static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
1131 unsigned &Value) {
1132 if (Memory + sizeof(unsigned) > MemoryEnd)
1133 return true;
1134
1135 memmove(&Value, Memory, sizeof(unsigned));
1136 Memory += sizeof(unsigned);
1137 return false;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001138}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001139
1140void clang_codeComplete(CXIndex CIdx,
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001141 const char *source_filename,
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001142 int num_command_line_args,
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001143 const char **command_line_args,
1144 const char *complete_filename,
1145 unsigned complete_line,
1146 unsigned complete_column,
1147 CXCompletionIterator completion_iterator,
1148 CXClientData client_data) {
1149 // The indexer, which is mainly used to determine where diagnostics go.
1150 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001151
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001152 // Build up the arguments for invoking 'clang'.
1153 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001154
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001155 // First add the complete path to the 'clang' executable.
1156 llvm::sys::Path ClangPath = CXXIdx->getClangPath();
1157 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001158
1159 // Add the '-fsyntax-only' argument so that we only perform a basic
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001160 // syntax check of the code.
1161 argv.push_back("-fsyntax-only");
1162
1163 // Add the appropriate '-code-completion-at=file:line:column' argument
1164 // to perform code completion, with an "-Xclang" preceding it.
1165 std::string code_complete_at;
1166 code_complete_at += "-code-completion-at=";
1167 code_complete_at += complete_filename;
1168 code_complete_at += ":";
1169 code_complete_at += llvm::utostr(complete_line);
1170 code_complete_at += ":";
1171 code_complete_at += llvm::utostr(complete_column);
1172 argv.push_back("-Xclang");
1173 argv.push_back(code_complete_at.c_str());
1174 argv.push_back("-Xclang");
Daniel Dunbar4db166b2009-11-19 05:32:09 +00001175 argv.push_back("-no-code-completion-debug-printer");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001176
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001177 // Add the source file name (FIXME: later, we'll want to build temporary
1178 // file from the buffer, or just feed the source text via standard input).
Ted Kremenek4633d1b2009-11-17 18:18:02 +00001179 if (source_filename)
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001180 argv.push_back(source_filename);
1181
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001182 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1183 for (int i = 0; i < num_command_line_args; ++i)
1184 if (const char *arg = command_line_args[i]) {
1185 if (strcmp(arg, "-o") == 0) {
1186 ++i; // Also skip the matching argument.
1187 continue;
1188 }
1189 if (strcmp(arg, "-emit-ast") == 0 ||
1190 strcmp(arg, "-c") == 0 ||
1191 strcmp(arg, "-fsyntax-only") == 0) {
1192 continue;
1193 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001194
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001195 // Keep the argument.
1196 argv.push_back(arg);
1197 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001198
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001199 // Add the null terminator.
1200 argv.push_back(NULL);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001201
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001202 // Generate a temporary name for the AST file.
1203 char tmpFile[L_tmpnam];
1204 char *tmpFileName = tmpnam(tmpFile);
1205 llvm::sys::Path ResultsFile(tmpFileName);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001206
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001207 // Invoke 'clang'.
1208 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1209 // on Unix or NUL (Windows).
1210 std::string ErrMsg;
1211 const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, &DevNull, 0 };
1212 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
1213 /* redirects */ &Redirects[0],
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001214 /* secondsToWait */ 0,
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001215 /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001216
Ted Kremenek0854d702009-11-10 19:18:52 +00001217 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001218 llvm::errs() << "clang_codeComplete: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +00001219 << '\n' << "Arguments: \n";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001220 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
1221 I!=E; ++I) {
1222 if (*I)
1223 llvm::errs() << ' ' << *I << '\n';
1224 }
1225 llvm::errs() << '\n';
1226 }
1227
1228 // Parse the resulting source file to find code-completion results.
1229 using llvm::MemoryBuffer;
1230 using llvm::StringRef;
1231 if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) {
1232 StringRef Buffer = F->getBuffer();
Douglas Gregor2b4074f2009-12-01 05:55:20 +00001233 for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size();
1234 Str < StrEnd;) {
1235 unsigned KindValue;
1236 if (ReadUnsigned(Str, StrEnd, KindValue))
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001237 break;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001238
Douglas Gregor2b4074f2009-12-01 05:55:20 +00001239 CodeCompletionString *CCStr
1240 = CodeCompletionString::Deserialize(Str, StrEnd);
1241 if (!CCStr)
1242 continue;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001243
Douglas Gregor2b4074f2009-12-01 05:55:20 +00001244 if (!CCStr->empty()) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001245 // Vend the code-completion result to the caller.
1246 CXCompletionResult Result;
Douglas Gregor2b4074f2009-12-01 05:55:20 +00001247 Result.CursorKind = (CXCursorKind)KindValue;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001248 Result.CompletionString = CCStr;
1249 if (completion_iterator)
1250 completion_iterator(&Result, client_data);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001251 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001252
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001253 delete CCStr;
Douglas Gregor2b4074f2009-12-01 05:55:20 +00001254 };
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001255 delete F;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001256 }
1257
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001258 ResultsFile.eraseFromDisk();
1259}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001260
Steve Naroff600866c2009-08-27 19:51:58 +00001261} // end extern "C"