Home | History | Annotate | Download | only in Frontend
      1 //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
      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 // ASTUnit Implementation.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/Frontend/ASTUnit.h"
     15 #include "clang/AST/ASTConsumer.h"
     16 #include "clang/AST/ASTContext.h"
     17 #include "clang/AST/DeclVisitor.h"
     18 #include "clang/AST/StmtVisitor.h"
     19 #include "clang/AST/TypeOrdering.h"
     20 #include "clang/Basic/Diagnostic.h"
     21 #include "clang/Basic/TargetInfo.h"
     22 #include "clang/Basic/TargetOptions.h"
     23 #include "clang/Basic/VirtualFileSystem.h"
     24 #include "clang/Frontend/CompilerInstance.h"
     25 #include "clang/Frontend/FrontendActions.h"
     26 #include "clang/Frontend/FrontendDiagnostic.h"
     27 #include "clang/Frontend/FrontendOptions.h"
     28 #include "clang/Frontend/MultiplexConsumer.h"
     29 #include "clang/Frontend/Utils.h"
     30 #include "clang/Lex/HeaderSearch.h"
     31 #include "clang/Lex/Preprocessor.h"
     32 #include "clang/Lex/PreprocessorOptions.h"
     33 #include "clang/Sema/Sema.h"
     34 #include "clang/Serialization/ASTReader.h"
     35 #include "clang/Serialization/ASTWriter.h"
     36 #include "llvm/ADT/ArrayRef.h"
     37 #include "llvm/ADT/StringExtras.h"
     38 #include "llvm/ADT/StringSet.h"
     39 #include "llvm/Support/CrashRecoveryContext.h"
     40 #include "llvm/Support/Host.h"
     41 #include "llvm/Support/MemoryBuffer.h"
     42 #include "llvm/Support/Mutex.h"
     43 #include "llvm/Support/MutexGuard.h"
     44 #include "llvm/Support/Path.h"
     45 #include "llvm/Support/Timer.h"
     46 #include "llvm/Support/raw_ostream.h"
     47 #include <atomic>
     48 #include <cstdio>
     49 #include <cstdlib>
     50 using namespace clang;
     51 
     52 using llvm::TimeRecord;
     53 
     54 namespace {
     55   class SimpleTimer {
     56     bool WantTiming;
     57     TimeRecord Start;
     58     std::string Output;
     59 
     60   public:
     61     explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
     62       if (WantTiming)
     63         Start = TimeRecord::getCurrentTime();
     64     }
     65 
     66     void setOutput(const Twine &Output) {
     67       if (WantTiming)
     68         this->Output = Output.str();
     69     }
     70 
     71     ~SimpleTimer() {
     72       if (WantTiming) {
     73         TimeRecord Elapsed = TimeRecord::getCurrentTime();
     74         Elapsed -= Start;
     75         llvm::errs() << Output << ':';
     76         Elapsed.print(Elapsed, llvm::errs());
     77         llvm::errs() << '\n';
     78       }
     79     }
     80   };
     81 
     82   struct OnDiskData {
     83     /// \brief The file in which the precompiled preamble is stored.
     84     std::string PreambleFile;
     85 
     86     /// \brief Temporary files that should be removed when the ASTUnit is
     87     /// destroyed.
     88     SmallVector<std::string, 4> TemporaryFiles;
     89 
     90     /// \brief Erase temporary files.
     91     void CleanTemporaryFiles();
     92 
     93     /// \brief Erase the preamble file.
     94     void CleanPreambleFile();
     95 
     96     /// \brief Erase temporary files and the preamble file.
     97     void Cleanup();
     98   };
     99 }
    100 
    101 static llvm::sys::SmartMutex<false> &getOnDiskMutex() {
    102   static llvm::sys::SmartMutex<false> M(/* recursive = */ true);
    103   return M;
    104 }
    105 
    106 static void cleanupOnDiskMapAtExit();
    107 
    108 typedef llvm::DenseMap<const ASTUnit *, OnDiskData *> OnDiskDataMap;
    109 static OnDiskDataMap &getOnDiskDataMap() {
    110   static OnDiskDataMap M;
    111   static bool hasRegisteredAtExit = false;
    112   if (!hasRegisteredAtExit) {
    113     hasRegisteredAtExit = true;
    114     atexit(cleanupOnDiskMapAtExit);
    115   }
    116   return M;
    117 }
    118 
    119 static void cleanupOnDiskMapAtExit() {
    120   // Use the mutex because there can be an alive thread destroying an ASTUnit.
    121   llvm::MutexGuard Guard(getOnDiskMutex());
    122   OnDiskDataMap &M = getOnDiskDataMap();
    123   for (OnDiskDataMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
    124     // We don't worry about freeing the memory associated with OnDiskDataMap.
    125     // All we care about is erasing stale files.
    126     I->second->Cleanup();
    127   }
    128 }
    129 
    130 static OnDiskData &getOnDiskData(const ASTUnit *AU) {
    131   // We require the mutex since we are modifying the structure of the
    132   // DenseMap.
    133   llvm::MutexGuard Guard(getOnDiskMutex());
    134   OnDiskDataMap &M = getOnDiskDataMap();
    135   OnDiskData *&D = M[AU];
    136   if (!D)
    137     D = new OnDiskData();
    138   return *D;
    139 }
    140 
    141 static void erasePreambleFile(const ASTUnit *AU) {
    142   getOnDiskData(AU).CleanPreambleFile();
    143 }
    144 
    145 static void removeOnDiskEntry(const ASTUnit *AU) {
    146   // We require the mutex since we are modifying the structure of the
    147   // DenseMap.
    148   llvm::MutexGuard Guard(getOnDiskMutex());
    149   OnDiskDataMap &M = getOnDiskDataMap();
    150   OnDiskDataMap::iterator I = M.find(AU);
    151   if (I != M.end()) {
    152     I->second->Cleanup();
    153     delete I->second;
    154     M.erase(AU);
    155   }
    156 }
    157 
    158 static void setPreambleFile(const ASTUnit *AU, StringRef preambleFile) {
    159   getOnDiskData(AU).PreambleFile = preambleFile;
    160 }
    161 
    162 static const std::string &getPreambleFile(const ASTUnit *AU) {
    163   return getOnDiskData(AU).PreambleFile;
    164 }
    165 
    166 void OnDiskData::CleanTemporaryFiles() {
    167   for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
    168     llvm::sys::fs::remove(TemporaryFiles[I]);
    169   TemporaryFiles.clear();
    170 }
    171 
    172 void OnDiskData::CleanPreambleFile() {
    173   if (!PreambleFile.empty()) {
    174     llvm::sys::fs::remove(PreambleFile);
    175     PreambleFile.clear();
    176   }
    177 }
    178 
    179 void OnDiskData::Cleanup() {
    180   CleanTemporaryFiles();
    181   CleanPreambleFile();
    182 }
    183 
    184 struct ASTUnit::ASTWriterData {
    185   SmallString<128> Buffer;
    186   llvm::BitstreamWriter Stream;
    187   ASTWriter Writer;
    188 
    189   ASTWriterData() : Stream(Buffer), Writer(Stream) { }
    190 };
    191 
    192 void ASTUnit::clearFileLevelDecls() {
    193   llvm::DeleteContainerSeconds(FileDecls);
    194 }
    195 
    196 void ASTUnit::CleanTemporaryFiles() {
    197   getOnDiskData(this).CleanTemporaryFiles();
    198 }
    199 
    200 void ASTUnit::addTemporaryFile(StringRef TempFile) {
    201   getOnDiskData(this).TemporaryFiles.push_back(TempFile);
    202 }
    203 
    204 /// \brief After failing to build a precompiled preamble (due to
    205 /// errors in the source that occurs in the preamble), the number of
    206 /// reparses during which we'll skip even trying to precompile the
    207 /// preamble.
    208 const unsigned DefaultPreambleRebuildInterval = 5;
    209 
    210 /// \brief Tracks the number of ASTUnit objects that are currently active.
    211 ///
    212 /// Used for debugging purposes only.
    213 static std::atomic<unsigned> ActiveASTUnitObjects;
    214 
    215 ASTUnit::ASTUnit(bool _MainFileIsAST)
    216   : Reader(nullptr), HadModuleLoaderFatalFailure(false),
    217     OnlyLocalDecls(false), CaptureDiagnostics(false),
    218     MainFileIsAST(_MainFileIsAST),
    219     TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")),
    220     OwnsRemappedFileBuffers(true),
    221     NumStoredDiagnosticsFromDriver(0),
    222     PreambleRebuildCounter(0), SavedMainFileBuffer(nullptr),
    223     PreambleBuffer(nullptr), NumWarningsInPreamble(0),
    224     ShouldCacheCodeCompletionResults(false),
    225     IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
    226     CompletionCacheTopLevelHashValue(0),
    227     PreambleTopLevelHashValue(0),
    228     CurrentTopLevelHashValue(0),
    229     UnsafeToFree(false) {
    230   if (getenv("LIBCLANG_OBJTRACKING"))
    231     fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects);
    232 }
    233 
    234 ASTUnit::~ASTUnit() {
    235   // If we loaded from an AST file, balance out the BeginSourceFile call.
    236   if (MainFileIsAST && getDiagnostics().getClient()) {
    237     getDiagnostics().getClient()->EndSourceFile();
    238   }
    239 
    240   clearFileLevelDecls();
    241 
    242   // Clean up the temporary files and the preamble file.
    243   removeOnDiskEntry(this);
    244 
    245   // Free the buffers associated with remapped files. We are required to
    246   // perform this operation here because we explicitly request that the
    247   // compiler instance *not* free these buffers for each invocation of the
    248   // parser.
    249   if (Invocation.get() && OwnsRemappedFileBuffers) {
    250     PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
    251     for (const auto &RB : PPOpts.RemappedFileBuffers)
    252       delete RB.second;
    253   }
    254 
    255   delete SavedMainFileBuffer;
    256   delete PreambleBuffer;
    257 
    258   ClearCachedCompletionResults();
    259 
    260   if (getenv("LIBCLANG_OBJTRACKING"))
    261     fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects);
    262 }
    263 
    264 void ASTUnit::setPreprocessor(Preprocessor *pp) { PP = pp; }
    265 
    266 /// \brief Determine the set of code-completion contexts in which this
    267 /// declaration should be shown.
    268 static unsigned getDeclShowContexts(const NamedDecl *ND,
    269                                     const LangOptions &LangOpts,
    270                                     bool &IsNestedNameSpecifier) {
    271   IsNestedNameSpecifier = false;
    272 
    273   if (isa<UsingShadowDecl>(ND))
    274     ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
    275   if (!ND)
    276     return 0;
    277 
    278   uint64_t Contexts = 0;
    279   if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
    280       isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
    281     // Types can appear in these contexts.
    282     if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
    283       Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
    284                |  (1LL << CodeCompletionContext::CCC_ObjCIvarList)
    285                |  (1LL << CodeCompletionContext::CCC_ClassStructUnion)
    286                |  (1LL << CodeCompletionContext::CCC_Statement)
    287                |  (1LL << CodeCompletionContext::CCC_Type)
    288                |  (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
    289 
    290     // In C++, types can appear in expressions contexts (for functional casts).
    291     if (LangOpts.CPlusPlus)
    292       Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
    293 
    294     // In Objective-C, message sends can send interfaces. In Objective-C++,
    295     // all types are available due to functional casts.
    296     if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
    297       Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
    298 
    299     // In Objective-C, you can only be a subclass of another Objective-C class
    300     if (isa<ObjCInterfaceDecl>(ND))
    301       Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName);
    302 
    303     // Deal with tag names.
    304     if (isa<EnumDecl>(ND)) {
    305       Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
    306 
    307       // Part of the nested-name-specifier in C++0x.
    308       if (LangOpts.CPlusPlus11)
    309         IsNestedNameSpecifier = true;
    310     } else if (const RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
    311       if (Record->isUnion())
    312         Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
    313       else
    314         Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
    315 
    316       if (LangOpts.CPlusPlus)
    317         IsNestedNameSpecifier = true;
    318     } else if (isa<ClassTemplateDecl>(ND))
    319       IsNestedNameSpecifier = true;
    320   } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
    321     // Values can appear in these contexts.
    322     Contexts = (1LL << CodeCompletionContext::CCC_Statement)
    323              | (1LL << CodeCompletionContext::CCC_Expression)
    324              | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
    325              | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
    326   } else if (isa<ObjCProtocolDecl>(ND)) {
    327     Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName);
    328   } else if (isa<ObjCCategoryDecl>(ND)) {
    329     Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName);
    330   } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
    331     Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
    332 
    333     // Part of the nested-name-specifier.
    334     IsNestedNameSpecifier = true;
    335   }
    336 
    337   return Contexts;
    338 }
    339 
    340 void ASTUnit::CacheCodeCompletionResults() {
    341   if (!TheSema)
    342     return;
    343 
    344   SimpleTimer Timer(WantTiming);
    345   Timer.setOutput("Cache global code completions for " + getMainFileName());
    346 
    347   // Clear out the previous results.
    348   ClearCachedCompletionResults();
    349 
    350   // Gather the set of global code completions.
    351   typedef CodeCompletionResult Result;
    352   SmallVector<Result, 8> Results;
    353   CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
    354   CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
    355   TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
    356                                        CCTUInfo, Results);
    357 
    358   // Translate global code completions into cached completions.
    359   llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
    360 
    361   for (unsigned I = 0, N = Results.size(); I != N; ++I) {
    362     switch (Results[I].Kind) {
    363     case Result::RK_Declaration: {
    364       bool IsNestedNameSpecifier = false;
    365       CachedCodeCompletionResult CachedResult;
    366       CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
    367                                                     *CachedCompletionAllocator,
    368                                                     CCTUInfo,
    369                                           IncludeBriefCommentsInCodeCompletion);
    370       CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
    371                                                         Ctx->getLangOpts(),
    372                                                         IsNestedNameSpecifier);
    373       CachedResult.Priority = Results[I].Priority;
    374       CachedResult.Kind = Results[I].CursorKind;
    375       CachedResult.Availability = Results[I].Availability;
    376 
    377       // Keep track of the type of this completion in an ASTContext-agnostic
    378       // way.
    379       QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
    380       if (UsageType.isNull()) {
    381         CachedResult.TypeClass = STC_Void;
    382         CachedResult.Type = 0;
    383       } else {
    384         CanQualType CanUsageType
    385           = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
    386         CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
    387 
    388         // Determine whether we have already seen this type. If so, we save
    389         // ourselves the work of formatting the type string by using the
    390         // temporary, CanQualType-based hash table to find the associated value.
    391         unsigned &TypeValue = CompletionTypes[CanUsageType];
    392         if (TypeValue == 0) {
    393           TypeValue = CompletionTypes.size();
    394           CachedCompletionTypes[QualType(CanUsageType).getAsString()]
    395             = TypeValue;
    396         }
    397 
    398         CachedResult.Type = TypeValue;
    399       }
    400 
    401       CachedCompletionResults.push_back(CachedResult);
    402 
    403       /// Handle nested-name-specifiers in C++.
    404       if (TheSema->Context.getLangOpts().CPlusPlus &&
    405           IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
    406         // The contexts in which a nested-name-specifier can appear in C++.
    407         uint64_t NNSContexts
    408           = (1LL << CodeCompletionContext::CCC_TopLevel)
    409           | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
    410           | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
    411           | (1LL << CodeCompletionContext::CCC_Statement)
    412           | (1LL << CodeCompletionContext::CCC_Expression)
    413           | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
    414           | (1LL << CodeCompletionContext::CCC_EnumTag)
    415           | (1LL << CodeCompletionContext::CCC_UnionTag)
    416           | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
    417           | (1LL << CodeCompletionContext::CCC_Type)
    418           | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName)
    419           | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
    420 
    421         if (isa<NamespaceDecl>(Results[I].Declaration) ||
    422             isa<NamespaceAliasDecl>(Results[I].Declaration))
    423           NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
    424 
    425         if (unsigned RemainingContexts
    426                                 = NNSContexts & ~CachedResult.ShowInContexts) {
    427           // If there any contexts where this completion can be a
    428           // nested-name-specifier but isn't already an option, create a
    429           // nested-name-specifier completion.
    430           Results[I].StartsNestedNameSpecifier = true;
    431           CachedResult.Completion
    432             = Results[I].CreateCodeCompletionString(*TheSema,
    433                                                     *CachedCompletionAllocator,
    434                                                     CCTUInfo,
    435                                         IncludeBriefCommentsInCodeCompletion);
    436           CachedResult.ShowInContexts = RemainingContexts;
    437           CachedResult.Priority = CCP_NestedNameSpecifier;
    438           CachedResult.TypeClass = STC_Void;
    439           CachedResult.Type = 0;
    440           CachedCompletionResults.push_back(CachedResult);
    441         }
    442       }
    443       break;
    444     }
    445 
    446     case Result::RK_Keyword:
    447     case Result::RK_Pattern:
    448       // Ignore keywords and patterns; we don't care, since they are so
    449       // easily regenerated.
    450       break;
    451 
    452     case Result::RK_Macro: {
    453       CachedCodeCompletionResult CachedResult;
    454       CachedResult.Completion
    455         = Results[I].CreateCodeCompletionString(*TheSema,
    456                                                 *CachedCompletionAllocator,
    457                                                 CCTUInfo,
    458                                           IncludeBriefCommentsInCodeCompletion);
    459       CachedResult.ShowInContexts
    460         = (1LL << CodeCompletionContext::CCC_TopLevel)
    461         | (1LL << CodeCompletionContext::CCC_ObjCInterface)
    462         | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
    463         | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
    464         | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
    465         | (1LL << CodeCompletionContext::CCC_Statement)
    466         | (1LL << CodeCompletionContext::CCC_Expression)
    467         | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
    468         | (1LL << CodeCompletionContext::CCC_MacroNameUse)
    469         | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
    470         | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
    471         | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
    472 
    473       CachedResult.Priority = Results[I].Priority;
    474       CachedResult.Kind = Results[I].CursorKind;
    475       CachedResult.Availability = Results[I].Availability;
    476       CachedResult.TypeClass = STC_Void;
    477       CachedResult.Type = 0;
    478       CachedCompletionResults.push_back(CachedResult);
    479       break;
    480     }
    481     }
    482   }
    483 
    484   // Save the current top-level hash value.
    485   CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
    486 }
    487 
    488 void ASTUnit::ClearCachedCompletionResults() {
    489   CachedCompletionResults.clear();
    490   CachedCompletionTypes.clear();
    491   CachedCompletionAllocator = nullptr;
    492 }
    493 
    494 namespace {
    495 
    496 /// \brief Gathers information from ASTReader that will be used to initialize
    497 /// a Preprocessor.
    498 class ASTInfoCollector : public ASTReaderListener {
    499   Preprocessor &PP;
    500   ASTContext &Context;
    501   LangOptions &LangOpt;
    502   std::shared_ptr<TargetOptions> &TargetOpts;
    503   IntrusiveRefCntPtr<TargetInfo> &Target;
    504   unsigned &Counter;
    505 
    506   bool InitializedLanguage;
    507 public:
    508   ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt,
    509                    std::shared_ptr<TargetOptions> &TargetOpts,
    510                    IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter)
    511       : PP(PP), Context(Context), LangOpt(LangOpt), TargetOpts(TargetOpts),
    512         Target(Target), Counter(Counter), InitializedLanguage(false) {}
    513 
    514   bool ReadLanguageOptions(const LangOptions &LangOpts,
    515                            bool Complain) override {
    516     if (InitializedLanguage)
    517       return false;
    518 
    519     LangOpt = LangOpts;
    520     InitializedLanguage = true;
    521 
    522     updated();
    523     return false;
    524   }
    525 
    526   bool ReadTargetOptions(const TargetOptions &TargetOpts,
    527                          bool Complain) override {
    528     // If we've already initialized the target, don't do it again.
    529     if (Target)
    530       return false;
    531 
    532     this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts);
    533     Target =
    534         TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts);
    535 
    536     updated();
    537     return false;
    538   }
    539 
    540   void ReadCounter(const serialization::ModuleFile &M,
    541                    unsigned Value) override {
    542     Counter = Value;
    543   }
    544 
    545 private:
    546   void updated() {
    547     if (!Target || !InitializedLanguage)
    548       return;
    549 
    550     // Inform the target of the language options.
    551     //
    552     // FIXME: We shouldn't need to do this, the target should be immutable once
    553     // created. This complexity should be lifted elsewhere.
    554     Target->adjust(LangOpt);
    555 
    556     // Initialize the preprocessor.
    557     PP.Initialize(*Target);
    558 
    559     // Initialize the ASTContext
    560     Context.InitBuiltinTypes(*Target);
    561 
    562     // We didn't have access to the comment options when the ASTContext was
    563     // constructed, so register them now.
    564     Context.getCommentCommandTraits().registerCommentOptions(
    565         LangOpt.CommentOpts);
    566   }
    567 };
    568 
    569   /// \brief Diagnostic consumer that saves each diagnostic it is given.
    570 class StoredDiagnosticConsumer : public DiagnosticConsumer {
    571   SmallVectorImpl<StoredDiagnostic> &StoredDiags;
    572   SourceManager *SourceMgr;
    573 
    574 public:
    575   explicit StoredDiagnosticConsumer(
    576                           SmallVectorImpl<StoredDiagnostic> &StoredDiags)
    577     : StoredDiags(StoredDiags), SourceMgr(nullptr) {}
    578 
    579   void BeginSourceFile(const LangOptions &LangOpts,
    580                        const Preprocessor *PP = nullptr) override {
    581     if (PP)
    582       SourceMgr = &PP->getSourceManager();
    583   }
    584 
    585   void HandleDiagnostic(DiagnosticsEngine::Level Level,
    586                         const Diagnostic &Info) override;
    587 };
    588 
    589 /// \brief RAII object that optionally captures diagnostics, if
    590 /// there is no diagnostic client to capture them already.
    591 class CaptureDroppedDiagnostics {
    592   DiagnosticsEngine &Diags;
    593   StoredDiagnosticConsumer Client;
    594   DiagnosticConsumer *PreviousClient;
    595 
    596 public:
    597   CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
    598                           SmallVectorImpl<StoredDiagnostic> &StoredDiags)
    599     : Diags(Diags), Client(StoredDiags), PreviousClient(nullptr)
    600   {
    601     if (RequestCapture || Diags.getClient() == nullptr) {
    602       PreviousClient = Diags.takeClient();
    603       Diags.setClient(&Client);
    604     }
    605   }
    606 
    607   ~CaptureDroppedDiagnostics() {
    608     if (Diags.getClient() == &Client) {
    609       Diags.takeClient();
    610       Diags.setClient(PreviousClient);
    611     }
    612   }
    613 };
    614 
    615 } // anonymous namespace
    616 
    617 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
    618                                               const Diagnostic &Info) {
    619   // Default implementation (Warnings/errors count).
    620   DiagnosticConsumer::HandleDiagnostic(Level, Info);
    621 
    622   // Only record the diagnostic if it's part of the source manager we know
    623   // about. This effectively drops diagnostics from modules we're building.
    624   // FIXME: In the long run, ee don't want to drop source managers from modules.
    625   if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr)
    626     StoredDiags.push_back(StoredDiagnostic(Level, Info));
    627 }
    628 
    629 ASTMutationListener *ASTUnit::getASTMutationListener() {
    630   if (WriterData)
    631     return &WriterData->Writer;
    632   return nullptr;
    633 }
    634 
    635 ASTDeserializationListener *ASTUnit::getDeserializationListener() {
    636   if (WriterData)
    637     return &WriterData->Writer;
    638   return nullptr;
    639 }
    640 
    641 llvm::MemoryBuffer *ASTUnit::getBufferForFile(StringRef Filename,
    642                                               std::string *ErrorStr) {
    643   assert(FileMgr);
    644   return FileMgr->getBufferForFile(Filename, ErrorStr);
    645 }
    646 
    647 /// \brief Configure the diagnostics object for use with ASTUnit.
    648 void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
    649                              const char **ArgBegin, const char **ArgEnd,
    650                              ASTUnit &AST, bool CaptureDiagnostics) {
    651   if (!Diags.get()) {
    652     // No diagnostics engine was provided, so create our own diagnostics object
    653     // with the default options.
    654     DiagnosticConsumer *Client = nullptr;
    655     if (CaptureDiagnostics)
    656       Client = new StoredDiagnosticConsumer(AST.StoredDiagnostics);
    657     Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions(),
    658                                                 Client,
    659                                                 /*ShouldOwnClient=*/true);
    660   } else if (CaptureDiagnostics) {
    661     Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics));
    662   }
    663 }
    664 
    665 ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
    666                               IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
    667                                   const FileSystemOptions &FileSystemOpts,
    668                                   bool OnlyLocalDecls,
    669                                   ArrayRef<RemappedFile> RemappedFiles,
    670                                   bool CaptureDiagnostics,
    671                                   bool AllowPCHWithCompilerErrors,
    672                                   bool UserFilesAreVolatile) {
    673   std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
    674 
    675   // Recover resources if we crash before exiting this method.
    676   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
    677     ASTUnitCleanup(AST.get());
    678   llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
    679     llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
    680     DiagCleanup(Diags.get());
    681 
    682   ConfigureDiags(Diags, nullptr, nullptr, *AST, CaptureDiagnostics);
    683 
    684   AST->OnlyLocalDecls = OnlyLocalDecls;
    685   AST->CaptureDiagnostics = CaptureDiagnostics;
    686   AST->Diagnostics = Diags;
    687   IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem();
    688   AST->FileMgr = new FileManager(FileSystemOpts, VFS);
    689   AST->UserFilesAreVolatile = UserFilesAreVolatile;
    690   AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
    691                                      AST->getFileManager(),
    692                                      UserFilesAreVolatile);
    693   AST->HSOpts = new HeaderSearchOptions();
    694 
    695   AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
    696                                          AST->getSourceManager(),
    697                                          AST->getDiagnostics(),
    698                                          AST->ASTFileLangOpts,
    699                                          /*Target=*/nullptr));
    700 
    701   PreprocessorOptions *PPOpts = new PreprocessorOptions();
    702 
    703   for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I)
    704     PPOpts->addRemappedFile(RemappedFiles[I].first, RemappedFiles[I].second);
    705 
    706   // Gather Info for preprocessor construction later on.
    707 
    708   HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
    709   unsigned Counter;
    710 
    711   AST->PP =
    712       new Preprocessor(PPOpts, AST->getDiagnostics(), AST->ASTFileLangOpts,
    713                        AST->getSourceManager(), HeaderInfo, *AST,
    714                        /*IILookup=*/nullptr,
    715                        /*OwnsHeaderSearch=*/false);
    716   Preprocessor &PP = *AST->PP;
    717 
    718   AST->Ctx = new ASTContext(AST->ASTFileLangOpts, AST->getSourceManager(),
    719                             PP.getIdentifierTable(), PP.getSelectorTable(),
    720                             PP.getBuiltinInfo());
    721   ASTContext &Context = *AST->Ctx;
    722 
    723   bool disableValid = false;
    724   if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
    725     disableValid = true;
    726   AST->Reader = new ASTReader(PP, Context,
    727                              /*isysroot=*/"",
    728                              /*DisableValidation=*/disableValid,
    729                              AllowPCHWithCompilerErrors);
    730 
    731   AST->Reader->setListener(new ASTInfoCollector(*AST->PP, Context,
    732                                            AST->ASTFileLangOpts,
    733                                            AST->TargetOpts, AST->Target,
    734                                            Counter));
    735 
    736   switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile,
    737                           SourceLocation(), ASTReader::ARR_None)) {
    738   case ASTReader::Success:
    739     break;
    740 
    741   case ASTReader::Failure:
    742   case ASTReader::Missing:
    743   case ASTReader::OutOfDate:
    744   case ASTReader::VersionMismatch:
    745   case ASTReader::ConfigurationMismatch:
    746   case ASTReader::HadErrors:
    747     AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
    748     return nullptr;
    749   }
    750 
    751   AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile();
    752 
    753   PP.setCounterValue(Counter);
    754 
    755   // Attach the AST reader to the AST context as an external AST
    756   // source, so that declarations will be deserialized from the
    757   // AST file as needed.
    758   Context.setExternalSource(AST->Reader);
    759 
    760   // Create an AST consumer, even though it isn't used.
    761   AST->Consumer.reset(new ASTConsumer);
    762 
    763   // Create a semantic analysis object and tell the AST reader about it.
    764   AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
    765   AST->TheSema->Initialize();
    766   AST->Reader->InitializeSema(*AST->TheSema);
    767 
    768   // Tell the diagnostic client that we have started a source file.
    769   AST->getDiagnostics().getClient()->BeginSourceFile(Context.getLangOpts(),&PP);
    770 
    771   return AST.release();
    772 }
    773 
    774 namespace {
    775 
    776 /// \brief Preprocessor callback class that updates a hash value with the names
    777 /// of all macros that have been defined by the translation unit.
    778 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
    779   unsigned &Hash;
    780 
    781 public:
    782   explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
    783 
    784   void MacroDefined(const Token &MacroNameTok,
    785                     const MacroDirective *MD) override {
    786     Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
    787   }
    788 };
    789 
    790 /// \brief Add the given declaration to the hash of all top-level entities.
    791 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
    792   if (!D)
    793     return;
    794 
    795   DeclContext *DC = D->getDeclContext();
    796   if (!DC)
    797     return;
    798 
    799   if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
    800     return;
    801 
    802   if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
    803     if (EnumDecl *EnumD = dyn_cast<EnumDecl>(D)) {
    804       // For an unscoped enum include the enumerators in the hash since they
    805       // enter the top-level namespace.
    806       if (!EnumD->isScoped()) {
    807         for (const auto *EI : EnumD->enumerators()) {
    808           if (EI->getIdentifier())
    809             Hash = llvm::HashString(EI->getIdentifier()->getName(), Hash);
    810         }
    811       }
    812     }
    813 
    814     if (ND->getIdentifier())
    815       Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
    816     else if (DeclarationName Name = ND->getDeclName()) {
    817       std::string NameStr = Name.getAsString();
    818       Hash = llvm::HashString(NameStr, Hash);
    819     }
    820     return;
    821   }
    822 
    823   if (ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) {
    824     if (Module *Mod = ImportD->getImportedModule()) {
    825       std::string ModName = Mod->getFullModuleName();
    826       Hash = llvm::HashString(ModName, Hash);
    827     }
    828     return;
    829   }
    830 }
    831 
    832 class TopLevelDeclTrackerConsumer : public ASTConsumer {
    833   ASTUnit &Unit;
    834   unsigned &Hash;
    835 
    836 public:
    837   TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
    838     : Unit(_Unit), Hash(Hash) {
    839     Hash = 0;
    840   }
    841 
    842   void handleTopLevelDecl(Decl *D) {
    843     if (!D)
    844       return;
    845 
    846     // FIXME: Currently ObjC method declarations are incorrectly being
    847     // reported as top-level declarations, even though their DeclContext
    848     // is the containing ObjC @interface/@implementation.  This is a
    849     // fundamental problem in the parser right now.
    850     if (isa<ObjCMethodDecl>(D))
    851       return;
    852 
    853     AddTopLevelDeclarationToHash(D, Hash);
    854     Unit.addTopLevelDecl(D);
    855 
    856     handleFileLevelDecl(D);
    857   }
    858 
    859   void handleFileLevelDecl(Decl *D) {
    860     Unit.addFileLevelDecl(D);
    861     if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) {
    862       for (auto *I : NSD->decls())
    863         handleFileLevelDecl(I);
    864     }
    865   }
    866 
    867   bool HandleTopLevelDecl(DeclGroupRef D) override {
    868     for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
    869       handleTopLevelDecl(*it);
    870     return true;
    871   }
    872 
    873   // We're not interested in "interesting" decls.
    874   void HandleInterestingDecl(DeclGroupRef) override {}
    875 
    876   void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
    877     for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
    878       handleTopLevelDecl(*it);
    879   }
    880 
    881   ASTMutationListener *GetASTMutationListener() override {
    882     return Unit.getASTMutationListener();
    883   }
    884 
    885   ASTDeserializationListener *GetASTDeserializationListener() override {
    886     return Unit.getDeserializationListener();
    887   }
    888 };
    889 
    890 class TopLevelDeclTrackerAction : public ASTFrontendAction {
    891 public:
    892   ASTUnit &Unit;
    893 
    894   ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
    895                                  StringRef InFile) override {
    896     CI.getPreprocessor().addPPCallbacks(
    897      new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
    898     return new TopLevelDeclTrackerConsumer(Unit,
    899                                            Unit.getCurrentTopLevelHashValue());
    900   }
    901 
    902 public:
    903   TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
    904 
    905   bool hasCodeCompletionSupport() const override { return false; }
    906   TranslationUnitKind getTranslationUnitKind() override {
    907     return Unit.getTranslationUnitKind();
    908   }
    909 };
    910 
    911 class PrecompilePreambleAction : public ASTFrontendAction {
    912   ASTUnit &Unit;
    913   bool HasEmittedPreamblePCH;
    914 
    915 public:
    916   explicit PrecompilePreambleAction(ASTUnit &Unit)
    917       : Unit(Unit), HasEmittedPreamblePCH(false) {}
    918 
    919   ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
    920                                  StringRef InFile) override;
    921   bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
    922   void setHasEmittedPreamblePCH() { HasEmittedPreamblePCH = true; }
    923   bool shouldEraseOutputFiles() override { return !hasEmittedPreamblePCH(); }
    924 
    925   bool hasCodeCompletionSupport() const override { return false; }
    926   bool hasASTFileSupport() const override { return false; }
    927   TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; }
    928 };
    929 
    930 class PrecompilePreambleConsumer : public PCHGenerator {
    931   ASTUnit &Unit;
    932   unsigned &Hash;
    933   std::vector<Decl *> TopLevelDecls;
    934   PrecompilePreambleAction *Action;
    935 
    936 public:
    937   PrecompilePreambleConsumer(ASTUnit &Unit, PrecompilePreambleAction *Action,
    938                              const Preprocessor &PP, StringRef isysroot,
    939                              raw_ostream *Out)
    940     : PCHGenerator(PP, "", nullptr, isysroot, Out, /*AllowASTWithErrors=*/true),
    941       Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()), Action(Action) {
    942     Hash = 0;
    943   }
    944 
    945   bool HandleTopLevelDecl(DeclGroupRef D) override {
    946     for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
    947       Decl *D = *it;
    948       // FIXME: Currently ObjC method declarations are incorrectly being
    949       // reported as top-level declarations, even though their DeclContext
    950       // is the containing ObjC @interface/@implementation.  This is a
    951       // fundamental problem in the parser right now.
    952       if (isa<ObjCMethodDecl>(D))
    953         continue;
    954       AddTopLevelDeclarationToHash(D, Hash);
    955       TopLevelDecls.push_back(D);
    956     }
    957     return true;
    958   }
    959 
    960   void HandleTranslationUnit(ASTContext &Ctx) override {
    961     PCHGenerator::HandleTranslationUnit(Ctx);
    962     if (hasEmittedPCH()) {
    963       // Translate the top-level declarations we captured during
    964       // parsing into declaration IDs in the precompiled
    965       // preamble. This will allow us to deserialize those top-level
    966       // declarations when requested.
    967       for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) {
    968         Decl *D = TopLevelDecls[I];
    969         // Invalid top-level decls may not have been serialized.
    970         if (D->isInvalidDecl())
    971           continue;
    972         Unit.addTopLevelDeclFromPreamble(getWriter().getDeclID(D));
    973       }
    974 
    975       Action->setHasEmittedPreamblePCH();
    976     }
    977   }
    978 };
    979 
    980 }
    981 
    982 ASTConsumer *PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
    983                                                          StringRef InFile) {
    984   std::string Sysroot;
    985   std::string OutputFile;
    986   raw_ostream *OS = nullptr;
    987   if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
    988                                                      OutputFile, OS))
    989     return nullptr;
    990 
    991   if (!CI.getFrontendOpts().RelocatablePCH)
    992     Sysroot.clear();
    993 
    994   CI.getPreprocessor().addPPCallbacks(new MacroDefinitionTrackerPPCallbacks(
    995       Unit.getCurrentTopLevelHashValue()));
    996   return new PrecompilePreambleConsumer(Unit, this, CI.getPreprocessor(),
    997                                         Sysroot, OS);
    998 }
    999 
   1000 static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
   1001   return StoredDiag.getLocation().isValid();
   1002 }
   1003 
   1004 static void
   1005 checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
   1006   // Get rid of stored diagnostics except the ones from the driver which do not
   1007   // have a source location.
   1008   StoredDiags.erase(
   1009       std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
   1010       StoredDiags.end());
   1011 }
   1012 
   1013 static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
   1014                                                               StoredDiagnostics,
   1015                                   SourceManager &SM) {
   1016   // The stored diagnostic has the old source manager in it; update
   1017   // the locations to refer into the new source manager. Since we've
   1018   // been careful to make sure that the source manager's state
   1019   // before and after are identical, so that we can reuse the source
   1020   // location itself.
   1021   for (unsigned I = 0, N = StoredDiagnostics.size(); I < N; ++I) {
   1022     if (StoredDiagnostics[I].getLocation().isValid()) {
   1023       FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SM);
   1024       StoredDiagnostics[I].setLocation(Loc);
   1025     }
   1026   }
   1027 }
   1028 
   1029 /// Parse the source file into a translation unit using the given compiler
   1030 /// invocation, replacing the current translation unit.
   1031 ///
   1032 /// \returns True if a failure occurred that causes the ASTUnit not to
   1033 /// contain any translation-unit information, false otherwise.
   1034 bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
   1035   delete SavedMainFileBuffer;
   1036   SavedMainFileBuffer = nullptr;
   1037 
   1038   if (!Invocation) {
   1039     delete OverrideMainBuffer;
   1040     return true;
   1041   }
   1042 
   1043   // Create the compiler instance to use for building the AST.
   1044   std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
   1045 
   1046   // Recover resources if we crash before exiting this method.
   1047   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
   1048     CICleanup(Clang.get());
   1049 
   1050   IntrusiveRefCntPtr<CompilerInvocation>
   1051     CCInvocation(new CompilerInvocation(*Invocation));
   1052 
   1053   Clang->setInvocation(CCInvocation.get());
   1054   OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
   1055 
   1056   // Set up diagnostics, capturing any diagnostics that would
   1057   // otherwise be dropped.
   1058   Clang->setDiagnostics(&getDiagnostics());
   1059 
   1060   // Create the target instance.
   1061   Clang->setTarget(TargetInfo::CreateTargetInfo(
   1062       Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
   1063   if (!Clang->hasTarget()) {
   1064     delete OverrideMainBuffer;
   1065     return true;
   1066   }
   1067 
   1068   // Inform the target of the language options.
   1069   //
   1070   // FIXME: We shouldn't need to do this, the target should be immutable once
   1071   // created. This complexity should be lifted elsewhere.
   1072   Clang->getTarget().adjust(Clang->getLangOpts());
   1073 
   1074   assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
   1075          "Invocation must have exactly one source file!");
   1076   assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
   1077          "FIXME: AST inputs not yet supported here!");
   1078   assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
   1079          "IR inputs not support here!");
   1080 
   1081   // Configure the various subsystems.
   1082   LangOpts = Clang->getInvocation().LangOpts;
   1083   FileSystemOpts = Clang->getFileSystemOpts();
   1084   IntrusiveRefCntPtr<vfs::FileSystem> VFS =
   1085       createVFSFromCompilerInvocation(Clang->getInvocation(), getDiagnostics());
   1086   if (!VFS) {
   1087     delete OverrideMainBuffer;
   1088     return true;
   1089   }
   1090   FileMgr = new FileManager(FileSystemOpts, VFS);
   1091   SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
   1092                                 UserFilesAreVolatile);
   1093   TheSema.reset();
   1094   Ctx = nullptr;
   1095   PP = nullptr;
   1096   Reader = nullptr;
   1097 
   1098   // Clear out old caches and data.
   1099   TopLevelDecls.clear();
   1100   clearFileLevelDecls();
   1101   CleanTemporaryFiles();
   1102 
   1103   if (!OverrideMainBuffer) {
   1104     checkAndRemoveNonDriverDiags(StoredDiagnostics);
   1105     TopLevelDeclsInPreamble.clear();
   1106   }
   1107 
   1108   // Create a file manager object to provide access to and cache the filesystem.
   1109   Clang->setFileManager(&getFileManager());
   1110 
   1111   // Create the source manager.
   1112   Clang->setSourceManager(&getSourceManager());
   1113 
   1114   // If the main file has been overridden due to the use of a preamble,
   1115   // make that override happen and introduce the preamble.
   1116   PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
   1117   if (OverrideMainBuffer) {
   1118     PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
   1119     PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
   1120     PreprocessorOpts.PrecompiledPreambleBytes.second
   1121                                                     = PreambleEndsAtStartOfLine;
   1122     PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
   1123     PreprocessorOpts.DisablePCHValidation = true;
   1124 
   1125     // The stored diagnostic has the old source manager in it; update
   1126     // the locations to refer into the new source manager. Since we've
   1127     // been careful to make sure that the source manager's state
   1128     // before and after are identical, so that we can reuse the source
   1129     // location itself.
   1130     checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
   1131 
   1132     // Keep track of the override buffer;
   1133     SavedMainFileBuffer = OverrideMainBuffer;
   1134   }
   1135 
   1136   std::unique_ptr<TopLevelDeclTrackerAction> Act(
   1137       new TopLevelDeclTrackerAction(*this));
   1138 
   1139   // Recover resources if we crash before exiting this method.
   1140   llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
   1141     ActCleanup(Act.get());
   1142 
   1143   if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
   1144     goto error;
   1145 
   1146   if (OverrideMainBuffer) {
   1147     std::string ModName = getPreambleFile(this);
   1148     TranslateStoredDiagnostics(getFileManager(), getSourceManager(),
   1149                                PreambleDiagnostics, StoredDiagnostics);
   1150   }
   1151 
   1152   if (!Act->Execute())
   1153     goto error;
   1154 
   1155   transferASTDataFromCompilerInstance(*Clang);
   1156 
   1157   Act->EndSourceFile();
   1158 
   1159   FailedParseDiagnostics.clear();
   1160 
   1161   return false;
   1162 
   1163 error:
   1164   // Remove the overridden buffer we used for the preamble.
   1165   if (OverrideMainBuffer) {
   1166     delete OverrideMainBuffer;
   1167     SavedMainFileBuffer = nullptr;
   1168   }
   1169 
   1170   // Keep the ownership of the data in the ASTUnit because the client may
   1171   // want to see the diagnostics.
   1172   transferASTDataFromCompilerInstance(*Clang);
   1173   FailedParseDiagnostics.swap(StoredDiagnostics);
   1174   StoredDiagnostics.clear();
   1175   NumStoredDiagnosticsFromDriver = 0;
   1176   return true;
   1177 }
   1178 
   1179 /// \brief Simple function to retrieve a path for a preamble precompiled header.
   1180 static std::string GetPreamblePCHPath() {
   1181   // FIXME: This is a hack so that we can override the preamble file during
   1182   // crash-recovery testing, which is the only case where the preamble files
   1183   // are not necessarily cleaned up.
   1184   const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
   1185   if (TmpFile)
   1186     return TmpFile;
   1187 
   1188   SmallString<128> Path;
   1189   llvm::sys::fs::createTemporaryFile("preamble", "pch", Path);
   1190 
   1191   return Path.str();
   1192 }
   1193 
   1194 /// \brief Compute the preamble for the main file, providing the source buffer
   1195 /// that corresponds to the main file along with a pair (bytes, start-of-line)
   1196 /// that describes the preamble.
   1197 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
   1198 ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
   1199                          unsigned MaxLines, bool &CreatedBuffer) {
   1200   FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
   1201   PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
   1202   CreatedBuffer = false;
   1203 
   1204   // Try to determine if the main file has been remapped, either from the
   1205   // command line (to another file) or directly through the compiler invocation
   1206   // (to a memory buffer).
   1207   llvm::MemoryBuffer *Buffer = nullptr;
   1208   std::string MainFilePath(FrontendOpts.Inputs[0].getFile());
   1209   llvm::sys::fs::UniqueID MainFileID;
   1210   if (!llvm::sys::fs::getUniqueID(MainFilePath, MainFileID)) {
   1211     // Check whether there is a file-file remapping of the main file
   1212     for (const auto &RF : PreprocessorOpts.RemappedFiles) {
   1213       std::string MPath(RF.first);
   1214       llvm::sys::fs::UniqueID MID;
   1215       if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
   1216         if (MainFileID == MID) {
   1217           // We found a remapping. Try to load the resulting, remapped source.
   1218           if (CreatedBuffer) {
   1219             delete Buffer;
   1220             CreatedBuffer = false;
   1221           }
   1222 
   1223           Buffer = getBufferForFile(RF.second);
   1224           if (!Buffer)
   1225             return std::make_pair(nullptr, std::make_pair(0, true));
   1226           CreatedBuffer = true;
   1227         }
   1228       }
   1229     }
   1230 
   1231     // Check whether there is a file-buffer remapping. It supercedes the
   1232     // file-file remapping.
   1233     for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
   1234       std::string MPath(RB.first);
   1235       llvm::sys::fs::UniqueID MID;
   1236       if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
   1237         if (MainFileID == MID) {
   1238           // We found a remapping.
   1239           if (CreatedBuffer) {
   1240             delete Buffer;
   1241             CreatedBuffer = false;
   1242           }
   1243 
   1244           Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
   1245         }
   1246       }
   1247     }
   1248   }
   1249 
   1250   // If the main source file was not remapped, load it now.
   1251   if (!Buffer) {
   1252     Buffer = getBufferForFile(FrontendOpts.Inputs[0].getFile());
   1253     if (!Buffer)
   1254       return std::make_pair(nullptr, std::make_pair(0, true));
   1255 
   1256     CreatedBuffer = true;
   1257   }
   1258 
   1259   return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer,
   1260                                                        *Invocation.getLangOpts(),
   1261                                                        MaxLines));
   1262 }
   1263 
   1264 ASTUnit::PreambleFileHash
   1265 ASTUnit::PreambleFileHash::createForFile(off_t Size, time_t ModTime) {
   1266   PreambleFileHash Result;
   1267   Result.Size = Size;
   1268   Result.ModTime = ModTime;
   1269   memset(Result.MD5, 0, sizeof(Result.MD5));
   1270   return Result;
   1271 }
   1272 
   1273 ASTUnit::PreambleFileHash ASTUnit::PreambleFileHash::createForMemoryBuffer(
   1274     const llvm::MemoryBuffer *Buffer) {
   1275   PreambleFileHash Result;
   1276   Result.Size = Buffer->getBufferSize();
   1277   Result.ModTime = 0;
   1278 
   1279   llvm::MD5 MD5Ctx;
   1280   MD5Ctx.update(Buffer->getBuffer().data());
   1281   MD5Ctx.final(Result.MD5);
   1282 
   1283   return Result;
   1284 }
   1285 
   1286 namespace clang {
   1287 bool operator==(const ASTUnit::PreambleFileHash &LHS,
   1288                 const ASTUnit::PreambleFileHash &RHS) {
   1289   return LHS.Size == RHS.Size && LHS.ModTime == RHS.ModTime &&
   1290          memcmp(LHS.MD5, RHS.MD5, sizeof(LHS.MD5)) == 0;
   1291 }
   1292 } // namespace clang
   1293 
   1294 static std::pair<unsigned, unsigned>
   1295 makeStandaloneRange(CharSourceRange Range, const SourceManager &SM,
   1296                     const LangOptions &LangOpts) {
   1297   CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts);
   1298   unsigned Offset = SM.getFileOffset(FileRange.getBegin());
   1299   unsigned EndOffset = SM.getFileOffset(FileRange.getEnd());
   1300   return std::make_pair(Offset, EndOffset);
   1301 }
   1302 
   1303 static void makeStandaloneFixIt(const SourceManager &SM,
   1304                                 const LangOptions &LangOpts,
   1305                                 const FixItHint &InFix,
   1306                                 ASTUnit::StandaloneFixIt &OutFix) {
   1307   OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts);
   1308   OutFix.InsertFromRange = makeStandaloneRange(InFix.InsertFromRange, SM,
   1309                                                LangOpts);
   1310   OutFix.CodeToInsert = InFix.CodeToInsert;
   1311   OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions;
   1312 }
   1313 
   1314 static void makeStandaloneDiagnostic(const LangOptions &LangOpts,
   1315                                      const StoredDiagnostic &InDiag,
   1316                                      ASTUnit::StandaloneDiagnostic &OutDiag) {
   1317   OutDiag.ID = InDiag.getID();
   1318   OutDiag.Level = InDiag.getLevel();
   1319   OutDiag.Message = InDiag.getMessage();
   1320   OutDiag.LocOffset = 0;
   1321   if (InDiag.getLocation().isInvalid())
   1322     return;
   1323   const SourceManager &SM = InDiag.getLocation().getManager();
   1324   SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation());
   1325   OutDiag.Filename = SM.getFilename(FileLoc);
   1326   if (OutDiag.Filename.empty())
   1327     return;
   1328   OutDiag.LocOffset = SM.getFileOffset(FileLoc);
   1329   for (StoredDiagnostic::range_iterator
   1330          I = InDiag.range_begin(), E = InDiag.range_end(); I != E; ++I) {
   1331     OutDiag.Ranges.push_back(makeStandaloneRange(*I, SM, LangOpts));
   1332   }
   1333   for (StoredDiagnostic::fixit_iterator
   1334          I = InDiag.fixit_begin(), E = InDiag.fixit_end(); I != E; ++I) {
   1335     ASTUnit::StandaloneFixIt Fix;
   1336     makeStandaloneFixIt(SM, LangOpts, *I, Fix);
   1337     OutDiag.FixIts.push_back(Fix);
   1338   }
   1339 }
   1340 
   1341 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
   1342 /// the source file.
   1343 ///
   1344 /// This routine will compute the preamble of the main source file. If a
   1345 /// non-trivial preamble is found, it will precompile that preamble into a
   1346 /// precompiled header so that the precompiled preamble can be used to reduce
   1347 /// reparsing time. If a precompiled preamble has already been constructed,
   1348 /// this routine will determine if it is still valid and, if so, avoid
   1349 /// rebuilding the precompiled preamble.
   1350 ///
   1351 /// \param AllowRebuild When true (the default), this routine is
   1352 /// allowed to rebuild the precompiled preamble if it is found to be
   1353 /// out-of-date.
   1354 ///
   1355 /// \param MaxLines When non-zero, the maximum number of lines that
   1356 /// can occur within the preamble.
   1357 ///
   1358 /// \returns If the precompiled preamble can be used, returns a newly-allocated
   1359 /// buffer that should be used in place of the main file when doing so.
   1360 /// Otherwise, returns a NULL pointer.
   1361 llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
   1362                               const CompilerInvocation &PreambleInvocationIn,
   1363                                                            bool AllowRebuild,
   1364                                                            unsigned MaxLines) {
   1365 
   1366   IntrusiveRefCntPtr<CompilerInvocation>
   1367     PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
   1368   FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
   1369   PreprocessorOptions &PreprocessorOpts
   1370     = PreambleInvocation->getPreprocessorOpts();
   1371 
   1372   bool CreatedPreambleBuffer = false;
   1373   std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
   1374     = ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer);
   1375 
   1376   // If ComputePreamble() Take ownership of the preamble buffer.
   1377   std::unique_ptr<llvm::MemoryBuffer> OwnedPreambleBuffer;
   1378   if (CreatedPreambleBuffer)
   1379     OwnedPreambleBuffer.reset(NewPreamble.first);
   1380 
   1381   if (!NewPreamble.second.first) {
   1382     // We couldn't find a preamble in the main source. Clear out the current
   1383     // preamble, if we have one. It's obviously no good any more.
   1384     Preamble.clear();
   1385     erasePreambleFile(this);
   1386 
   1387     // The next time we actually see a preamble, precompile it.
   1388     PreambleRebuildCounter = 1;
   1389     return nullptr;
   1390   }
   1391 
   1392   if (!Preamble.empty()) {
   1393     // We've previously computed a preamble. Check whether we have the same
   1394     // preamble now that we did before, and that there's enough space in
   1395     // the main-file buffer within the precompiled preamble to fit the
   1396     // new main file.
   1397     if (Preamble.size() == NewPreamble.second.first &&
   1398         PreambleEndsAtStartOfLine == NewPreamble.second.second &&
   1399         memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(),
   1400                NewPreamble.second.first) == 0) {
   1401       // The preamble has not changed. We may be able to re-use the precompiled
   1402       // preamble.
   1403 
   1404       // Check that none of the files used by the preamble have changed.
   1405       bool AnyFileChanged = false;
   1406 
   1407       // First, make a record of those files that have been overridden via
   1408       // remapping or unsaved_files.
   1409       llvm::StringMap<PreambleFileHash> OverriddenFiles;
   1410       for (const auto &R : PreprocessorOpts.RemappedFiles) {
   1411         if (AnyFileChanged)
   1412           break;
   1413 
   1414         vfs::Status Status;
   1415         if (FileMgr->getNoncachedStatValue(R.second, Status)) {
   1416           // If we can't stat the file we're remapping to, assume that something
   1417           // horrible happened.
   1418           AnyFileChanged = true;
   1419           break;
   1420         }
   1421 
   1422         OverriddenFiles[R.first] = PreambleFileHash::createForFile(
   1423             Status.getSize(), Status.getLastModificationTime().toEpochTime());
   1424       }
   1425 
   1426       for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
   1427         if (AnyFileChanged)
   1428           break;
   1429         OverriddenFiles[RB.first] =
   1430             PreambleFileHash::createForMemoryBuffer(RB.second);
   1431       }
   1432 
   1433       // Check whether anything has changed.
   1434       for (llvm::StringMap<PreambleFileHash>::iterator
   1435              F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
   1436            !AnyFileChanged && F != FEnd;
   1437            ++F) {
   1438         llvm::StringMap<PreambleFileHash>::iterator Overridden
   1439           = OverriddenFiles.find(F->first());
   1440         if (Overridden != OverriddenFiles.end()) {
   1441           // This file was remapped; check whether the newly-mapped file
   1442           // matches up with the previous mapping.
   1443           if (Overridden->second != F->second)
   1444             AnyFileChanged = true;
   1445           continue;
   1446         }
   1447 
   1448         // The file was not remapped; check whether it has changed on disk.
   1449         vfs::Status Status;
   1450         if (FileMgr->getNoncachedStatValue(F->first(), Status)) {
   1451           // If we can't stat the file, assume that something horrible happened.
   1452           AnyFileChanged = true;
   1453         } else if (Status.getSize() != uint64_t(F->second.Size) ||
   1454                    Status.getLastModificationTime().toEpochTime() !=
   1455                        uint64_t(F->second.ModTime))
   1456           AnyFileChanged = true;
   1457       }
   1458 
   1459       if (!AnyFileChanged) {
   1460         // Okay! We can re-use the precompiled preamble.
   1461 
   1462         // Set the state of the diagnostic object to mimic its state
   1463         // after parsing the preamble.
   1464         getDiagnostics().Reset();
   1465         ProcessWarningOptions(getDiagnostics(),
   1466                               PreambleInvocation->getDiagnosticOpts());
   1467         getDiagnostics().setNumWarnings(NumWarningsInPreamble);
   1468 
   1469         return llvm::MemoryBuffer::getMemBufferCopy(
   1470             NewPreamble.first->getBuffer(), FrontendOpts.Inputs[0].getFile());
   1471       }
   1472     }
   1473 
   1474     // If we aren't allowed to rebuild the precompiled preamble, just
   1475     // return now.
   1476     if (!AllowRebuild)
   1477       return nullptr;
   1478 
   1479     // We can't reuse the previously-computed preamble. Build a new one.
   1480     Preamble.clear();
   1481     PreambleDiagnostics.clear();
   1482     erasePreambleFile(this);
   1483     PreambleRebuildCounter = 1;
   1484   } else if (!AllowRebuild) {
   1485     // We aren't allowed to rebuild the precompiled preamble; just
   1486     // return now.
   1487     return nullptr;
   1488   }
   1489 
   1490   // If the preamble rebuild counter > 1, it's because we previously
   1491   // failed to build a preamble and we're not yet ready to try
   1492   // again. Decrement the counter and return a failure.
   1493   if (PreambleRebuildCounter > 1) {
   1494     --PreambleRebuildCounter;
   1495     return nullptr;
   1496   }
   1497 
   1498   // Create a temporary file for the precompiled preamble. In rare
   1499   // circumstances, this can fail.
   1500   std::string PreamblePCHPath = GetPreamblePCHPath();
   1501   if (PreamblePCHPath.empty()) {
   1502     // Try again next time.
   1503     PreambleRebuildCounter = 1;
   1504     return nullptr;
   1505   }
   1506 
   1507   // We did not previously compute a preamble, or it can't be reused anyway.
   1508   SimpleTimer PreambleTimer(WantTiming);
   1509   PreambleTimer.setOutput("Precompiling preamble");
   1510 
   1511   // Save the preamble text for later; we'll need to compare against it for
   1512   // subsequent reparses.
   1513   StringRef MainFilename = FrontendOpts.Inputs[0].getFile();
   1514   Preamble.assign(FileMgr->getFile(MainFilename),
   1515                   NewPreamble.first->getBufferStart(),
   1516                   NewPreamble.first->getBufferStart()
   1517                                                   + NewPreamble.second.first);
   1518   PreambleEndsAtStartOfLine = NewPreamble.second.second;
   1519 
   1520   delete PreambleBuffer;
   1521   PreambleBuffer
   1522     = llvm::MemoryBuffer::getMemBufferCopy(
   1523         NewPreamble.first->getBuffer().slice(0, Preamble.size()), MainFilename);
   1524 
   1525   // Remap the main source file to the preamble buffer.
   1526   StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
   1527   PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer);
   1528 
   1529   // Tell the compiler invocation to generate a temporary precompiled header.
   1530   FrontendOpts.ProgramAction = frontend::GeneratePCH;
   1531   // FIXME: Generate the precompiled header into memory?
   1532   FrontendOpts.OutputFile = PreamblePCHPath;
   1533   PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
   1534   PreprocessorOpts.PrecompiledPreambleBytes.second = false;
   1535 
   1536   // Create the compiler instance to use for building the precompiled preamble.
   1537   std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
   1538 
   1539   // Recover resources if we crash before exiting this method.
   1540   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
   1541     CICleanup(Clang.get());
   1542 
   1543   Clang->setInvocation(&*PreambleInvocation);
   1544   OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
   1545 
   1546   // Set up diagnostics, capturing all of the diagnostics produced.
   1547   Clang->setDiagnostics(&getDiagnostics());
   1548 
   1549   // Create the target instance.
   1550   Clang->setTarget(TargetInfo::CreateTargetInfo(
   1551       Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
   1552   if (!Clang->hasTarget()) {
   1553     llvm::sys::fs::remove(FrontendOpts.OutputFile);
   1554     Preamble.clear();
   1555     PreambleRebuildCounter = DefaultPreambleRebuildInterval;
   1556     PreprocessorOpts.RemappedFileBuffers.pop_back();
   1557     return nullptr;
   1558   }
   1559 
   1560   // Inform the target of the language options.
   1561   //
   1562   // FIXME: We shouldn't need to do this, the target should be immutable once
   1563   // created. This complexity should be lifted elsewhere.
   1564   Clang->getTarget().adjust(Clang->getLangOpts());
   1565 
   1566   assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
   1567          "Invocation must have exactly one source file!");
   1568   assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
   1569          "FIXME: AST inputs not yet supported here!");
   1570   assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
   1571          "IR inputs not support here!");
   1572 
   1573   // Clear out old caches and data.
   1574   getDiagnostics().Reset();
   1575   ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
   1576   checkAndRemoveNonDriverDiags(StoredDiagnostics);
   1577   TopLevelDecls.clear();
   1578   TopLevelDeclsInPreamble.clear();
   1579   PreambleDiagnostics.clear();
   1580 
   1581   IntrusiveRefCntPtr<vfs::FileSystem> VFS =
   1582       createVFSFromCompilerInvocation(Clang->getInvocation(), getDiagnostics());
   1583   if (!VFS)
   1584     return nullptr;
   1585 
   1586   // Create a file manager object to provide access to and cache the filesystem.
   1587   Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
   1588 
   1589   // Create the source manager.
   1590   Clang->setSourceManager(new SourceManager(getDiagnostics(),
   1591                                             Clang->getFileManager()));
   1592 
   1593   auto PreambleDepCollector = std::make_shared<DependencyCollector>();
   1594   Clang->addDependencyCollector(PreambleDepCollector);
   1595 
   1596   std::unique_ptr<PrecompilePreambleAction> Act;
   1597   Act.reset(new PrecompilePreambleAction(*this));
   1598   if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
   1599     llvm::sys::fs::remove(FrontendOpts.OutputFile);
   1600     Preamble.clear();
   1601     PreambleRebuildCounter = DefaultPreambleRebuildInterval;
   1602     PreprocessorOpts.RemappedFileBuffers.pop_back();
   1603     return nullptr;
   1604   }
   1605 
   1606   Act->Execute();
   1607 
   1608   // Transfer any diagnostics generated when parsing the preamble into the set
   1609   // of preamble diagnostics.
   1610   for (stored_diag_iterator
   1611          I = stored_diag_afterDriver_begin(),
   1612          E = stored_diag_end(); I != E; ++I) {
   1613     StandaloneDiagnostic Diag;
   1614     makeStandaloneDiagnostic(Clang->getLangOpts(), *I, Diag);
   1615     PreambleDiagnostics.push_back(Diag);
   1616   }
   1617 
   1618   Act->EndSourceFile();
   1619 
   1620   checkAndRemoveNonDriverDiags(StoredDiagnostics);
   1621 
   1622   if (!Act->hasEmittedPreamblePCH()) {
   1623     // The preamble PCH failed (e.g. there was a module loading fatal error),
   1624     // so no precompiled header was generated. Forget that we even tried.
   1625     // FIXME: Should we leave a note for ourselves to try again?
   1626     llvm::sys::fs::remove(FrontendOpts.OutputFile);
   1627     Preamble.clear();
   1628     TopLevelDeclsInPreamble.clear();
   1629     PreambleRebuildCounter = DefaultPreambleRebuildInterval;
   1630     PreprocessorOpts.RemappedFileBuffers.pop_back();
   1631     return nullptr;
   1632   }
   1633 
   1634   // Keep track of the preamble we precompiled.
   1635   setPreambleFile(this, FrontendOpts.OutputFile);
   1636   NumWarningsInPreamble = getDiagnostics().getNumWarnings();
   1637 
   1638   // Keep track of all of the files that the source manager knows about,
   1639   // so we can verify whether they have changed or not.
   1640   FilesInPreamble.clear();
   1641   SourceManager &SourceMgr = Clang->getSourceManager();
   1642   for (auto &Filename : PreambleDepCollector->getDependencies()) {
   1643     const FileEntry *File = Clang->getFileManager().getFile(Filename);
   1644     if (!File || File == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))
   1645       continue;
   1646     if (time_t ModTime = File->getModificationTime()) {
   1647       FilesInPreamble[File->getName()] = PreambleFileHash::createForFile(
   1648           File->getSize(), ModTime);
   1649     } else {
   1650       llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File);
   1651       FilesInPreamble[File->getName()] =
   1652           PreambleFileHash::createForMemoryBuffer(Buffer);
   1653     }
   1654   }
   1655 
   1656   PreambleRebuildCounter = 1;
   1657   PreprocessorOpts.RemappedFileBuffers.pop_back();
   1658 
   1659   // If the hash of top-level entities differs from the hash of the top-level
   1660   // entities the last time we rebuilt the preamble, clear out the completion
   1661   // cache.
   1662   if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
   1663     CompletionCacheTopLevelHashValue = 0;
   1664     PreambleTopLevelHashValue = CurrentTopLevelHashValue;
   1665   }
   1666 
   1667   return llvm::MemoryBuffer::getMemBufferCopy(NewPreamble.first->getBuffer(),
   1668                                               MainFilename);
   1669 }
   1670 
   1671 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
   1672   std::vector<Decl *> Resolved;
   1673   Resolved.reserve(TopLevelDeclsInPreamble.size());
   1674   ExternalASTSource &Source = *getASTContext().getExternalSource();
   1675   for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
   1676     // Resolve the declaration ID to an actual declaration, possibly
   1677     // deserializing the declaration in the process.
   1678     Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
   1679     if (D)
   1680       Resolved.push_back(D);
   1681   }
   1682   TopLevelDeclsInPreamble.clear();
   1683   TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
   1684 }
   1685 
   1686 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
   1687   // Steal the created target, context, and preprocessor if they have been
   1688   // created.
   1689   assert(CI.hasInvocation() && "missing invocation");
   1690   LangOpts = CI.getInvocation().LangOpts;
   1691   TheSema.reset(CI.takeSema());
   1692   Consumer.reset(CI.takeASTConsumer());
   1693   if (CI.hasASTContext())
   1694     Ctx = &CI.getASTContext();
   1695   if (CI.hasPreprocessor())
   1696     PP = &CI.getPreprocessor();
   1697   CI.setSourceManager(nullptr);
   1698   CI.setFileManager(nullptr);
   1699   if (CI.hasTarget())
   1700     Target = &CI.getTarget();
   1701   Reader = CI.getModuleManager();
   1702   HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
   1703 }
   1704 
   1705 StringRef ASTUnit::getMainFileName() const {
   1706   if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
   1707     const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
   1708     if (Input.isFile())
   1709       return Input.getFile();
   1710     else
   1711       return Input.getBuffer()->getBufferIdentifier();
   1712   }
   1713 
   1714   if (SourceMgr) {
   1715     if (const FileEntry *
   1716           FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
   1717       return FE->getName();
   1718   }
   1719 
   1720   return StringRef();
   1721 }
   1722 
   1723 StringRef ASTUnit::getASTFileName() const {
   1724   if (!isMainFileAST())
   1725     return StringRef();
   1726 
   1727   serialization::ModuleFile &
   1728     Mod = Reader->getModuleManager().getPrimaryModule();
   1729   return Mod.FileName;
   1730 }
   1731 
   1732 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
   1733                          IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
   1734                          bool CaptureDiagnostics,
   1735                          bool UserFilesAreVolatile) {
   1736   std::unique_ptr<ASTUnit> AST;
   1737   AST.reset(new ASTUnit(false));
   1738   ConfigureDiags(Diags, nullptr, nullptr, *AST, CaptureDiagnostics);
   1739   AST->Diagnostics = Diags;
   1740   AST->Invocation = CI;
   1741   AST->FileSystemOpts = CI->getFileSystemOpts();
   1742   IntrusiveRefCntPtr<vfs::FileSystem> VFS =
   1743       createVFSFromCompilerInvocation(*CI, *Diags);
   1744   if (!VFS)
   1745     return nullptr;
   1746   AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
   1747   AST->UserFilesAreVolatile = UserFilesAreVolatile;
   1748   AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
   1749                                      UserFilesAreVolatile);
   1750 
   1751   return AST.release();
   1752 }
   1753 
   1754 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
   1755     CompilerInvocation *CI, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
   1756     ASTFrontendAction *Action, ASTUnit *Unit, bool Persistent,
   1757     StringRef ResourceFilesPath, bool OnlyLocalDecls, bool CaptureDiagnostics,
   1758     bool PrecompilePreamble, bool CacheCodeCompletionResults,
   1759     bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
   1760     std::unique_ptr<ASTUnit> *ErrAST) {
   1761   assert(CI && "A CompilerInvocation is required");
   1762 
   1763   std::unique_ptr<ASTUnit> OwnAST;
   1764   ASTUnit *AST = Unit;
   1765   if (!AST) {
   1766     // Create the AST unit.
   1767     OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile));
   1768     AST = OwnAST.get();
   1769     if (!AST)
   1770       return nullptr;
   1771   }
   1772 
   1773   if (!ResourceFilesPath.empty()) {
   1774     // Override the resources path.
   1775     CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
   1776   }
   1777   AST->OnlyLocalDecls = OnlyLocalDecls;
   1778   AST->CaptureDiagnostics = CaptureDiagnostics;
   1779   if (PrecompilePreamble)
   1780     AST->PreambleRebuildCounter = 2;
   1781   AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
   1782   AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
   1783   AST->IncludeBriefCommentsInCodeCompletion
   1784     = IncludeBriefCommentsInCodeCompletion;
   1785 
   1786   // Recover resources if we crash before exiting this method.
   1787   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
   1788     ASTUnitCleanup(OwnAST.get());
   1789   llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
   1790     llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
   1791     DiagCleanup(Diags.get());
   1792 
   1793   // We'll manage file buffers ourselves.
   1794   CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
   1795   CI->getFrontendOpts().DisableFree = false;
   1796   ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
   1797 
   1798   // Create the compiler instance to use for building the AST.
   1799   std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
   1800 
   1801   // Recover resources if we crash before exiting this method.
   1802   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
   1803     CICleanup(Clang.get());
   1804 
   1805   Clang->setInvocation(CI);
   1806   AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
   1807 
   1808   // Set up diagnostics, capturing any diagnostics that would
   1809   // otherwise be dropped.
   1810   Clang->setDiagnostics(&AST->getDiagnostics());
   1811 
   1812   // Create the target instance.
   1813   Clang->setTarget(TargetInfo::CreateTargetInfo(
   1814       Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
   1815   if (!Clang->hasTarget())
   1816     return nullptr;
   1817 
   1818   // Inform the target of the language options.
   1819   //
   1820   // FIXME: We shouldn't need to do this, the target should be immutable once
   1821   // created. This complexity should be lifted elsewhere.
   1822   Clang->getTarget().adjust(Clang->getLangOpts());
   1823 
   1824   assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
   1825          "Invocation must have exactly one source file!");
   1826   assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
   1827          "FIXME: AST inputs not yet supported here!");
   1828   assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
   1829          "IR inputs not supported here!");
   1830 
   1831   // Configure the various subsystems.
   1832   AST->TheSema.reset();
   1833   AST->Ctx = nullptr;
   1834   AST->PP = nullptr;
   1835   AST->Reader = nullptr;
   1836 
   1837   // Create a file manager object to provide access to and cache the filesystem.
   1838   Clang->setFileManager(&AST->getFileManager());
   1839 
   1840   // Create the source manager.
   1841   Clang->setSourceManager(&AST->getSourceManager());
   1842 
   1843   ASTFrontendAction *Act = Action;
   1844 
   1845   std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
   1846   if (!Act) {
   1847     TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
   1848     Act = TrackerAct.get();
   1849   }
   1850 
   1851   // Recover resources if we crash before exiting this method.
   1852   llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
   1853     ActCleanup(TrackerAct.get());
   1854 
   1855   if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
   1856     AST->transferASTDataFromCompilerInstance(*Clang);
   1857     if (OwnAST && ErrAST)
   1858       ErrAST->swap(OwnAST);
   1859 
   1860     return nullptr;
   1861   }
   1862 
   1863   if (Persistent && !TrackerAct) {
   1864     Clang->getPreprocessor().addPPCallbacks(
   1865      new MacroDefinitionTrackerPPCallbacks(AST->getCurrentTopLevelHashValue()));
   1866     std::vector<ASTConsumer*> Consumers;
   1867     if (Clang->hasASTConsumer())
   1868       Consumers.push_back(Clang->takeASTConsumer());
   1869     Consumers.push_back(new TopLevelDeclTrackerConsumer(*AST,
   1870                                            AST->getCurrentTopLevelHashValue()));
   1871     Clang->setASTConsumer(new MultiplexConsumer(Consumers));
   1872   }
   1873   if (!Act->Execute()) {
   1874     AST->transferASTDataFromCompilerInstance(*Clang);
   1875     if (OwnAST && ErrAST)
   1876       ErrAST->swap(OwnAST);
   1877 
   1878     return nullptr;
   1879   }
   1880 
   1881   // Steal the created target, context, and preprocessor.
   1882   AST->transferASTDataFromCompilerInstance(*Clang);
   1883 
   1884   Act->EndSourceFile();
   1885 
   1886   if (OwnAST)
   1887     return OwnAST.release();
   1888   else
   1889     return AST;
   1890 }
   1891 
   1892 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
   1893   if (!Invocation)
   1894     return true;
   1895 
   1896   // We'll manage file buffers ourselves.
   1897   Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
   1898   Invocation->getFrontendOpts().DisableFree = false;
   1899   ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
   1900 
   1901   llvm::MemoryBuffer *OverrideMainBuffer = nullptr;
   1902   if (PrecompilePreamble) {
   1903     PreambleRebuildCounter = 2;
   1904     OverrideMainBuffer
   1905       = getMainBufferWithPrecompiledPreamble(*Invocation);
   1906   }
   1907 
   1908   SimpleTimer ParsingTimer(WantTiming);
   1909   ParsingTimer.setOutput("Parsing " + getMainFileName());
   1910 
   1911   // Recover resources if we crash before exiting this method.
   1912   llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
   1913     MemBufferCleanup(OverrideMainBuffer);
   1914 
   1915   return Parse(OverrideMainBuffer);
   1916 }
   1917 
   1918 std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
   1919     CompilerInvocation *CI, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
   1920     bool OnlyLocalDecls, bool CaptureDiagnostics, bool PrecompilePreamble,
   1921     TranslationUnitKind TUKind, bool CacheCodeCompletionResults,
   1922     bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile) {
   1923   // Create the AST unit.
   1924   std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
   1925   ConfigureDiags(Diags, nullptr, nullptr, *AST, CaptureDiagnostics);
   1926   AST->Diagnostics = Diags;
   1927   AST->OnlyLocalDecls = OnlyLocalDecls;
   1928   AST->CaptureDiagnostics = CaptureDiagnostics;
   1929   AST->TUKind = TUKind;
   1930   AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
   1931   AST->IncludeBriefCommentsInCodeCompletion
   1932     = IncludeBriefCommentsInCodeCompletion;
   1933   AST->Invocation = CI;
   1934   AST->FileSystemOpts = CI->getFileSystemOpts();
   1935   IntrusiveRefCntPtr<vfs::FileSystem> VFS =
   1936       createVFSFromCompilerInvocation(*CI, *Diags);
   1937   if (!VFS)
   1938     return nullptr;
   1939   AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
   1940   AST->UserFilesAreVolatile = UserFilesAreVolatile;
   1941 
   1942   // Recover resources if we crash before exiting this method.
   1943   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
   1944     ASTUnitCleanup(AST.get());
   1945   llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
   1946     llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
   1947     DiagCleanup(Diags.get());
   1948 
   1949   if (AST->LoadFromCompilerInvocation(PrecompilePreamble))
   1950     return nullptr;
   1951   return AST;
   1952 }
   1953 
   1954 ASTUnit *ASTUnit::LoadFromCommandLine(
   1955     const char **ArgBegin, const char **ArgEnd,
   1956     IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
   1957     bool OnlyLocalDecls, bool CaptureDiagnostics,
   1958     ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
   1959     bool PrecompilePreamble, TranslationUnitKind TUKind,
   1960     bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
   1961     bool AllowPCHWithCompilerErrors, bool SkipFunctionBodies,
   1962     bool UserFilesAreVolatile, bool ForSerialization,
   1963     std::unique_ptr<ASTUnit> *ErrAST) {
   1964   if (!Diags.get()) {
   1965     // No diagnostics engine was provided, so create our own diagnostics object
   1966     // with the default options.
   1967     Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions());
   1968   }
   1969 
   1970   SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
   1971 
   1972   IntrusiveRefCntPtr<CompilerInvocation> CI;
   1973 
   1974   {
   1975 
   1976     CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
   1977                                       StoredDiagnostics);
   1978 
   1979     CI = clang::createInvocationFromCommandLine(
   1980                                            llvm::makeArrayRef(ArgBegin, ArgEnd),
   1981                                            Diags);
   1982     if (!CI)
   1983       return nullptr;
   1984   }
   1985 
   1986   // Override any files that need remapping
   1987   for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
   1988     CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
   1989                                               RemappedFiles[I].second);
   1990   }
   1991   PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
   1992   PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
   1993   PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
   1994 
   1995   // Override the resources path.
   1996   CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
   1997 
   1998   CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
   1999 
   2000   // Create the AST unit.
   2001   std::unique_ptr<ASTUnit> AST;
   2002   AST.reset(new ASTUnit(false));
   2003   ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
   2004   AST->Diagnostics = Diags;
   2005   Diags = nullptr; // Zero out now to ease cleanup during crash recovery.
   2006   AST->FileSystemOpts = CI->getFileSystemOpts();
   2007   IntrusiveRefCntPtr<vfs::FileSystem> VFS =
   2008       createVFSFromCompilerInvocation(*CI, *Diags);
   2009   if (!VFS)
   2010     return nullptr;
   2011   AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
   2012   AST->OnlyLocalDecls = OnlyLocalDecls;
   2013   AST->CaptureDiagnostics = CaptureDiagnostics;
   2014   AST->TUKind = TUKind;
   2015   AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
   2016   AST->IncludeBriefCommentsInCodeCompletion
   2017     = IncludeBriefCommentsInCodeCompletion;
   2018   AST->UserFilesAreVolatile = UserFilesAreVolatile;
   2019   AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
   2020   AST->StoredDiagnostics.swap(StoredDiagnostics);
   2021   AST->Invocation = CI;
   2022   if (ForSerialization)
   2023     AST->WriterData.reset(new ASTWriterData());
   2024   CI = nullptr; // Zero out now to ease cleanup during crash recovery.
   2025 
   2026   // Recover resources if we crash before exiting this method.
   2027   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
   2028     ASTUnitCleanup(AST.get());
   2029 
   2030   if (AST->LoadFromCompilerInvocation(PrecompilePreamble)) {
   2031     // Some error occurred, if caller wants to examine diagnostics, pass it the
   2032     // ASTUnit.
   2033     if (ErrAST) {
   2034       AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
   2035       ErrAST->swap(AST);
   2036     }
   2037     return nullptr;
   2038   }
   2039 
   2040   return AST.release();
   2041 }
   2042 
   2043 bool ASTUnit::Reparse(ArrayRef<RemappedFile> RemappedFiles) {
   2044   if (!Invocation)
   2045     return true;
   2046 
   2047   clearFileLevelDecls();
   2048 
   2049   SimpleTimer ParsingTimer(WantTiming);
   2050   ParsingTimer.setOutput("Reparsing " + getMainFileName());
   2051 
   2052   // Remap files.
   2053   PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
   2054   for (const auto &RB : PPOpts.RemappedFileBuffers)
   2055     delete RB.second;
   2056 
   2057   Invocation->getPreprocessorOpts().clearRemappedFiles();
   2058   for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
   2059     Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
   2060                                                       RemappedFiles[I].second);
   2061   }
   2062 
   2063   // If we have a preamble file lying around, or if we might try to
   2064   // build a precompiled preamble, do so now.
   2065   llvm::MemoryBuffer *OverrideMainBuffer = nullptr;
   2066   if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
   2067     OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
   2068 
   2069   // Clear out the diagnostics state.
   2070   getDiagnostics().Reset();
   2071   ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
   2072   if (OverrideMainBuffer)
   2073     getDiagnostics().setNumWarnings(NumWarningsInPreamble);
   2074 
   2075   // Parse the sources
   2076   bool Result = Parse(OverrideMainBuffer);
   2077 
   2078   // If we're caching global code-completion results, and the top-level
   2079   // declarations have changed, clear out the code-completion cache.
   2080   if (!Result && ShouldCacheCodeCompletionResults &&
   2081       CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
   2082     CacheCodeCompletionResults();
   2083 
   2084   // We now need to clear out the completion info related to this translation
   2085   // unit; it'll be recreated if necessary.
   2086   CCTUInfo.reset();
   2087 
   2088   return Result;
   2089 }
   2090 
   2091 //----------------------------------------------------------------------------//
   2092 // Code completion
   2093 //----------------------------------------------------------------------------//
   2094 
   2095 namespace {
   2096   /// \brief Code completion consumer that combines the cached code-completion
   2097   /// results from an ASTUnit with the code-completion results provided to it,
   2098   /// then passes the result on to
   2099   class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
   2100     uint64_t NormalContexts;
   2101     ASTUnit &AST;
   2102     CodeCompleteConsumer &Next;
   2103 
   2104   public:
   2105     AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
   2106                                   const CodeCompleteOptions &CodeCompleteOpts)
   2107       : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
   2108         AST(AST), Next(Next)
   2109     {
   2110       // Compute the set of contexts in which we will look when we don't have
   2111       // any information about the specific context.
   2112       NormalContexts
   2113         = (1LL << CodeCompletionContext::CCC_TopLevel)
   2114         | (1LL << CodeCompletionContext::CCC_ObjCInterface)
   2115         | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
   2116         | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
   2117         | (1LL << CodeCompletionContext::CCC_Statement)
   2118         | (1LL << CodeCompletionContext::CCC_Expression)
   2119         | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
   2120         | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
   2121         | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
   2122         | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
   2123         | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
   2124         | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
   2125         | (1LL << CodeCompletionContext::CCC_Recovery);
   2126 
   2127       if (AST.getASTContext().getLangOpts().CPlusPlus)
   2128         NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
   2129                        |  (1LL << CodeCompletionContext::CCC_UnionTag)
   2130                        |  (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
   2131     }
   2132 
   2133     void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
   2134                                     CodeCompletionResult *Results,
   2135                                     unsigned NumResults) override;
   2136 
   2137     void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
   2138                                    OverloadCandidate *Candidates,
   2139                                    unsigned NumCandidates) override {
   2140       Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
   2141     }
   2142 
   2143     CodeCompletionAllocator &getAllocator() override {
   2144       return Next.getAllocator();
   2145     }
   2146 
   2147     CodeCompletionTUInfo &getCodeCompletionTUInfo() override {
   2148       return Next.getCodeCompletionTUInfo();
   2149     }
   2150   };
   2151 }
   2152 
   2153 /// \brief Helper function that computes which global names are hidden by the
   2154 /// local code-completion results.
   2155 static void CalculateHiddenNames(const CodeCompletionContext &Context,
   2156                                  CodeCompletionResult *Results,
   2157                                  unsigned NumResults,
   2158                                  ASTContext &Ctx,
   2159                           llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
   2160   bool OnlyTagNames = false;
   2161   switch (Context.getKind()) {
   2162   case CodeCompletionContext::CCC_Recovery:
   2163   case CodeCompletionContext::CCC_TopLevel:
   2164   case CodeCompletionContext::CCC_ObjCInterface:
   2165   case CodeCompletionContext::CCC_ObjCImplementation:
   2166   case CodeCompletionContext::CCC_ObjCIvarList:
   2167   case CodeCompletionContext::CCC_ClassStructUnion:
   2168   case CodeCompletionContext::CCC_Statement:
   2169   case CodeCompletionContext::CCC_Expression:
   2170   case CodeCompletionContext::CCC_ObjCMessageReceiver:
   2171   case CodeCompletionContext::CCC_DotMemberAccess:
   2172   case CodeCompletionContext::CCC_ArrowMemberAccess:
   2173   case CodeCompletionContext::CCC_ObjCPropertyAccess:
   2174   case CodeCompletionContext::CCC_Namespace:
   2175   case CodeCompletionContext::CCC_Type:
   2176   case CodeCompletionContext::CCC_Name:
   2177   case CodeCompletionContext::CCC_PotentiallyQualifiedName:
   2178   case CodeCompletionContext::CCC_ParenthesizedExpression:
   2179   case CodeCompletionContext::CCC_ObjCInterfaceName:
   2180     break;
   2181 
   2182   case CodeCompletionContext::CCC_EnumTag:
   2183   case CodeCompletionContext::CCC_UnionTag:
   2184   case CodeCompletionContext::CCC_ClassOrStructTag:
   2185     OnlyTagNames = true;
   2186     break;
   2187 
   2188   case CodeCompletionContext::CCC_ObjCProtocolName:
   2189   case CodeCompletionContext::CCC_MacroName:
   2190   case CodeCompletionContext::CCC_MacroNameUse:
   2191   case CodeCompletionContext::CCC_PreprocessorExpression:
   2192   case CodeCompletionContext::CCC_PreprocessorDirective:
   2193   case CodeCompletionContext::CCC_NaturalLanguage:
   2194   case CodeCompletionContext::CCC_SelectorName:
   2195   case CodeCompletionContext::CCC_TypeQualifiers:
   2196   case CodeCompletionContext::CCC_Other:
   2197   case CodeCompletionContext::CCC_OtherWithMacros:
   2198   case CodeCompletionContext::CCC_ObjCInstanceMessage:
   2199   case CodeCompletionContext::CCC_ObjCClassMessage:
   2200   case CodeCompletionContext::CCC_ObjCCategoryName:
   2201     // We're looking for nothing, or we're looking for names that cannot
   2202     // be hidden.
   2203     return;
   2204   }
   2205 
   2206   typedef CodeCompletionResult Result;
   2207   for (unsigned I = 0; I != NumResults; ++I) {
   2208     if (Results[I].Kind != Result::RK_Declaration)
   2209       continue;
   2210 
   2211     unsigned IDNS
   2212       = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
   2213 
   2214     bool Hiding = false;
   2215     if (OnlyTagNames)
   2216       Hiding = (IDNS & Decl::IDNS_Tag);
   2217     else {
   2218       unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
   2219                              Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
   2220                              Decl::IDNS_NonMemberOperator);
   2221       if (Ctx.getLangOpts().CPlusPlus)
   2222         HiddenIDNS |= Decl::IDNS_Tag;
   2223       Hiding = (IDNS & HiddenIDNS);
   2224     }
   2225 
   2226     if (!Hiding)
   2227       continue;
   2228 
   2229     DeclarationName Name = Results[I].Declaration->getDeclName();
   2230     if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
   2231       HiddenNames.insert(Identifier->getName());
   2232     else
   2233       HiddenNames.insert(Name.getAsString());
   2234   }
   2235 }
   2236 
   2237 
   2238 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
   2239                                             CodeCompletionContext Context,
   2240                                             CodeCompletionResult *Results,
   2241                                             unsigned NumResults) {
   2242   // Merge the results we were given with the results we cached.
   2243   bool AddedResult = false;
   2244   uint64_t InContexts =
   2245       Context.getKind() == CodeCompletionContext::CCC_Recovery
   2246         ? NormalContexts : (1LL << Context.getKind());
   2247   // Contains the set of names that are hidden by "local" completion results.
   2248   llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
   2249   typedef CodeCompletionResult Result;
   2250   SmallVector<Result, 8> AllResults;
   2251   for (ASTUnit::cached_completion_iterator
   2252             C = AST.cached_completion_begin(),
   2253          CEnd = AST.cached_completion_end();
   2254        C != CEnd; ++C) {
   2255     // If the context we are in matches any of the contexts we are
   2256     // interested in, we'll add this result.
   2257     if ((C->ShowInContexts & InContexts) == 0)
   2258       continue;
   2259 
   2260     // If we haven't added any results previously, do so now.
   2261     if (!AddedResult) {
   2262       CalculateHiddenNames(Context, Results, NumResults, S.Context,
   2263                            HiddenNames);
   2264       AllResults.insert(AllResults.end(), Results, Results + NumResults);
   2265       AddedResult = true;
   2266     }
   2267 
   2268     // Determine whether this global completion result is hidden by a local
   2269     // completion result. If so, skip it.
   2270     if (C->Kind != CXCursor_MacroDefinition &&
   2271         HiddenNames.count(C->Completion->getTypedText()))
   2272       continue;
   2273 
   2274     // Adjust priority based on similar type classes.
   2275     unsigned Priority = C->Priority;
   2276     CodeCompletionString *Completion = C->Completion;
   2277     if (!Context.getPreferredType().isNull()) {
   2278       if (C->Kind == CXCursor_MacroDefinition) {
   2279         Priority = getMacroUsagePriority(C->Completion->getTypedText(),
   2280                                          S.getLangOpts(),
   2281                                Context.getPreferredType()->isAnyPointerType());
   2282       } else if (C->Type) {
   2283         CanQualType Expected
   2284           = S.Context.getCanonicalType(
   2285                                Context.getPreferredType().getUnqualifiedType());
   2286         SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
   2287         if (ExpectedSTC == C->TypeClass) {
   2288           // We know this type is similar; check for an exact match.
   2289           llvm::StringMap<unsigned> &CachedCompletionTypes
   2290             = AST.getCachedCompletionTypes();
   2291           llvm::StringMap<unsigned>::iterator Pos
   2292             = CachedCompletionTypes.find(QualType(Expected).getAsString());
   2293           if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
   2294             Priority /= CCF_ExactTypeMatch;
   2295           else
   2296             Priority /= CCF_SimilarTypeMatch;
   2297         }
   2298       }
   2299     }
   2300 
   2301     // Adjust the completion string, if required.
   2302     if (C->Kind == CXCursor_MacroDefinition &&
   2303         Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
   2304       // Create a new code-completion string that just contains the
   2305       // macro name, without its arguments.
   2306       CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
   2307                                     CCP_CodePattern, C->Availability);
   2308       Builder.AddTypedTextChunk(C->Completion->getTypedText());
   2309       Priority = CCP_CodePattern;
   2310       Completion = Builder.TakeString();
   2311     }
   2312 
   2313     AllResults.push_back(Result(Completion, Priority, C->Kind,
   2314                                 C->Availability));
   2315   }
   2316 
   2317   // If we did not add any cached completion results, just forward the
   2318   // results we were given to the next consumer.
   2319   if (!AddedResult) {
   2320     Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
   2321     return;
   2322   }
   2323 
   2324   Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
   2325                                   AllResults.size());
   2326 }
   2327 
   2328 
   2329 
   2330 void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
   2331                            ArrayRef<RemappedFile> RemappedFiles,
   2332                            bool IncludeMacros,
   2333                            bool IncludeCodePatterns,
   2334                            bool IncludeBriefComments,
   2335                            CodeCompleteConsumer &Consumer,
   2336                            DiagnosticsEngine &Diag, LangOptions &LangOpts,
   2337                            SourceManager &SourceMgr, FileManager &FileMgr,
   2338                    SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
   2339              SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
   2340   if (!Invocation)
   2341     return;
   2342 
   2343   SimpleTimer CompletionTimer(WantTiming);
   2344   CompletionTimer.setOutput("Code completion @ " + File + ":" +
   2345                             Twine(Line) + ":" + Twine(Column));
   2346 
   2347   IntrusiveRefCntPtr<CompilerInvocation>
   2348     CCInvocation(new CompilerInvocation(*Invocation));
   2349 
   2350   FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
   2351   CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
   2352   PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
   2353 
   2354   CodeCompleteOpts.IncludeMacros = IncludeMacros &&
   2355                                    CachedCompletionResults.empty();
   2356   CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
   2357   CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
   2358   CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
   2359 
   2360   assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
   2361 
   2362   FrontendOpts.CodeCompletionAt.FileName = File;
   2363   FrontendOpts.CodeCompletionAt.Line = Line;
   2364   FrontendOpts.CodeCompletionAt.Column = Column;
   2365 
   2366   // Set the language options appropriately.
   2367   LangOpts = *CCInvocation->getLangOpts();
   2368 
   2369   std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
   2370 
   2371   // Recover resources if we crash before exiting this method.
   2372   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
   2373     CICleanup(Clang.get());
   2374 
   2375   Clang->setInvocation(&*CCInvocation);
   2376   OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
   2377 
   2378   // Set up diagnostics, capturing any diagnostics produced.
   2379   Clang->setDiagnostics(&Diag);
   2380   CaptureDroppedDiagnostics Capture(true,
   2381                                     Clang->getDiagnostics(),
   2382                                     StoredDiagnostics);
   2383   ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
   2384 
   2385   // Create the target instance.
   2386   Clang->setTarget(TargetInfo::CreateTargetInfo(
   2387       Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
   2388   if (!Clang->hasTarget()) {
   2389     Clang->setInvocation(nullptr);
   2390     return;
   2391   }
   2392 
   2393   // Inform the target of the language options.
   2394   //
   2395   // FIXME: We shouldn't need to do this, the target should be immutable once
   2396   // created. This complexity should be lifted elsewhere.
   2397   Clang->getTarget().adjust(Clang->getLangOpts());
   2398 
   2399   assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
   2400          "Invocation must have exactly one source file!");
   2401   assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
   2402          "FIXME: AST inputs not yet supported here!");
   2403   assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
   2404          "IR inputs not support here!");
   2405 
   2406 
   2407   // Use the source and file managers that we were given.
   2408   Clang->setFileManager(&FileMgr);
   2409   Clang->setSourceManager(&SourceMgr);
   2410 
   2411   // Remap files.
   2412   PreprocessorOpts.clearRemappedFiles();
   2413   PreprocessorOpts.RetainRemappedFileBuffers = true;
   2414   for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
   2415     PreprocessorOpts.addRemappedFile(RemappedFiles[I].first,
   2416                                      RemappedFiles[I].second);
   2417     OwnedBuffers.push_back(RemappedFiles[I].second);
   2418   }
   2419 
   2420   // Use the code completion consumer we were given, but adding any cached
   2421   // code-completion results.
   2422   AugmentedCodeCompleteConsumer *AugmentedConsumer
   2423     = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
   2424   Clang->setCodeCompletionConsumer(AugmentedConsumer);
   2425 
   2426   // If we have a precompiled preamble, try to use it. We only allow
   2427   // the use of the precompiled preamble if we're if the completion
   2428   // point is within the main file, after the end of the precompiled
   2429   // preamble.
   2430   llvm::MemoryBuffer *OverrideMainBuffer = nullptr;
   2431   if (!getPreambleFile(this).empty()) {
   2432     std::string CompleteFilePath(File);
   2433     llvm::sys::fs::UniqueID CompleteFileID;
   2434 
   2435     if (!llvm::sys::fs::getUniqueID(CompleteFilePath, CompleteFileID)) {
   2436       std::string MainPath(OriginalSourceFile);
   2437       llvm::sys::fs::UniqueID MainID;
   2438       if (!llvm::sys::fs::getUniqueID(MainPath, MainID)) {
   2439         if (CompleteFileID == MainID && Line > 1)
   2440           OverrideMainBuffer
   2441             = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
   2442                                                    Line - 1);
   2443       }
   2444     }
   2445   }
   2446 
   2447   // If the main file has been overridden due to the use of a preamble,
   2448   // make that override happen and introduce the preamble.
   2449   if (OverrideMainBuffer) {
   2450     PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
   2451     PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
   2452     PreprocessorOpts.PrecompiledPreambleBytes.second
   2453                                                     = PreambleEndsAtStartOfLine;
   2454     PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
   2455     PreprocessorOpts.DisablePCHValidation = true;
   2456 
   2457     OwnedBuffers.push_back(OverrideMainBuffer);
   2458   } else {
   2459     PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
   2460     PreprocessorOpts.PrecompiledPreambleBytes.second = false;
   2461   }
   2462 
   2463   // Disable the preprocessing record if modules are not enabled.
   2464   if (!Clang->getLangOpts().Modules)
   2465     PreprocessorOpts.DetailedRecord = false;
   2466 
   2467   std::unique_ptr<SyntaxOnlyAction> Act;
   2468   Act.reset(new SyntaxOnlyAction);
   2469   if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
   2470     Act->Execute();
   2471     Act->EndSourceFile();
   2472   }
   2473 }
   2474 
   2475 bool ASTUnit::Save(StringRef File) {
   2476   if (HadModuleLoaderFatalFailure)
   2477     return true;
   2478 
   2479   // Write to a temporary file and later rename it to the actual file, to avoid
   2480   // possible race conditions.
   2481   SmallString<128> TempPath;
   2482   TempPath = File;
   2483   TempPath += "-%%%%%%%%";
   2484   int fd;
   2485   if (llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath))
   2486     return true;
   2487 
   2488   // FIXME: Can we somehow regenerate the stat cache here, or do we need to
   2489   // unconditionally create a stat cache when we parse the file?
   2490   llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
   2491 
   2492   serialize(Out);
   2493   Out.close();
   2494   if (Out.has_error()) {
   2495     Out.clear_error();
   2496     return true;
   2497   }
   2498 
   2499   if (llvm::sys::fs::rename(TempPath.str(), File)) {
   2500     llvm::sys::fs::remove(TempPath.str());
   2501     return true;
   2502   }
   2503 
   2504   return false;
   2505 }
   2506 
   2507 static bool serializeUnit(ASTWriter &Writer,
   2508                           SmallVectorImpl<char> &Buffer,
   2509                           Sema &S,
   2510                           bool hasErrors,
   2511                           raw_ostream &OS) {
   2512   Writer.WriteAST(S, std::string(), nullptr, "", hasErrors);
   2513 
   2514   // Write the generated bitstream to "Out".
   2515   if (!Buffer.empty())
   2516     OS.write(Buffer.data(), Buffer.size());
   2517 
   2518   return false;
   2519 }
   2520 
   2521 bool ASTUnit::serialize(raw_ostream &OS) {
   2522   bool hasErrors = getDiagnostics().hasErrorOccurred();
   2523 
   2524   if (WriterData)
   2525     return serializeUnit(WriterData->Writer, WriterData->Buffer,
   2526                          getSema(), hasErrors, OS);
   2527 
   2528   SmallString<128> Buffer;
   2529   llvm::BitstreamWriter Stream(Buffer);
   2530   ASTWriter Writer(Stream);
   2531   return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
   2532 }
   2533 
   2534 typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
   2535 
   2536 void ASTUnit::TranslateStoredDiagnostics(
   2537                           FileManager &FileMgr,
   2538                           SourceManager &SrcMgr,
   2539                           const SmallVectorImpl<StandaloneDiagnostic> &Diags,
   2540                           SmallVectorImpl<StoredDiagnostic> &Out) {
   2541   // Map the standalone diagnostic into the new source manager. We also need to
   2542   // remap all the locations to the new view. This includes the diag location,
   2543   // any associated source ranges, and the source ranges of associated fix-its.
   2544   // FIXME: There should be a cleaner way to do this.
   2545 
   2546   SmallVector<StoredDiagnostic, 4> Result;
   2547   Result.reserve(Diags.size());
   2548   for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
   2549     // Rebuild the StoredDiagnostic.
   2550     const StandaloneDiagnostic &SD = Diags[I];
   2551     if (SD.Filename.empty())
   2552       continue;
   2553     const FileEntry *FE = FileMgr.getFile(SD.Filename);
   2554     if (!FE)
   2555       continue;
   2556     FileID FID = SrcMgr.translateFile(FE);
   2557     SourceLocation FileLoc = SrcMgr.getLocForStartOfFile(FID);
   2558     if (FileLoc.isInvalid())
   2559       continue;
   2560     SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset);
   2561     FullSourceLoc Loc(L, SrcMgr);
   2562 
   2563     SmallVector<CharSourceRange, 4> Ranges;
   2564     Ranges.reserve(SD.Ranges.size());
   2565     for (std::vector<std::pair<unsigned, unsigned> >::const_iterator
   2566            I = SD.Ranges.begin(), E = SD.Ranges.end(); I != E; ++I) {
   2567       SourceLocation BL = FileLoc.getLocWithOffset((*I).first);
   2568       SourceLocation EL = FileLoc.getLocWithOffset((*I).second);
   2569       Ranges.push_back(CharSourceRange::getCharRange(BL, EL));
   2570     }
   2571 
   2572     SmallVector<FixItHint, 2> FixIts;
   2573     FixIts.reserve(SD.FixIts.size());
   2574     for (std::vector<StandaloneFixIt>::const_iterator
   2575            I = SD.FixIts.begin(), E = SD.FixIts.end();
   2576          I != E; ++I) {
   2577       FixIts.push_back(FixItHint());
   2578       FixItHint &FH = FixIts.back();
   2579       FH.CodeToInsert = I->CodeToInsert;
   2580       SourceLocation BL = FileLoc.getLocWithOffset(I->RemoveRange.first);
   2581       SourceLocation EL = FileLoc.getLocWithOffset(I->RemoveRange.second);
   2582       FH.RemoveRange = CharSourceRange::getCharRange(BL, EL);
   2583     }
   2584 
   2585     Result.push_back(StoredDiagnostic(SD.Level, SD.ID,
   2586                                       SD.Message, Loc, Ranges, FixIts));
   2587   }
   2588   Result.swap(Out);
   2589 }
   2590 
   2591 void ASTUnit::addFileLevelDecl(Decl *D) {
   2592   assert(D);
   2593 
   2594   // We only care about local declarations.
   2595   if (D->isFromASTFile())
   2596     return;
   2597 
   2598   SourceManager &SM = *SourceMgr;
   2599   SourceLocation Loc = D->getLocation();
   2600   if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
   2601     return;
   2602 
   2603   // We only keep track of the file-level declarations of each file.
   2604   if (!D->getLexicalDeclContext()->isFileContext())
   2605     return;
   2606 
   2607   SourceLocation FileLoc = SM.getFileLoc(Loc);
   2608   assert(SM.isLocalSourceLocation(FileLoc));
   2609   FileID FID;
   2610   unsigned Offset;
   2611   std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
   2612   if (FID.isInvalid())
   2613     return;
   2614 
   2615   LocDeclsTy *&Decls = FileDecls[FID];
   2616   if (!Decls)
   2617     Decls = new LocDeclsTy();
   2618 
   2619   std::pair<unsigned, Decl *> LocDecl(Offset, D);
   2620 
   2621   if (Decls->empty() || Decls->back().first <= Offset) {
   2622     Decls->push_back(LocDecl);
   2623     return;
   2624   }
   2625 
   2626   LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
   2627                                             LocDecl, llvm::less_first());
   2628 
   2629   Decls->insert(I, LocDecl);
   2630 }
   2631 
   2632 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
   2633                                   SmallVectorImpl<Decl *> &Decls) {
   2634   if (File.isInvalid())
   2635     return;
   2636 
   2637   if (SourceMgr->isLoadedFileID(File)) {
   2638     assert(Ctx->getExternalSource() && "No external source!");
   2639     return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
   2640                                                          Decls);
   2641   }
   2642 
   2643   FileDeclsTy::iterator I = FileDecls.find(File);
   2644   if (I == FileDecls.end())
   2645     return;
   2646 
   2647   LocDeclsTy &LocDecls = *I->second;
   2648   if (LocDecls.empty())
   2649     return;
   2650 
   2651   LocDeclsTy::iterator BeginIt =
   2652       std::lower_bound(LocDecls.begin(), LocDecls.end(),
   2653                        std::make_pair(Offset, (Decl *)nullptr),
   2654                        llvm::less_first());
   2655   if (BeginIt != LocDecls.begin())
   2656     --BeginIt;
   2657 
   2658   // If we are pointing at a top-level decl inside an objc container, we need
   2659   // to backtrack until we find it otherwise we will fail to report that the
   2660   // region overlaps with an objc container.
   2661   while (BeginIt != LocDecls.begin() &&
   2662          BeginIt->second->isTopLevelDeclInObjCContainer())
   2663     --BeginIt;
   2664 
   2665   LocDeclsTy::iterator EndIt = std::upper_bound(
   2666       LocDecls.begin(), LocDecls.end(),
   2667       std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first());
   2668   if (EndIt != LocDecls.end())
   2669     ++EndIt;
   2670 
   2671   for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
   2672     Decls.push_back(DIt->second);
   2673 }
   2674 
   2675 SourceLocation ASTUnit::getLocation(const FileEntry *File,
   2676                                     unsigned Line, unsigned Col) const {
   2677   const SourceManager &SM = getSourceManager();
   2678   SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
   2679   return SM.getMacroArgExpandedLocation(Loc);
   2680 }
   2681 
   2682 SourceLocation ASTUnit::getLocation(const FileEntry *File,
   2683                                     unsigned Offset) const {
   2684   const SourceManager &SM = getSourceManager();
   2685   SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
   2686   return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
   2687 }
   2688 
   2689 /// \brief If \arg Loc is a loaded location from the preamble, returns
   2690 /// the corresponding local location of the main file, otherwise it returns
   2691 /// \arg Loc.
   2692 SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
   2693   FileID PreambleID;
   2694   if (SourceMgr)
   2695     PreambleID = SourceMgr->getPreambleFileID();
   2696 
   2697   if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
   2698     return Loc;
   2699 
   2700   unsigned Offs;
   2701   if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
   2702     SourceLocation FileLoc
   2703         = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
   2704     return FileLoc.getLocWithOffset(Offs);
   2705   }
   2706 
   2707   return Loc;
   2708 }
   2709 
   2710 /// \brief If \arg Loc is a local location of the main file but inside the
   2711 /// preamble chunk, returns the corresponding loaded location from the
   2712 /// preamble, otherwise it returns \arg Loc.
   2713 SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
   2714   FileID PreambleID;
   2715   if (SourceMgr)
   2716     PreambleID = SourceMgr->getPreambleFileID();
   2717 
   2718   if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
   2719     return Loc;
   2720 
   2721   unsigned Offs;
   2722   if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
   2723       Offs < Preamble.size()) {
   2724     SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
   2725     return FileLoc.getLocWithOffset(Offs);
   2726   }
   2727 
   2728   return Loc;
   2729 }
   2730 
   2731 bool ASTUnit::isInPreambleFileID(SourceLocation Loc) {
   2732   FileID FID;
   2733   if (SourceMgr)
   2734     FID = SourceMgr->getPreambleFileID();
   2735 
   2736   if (Loc.isInvalid() || FID.isInvalid())
   2737     return false;
   2738 
   2739   return SourceMgr->isInFileID(Loc, FID);
   2740 }
   2741 
   2742 bool ASTUnit::isInMainFileID(SourceLocation Loc) {
   2743   FileID FID;
   2744   if (SourceMgr)
   2745     FID = SourceMgr->getMainFileID();
   2746 
   2747   if (Loc.isInvalid() || FID.isInvalid())
   2748     return false;
   2749 
   2750   return SourceMgr->isInFileID(Loc, FID);
   2751 }
   2752 
   2753 SourceLocation ASTUnit::getEndOfPreambleFileID() {
   2754   FileID FID;
   2755   if (SourceMgr)
   2756     FID = SourceMgr->getPreambleFileID();
   2757 
   2758   if (FID.isInvalid())
   2759     return SourceLocation();
   2760 
   2761   return SourceMgr->getLocForEndOfFile(FID);
   2762 }
   2763 
   2764 SourceLocation ASTUnit::getStartOfMainFileID() {
   2765   FileID FID;
   2766   if (SourceMgr)
   2767     FID = SourceMgr->getMainFileID();
   2768 
   2769   if (FID.isInvalid())
   2770     return SourceLocation();
   2771 
   2772   return SourceMgr->getLocForStartOfFile(FID);
   2773 }
   2774 
   2775 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
   2776 ASTUnit::getLocalPreprocessingEntities() const {
   2777   if (isMainFileAST()) {
   2778     serialization::ModuleFile &
   2779       Mod = Reader->getModuleManager().getPrimaryModule();
   2780     return Reader->getModulePreprocessedEntities(Mod);
   2781   }
   2782 
   2783   if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
   2784     return std::make_pair(PPRec->local_begin(), PPRec->local_end());
   2785 
   2786   return std::make_pair(PreprocessingRecord::iterator(),
   2787                         PreprocessingRecord::iterator());
   2788 }
   2789 
   2790 bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
   2791   if (isMainFileAST()) {
   2792     serialization::ModuleFile &
   2793       Mod = Reader->getModuleManager().getPrimaryModule();
   2794     ASTReader::ModuleDeclIterator MDI, MDE;
   2795     std::tie(MDI, MDE) = Reader->getModuleFileLevelDecls(Mod);
   2796     for (; MDI != MDE; ++MDI) {
   2797       if (!Fn(context, *MDI))
   2798         return false;
   2799     }
   2800 
   2801     return true;
   2802   }
   2803 
   2804   for (ASTUnit::top_level_iterator TL = top_level_begin(),
   2805                                 TLEnd = top_level_end();
   2806          TL != TLEnd; ++TL) {
   2807     if (!Fn(context, *TL))
   2808       return false;
   2809   }
   2810 
   2811   return true;
   2812 }
   2813 
   2814 namespace {
   2815 struct PCHLocatorInfo {
   2816   serialization::ModuleFile *Mod;
   2817   PCHLocatorInfo() : Mod(nullptr) {}
   2818 };
   2819 }
   2820 
   2821 static bool PCHLocator(serialization::ModuleFile &M, void *UserData) {
   2822   PCHLocatorInfo &Info = *static_cast<PCHLocatorInfo*>(UserData);
   2823   switch (M.Kind) {
   2824   case serialization::MK_Module:
   2825     return true; // skip dependencies.
   2826   case serialization::MK_PCH:
   2827     Info.Mod = &M;
   2828     return true; // found it.
   2829   case serialization::MK_Preamble:
   2830     return false; // look in dependencies.
   2831   case serialization::MK_MainFile:
   2832     return false; // look in dependencies.
   2833   }
   2834 
   2835   return true;
   2836 }
   2837 
   2838 const FileEntry *ASTUnit::getPCHFile() {
   2839   if (!Reader)
   2840     return nullptr;
   2841 
   2842   PCHLocatorInfo Info;
   2843   Reader->getModuleManager().visit(PCHLocator, &Info);
   2844   if (Info.Mod)
   2845     return Info.Mod->File;
   2846 
   2847   return nullptr;
   2848 }
   2849 
   2850 bool ASTUnit::isModuleFile() {
   2851   return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
   2852 }
   2853 
   2854 void ASTUnit::PreambleData::countLines() const {
   2855   NumLines = 0;
   2856   if (empty())
   2857     return;
   2858 
   2859   for (std::vector<char>::const_iterator
   2860          I = Buffer.begin(), E = Buffer.end(); I != E; ++I) {
   2861     if (*I == '\n')
   2862       ++NumLines;
   2863   }
   2864   if (Buffer.back() != '\n')
   2865     ++NumLines;
   2866 }
   2867 
   2868 #ifndef NDEBUG
   2869 ASTUnit::ConcurrencyState::ConcurrencyState() {
   2870   Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
   2871 }
   2872 
   2873 ASTUnit::ConcurrencyState::~ConcurrencyState() {
   2874   delete static_cast<llvm::sys::MutexImpl *>(Mutex);
   2875 }
   2876 
   2877 void ASTUnit::ConcurrencyState::start() {
   2878   bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
   2879   assert(acquired && "Concurrent access to ASTUnit!");
   2880 }
   2881 
   2882 void ASTUnit::ConcurrencyState::finish() {
   2883   static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
   2884 }
   2885 
   2886 #else // NDEBUG
   2887 
   2888 ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = 0; }
   2889 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
   2890 void ASTUnit::ConcurrencyState::start() {}
   2891 void ASTUnit::ConcurrencyState::finish() {}
   2892 
   2893 #endif
   2894