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