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