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