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