blob: 5af1d50e7206ca3c6cff6dabb15eb2bfdbcbd235 [file] [log] [blame]
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00001//===- CIndexHigh.cpp - Higher level API functions ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "IndexingContext.h"
11#include "CXCursor.h"
12#include "CXSourceLocation.h"
13#include "CXTranslationUnit.h"
14#include "CXString.h"
15#include "CIndexer.h"
16
17#include "clang/Frontend/ASTUnit.h"
18#include "clang/Frontend/CompilerInvocation.h"
19#include "clang/Frontend/CompilerInstance.h"
20#include "clang/Frontend/Utils.h"
21#include "clang/Sema/SemaConsumer.h"
22#include "clang/AST/ASTConsumer.h"
23#include "clang/AST/DeclVisitor.h"
24#include "clang/Lex/Preprocessor.h"
25#include "clang/Lex/PPCallbacks.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/CrashRecoveryContext.h"
28
29using namespace clang;
30using namespace cxstring;
31using namespace cxtu;
32using namespace cxindex;
33
34namespace {
35
36//===----------------------------------------------------------------------===//
37// IndexPPCallbacks
38//===----------------------------------------------------------------------===//
39
40class IndexPPCallbacks : public PPCallbacks {
41 Preprocessor &PP;
42 IndexingContext &IndexCtx;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +000043 bool IsMainFileEntered;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +000044
45public:
46 IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx)
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +000047 : PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { }
48
49 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
50 SrcMgr::CharacteristicKind FileType, FileID PrevFID) {
51 if (IsMainFileEntered)
52 return;
53
54 SourceManager &SM = PP.getSourceManager();
55 SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID());
56
57 if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) {
58 IsMainFileEntered = true;
59 IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID()));
60 }
61 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +000062
63 virtual void InclusionDirective(SourceLocation HashLoc,
64 const Token &IncludeTok,
65 StringRef FileName,
66 bool IsAngled,
67 const FileEntry *File,
68 SourceLocation EndLoc,
69 StringRef SearchPath,
70 StringRef RelativePath) {
71 bool isImport = (IncludeTok.is(tok::identifier) &&
72 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
73 IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled);
74 }
75
76 /// MacroDefined - This hook is called whenever a macro definition is seen.
77 virtual void MacroDefined(const Token &Id, const MacroInfo *MI) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +000078 }
79
80 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
81 /// MI is released immediately following this callback.
82 virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +000083 }
84
85 /// MacroExpands - This is called by when a macro invocation is found.
86 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
87 SourceRange Range) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +000088 }
89
90 /// SourceRangeSkipped - This hook is called when a source range is skipped.
91 /// \param Range The SourceRange that was skipped. The range begins at the
92 /// #if/#else directive and ends after the #endif/#else directive.
93 virtual void SourceRangeSkipped(SourceRange Range) {
94 }
95};
96
97//===----------------------------------------------------------------------===//
98// IndexingConsumer
99//===----------------------------------------------------------------------===//
100
101class IndexingConsumer : public ASTConsumer {
102 IndexingContext &IndexCtx;
103
104public:
105 explicit IndexingConsumer(IndexingContext &indexCtx)
106 : IndexCtx(indexCtx) { }
107
108 // ASTConsumer Implementation
109
110 virtual void Initialize(ASTContext &Context) {
111 IndexCtx.setASTContext(Context);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +0000112 IndexCtx.startedTranslationUnit();
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000113 }
114
115 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000116 }
117
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000118 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000119 IndexCtx.indexDeclGroupRef(DG);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000120 return !IndexCtx.shouldAbort();
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000121 }
122
123 /// \brief Handle the specified top-level declaration that occurred inside
124 /// and ObjC container.
125 virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
126 // They will be handled after the interface is seen first.
127 IndexCtx.addTUDeclInObjCContainer(D);
128 }
129
130 /// \brief This is called by the AST reader when deserializing things.
131 /// The default implementation forwards to HandleTopLevelDecl but we don't
132 /// care about them when indexing, so have an empty definition.
133 virtual void HandleInterestingDecl(DeclGroupRef D) {}
134};
135
136//===----------------------------------------------------------------------===//
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000137// CaptureDiagnosticConsumer
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000138//===----------------------------------------------------------------------===//
139
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000140class CaptureDiagnosticConsumer : public DiagnosticConsumer {
141 SmallVector<StoredDiagnostic, 4> Errors;
142public:
143
144 virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
145 const Diagnostic &Info) {
146 if (level >= DiagnosticsEngine::Error)
147 Errors.push_back(StoredDiagnostic(level, Info));
148 }
149
150 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
151 return new IgnoringDiagConsumer();
152 }
153};
154
155//===----------------------------------------------------------------------===//
156// IndexingFrontendAction
157//===----------------------------------------------------------------------===//
158
159class IndexingFrontendAction : public ASTFrontendAction {
160 IndexingContext IndexCtx;
161
162public:
163 IndexingFrontendAction(CXClientData clientData,
164 IndexerCallbacks &indexCallbacks,
165 unsigned indexOptions,
166 CXTranslationUnit cxTU)
167 : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU) { }
168
169 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
170 StringRef InFile) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000171 IndexCtx.setASTContext(CI.getASTContext());
172 Preprocessor &PP = CI.getPreprocessor();
173 PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx));
174 return new IndexingConsumer(IndexCtx);
175 }
176
177 virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
178 virtual bool hasCodeCompletionSupport() const { return false; }
179};
180
181//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000182// clang_indexSourceFileUnit Implementation
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000183//===----------------------------------------------------------------------===//
184
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000185struct IndexSourceFileInfo {
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000186 CXIndexAction idxAction;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000187 CXClientData client_data;
188 IndexerCallbacks *index_callbacks;
189 unsigned index_callbacks_size;
190 unsigned index_options;
191 const char *source_filename;
192 const char *const *command_line_args;
193 int num_command_line_args;
194 struct CXUnsavedFile *unsaved_files;
195 unsigned num_unsaved_files;
196 CXTranslationUnit *out_TU;
197 unsigned TU_options;
198 int result;
199};
200
201struct MemBufferOwner {
202 SmallVector<const llvm::MemoryBuffer *, 8> Buffers;
203
204 ~MemBufferOwner() {
205 for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator
206 I = Buffers.begin(), E = Buffers.end(); I != E; ++I)
207 delete *I;
208 }
209};
210
211} // anonymous namespace
212
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000213static void clang_indexSourceFile_Impl(void *UserData) {
214 IndexSourceFileInfo *ITUI =
215 static_cast<IndexSourceFileInfo*>(UserData);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000216 CXIndex CIdx = (CXIndex)ITUI->idxAction;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000217 CXClientData client_data = ITUI->client_data;
218 IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
219 unsigned index_callbacks_size = ITUI->index_callbacks_size;
220 unsigned index_options = ITUI->index_options;
221 const char *source_filename = ITUI->source_filename;
222 const char * const *command_line_args = ITUI->command_line_args;
223 int num_command_line_args = ITUI->num_command_line_args;
224 struct CXUnsavedFile *unsaved_files = ITUI->unsaved_files;
225 unsigned num_unsaved_files = ITUI->num_unsaved_files;
226 CXTranslationUnit *out_TU = ITUI->out_TU;
227 unsigned TU_options = ITUI->TU_options;
228 ITUI->result = 1; // init as error.
229
230 if (out_TU)
231 *out_TU = 0;
232 bool requestedToGetTU = (out_TU != 0);
233
234 if (!CIdx)
235 return;
236 if (!client_index_callbacks || index_callbacks_size == 0)
237 return;
238
239 IndexerCallbacks CB;
240 memset(&CB, 0, sizeof(CB));
241 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
242 ? index_callbacks_size : sizeof(CB);
243 memcpy(&CB, client_index_callbacks, ClientCBSize);
244
245 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
246
247 (void)CXXIdx;
248 (void)TU_options;
249
250 CaptureDiagnosticConsumer *CaptureDiag = new CaptureDiagnosticConsumer();
251
252 // Configure the diagnostics.
253 DiagnosticOptions DiagOpts;
254 llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
255 Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args,
256 command_line_args,
257 CaptureDiag,
258 /*ShouldOwnClient=*/true));
259
260 // Recover resources if we crash before exiting this function.
261 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
262 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
263 DiagCleanup(Diags.getPtr());
264
265 llvm::OwningPtr<std::vector<const char *> >
266 Args(new std::vector<const char*>());
267
268 // Recover resources if we crash before exiting this method.
269 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
270 ArgsCleanup(Args.get());
271
272 Args->insert(Args->end(), command_line_args,
273 command_line_args + num_command_line_args);
274
275 // The 'source_filename' argument is optional. If the caller does not
276 // specify it then it is assumed that the source file is specified
277 // in the actual argument list.
278 // Put the source file after command_line_args otherwise if '-x' flag is
279 // present it will be unused.
280 if (source_filename)
281 Args->push_back(source_filename);
282
283 llvm::IntrusiveRefCntPtr<CompilerInvocation>
284 CInvok(createInvocationFromCommandLine(*Args, Diags));
285
286 if (!CInvok)
287 return;
288
289 // Recover resources if we crash before exiting this function.
290 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
291 llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
292 CInvokCleanup(CInvok.getPtr());
293
294 if (CInvok->getFrontendOpts().Inputs.empty())
295 return;
296
297 llvm::OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner());
298
299 // Recover resources if we crash before exiting this method.
300 llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner>
301 BufOwnerCleanup(BufOwner.get());
302
303 for (unsigned I = 0; I != num_unsaved_files; ++I) {
304 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
305 const llvm::MemoryBuffer *Buffer
306 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
307 CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer);
308 BufOwner->Buffers.push_back(Buffer);
309 }
310
311 // Since libclang is primarily used by batch tools dealing with
312 // (often very broken) source code, where spell-checking can have a
313 // significant negative impact on performance (particularly when
314 // precompiled headers are involved), we disable it.
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000315 CInvok->getLangOpts()->SpellChecking = false;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000316
317 if (!requestedToGetTU)
318 CInvok->getPreprocessorOpts().DetailedRecord = false;
319
Argyrios Kyrtzidis67aa7db2011-11-28 04:55:55 +0000320 ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags,
321 /*CaptureDiagnostics=*/true);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000322 llvm::OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(Unit)));
323
324 // Recover resources if we crash before exiting this method.
325 llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
326 CXTUCleanup(CXTU.get());
327
328 llvm::OwningPtr<IndexingFrontendAction> IndexAction;
329 IndexAction.reset(new IndexingFrontendAction(client_data, CB,
330 index_options, CXTU->getTU()));
331
332 // Recover resources if we crash before exiting this method.
333 llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction>
334 IndexActionCleanup(IndexAction.get());
335
336 Unit = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags,
337 IndexAction.get(),
338 Unit);
339 if (!Unit)
340 return;
341
342 // FIXME: Set state of the ASTUnit according to the TU_options.
343 if (out_TU)
344 *out_TU = CXTU->takeTU();
345
346 ITUI->result = 0; // success.
347}
348
349//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000350// clang_indexTranslationUnit Implementation
351//===----------------------------------------------------------------------===//
352
353namespace {
354
355struct IndexTranslationUnitInfo {
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000356 CXIndexAction idxAction;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000357 CXClientData client_data;
358 IndexerCallbacks *index_callbacks;
359 unsigned index_callbacks_size;
360 unsigned index_options;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000361 CXTranslationUnit TU;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000362 int result;
363};
364
365} // anonymous namespace
366
367static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) {
368 Preprocessor &PP = Unit.getPreprocessor();
369 if (!PP.getPreprocessingRecord())
370 return;
371
372 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
373
374 // FIXME: Only deserialize inclusion directives.
375 // FIXME: Only deserialize stuff from the last chained PCH, not the PCH/Module
376 // that it depends on.
377
378 bool OnlyLocal = !Unit.isMainFileAST() && Unit.getOnlyLocalDecls();
379 PreprocessingRecord::iterator I, E;
380 if (OnlyLocal) {
381 I = PPRec.local_begin();
382 E = PPRec.local_end();
383 } else {
384 I = PPRec.begin();
385 E = PPRec.end();
386 }
387
388 for (; I != E; ++I) {
389 PreprocessedEntity *PPE = *I;
390
391 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
392 IdxCtx.ppIncludedFile(ID->getSourceRange().getBegin(), ID->getFileName(),
393 ID->getFile(), ID->getKind() == InclusionDirective::Import,
394 !ID->wasInQuotes());
395 }
396 }
397}
398
399static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) {
400 // FIXME: Only deserialize stuff from the last chained PCH, not the PCH/Module
401 // that it depends on.
402
403 bool OnlyLocal = !Unit.isMainFileAST() && Unit.getOnlyLocalDecls();
404
405 if (OnlyLocal) {
406 for (ASTUnit::top_level_iterator TL = Unit.top_level_begin(),
407 TLEnd = Unit.top_level_end();
408 TL != TLEnd; ++TL) {
409 IdxCtx.indexTopLevelDecl(*TL);
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000410 if (IdxCtx.shouldAbort())
411 return;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000412 }
413
414 } else {
415 TranslationUnitDecl *TUDecl = Unit.getASTContext().getTranslationUnitDecl();
416 for (TranslationUnitDecl::decl_iterator
417 I = TUDecl->decls_begin(), E = TUDecl->decls_end(); I != E; ++I) {
418 IdxCtx.indexTopLevelDecl(*I);
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000419 if (IdxCtx.shouldAbort())
420 return;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000421 }
422 }
423}
424
425static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) {
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000426 // FIXME: Create a CXDiagnosticSet from TU;
427 // IdxCtx.handleDiagnosticSet(Set);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000428}
429
430static void clang_indexTranslationUnit_Impl(void *UserData) {
431 IndexTranslationUnitInfo *ITUI =
432 static_cast<IndexTranslationUnitInfo*>(UserData);
433 CXTranslationUnit TU = ITUI->TU;
434 CXClientData client_data = ITUI->client_data;
435 IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
436 unsigned index_callbacks_size = ITUI->index_callbacks_size;
437 unsigned index_options = ITUI->index_options;
438 ITUI->result = 1; // init as error.
439
440 if (!TU)
441 return;
442 if (!client_index_callbacks || index_callbacks_size == 0)
443 return;
444
445 IndexerCallbacks CB;
446 memset(&CB, 0, sizeof(CB));
447 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
448 ? index_callbacks_size : sizeof(CB);
449 memcpy(&CB, client_index_callbacks, ClientCBSize);
450
451 llvm::OwningPtr<IndexingContext> IndexCtx;
452 IndexCtx.reset(new IndexingContext(client_data, CB, index_options, TU));
453
454 // Recover resources if we crash before exiting this method.
455 llvm::CrashRecoveryContextCleanupRegistrar<IndexingContext>
456 IndexCtxCleanup(IndexCtx.get());
457
458 llvm::OwningPtr<IndexingConsumer> IndexConsumer;
459 IndexConsumer.reset(new IndexingConsumer(*IndexCtx));
460
461 // Recover resources if we crash before exiting this method.
462 llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer>
463 IndexConsumerCleanup(IndexConsumer.get());
464
465 ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData);
466 if (!Unit)
467 return;
468
469 FileManager &FileMgr = Unit->getFileManager();
470
471 if (Unit->getOriginalSourceFileName().empty())
472 IndexCtx->enteredMainFile(0);
473 else
474 IndexCtx->enteredMainFile(FileMgr.getFile(Unit->getOriginalSourceFileName()));
475
476 IndexConsumer->Initialize(Unit->getASTContext());
477
478 indexPreprocessingRecord(*Unit, *IndexCtx);
479 indexTranslationUnit(*Unit, *IndexCtx);
480 indexDiagnostics(TU, *IndexCtx);
481
482 ITUI->result = 0;
483}
484
485//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000486// libclang public APIs.
487//===----------------------------------------------------------------------===//
488
489extern "C" {
490
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +0000491int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) {
492 return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory;
493}
494
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +0000495const CXIdxObjCContainerDeclInfo *
496clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) {
497 if (!DInfo)
498 return 0;
499
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +0000500 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
501 if (const ObjCContainerDeclInfo *
502 ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI))
503 return &ContInfo->ObjCContDeclInfo;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +0000504
505 return 0;
506}
507
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +0000508const CXIdxObjCInterfaceDeclInfo *
509clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) {
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +0000510 if (!DInfo)
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +0000511 return 0;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +0000512
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +0000513 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
514 if (const ObjCInterfaceDeclInfo *
515 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
516 return &InterInfo->ObjCInterDeclInfo;
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +0000517
518 return 0;
519}
520
521const CXIdxObjCCategoryDeclInfo *
522clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +0000523 if (!DInfo)
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +0000524 return 0;
525
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +0000526 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
527 if (const ObjCCategoryDeclInfo *
528 CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
529 return &CatInfo->ObjCCatDeclInfo;
530
531 return 0;
532}
533
534const CXIdxObjCProtocolRefListInfo *
535clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
536 if (!DInfo)
537 return 0;
538
539 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
540
541 if (const ObjCInterfaceDeclInfo *
542 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
543 return InterInfo->ObjCInterDeclInfo.protocols;
544
545 if (const ObjCProtocolDeclInfo *
546 ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI))
547 return &ProtInfo->ObjCProtoRefListInfo;
548
549 return 0;
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +0000550}
551
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000552const CXIdxIBOutletCollectionAttrInfo *
553clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
554 if (!AInfo)
555 return 0;
556
557 const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
558 if (const IBOutletCollectionInfo *
559 IBInfo = dyn_cast<IBOutletCollectionInfo>(DI))
560 return &IBInfo->IBCollInfo;
561
562 return 0;
563}
564
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000565const CXIdxCXXClassDeclInfo *
566clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
567 if (!DInfo)
568 return 0;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000569
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000570 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
571 if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI))
572 return &ClassInfo->CXXClassInfo;
573
574 return 0;
575}
576
577CXIdxClientContainer
578clang_index_getClientContainer(const CXIdxContainerInfo *info) {
579 if (!info)
580 return 0;
581 ContainerInfo *Container = (ContainerInfo*)info;
582 return Container->IndexCtx->getClientContainerForDC(Container->DC);
583}
584
585void clang_index_setClientContainer(const CXIdxContainerInfo *info,
586 CXIdxClientContainer client) {
587 if (!info)
588 return;
589 ContainerInfo *Container = (ContainerInfo*)info;
590 Container->IndexCtx->addContainerInMap(Container->DC, client);
591}
592
593CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
594 if (!info)
595 return 0;
596 EntityInfo *Entity = (EntityInfo*)info;
597 return Entity->IndexCtx->getClientEntity(Entity->Dcl);
598}
599
600void clang_index_setClientEntity(const CXIdxEntityInfo *info,
601 CXIdxClientEntity client) {
602 if (!info)
603 return;
604 EntityInfo *Entity = (EntityInfo*)info;
605 Entity->IndexCtx->setClientEntity(Entity->Dcl, client);
606}
607
608CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
609 // For now, CXIndexAction is featureless.
610 return CIdx;
611}
612
613void clang_IndexAction_dispose(CXIndexAction idxAction) {
614 // For now, CXIndexAction is featureless.
615}
616
617int clang_indexSourceFile(CXIndexAction idxAction,
618 CXClientData client_data,
619 IndexerCallbacks *index_callbacks,
620 unsigned index_callbacks_size,
621 unsigned index_options,
622 const char *source_filename,
623 const char * const *command_line_args,
624 int num_command_line_args,
625 struct CXUnsavedFile *unsaved_files,
626 unsigned num_unsaved_files,
627 CXTranslationUnit *out_TU,
628 unsigned TU_options) {
629
630 IndexSourceFileInfo ITUI = { idxAction, client_data, index_callbacks,
631 index_callbacks_size, index_options,
632 source_filename, command_line_args,
633 num_command_line_args, unsaved_files,
634 num_unsaved_files, out_TU, TU_options, 0 };
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000635
Argyrios Kyrtzidis97805532011-10-29 19:32:39 +0000636 if (getenv("LIBCLANG_NOTHREADS")) {
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000637 clang_indexSourceFile_Impl(&ITUI);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000638 return ITUI.result;
639 }
640
641 llvm::CrashRecoveryContext CRC;
642
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000643 if (!RunSafely(CRC, clang_indexSourceFile_Impl, &ITUI)) {
644 fprintf(stderr, "libclang: crash detected during indexing source file: {\n");
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000645 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
646 fprintf(stderr, " 'command_line_args' : [");
647 for (int i = 0; i != num_command_line_args; ++i) {
648 if (i)
649 fprintf(stderr, ", ");
650 fprintf(stderr, "'%s'", command_line_args[i]);
651 }
652 fprintf(stderr, "],\n");
653 fprintf(stderr, " 'unsaved_files' : [");
654 for (unsigned i = 0; i != num_unsaved_files; ++i) {
655 if (i)
656 fprintf(stderr, ", ");
657 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
658 unsaved_files[i].Length);
659 }
660 fprintf(stderr, "],\n");
661 fprintf(stderr, " 'options' : %d,\n", TU_options);
662 fprintf(stderr, "}\n");
663
664 return 1;
665 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
666 if (out_TU)
667 PrintLibclangResourceUsage(*out_TU);
668 }
669
670 return ITUI.result;
671}
672
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000673int clang_indexTranslationUnit(CXIndexAction idxAction,
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000674 CXClientData client_data,
675 IndexerCallbacks *index_callbacks,
676 unsigned index_callbacks_size,
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000677 unsigned index_options,
678 CXTranslationUnit TU) {
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000679
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000680 IndexTranslationUnitInfo ITUI = { idxAction, client_data, index_callbacks,
681 index_callbacks_size, index_options, TU,
682 0 };
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +0000683
684 if (getenv("LIBCLANG_NOTHREADS")) {
685 clang_indexTranslationUnit_Impl(&ITUI);
686 return ITUI.result;
687 }
688
689 llvm::CrashRecoveryContext CRC;
690
691 if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) {
692 fprintf(stderr, "libclang: crash detected during indexing TU\n");
693
694 return 1;
695 }
696
697 return ITUI.result;
698}
699
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000700void clang_indexLoc_getFileLocation(CXIdxLoc location,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +0000701 CXIdxClientFile *indexFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000702 CXFile *file,
703 unsigned *line,
704 unsigned *column,
705 unsigned *offset) {
706 if (indexFile) *indexFile = 0;
707 if (file) *file = 0;
708 if (line) *line = 0;
709 if (column) *column = 0;
710 if (offset) *offset = 0;
711
712 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
713 if (!location.ptr_data[0] || Loc.isInvalid())
714 return;
715
716 IndexingContext &IndexCtx =
717 *static_cast<IndexingContext*>(location.ptr_data[0]);
718 IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset);
719}
720
721CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
722 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
723 if (!location.ptr_data[0] || Loc.isInvalid())
724 return clang_getNullLocation();
725
726 IndexingContext &IndexCtx =
727 *static_cast<IndexingContext*>(location.ptr_data[0]);
728 return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc);
729}
730
731} // end: extern "C"
732