1 //===- 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 "CIndexDiagnostic.h"
12 #include "CIndexer.h"
13 #include "CLog.h"
14 #include "CXCursor.h"
15 #include "CXSourceLocation.h"
16 #include "CXString.h"
17 #include "CXTranslationUnit.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/DeclVisitor.h"
20 #include "clang/Frontend/ASTUnit.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/CompilerInvocation.h"
23 #include "clang/Frontend/FrontendAction.h"
24 #include "clang/Frontend/Utils.h"
25 #include "clang/Lex/HeaderSearch.h"
26 #include "clang/Lex/PPCallbacks.h"
27 #include "clang/Lex/PPConditionalDirectiveRecord.h"
28 #include "clang/Lex/Preprocessor.h"
29 #include "clang/Sema/SemaConsumer.h"
30 #include "llvm/Support/CrashRecoveryContext.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/Mutex.h"
33 #include "llvm/Support/MutexGuard.h"
34
35 using namespace clang;
36 using namespace cxtu;
37 using namespace cxindex;
38
39 static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx);
40
41 namespace {
42
43 //===----------------------------------------------------------------------===//
44 // Skip Parsed Bodies
45 //===----------------------------------------------------------------------===//
46
47 #ifdef LLVM_ON_WIN32
48
49 // FIXME: On windows it is disabled since current implementation depends on
50 // file inodes.
51
52 class SessionSkipBodyData { };
53
54 class TUSkipBodyControl {
55 public:
TUSkipBodyControl(SessionSkipBodyData & sessionData,PPConditionalDirectiveRecord & ppRec,Preprocessor & pp)56 TUSkipBodyControl(SessionSkipBodyData &sessionData,
57 PPConditionalDirectiveRecord &ppRec,
58 Preprocessor &pp) { }
isParsed(SourceLocation Loc,FileID FID,const FileEntry * FE)59 bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) {
60 return false;
61 }
finished()62 void finished() { }
63 };
64
65 #else
66
67 /// \brief A "region" in source code identified by the file/offset of the
68 /// preprocessor conditional directive that it belongs to.
69 /// Multiple, non-consecutive ranges can be parts of the same region.
70 ///
71 /// As an example of different regions separated by preprocessor directives:
72 ///
73 /// \code
74 /// #1
75 /// #ifdef BLAH
76 /// #2
77 /// #ifdef CAKE
78 /// #3
79 /// #endif
80 /// #2
81 /// #endif
82 /// #1
83 /// \endcode
84 ///
85 /// There are 3 regions, with non-consecutive parts:
86 /// #1 is identified as the beginning of the file
87 /// #2 is identified as the location of "#ifdef BLAH"
88 /// #3 is identified as the location of "#ifdef CAKE"
89 ///
90 class PPRegion {
91 ino_t ino;
92 time_t ModTime;
93 dev_t dev;
94 unsigned Offset;
95 public:
96 PPRegion() : ino(), ModTime(), dev(), Offset() {}
97 PPRegion(dev_t dev, ino_t ino, unsigned offset, time_t modTime)
98 : ino(ino), ModTime(modTime), dev(dev), Offset(offset) {}
99
100 ino_t getIno() const { return ino; }
101 dev_t getDev() const { return dev; }
102 unsigned getOffset() const { return Offset; }
103 time_t getModTime() const { return ModTime; }
104
105 bool isInvalid() const { return *this == PPRegion(); }
106
107 friend bool operator==(const PPRegion &lhs, const PPRegion &rhs) {
108 return lhs.dev == rhs.dev && lhs.ino == rhs.ino &&
109 lhs.Offset == rhs.Offset && lhs.ModTime == rhs.ModTime;
110 }
111 };
112
113 typedef llvm::DenseSet<PPRegion> PPRegionSetTy;
114
115 } // end anonymous namespace
116
117 namespace llvm {
118 template <> struct isPodLike<PPRegion> {
119 static const bool value = true;
120 };
121
122 template <>
123 struct DenseMapInfo<PPRegion> {
124 static inline PPRegion getEmptyKey() {
125 return PPRegion(0, 0, unsigned(-1), 0);
126 }
127 static inline PPRegion getTombstoneKey() {
128 return PPRegion(0, 0, unsigned(-2), 0);
129 }
130
131 static unsigned getHashValue(const PPRegion &S) {
132 llvm::FoldingSetNodeID ID;
133 ID.AddInteger(S.getIno());
134 ID.AddInteger(S.getDev());
135 ID.AddInteger(S.getOffset());
136 ID.AddInteger(S.getModTime());
137 return ID.ComputeHash();
138 }
139
140 static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) {
141 return LHS == RHS;
142 }
143 };
144 }
145
146 namespace {
147
148 class SessionSkipBodyData {
149 llvm::sys::Mutex Mux;
150 PPRegionSetTy ParsedRegions;
151
152 public:
153 SessionSkipBodyData() : Mux(/*recursive=*/false) {}
154 ~SessionSkipBodyData() {
155 //llvm::errs() << "RegionData: " << Skipped.size() << " - " << Skipped.getMemorySize() << "\n";
156 }
157
158 void copyTo(PPRegionSetTy &Set) {
159 llvm::MutexGuard MG(Mux);
160 Set = ParsedRegions;
161 }
162
163 void update(ArrayRef<PPRegion> Regions) {
164 llvm::MutexGuard MG(Mux);
165 ParsedRegions.insert(Regions.begin(), Regions.end());
166 }
167 };
168
169 class TUSkipBodyControl {
170 SessionSkipBodyData &SessionData;
171 PPConditionalDirectiveRecord &PPRec;
172 Preprocessor &PP;
173
174 PPRegionSetTy ParsedRegions;
175 SmallVector<PPRegion, 32> NewParsedRegions;
176 PPRegion LastRegion;
177 bool LastIsParsed;
178
179 public:
180 TUSkipBodyControl(SessionSkipBodyData &sessionData,
181 PPConditionalDirectiveRecord &ppRec,
182 Preprocessor &pp)
183 : SessionData(sessionData), PPRec(ppRec), PP(pp) {
184 SessionData.copyTo(ParsedRegions);
185 }
186
187 bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) {
188 PPRegion region = getRegion(Loc, FID, FE);
189 if (region.isInvalid())
190 return false;
191
192 // Check common case, consecutive functions in the same region.
193 if (LastRegion == region)
194 return LastIsParsed;
195
196 LastRegion = region;
197 LastIsParsed = ParsedRegions.count(region);
198 if (!LastIsParsed)
199 NewParsedRegions.push_back(region);
200 return LastIsParsed;
201 }
202
203 void finished() {
204 SessionData.update(NewParsedRegions);
205 }
206
207 private:
208 PPRegion getRegion(SourceLocation Loc, FileID FID, const FileEntry *FE) {
209 SourceLocation RegionLoc = PPRec.findConditionalDirectiveRegionLoc(Loc);
210 if (RegionLoc.isInvalid()) {
211 if (isParsedOnceInclude(FE))
212 return PPRegion(FE->getDevice(), FE->getInode(), 0,
213 FE->getModificationTime());
214 return PPRegion();
215 }
216
217 const SourceManager &SM = PPRec.getSourceManager();
218 assert(RegionLoc.isFileID());
219 FileID RegionFID;
220 unsigned RegionOffset;
221 llvm::tie(RegionFID, RegionOffset) = SM.getDecomposedLoc(RegionLoc);
222
223 if (RegionFID != FID) {
224 if (isParsedOnceInclude(FE))
225 return PPRegion(FE->getDevice(), FE->getInode(), 0,
226 FE->getModificationTime());
227 return PPRegion();
228 }
229
230 return PPRegion(FE->getDevice(), FE->getInode(), RegionOffset,
231 FE->getModificationTime());
232 }
233
234 bool isParsedOnceInclude(const FileEntry *FE) {
235 return PP.getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE);
236 }
237 };
238
239 #endif
240
241 //===----------------------------------------------------------------------===//
242 // IndexPPCallbacks
243 //===----------------------------------------------------------------------===//
244
245 class IndexPPCallbacks : public PPCallbacks {
246 Preprocessor &PP;
247 IndexingContext &IndexCtx;
248 bool IsMainFileEntered;
249
250 public:
IndexPPCallbacks(Preprocessor & PP,IndexingContext & indexCtx)251 IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx)
252 : PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { }
253
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind FileType,FileID PrevFID)254 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
255 SrcMgr::CharacteristicKind FileType, FileID PrevFID) {
256 if (IsMainFileEntered)
257 return;
258
259 SourceManager &SM = PP.getSourceManager();
260 SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID());
261
262 if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) {
263 IsMainFileEntered = true;
264 IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID()));
265 }
266 }
267
InclusionDirective(SourceLocation HashLoc,const Token & IncludeTok,StringRef FileName,bool IsAngled,CharSourceRange FilenameRange,const FileEntry * File,StringRef SearchPath,StringRef RelativePath,const Module * Imported)268 virtual void InclusionDirective(SourceLocation HashLoc,
269 const Token &IncludeTok,
270 StringRef FileName,
271 bool IsAngled,
272 CharSourceRange FilenameRange,
273 const FileEntry *File,
274 StringRef SearchPath,
275 StringRef RelativePath,
276 const Module *Imported) {
277 bool isImport = (IncludeTok.is(tok::identifier) &&
278 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
279 IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled,
280 Imported);
281 }
282
283 /// MacroDefined - This hook is called whenever a macro definition is seen.
MacroDefined(const Token & Id,const MacroDirective * MD)284 virtual void MacroDefined(const Token &Id, const MacroDirective *MD) {
285 }
286
287 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
288 /// MI is released immediately following this callback.
MacroUndefined(const Token & MacroNameTok,const MacroDirective * MD)289 virtual void MacroUndefined(const Token &MacroNameTok,
290 const MacroDirective *MD) {
291 }
292
293 /// MacroExpands - This is called by when a macro invocation is found.
MacroExpands(const Token & MacroNameTok,const MacroDirective * MD,SourceRange Range)294 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
295 SourceRange Range) {
296 }
297
298 /// SourceRangeSkipped - This hook is called when a source range is skipped.
299 /// \param Range The SourceRange that was skipped. The range begins at the
300 /// #if/#else directive and ends after the #endif/#else directive.
SourceRangeSkipped(SourceRange Range)301 virtual void SourceRangeSkipped(SourceRange Range) {
302 }
303 };
304
305 //===----------------------------------------------------------------------===//
306 // IndexingConsumer
307 //===----------------------------------------------------------------------===//
308
309 class IndexingConsumer : public ASTConsumer {
310 IndexingContext &IndexCtx;
311 TUSkipBodyControl *SKCtrl;
312
313 public:
IndexingConsumer(IndexingContext & indexCtx,TUSkipBodyControl * skCtrl)314 IndexingConsumer(IndexingContext &indexCtx, TUSkipBodyControl *skCtrl)
315 : IndexCtx(indexCtx), SKCtrl(skCtrl) { }
316
317 // ASTConsumer Implementation
318
Initialize(ASTContext & Context)319 virtual void Initialize(ASTContext &Context) {
320 IndexCtx.setASTContext(Context);
321 IndexCtx.startedTranslationUnit();
322 }
323
HandleTranslationUnit(ASTContext & Ctx)324 virtual void HandleTranslationUnit(ASTContext &Ctx) {
325 if (SKCtrl)
326 SKCtrl->finished();
327 }
328
HandleTopLevelDecl(DeclGroupRef DG)329 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
330 IndexCtx.indexDeclGroupRef(DG);
331 return !IndexCtx.shouldAbort();
332 }
333
334 /// \brief Handle the specified top-level declaration that occurred inside
335 /// and ObjC container.
HandleTopLevelDeclInObjCContainer(DeclGroupRef D)336 virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
337 // They will be handled after the interface is seen first.
338 IndexCtx.addTUDeclInObjCContainer(D);
339 }
340
341 /// \brief This is called by the AST reader when deserializing things.
342 /// The default implementation forwards to HandleTopLevelDecl but we don't
343 /// care about them when indexing, so have an empty definition.
HandleInterestingDecl(DeclGroupRef D)344 virtual void HandleInterestingDecl(DeclGroupRef D) {}
345
HandleTagDeclDefinition(TagDecl * D)346 virtual void HandleTagDeclDefinition(TagDecl *D) {
347 if (!IndexCtx.shouldIndexImplicitTemplateInsts())
348 return;
349
350 if (IndexCtx.isTemplateImplicitInstantiation(D))
351 IndexCtx.indexDecl(D);
352 }
353
HandleCXXImplicitFunctionInstantiation(FunctionDecl * D)354 virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) {
355 if (!IndexCtx.shouldIndexImplicitTemplateInsts())
356 return;
357
358 IndexCtx.indexDecl(D);
359 }
360
shouldSkipFunctionBody(Decl * D)361 virtual bool shouldSkipFunctionBody(Decl *D) {
362 if (!SKCtrl) {
363 // Always skip bodies.
364 return true;
365 }
366
367 const SourceManager &SM = IndexCtx.getASTContext().getSourceManager();
368 SourceLocation Loc = D->getLocation();
369 if (Loc.isMacroID())
370 return false;
371 if (SM.isInSystemHeader(Loc))
372 return true; // always skip bodies from system headers.
373
374 FileID FID;
375 unsigned Offset;
376 llvm::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
377 // Don't skip bodies from main files; this may be revisited.
378 if (SM.getMainFileID() == FID)
379 return false;
380 const FileEntry *FE = SM.getFileEntryForID(FID);
381 if (!FE)
382 return false;
383
384 return SKCtrl->isParsed(Loc, FID, FE);
385 }
386 };
387
388 //===----------------------------------------------------------------------===//
389 // CaptureDiagnosticConsumer
390 //===----------------------------------------------------------------------===//
391
392 class CaptureDiagnosticConsumer : public DiagnosticConsumer {
393 SmallVector<StoredDiagnostic, 4> Errors;
394 public:
395
HandleDiagnostic(DiagnosticsEngine::Level level,const Diagnostic & Info)396 virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
397 const Diagnostic &Info) {
398 if (level >= DiagnosticsEngine::Error)
399 Errors.push_back(StoredDiagnostic(level, Info));
400 }
401
clone(DiagnosticsEngine & Diags) const402 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
403 return new IgnoringDiagConsumer();
404 }
405 };
406
407 //===----------------------------------------------------------------------===//
408 // IndexingFrontendAction
409 //===----------------------------------------------------------------------===//
410
411 class IndexingFrontendAction : public ASTFrontendAction {
412 IndexingContext IndexCtx;
413 CXTranslationUnit CXTU;
414
415 SessionSkipBodyData *SKData;
416 OwningPtr<TUSkipBodyControl> SKCtrl;
417
418 public:
IndexingFrontendAction(CXClientData clientData,IndexerCallbacks & indexCallbacks,unsigned indexOptions,CXTranslationUnit cxTU,SessionSkipBodyData * skData)419 IndexingFrontendAction(CXClientData clientData,
420 IndexerCallbacks &indexCallbacks,
421 unsigned indexOptions,
422 CXTranslationUnit cxTU,
423 SessionSkipBodyData *skData)
424 : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU),
425 CXTU(cxTU), SKData(skData) { }
426
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)427 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
428 StringRef InFile) {
429 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
430
431 if (!PPOpts.ImplicitPCHInclude.empty()) {
432 IndexCtx.importedPCH(
433 CI.getFileManager().getFile(PPOpts.ImplicitPCHInclude));
434 }
435
436 IndexCtx.setASTContext(CI.getASTContext());
437 Preprocessor &PP = CI.getPreprocessor();
438 PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx));
439 IndexCtx.setPreprocessor(PP);
440
441 if (SKData) {
442 PPConditionalDirectiveRecord *
443 PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager());
444 PP.addPPCallbacks(PPRec);
445 SKCtrl.reset(new TUSkipBodyControl(*SKData, *PPRec, PP));
446 }
447
448 return new IndexingConsumer(IndexCtx, SKCtrl.get());
449 }
450
EndSourceFileAction()451 virtual void EndSourceFileAction() {
452 indexDiagnostics(CXTU, IndexCtx);
453 }
454
getTranslationUnitKind()455 virtual TranslationUnitKind getTranslationUnitKind() {
456 if (IndexCtx.shouldIndexImplicitTemplateInsts())
457 return TU_Complete;
458 else
459 return TU_Prefix;
460 }
hasCodeCompletionSupport() const461 virtual bool hasCodeCompletionSupport() const { return false; }
462 };
463
464 //===----------------------------------------------------------------------===//
465 // clang_indexSourceFileUnit Implementation
466 //===----------------------------------------------------------------------===//
467
468 struct IndexSessionData {
469 CXIndex CIdx;
470 OwningPtr<SessionSkipBodyData> SkipBodyData;
471
IndexSessionData__anon5053fbfd0111::IndexSessionData472 explicit IndexSessionData(CXIndex cIdx)
473 : CIdx(cIdx), SkipBodyData(new SessionSkipBodyData) {}
474 };
475
476 struct IndexSourceFileInfo {
477 CXIndexAction idxAction;
478 CXClientData client_data;
479 IndexerCallbacks *index_callbacks;
480 unsigned index_callbacks_size;
481 unsigned index_options;
482 const char *source_filename;
483 const char *const *command_line_args;
484 int num_command_line_args;
485 struct CXUnsavedFile *unsaved_files;
486 unsigned num_unsaved_files;
487 CXTranslationUnit *out_TU;
488 unsigned TU_options;
489 int result;
490 };
491
492 struct MemBufferOwner {
493 SmallVector<const llvm::MemoryBuffer *, 8> Buffers;
494
~MemBufferOwner__anon5053fbfd0111::MemBufferOwner495 ~MemBufferOwner() {
496 for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator
497 I = Buffers.begin(), E = Buffers.end(); I != E; ++I)
498 delete *I;
499 }
500 };
501
502 } // anonymous namespace
503
clang_indexSourceFile_Impl(void * UserData)504 static void clang_indexSourceFile_Impl(void *UserData) {
505 IndexSourceFileInfo *ITUI =
506 static_cast<IndexSourceFileInfo*>(UserData);
507 CXIndexAction cxIdxAction = ITUI->idxAction;
508 CXClientData client_data = ITUI->client_data;
509 IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
510 unsigned index_callbacks_size = ITUI->index_callbacks_size;
511 unsigned index_options = ITUI->index_options;
512 const char *source_filename = ITUI->source_filename;
513 const char * const *command_line_args = ITUI->command_line_args;
514 int num_command_line_args = ITUI->num_command_line_args;
515 struct CXUnsavedFile *unsaved_files = ITUI->unsaved_files;
516 unsigned num_unsaved_files = ITUI->num_unsaved_files;
517 CXTranslationUnit *out_TU = ITUI->out_TU;
518 unsigned TU_options = ITUI->TU_options;
519 ITUI->result = 1; // init as error.
520
521 if (out_TU)
522 *out_TU = 0;
523 bool requestedToGetTU = (out_TU != 0);
524
525 if (!cxIdxAction)
526 return;
527 if (!client_index_callbacks || index_callbacks_size == 0)
528 return;
529
530 IndexerCallbacks CB;
531 memset(&CB, 0, sizeof(CB));
532 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
533 ? index_callbacks_size : sizeof(CB);
534 memcpy(&CB, client_index_callbacks, ClientCBSize);
535
536 IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction);
537 CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx);
538
539 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
540 setThreadBackgroundPriority();
541
542 CaptureDiagnosticConsumer *CaptureDiag = new CaptureDiagnosticConsumer();
543
544 // Configure the diagnostics.
545 IntrusiveRefCntPtr<DiagnosticsEngine>
546 Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions,
547 CaptureDiag,
548 /*ShouldOwnClient=*/true,
549 /*ShouldCloneClient=*/false));
550
551 // Recover resources if we crash before exiting this function.
552 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
553 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
554 DiagCleanup(Diags.getPtr());
555
556 OwningPtr<std::vector<const char *> >
557 Args(new std::vector<const char*>());
558
559 // Recover resources if we crash before exiting this method.
560 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
561 ArgsCleanup(Args.get());
562
563 Args->insert(Args->end(), command_line_args,
564 command_line_args + num_command_line_args);
565
566 // The 'source_filename' argument is optional. If the caller does not
567 // specify it then it is assumed that the source file is specified
568 // in the actual argument list.
569 // Put the source file after command_line_args otherwise if '-x' flag is
570 // present it will be unused.
571 if (source_filename)
572 Args->push_back(source_filename);
573
574 IntrusiveRefCntPtr<CompilerInvocation>
575 CInvok(createInvocationFromCommandLine(*Args, Diags));
576
577 if (!CInvok)
578 return;
579
580 // Recover resources if we crash before exiting this function.
581 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
582 llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
583 CInvokCleanup(CInvok.getPtr());
584
585 if (CInvok->getFrontendOpts().Inputs.empty())
586 return;
587
588 OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner());
589
590 // Recover resources if we crash before exiting this method.
591 llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner>
592 BufOwnerCleanup(BufOwner.get());
593
594 for (unsigned I = 0; I != num_unsaved_files; ++I) {
595 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
596 const llvm::MemoryBuffer *Buffer
597 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
598 CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer);
599 BufOwner->Buffers.push_back(Buffer);
600 }
601
602 // Since libclang is primarily used by batch tools dealing with
603 // (often very broken) source code, where spell-checking can have a
604 // significant negative impact on performance (particularly when
605 // precompiled headers are involved), we disable it.
606 CInvok->getLangOpts()->SpellChecking = false;
607
608 if (index_options & CXIndexOpt_SuppressWarnings)
609 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
610
611 ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags,
612 /*CaptureDiagnostics=*/true,
613 /*UserFilesAreVolatile=*/true);
614 OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(CXXIdx, Unit)));
615
616 // Recover resources if we crash before exiting this method.
617 llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
618 CXTUCleanup(CXTU.get());
619
620 // Enable the skip-parsed-bodies optimization only for C++; this may be
621 // revisited.
622 bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) &&
623 CInvok->getLangOpts()->CPlusPlus;
624 if (SkipBodies)
625 CInvok->getFrontendOpts().SkipFunctionBodies = true;
626
627 OwningPtr<IndexingFrontendAction> IndexAction;
628 IndexAction.reset(new IndexingFrontendAction(client_data, CB,
629 index_options, CXTU->getTU(),
630 SkipBodies ? IdxSession->SkipBodyData.get() : 0));
631
632 // Recover resources if we crash before exiting this method.
633 llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction>
634 IndexActionCleanup(IndexAction.get());
635
636 bool Persistent = requestedToGetTU;
637 bool OnlyLocalDecls = false;
638 bool PrecompilePreamble = false;
639 bool CacheCodeCompletionResults = false;
640 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
641 PPOpts.AllowPCHWithCompilerErrors = true;
642
643 if (requestedToGetTU) {
644 OnlyLocalDecls = CXXIdx->getOnlyLocalDecls();
645 PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble;
646 // FIXME: Add a flag for modules.
647 CacheCodeCompletionResults
648 = TU_options & CXTranslationUnit_CacheCompletionResults;
649 }
650
651 if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) {
652 PPOpts.DetailedRecord = true;
653 }
654
655 if (!requestedToGetTU && !CInvok->getLangOpts()->Modules)
656 PPOpts.DetailedRecord = false;
657
658 DiagnosticErrorTrap DiagTrap(*Diags);
659 bool Success = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags,
660 IndexAction.get(),
661 Unit,
662 Persistent,
663 CXXIdx->getClangResourcesPath(),
664 OnlyLocalDecls,
665 /*CaptureDiagnostics=*/true,
666 PrecompilePreamble,
667 CacheCodeCompletionResults,
668 /*IncludeBriefCommentsInCodeCompletion=*/false,
669 /*UserFilesAreVolatile=*/true);
670 if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics())
671 printDiagsToStderr(Unit);
672
673 if (!Success)
674 return;
675
676 if (out_TU)
677 *out_TU = CXTU->takeTU();
678
679 ITUI->result = 0; // success.
680 }
681
682 //===----------------------------------------------------------------------===//
683 // clang_indexTranslationUnit Implementation
684 //===----------------------------------------------------------------------===//
685
686 namespace {
687
688 struct IndexTranslationUnitInfo {
689 CXIndexAction idxAction;
690 CXClientData client_data;
691 IndexerCallbacks *index_callbacks;
692 unsigned index_callbacks_size;
693 unsigned index_options;
694 CXTranslationUnit TU;
695 int result;
696 };
697
698 } // anonymous namespace
699
indexPreprocessingRecord(ASTUnit & Unit,IndexingContext & IdxCtx)700 static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) {
701 Preprocessor &PP = Unit.getPreprocessor();
702 if (!PP.getPreprocessingRecord())
703 return;
704
705 // FIXME: Only deserialize inclusion directives.
706
707 PreprocessingRecord::iterator I, E;
708 llvm::tie(I, E) = Unit.getLocalPreprocessingEntities();
709
710 bool isModuleFile = Unit.isModuleFile();
711 for (; I != E; ++I) {
712 PreprocessedEntity *PPE = *I;
713
714 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
715 SourceLocation Loc = ID->getSourceRange().getBegin();
716 // Modules have synthetic main files as input, give an invalid location
717 // if the location points to such a file.
718 if (isModuleFile && Unit.isInMainFileID(Loc))
719 Loc = SourceLocation();
720 IdxCtx.ppIncludedFile(Loc, ID->getFileName(),
721 ID->getFile(),
722 ID->getKind() == InclusionDirective::Import,
723 !ID->wasInQuotes(), ID->importedModule());
724 }
725 }
726 }
727
topLevelDeclVisitor(void * context,const Decl * D)728 static bool topLevelDeclVisitor(void *context, const Decl *D) {
729 IndexingContext &IdxCtx = *static_cast<IndexingContext*>(context);
730 IdxCtx.indexTopLevelDecl(D);
731 if (IdxCtx.shouldAbort())
732 return false;
733 return true;
734 }
735
indexTranslationUnit(ASTUnit & Unit,IndexingContext & IdxCtx)736 static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) {
737 Unit.visitLocalTopLevelDecls(&IdxCtx, topLevelDeclVisitor);
738 }
739
indexDiagnostics(CXTranslationUnit TU,IndexingContext & IdxCtx)740 static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) {
741 if (!IdxCtx.hasDiagnosticCallback())
742 return;
743
744 CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(TU);
745 IdxCtx.handleDiagnosticSet(DiagSet);
746 }
747
clang_indexTranslationUnit_Impl(void * UserData)748 static void clang_indexTranslationUnit_Impl(void *UserData) {
749 IndexTranslationUnitInfo *ITUI =
750 static_cast<IndexTranslationUnitInfo*>(UserData);
751 CXTranslationUnit TU = ITUI->TU;
752 CXClientData client_data = ITUI->client_data;
753 IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
754 unsigned index_callbacks_size = ITUI->index_callbacks_size;
755 unsigned index_options = ITUI->index_options;
756 ITUI->result = 1; // init as error.
757
758 if (!TU)
759 return;
760 if (!client_index_callbacks || index_callbacks_size == 0)
761 return;
762
763 CIndexer *CXXIdx = TU->CIdx;
764 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
765 setThreadBackgroundPriority();
766
767 IndexerCallbacks CB;
768 memset(&CB, 0, sizeof(CB));
769 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
770 ? index_callbacks_size : sizeof(CB);
771 memcpy(&CB, client_index_callbacks, ClientCBSize);
772
773 OwningPtr<IndexingContext> IndexCtx;
774 IndexCtx.reset(new IndexingContext(client_data, CB, index_options, TU));
775
776 // Recover resources if we crash before exiting this method.
777 llvm::CrashRecoveryContextCleanupRegistrar<IndexingContext>
778 IndexCtxCleanup(IndexCtx.get());
779
780 OwningPtr<IndexingConsumer> IndexConsumer;
781 IndexConsumer.reset(new IndexingConsumer(*IndexCtx, 0));
782
783 // Recover resources if we crash before exiting this method.
784 llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer>
785 IndexConsumerCleanup(IndexConsumer.get());
786
787 ASTUnit *Unit = cxtu::getASTUnit(TU);
788 if (!Unit)
789 return;
790
791 ASTUnit::ConcurrencyCheck Check(*Unit);
792
793 if (const FileEntry *PCHFile = Unit->getPCHFile())
794 IndexCtx->importedPCH(PCHFile);
795
796 FileManager &FileMgr = Unit->getFileManager();
797
798 if (Unit->getOriginalSourceFileName().empty())
799 IndexCtx->enteredMainFile(0);
800 else
801 IndexCtx->enteredMainFile(FileMgr.getFile(Unit->getOriginalSourceFileName()));
802
803 IndexConsumer->Initialize(Unit->getASTContext());
804
805 indexPreprocessingRecord(*Unit, *IndexCtx);
806 indexTranslationUnit(*Unit, *IndexCtx);
807 indexDiagnostics(TU, *IndexCtx);
808
809 ITUI->result = 0;
810 }
811
812 //===----------------------------------------------------------------------===//
813 // libclang public APIs.
814 //===----------------------------------------------------------------------===//
815
816 extern "C" {
817
clang_index_isEntityObjCContainerKind(CXIdxEntityKind K)818 int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) {
819 return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory;
820 }
821
822 const CXIdxObjCContainerDeclInfo *
clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo * DInfo)823 clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) {
824 if (!DInfo)
825 return 0;
826
827 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
828 if (const ObjCContainerDeclInfo *
829 ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI))
830 return &ContInfo->ObjCContDeclInfo;
831
832 return 0;
833 }
834
835 const CXIdxObjCInterfaceDeclInfo *
clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo * DInfo)836 clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) {
837 if (!DInfo)
838 return 0;
839
840 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
841 if (const ObjCInterfaceDeclInfo *
842 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
843 return &InterInfo->ObjCInterDeclInfo;
844
845 return 0;
846 }
847
848 const CXIdxObjCCategoryDeclInfo *
clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo * DInfo)849 clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
850 if (!DInfo)
851 return 0;
852
853 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
854 if (const ObjCCategoryDeclInfo *
855 CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
856 return &CatInfo->ObjCCatDeclInfo;
857
858 return 0;
859 }
860
861 const CXIdxObjCProtocolRefListInfo *
clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo * DInfo)862 clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
863 if (!DInfo)
864 return 0;
865
866 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
867
868 if (const ObjCInterfaceDeclInfo *
869 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
870 return InterInfo->ObjCInterDeclInfo.protocols;
871
872 if (const ObjCProtocolDeclInfo *
873 ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI))
874 return &ProtInfo->ObjCProtoRefListInfo;
875
876 if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
877 return CatInfo->ObjCCatDeclInfo.protocols;
878
879 return 0;
880 }
881
882 const CXIdxObjCPropertyDeclInfo *
clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo * DInfo)883 clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) {
884 if (!DInfo)
885 return 0;
886
887 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
888 if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI))
889 return &PropInfo->ObjCPropDeclInfo;
890
891 return 0;
892 }
893
894 const CXIdxIBOutletCollectionAttrInfo *
clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo * AInfo)895 clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
896 if (!AInfo)
897 return 0;
898
899 const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
900 if (const IBOutletCollectionInfo *
901 IBInfo = dyn_cast<IBOutletCollectionInfo>(DI))
902 return &IBInfo->IBCollInfo;
903
904 return 0;
905 }
906
907 const CXIdxCXXClassDeclInfo *
clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo * DInfo)908 clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
909 if (!DInfo)
910 return 0;
911
912 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
913 if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI))
914 return &ClassInfo->CXXClassInfo;
915
916 return 0;
917 }
918
919 CXIdxClientContainer
clang_index_getClientContainer(const CXIdxContainerInfo * info)920 clang_index_getClientContainer(const CXIdxContainerInfo *info) {
921 if (!info)
922 return 0;
923 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
924 return Container->IndexCtx->getClientContainerForDC(Container->DC);
925 }
926
clang_index_setClientContainer(const CXIdxContainerInfo * info,CXIdxClientContainer client)927 void clang_index_setClientContainer(const CXIdxContainerInfo *info,
928 CXIdxClientContainer client) {
929 if (!info)
930 return;
931 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
932 Container->IndexCtx->addContainerInMap(Container->DC, client);
933 }
934
clang_index_getClientEntity(const CXIdxEntityInfo * info)935 CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
936 if (!info)
937 return 0;
938 const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
939 return Entity->IndexCtx->getClientEntity(Entity->Dcl);
940 }
941
clang_index_setClientEntity(const CXIdxEntityInfo * info,CXIdxClientEntity client)942 void clang_index_setClientEntity(const CXIdxEntityInfo *info,
943 CXIdxClientEntity client) {
944 if (!info)
945 return;
946 const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
947 Entity->IndexCtx->setClientEntity(Entity->Dcl, client);
948 }
949
clang_IndexAction_create(CXIndex CIdx)950 CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
951 return new IndexSessionData(CIdx);
952 }
953
clang_IndexAction_dispose(CXIndexAction idxAction)954 void clang_IndexAction_dispose(CXIndexAction idxAction) {
955 if (idxAction)
956 delete static_cast<IndexSessionData *>(idxAction);
957 }
958
clang_indexSourceFile(CXIndexAction idxAction,CXClientData client_data,IndexerCallbacks * index_callbacks,unsigned index_callbacks_size,unsigned index_options,const char * source_filename,const char * const * command_line_args,int num_command_line_args,struct CXUnsavedFile * unsaved_files,unsigned num_unsaved_files,CXTranslationUnit * out_TU,unsigned TU_options)959 int clang_indexSourceFile(CXIndexAction idxAction,
960 CXClientData client_data,
961 IndexerCallbacks *index_callbacks,
962 unsigned index_callbacks_size,
963 unsigned index_options,
964 const char *source_filename,
965 const char * const *command_line_args,
966 int num_command_line_args,
967 struct CXUnsavedFile *unsaved_files,
968 unsigned num_unsaved_files,
969 CXTranslationUnit *out_TU,
970 unsigned TU_options) {
971 LOG_FUNC_SECTION {
972 *Log << source_filename << ": ";
973 for (int i = 0; i != num_command_line_args; ++i)
974 *Log << command_line_args[i] << " ";
975 }
976
977 IndexSourceFileInfo ITUI = { idxAction, client_data, index_callbacks,
978 index_callbacks_size, index_options,
979 source_filename, command_line_args,
980 num_command_line_args, unsaved_files,
981 num_unsaved_files, out_TU, TU_options, 0 };
982
983 if (getenv("LIBCLANG_NOTHREADS")) {
984 clang_indexSourceFile_Impl(&ITUI);
985 return ITUI.result;
986 }
987
988 llvm::CrashRecoveryContext CRC;
989
990 if (!RunSafely(CRC, clang_indexSourceFile_Impl, &ITUI)) {
991 fprintf(stderr, "libclang: crash detected during indexing source file: {\n");
992 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
993 fprintf(stderr, " 'command_line_args' : [");
994 for (int i = 0; i != num_command_line_args; ++i) {
995 if (i)
996 fprintf(stderr, ", ");
997 fprintf(stderr, "'%s'", command_line_args[i]);
998 }
999 fprintf(stderr, "],\n");
1000 fprintf(stderr, " 'unsaved_files' : [");
1001 for (unsigned i = 0; i != num_unsaved_files; ++i) {
1002 if (i)
1003 fprintf(stderr, ", ");
1004 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
1005 unsaved_files[i].Length);
1006 }
1007 fprintf(stderr, "],\n");
1008 fprintf(stderr, " 'options' : %d,\n", TU_options);
1009 fprintf(stderr, "}\n");
1010
1011 return 1;
1012 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
1013 if (out_TU)
1014 PrintLibclangResourceUsage(*out_TU);
1015 }
1016
1017 return ITUI.result;
1018 }
1019
clang_indexTranslationUnit(CXIndexAction idxAction,CXClientData client_data,IndexerCallbacks * index_callbacks,unsigned index_callbacks_size,unsigned index_options,CXTranslationUnit TU)1020 int clang_indexTranslationUnit(CXIndexAction idxAction,
1021 CXClientData client_data,
1022 IndexerCallbacks *index_callbacks,
1023 unsigned index_callbacks_size,
1024 unsigned index_options,
1025 CXTranslationUnit TU) {
1026 LOG_FUNC_SECTION {
1027 *Log << TU;
1028 }
1029
1030 IndexTranslationUnitInfo ITUI = { idxAction, client_data, index_callbacks,
1031 index_callbacks_size, index_options, TU,
1032 0 };
1033
1034 if (getenv("LIBCLANG_NOTHREADS")) {
1035 clang_indexTranslationUnit_Impl(&ITUI);
1036 return ITUI.result;
1037 }
1038
1039 llvm::CrashRecoveryContext CRC;
1040
1041 if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) {
1042 fprintf(stderr, "libclang: crash detected during indexing TU\n");
1043
1044 return 1;
1045 }
1046
1047 return ITUI.result;
1048 }
1049
clang_indexLoc_getFileLocation(CXIdxLoc location,CXIdxClientFile * indexFile,CXFile * file,unsigned * line,unsigned * column,unsigned * offset)1050 void clang_indexLoc_getFileLocation(CXIdxLoc location,
1051 CXIdxClientFile *indexFile,
1052 CXFile *file,
1053 unsigned *line,
1054 unsigned *column,
1055 unsigned *offset) {
1056 if (indexFile) *indexFile = 0;
1057 if (file) *file = 0;
1058 if (line) *line = 0;
1059 if (column) *column = 0;
1060 if (offset) *offset = 0;
1061
1062 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1063 if (!location.ptr_data[0] || Loc.isInvalid())
1064 return;
1065
1066 IndexingContext &IndexCtx =
1067 *static_cast<IndexingContext*>(location.ptr_data[0]);
1068 IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset);
1069 }
1070
clang_indexLoc_getCXSourceLocation(CXIdxLoc location)1071 CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
1072 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1073 if (!location.ptr_data[0] || Loc.isInvalid())
1074 return clang_getNullLocation();
1075
1076 IndexingContext &IndexCtx =
1077 *static_cast<IndexingContext*>(location.ptr_data[0]);
1078 return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc);
1079 }
1080
1081 } // end: extern "C"
1082
1083