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