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