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