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