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