1 //===- ASTUnit.cpp - ASTUnit utility --------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // ASTUnit Implementation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/CommentCommandTraits.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclGroup.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "clang/AST/DeclarationName.h"
25 #include "clang/AST/ExternalASTSource.h"
26 #include "clang/AST/PrettyPrinter.h"
27 #include "clang/AST/Type.h"
28 #include "clang/AST/TypeOrdering.h"
29 #include "clang/Basic/Diagnostic.h"
30 #include "clang/Basic/FileManager.h"
31 #include "clang/Basic/IdentifierTable.h"
32 #include "clang/Basic/LLVM.h"
33 #include "clang/Basic/LangOptions.h"
34 #include "clang/Basic/MemoryBufferCache.h"
35 #include "clang/Basic/Module.h"
36 #include "clang/Basic/SourceLocation.h"
37 #include "clang/Basic/SourceManager.h"
38 #include "clang/Basic/TargetInfo.h"
39 #include "clang/Basic/TargetOptions.h"
40 #include "clang/Basic/VirtualFileSystem.h"
41 #include "clang/Frontend/CompilerInstance.h"
42 #include "clang/Frontend/CompilerInvocation.h"
43 #include "clang/Frontend/FrontendAction.h"
44 #include "clang/Frontend/FrontendActions.h"
45 #include "clang/Frontend/FrontendDiagnostic.h"
46 #include "clang/Frontend/FrontendOptions.h"
47 #include "clang/Frontend/MultiplexConsumer.h"
48 #include "clang/Frontend/PCHContainerOperations.h"
49 #include "clang/Frontend/PrecompiledPreamble.h"
50 #include "clang/Frontend/Utils.h"
51 #include "clang/Lex/HeaderSearch.h"
52 #include "clang/Lex/HeaderSearchOptions.h"
53 #include "clang/Lex/Lexer.h"
54 #include "clang/Lex/PPCallbacks.h"
55 #include "clang/Lex/PreprocessingRecord.h"
56 #include "clang/Lex/Preprocessor.h"
57 #include "clang/Lex/PreprocessorOptions.h"
58 #include "clang/Lex/Token.h"
59 #include "clang/Sema/CodeCompleteConsumer.h"
60 #include "clang/Sema/CodeCompleteOptions.h"
61 #include "clang/Sema/Sema.h"
62 #include "clang/Serialization/ASTBitCodes.h"
63 #include "clang/Serialization/ASTReader.h"
64 #include "clang/Serialization/ASTWriter.h"
65 #include "clang/Serialization/ContinuousRangeMap.h"
66 #include "clang/Serialization/Module.h"
67 #include "llvm/ADT/ArrayRef.h"
68 #include "llvm/ADT/DenseMap.h"
69 #include "llvm/ADT/IntrusiveRefCntPtr.h"
70 #include "llvm/ADT/None.h"
71 #include "llvm/ADT/Optional.h"
72 #include "llvm/ADT/STLExtras.h"
73 #include "llvm/ADT/SmallString.h"
74 #include "llvm/ADT/SmallVector.h"
75 #include "llvm/ADT/StringMap.h"
76 #include "llvm/ADT/StringRef.h"
77 #include "llvm/ADT/StringSet.h"
78 #include "llvm/ADT/Twine.h"
79 #include "llvm/ADT/iterator_range.h"
80 #include "llvm/Bitcode/BitstreamWriter.h"
81 #include "llvm/Support/Allocator.h"
82 #include "llvm/Support/Casting.h"
83 #include "llvm/Support/CrashRecoveryContext.h"
84 #include "llvm/Support/DJB.h"
85 #include "llvm/Support/ErrorHandling.h"
86 #include "llvm/Support/ErrorOr.h"
87 #include "llvm/Support/FileSystem.h"
88 #include "llvm/Support/MemoryBuffer.h"
89 #include "llvm/Support/Mutex.h"
90 #include "llvm/Support/Timer.h"
91 #include "llvm/Support/raw_ostream.h"
92 #include <algorithm>
93 #include <atomic>
94 #include <cassert>
95 #include <cstdint>
96 #include <cstdio>
97 #include <cstdlib>
98 #include <memory>
99 #include <string>
100 #include <tuple>
101 #include <utility>
102 #include <vector>
103 
104 using namespace clang;
105 
106 using llvm::TimeRecord;
107 
108 namespace {
109 
110   class SimpleTimer {
111     bool WantTiming;
112     TimeRecord Start;
113     std::string Output;
114 
115   public:
116     explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
117       if (WantTiming)
118         Start = TimeRecord::getCurrentTime();
119     }
120 
121     ~SimpleTimer() {
122       if (WantTiming) {
123         TimeRecord Elapsed = TimeRecord::getCurrentTime();
124         Elapsed -= Start;
125         llvm::errs() << Output << ':';
126         Elapsed.print(Elapsed, llvm::errs());
127         llvm::errs() << '\n';
128       }
129     }
130 
131     void setOutput(const Twine &Output) {
132       if (WantTiming)
133         this->Output = Output.str();
134     }
135   };
136 
137 } // namespace
138 
139 template <class T>
140 static std::unique_ptr<T> valueOrNull(llvm::ErrorOr<std::unique_ptr<T>> Val) {
141   if (!Val)
142     return nullptr;
143   return std::move(*Val);
144 }
145 
146 template <class T>
147 static bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
148   if (!Val)
149     return false;
150   Output = std::move(*Val);
151   return true;
152 }
153 
154 /// Get a source buffer for \p MainFilePath, handling all file-to-file
155 /// and file-to-buffer remappings inside \p Invocation.
156 static std::unique_ptr<llvm::MemoryBuffer>
157 getBufferForFileHandlingRemapping(const CompilerInvocation &Invocation,
158                                   vfs::FileSystem *VFS,
159                                   StringRef FilePath,
160                                   bool isVolatile) {
161   const auto &PreprocessorOpts = Invocation.getPreprocessorOpts();
162 
163   // Try to determine if the main file has been remapped, either from the
164   // command line (to another file) or directly through the compiler
165   // invocation (to a memory buffer).
166   llvm::MemoryBuffer *Buffer = nullptr;
167   std::unique_ptr<llvm::MemoryBuffer> BufferOwner;
168   auto FileStatus = VFS->status(FilePath);
169   if (FileStatus) {
170     llvm::sys::fs::UniqueID MainFileID = FileStatus->getUniqueID();
171 
172     // Check whether there is a file-file remapping of the main file
173     for (const auto &RF : PreprocessorOpts.RemappedFiles) {
174       std::string MPath(RF.first);
175       auto MPathStatus = VFS->status(MPath);
176       if (MPathStatus) {
177         llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
178         if (MainFileID == MID) {
179           // We found a remapping. Try to load the resulting, remapped source.
180           BufferOwner = valueOrNull(VFS->getBufferForFile(RF.second, -1, true, isVolatile));
181           if (!BufferOwner)
182             return nullptr;
183         }
184       }
185     }
186 
187     // Check whether there is a file-buffer remapping. It supercedes the
188     // file-file remapping.
189     for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
190       std::string MPath(RB.first);
191       auto MPathStatus = VFS->status(MPath);
192       if (MPathStatus) {
193         llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
194         if (MainFileID == MID) {
195           // We found a remapping.
196           BufferOwner.reset();
197           Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
198         }
199       }
200     }
201   }
202 
203   // If the main source file was not remapped, load it now.
204   if (!Buffer && !BufferOwner) {
205     BufferOwner = valueOrNull(VFS->getBufferForFile(FilePath, -1, true, isVolatile));
206     if (!BufferOwner)
207       return nullptr;
208   }
209 
210   if (BufferOwner)
211     return BufferOwner;
212   if (!Buffer)
213     return nullptr;
214   return llvm::MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(), FilePath);
215 }
216 
217 struct ASTUnit::ASTWriterData {
218   SmallString<128> Buffer;
219   llvm::BitstreamWriter Stream;
220   ASTWriter Writer;
221 
222   ASTWriterData(MemoryBufferCache &PCMCache)
223       : Stream(Buffer), Writer(Stream, Buffer, PCMCache, {}) {}
224 };
225 
226 void ASTUnit::clearFileLevelDecls() {
227   llvm::DeleteContainerSeconds(FileDecls);
228 }
229 
230 /// After failing to build a precompiled preamble (due to
231 /// errors in the source that occurs in the preamble), the number of
232 /// reparses during which we'll skip even trying to precompile the
233 /// preamble.
234 const unsigned DefaultPreambleRebuildInterval = 5;
235 
236 /// Tracks the number of ASTUnit objects that are currently active.
237 ///
238 /// Used for debugging purposes only.
239 static std::atomic<unsigned> ActiveASTUnitObjects;
240 
241 ASTUnit::ASTUnit(bool _MainFileIsAST)
242     : MainFileIsAST(_MainFileIsAST), WantTiming(getenv("LIBCLANG_TIMING")),
243       ShouldCacheCodeCompletionResults(false),
244       IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
245       UnsafeToFree(false) {
246   if (getenv("LIBCLANG_OBJTRACKING"))
247     fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects);
248 }
249 
250 ASTUnit::~ASTUnit() {
251   // If we loaded from an AST file, balance out the BeginSourceFile call.
252   if (MainFileIsAST && getDiagnostics().getClient()) {
253     getDiagnostics().getClient()->EndSourceFile();
254   }
255 
256   clearFileLevelDecls();
257 
258   // Free the buffers associated with remapped files. We are required to
259   // perform this operation here because we explicitly request that the
260   // compiler instance *not* free these buffers for each invocation of the
261   // parser.
262   if (Invocation && OwnsRemappedFileBuffers) {
263     PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
264     for (const auto &RB : PPOpts.RemappedFileBuffers)
265       delete RB.second;
266   }
267 
268   ClearCachedCompletionResults();
269 
270   if (getenv("LIBCLANG_OBJTRACKING"))
271     fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects);
272 }
273 
274 void ASTUnit::setPreprocessor(std::shared_ptr<Preprocessor> PP) {
275   this->PP = std::move(PP);
276 }
277 
278 void ASTUnit::enableSourceFileDiagnostics() {
279   assert(getDiagnostics().getClient() && Ctx &&
280       "Bad context for source file");
281   getDiagnostics().getClient()->BeginSourceFile(Ctx->getLangOpts(), PP.get());
282 }
283 
284 /// Determine the set of code-completion contexts in which this
285 /// declaration should be shown.
286 static unsigned getDeclShowContexts(const NamedDecl *ND,
287                                     const LangOptions &LangOpts,
288                                     bool &IsNestedNameSpecifier) {
289   IsNestedNameSpecifier = false;
290 
291   if (isa<UsingShadowDecl>(ND))
292     ND = ND->getUnderlyingDecl();
293   if (!ND)
294     return 0;
295 
296   uint64_t Contexts = 0;
297   if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
298       isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND) ||
299       isa<TypeAliasTemplateDecl>(ND)) {
300     // Types can appear in these contexts.
301     if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
302       Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
303                |  (1LL << CodeCompletionContext::CCC_ObjCIvarList)
304                |  (1LL << CodeCompletionContext::CCC_ClassStructUnion)
305                |  (1LL << CodeCompletionContext::CCC_Statement)
306                |  (1LL << CodeCompletionContext::CCC_Type)
307                |  (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
308 
309     // In C++, types can appear in expressions contexts (for functional casts).
310     if (LangOpts.CPlusPlus)
311       Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
312 
313     // In Objective-C, message sends can send interfaces. In Objective-C++,
314     // all types are available due to functional casts.
315     if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
316       Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
317 
318     // In Objective-C, you can only be a subclass of another Objective-C class
319     if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) {
320       // Objective-C interfaces can be used in a class property expression.
321       if (ID->getDefinition())
322         Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
323       Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName);
324     }
325 
326     // Deal with tag names.
327     if (isa<EnumDecl>(ND)) {
328       Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
329 
330       // Part of the nested-name-specifier in C++0x.
331       if (LangOpts.CPlusPlus11)
332         IsNestedNameSpecifier = true;
333     } else if (const auto *Record = dyn_cast<RecordDecl>(ND)) {
334       if (Record->isUnion())
335         Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
336       else
337         Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
338 
339       if (LangOpts.CPlusPlus)
340         IsNestedNameSpecifier = true;
341     } else if (isa<ClassTemplateDecl>(ND))
342       IsNestedNameSpecifier = true;
343   } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
344     // Values can appear in these contexts.
345     Contexts = (1LL << CodeCompletionContext::CCC_Statement)
346              | (1LL << CodeCompletionContext::CCC_Expression)
347              | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
348              | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
349   } else if (isa<ObjCProtocolDecl>(ND)) {
350     Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName);
351   } else if (isa<ObjCCategoryDecl>(ND)) {
352     Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName);
353   } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
354     Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
355 
356     // Part of the nested-name-specifier.
357     IsNestedNameSpecifier = true;
358   }
359 
360   return Contexts;
361 }
362 
363 void ASTUnit::CacheCodeCompletionResults() {
364   if (!TheSema)
365     return;
366 
367   SimpleTimer Timer(WantTiming);
368   Timer.setOutput("Cache global code completions for " + getMainFileName());
369 
370   // Clear out the previous results.
371   ClearCachedCompletionResults();
372 
373   // Gather the set of global code completions.
374   using Result = CodeCompletionResult;
375   SmallVector<Result, 8> Results;
376   CachedCompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
377   CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
378   TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
379                                        CCTUInfo, Results);
380 
381   // Translate global code completions into cached completions.
382   llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
383   CodeCompletionContext CCContext(CodeCompletionContext::CCC_TopLevel);
384 
385   for (auto &R : Results) {
386     switch (R.Kind) {
387     case Result::RK_Declaration: {
388       bool IsNestedNameSpecifier = false;
389       CachedCodeCompletionResult CachedResult;
390       CachedResult.Completion = R.CreateCodeCompletionString(
391           *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
392           IncludeBriefCommentsInCodeCompletion);
393       CachedResult.ShowInContexts = getDeclShowContexts(
394           R.Declaration, Ctx->getLangOpts(), IsNestedNameSpecifier);
395       CachedResult.Priority = R.Priority;
396       CachedResult.Kind = R.CursorKind;
397       CachedResult.Availability = R.Availability;
398 
399       // Keep track of the type of this completion in an ASTContext-agnostic
400       // way.
401       QualType UsageType = getDeclUsageType(*Ctx, R.Declaration);
402       if (UsageType.isNull()) {
403         CachedResult.TypeClass = STC_Void;
404         CachedResult.Type = 0;
405       } else {
406         CanQualType CanUsageType
407           = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
408         CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
409 
410         // Determine whether we have already seen this type. If so, we save
411         // ourselves the work of formatting the type string by using the
412         // temporary, CanQualType-based hash table to find the associated value.
413         unsigned &TypeValue = CompletionTypes[CanUsageType];
414         if (TypeValue == 0) {
415           TypeValue = CompletionTypes.size();
416           CachedCompletionTypes[QualType(CanUsageType).getAsString()]
417             = TypeValue;
418         }
419 
420         CachedResult.Type = TypeValue;
421       }
422 
423       CachedCompletionResults.push_back(CachedResult);
424 
425       /// Handle nested-name-specifiers in C++.
426       if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier &&
427           !R.StartsNestedNameSpecifier) {
428         // The contexts in which a nested-name-specifier can appear in C++.
429         uint64_t NNSContexts
430           = (1LL << CodeCompletionContext::CCC_TopLevel)
431           | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
432           | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
433           | (1LL << CodeCompletionContext::CCC_Statement)
434           | (1LL << CodeCompletionContext::CCC_Expression)
435           | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
436           | (1LL << CodeCompletionContext::CCC_EnumTag)
437           | (1LL << CodeCompletionContext::CCC_UnionTag)
438           | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
439           | (1LL << CodeCompletionContext::CCC_Type)
440           | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName)
441           | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
442 
443         if (isa<NamespaceDecl>(R.Declaration) ||
444             isa<NamespaceAliasDecl>(R.Declaration))
445           NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
446 
447         if (unsigned RemainingContexts
448                                 = NNSContexts & ~CachedResult.ShowInContexts) {
449           // If there any contexts where this completion can be a
450           // nested-name-specifier but isn't already an option, create a
451           // nested-name-specifier completion.
452           R.StartsNestedNameSpecifier = true;
453           CachedResult.Completion = R.CreateCodeCompletionString(
454               *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
455               IncludeBriefCommentsInCodeCompletion);
456           CachedResult.ShowInContexts = RemainingContexts;
457           CachedResult.Priority = CCP_NestedNameSpecifier;
458           CachedResult.TypeClass = STC_Void;
459           CachedResult.Type = 0;
460           CachedCompletionResults.push_back(CachedResult);
461         }
462       }
463       break;
464     }
465 
466     case Result::RK_Keyword:
467     case Result::RK_Pattern:
468       // Ignore keywords and patterns; we don't care, since they are so
469       // easily regenerated.
470       break;
471 
472     case Result::RK_Macro: {
473       CachedCodeCompletionResult CachedResult;
474       CachedResult.Completion = R.CreateCodeCompletionString(
475           *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
476           IncludeBriefCommentsInCodeCompletion);
477       CachedResult.ShowInContexts
478         = (1LL << CodeCompletionContext::CCC_TopLevel)
479         | (1LL << CodeCompletionContext::CCC_ObjCInterface)
480         | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
481         | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
482         | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
483         | (1LL << CodeCompletionContext::CCC_Statement)
484         | (1LL << CodeCompletionContext::CCC_Expression)
485         | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
486         | (1LL << CodeCompletionContext::CCC_MacroNameUse)
487         | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
488         | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
489         | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
490 
491       CachedResult.Priority = R.Priority;
492       CachedResult.Kind = R.CursorKind;
493       CachedResult.Availability = R.Availability;
494       CachedResult.TypeClass = STC_Void;
495       CachedResult.Type = 0;
496       CachedCompletionResults.push_back(CachedResult);
497       break;
498     }
499     }
500   }
501 
502   // Save the current top-level hash value.
503   CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
504 }
505 
506 void ASTUnit::ClearCachedCompletionResults() {
507   CachedCompletionResults.clear();
508   CachedCompletionTypes.clear();
509   CachedCompletionAllocator = nullptr;
510 }
511 
512 namespace {
513 
514 /// Gathers information from ASTReader that will be used to initialize
515 /// a Preprocessor.
516 class ASTInfoCollector : public ASTReaderListener {
517   Preprocessor &PP;
518   ASTContext *Context;
519   HeaderSearchOptions &HSOpts;
520   PreprocessorOptions &PPOpts;
521   LangOptions &LangOpt;
522   std::shared_ptr<TargetOptions> &TargetOpts;
523   IntrusiveRefCntPtr<TargetInfo> &Target;
524   unsigned &Counter;
525   bool InitializedLanguage = false;
526 
527 public:
528   ASTInfoCollector(Preprocessor &PP, ASTContext *Context,
529                    HeaderSearchOptions &HSOpts, PreprocessorOptions &PPOpts,
530                    LangOptions &LangOpt,
531                    std::shared_ptr<TargetOptions> &TargetOpts,
532                    IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter)
533       : PP(PP), Context(Context), HSOpts(HSOpts), PPOpts(PPOpts),
534         LangOpt(LangOpt), TargetOpts(TargetOpts), Target(Target),
535         Counter(Counter) {}
536 
537   bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
538                            bool AllowCompatibleDifferences) override {
539     if (InitializedLanguage)
540       return false;
541 
542     LangOpt = LangOpts;
543     InitializedLanguage = true;
544 
545     updated();
546     return false;
547   }
548 
549   bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
550                                StringRef SpecificModuleCachePath,
551                                bool Complain) override {
552     this->HSOpts = HSOpts;
553     return false;
554   }
555 
556   bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain,
557                                std::string &SuggestedPredefines) override {
558     this->PPOpts = PPOpts;
559     return false;
560   }
561 
562   bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
563                          bool AllowCompatibleDifferences) override {
564     // If we've already initialized the target, don't do it again.
565     if (Target)
566       return false;
567 
568     this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts);
569     Target =
570         TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts);
571 
572     updated();
573     return false;
574   }
575 
576   void ReadCounter(const serialization::ModuleFile &M,
577                    unsigned Value) override {
578     Counter = Value;
579   }
580 
581 private:
582   void updated() {
583     if (!Target || !InitializedLanguage)
584       return;
585 
586     // Inform the target of the language options.
587     //
588     // FIXME: We shouldn't need to do this, the target should be immutable once
589     // created. This complexity should be lifted elsewhere.
590     Target->adjust(LangOpt);
591 
592     // Initialize the preprocessor.
593     PP.Initialize(*Target);
594 
595     if (!Context)
596       return;
597 
598     // Initialize the ASTContext
599     Context->InitBuiltinTypes(*Target);
600 
601     // Adjust printing policy based on language options.
602     Context->setPrintingPolicy(PrintingPolicy(LangOpt));
603 
604     // We didn't have access to the comment options when the ASTContext was
605     // constructed, so register them now.
606     Context->getCommentCommandTraits().registerCommentOptions(
607         LangOpt.CommentOpts);
608   }
609 };
610 
611 /// Diagnostic consumer that saves each diagnostic it is given.
612 class StoredDiagnosticConsumer : public DiagnosticConsumer {
613   SmallVectorImpl<StoredDiagnostic> *StoredDiags;
614   SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags;
615   const LangOptions *LangOpts = nullptr;
616   SourceManager *SourceMgr = nullptr;
617 
618 public:
619   StoredDiagnosticConsumer(
620       SmallVectorImpl<StoredDiagnostic> *StoredDiags,
621       SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags)
622       : StoredDiags(StoredDiags), StandaloneDiags(StandaloneDiags) {
623     assert((StoredDiags || StandaloneDiags) &&
624            "No output collections were passed to StoredDiagnosticConsumer.");
625   }
626 
627   void BeginSourceFile(const LangOptions &LangOpts,
628                        const Preprocessor *PP = nullptr) override {
629     this->LangOpts = &LangOpts;
630     if (PP)
631       SourceMgr = &PP->getSourceManager();
632   }
633 
634   void HandleDiagnostic(DiagnosticsEngine::Level Level,
635                         const Diagnostic &Info) override;
636 };
637 
638 /// RAII object that optionally captures diagnostics, if
639 /// there is no diagnostic client to capture them already.
640 class CaptureDroppedDiagnostics {
641   DiagnosticsEngine &Diags;
642   StoredDiagnosticConsumer Client;
643   DiagnosticConsumer *PreviousClient = nullptr;
644   std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
645 
646 public:
647   CaptureDroppedDiagnostics(
648       bool RequestCapture, DiagnosticsEngine &Diags,
649       SmallVectorImpl<StoredDiagnostic> *StoredDiags,
650       SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags)
651       : Diags(Diags), Client(StoredDiags, StandaloneDiags) {
652     if (RequestCapture || Diags.getClient() == nullptr) {
653       OwningPreviousClient = Diags.takeClient();
654       PreviousClient = Diags.getClient();
655       Diags.setClient(&Client, false);
656     }
657   }
658 
659   ~CaptureDroppedDiagnostics() {
660     if (Diags.getClient() == &Client)
661       Diags.setClient(PreviousClient, !!OwningPreviousClient.release());
662   }
663 };
664 
665 } // namespace
666 
667 static ASTUnit::StandaloneDiagnostic
668 makeStandaloneDiagnostic(const LangOptions &LangOpts,
669                          const StoredDiagnostic &InDiag);
670 
671 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
672                                                 const Diagnostic &Info) {
673   // Default implementation (Warnings/errors count).
674   DiagnosticConsumer::HandleDiagnostic(Level, Info);
675 
676   // Only record the diagnostic if it's part of the source manager we know
677   // about. This effectively drops diagnostics from modules we're building.
678   // FIXME: In the long run, ee don't want to drop source managers from modules.
679   if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr) {
680     StoredDiagnostic *ResultDiag = nullptr;
681     if (StoredDiags) {
682       StoredDiags->emplace_back(Level, Info);
683       ResultDiag = &StoredDiags->back();
684     }
685 
686     if (StandaloneDiags) {
687       llvm::Optional<StoredDiagnostic> StoredDiag = None;
688       if (!ResultDiag) {
689         StoredDiag.emplace(Level, Info);
690         ResultDiag = StoredDiag.getPointer();
691       }
692       StandaloneDiags->push_back(
693           makeStandaloneDiagnostic(*LangOpts, *ResultDiag));
694     }
695   }
696 }
697 
698 IntrusiveRefCntPtr<ASTReader> ASTUnit::getASTReader() const {
699   return Reader;
700 }
701 
702 ASTMutationListener *ASTUnit::getASTMutationListener() {
703   if (WriterData)
704     return &WriterData->Writer;
705   return nullptr;
706 }
707 
708 ASTDeserializationListener *ASTUnit::getDeserializationListener() {
709   if (WriterData)
710     return &WriterData->Writer;
711   return nullptr;
712 }
713 
714 std::unique_ptr<llvm::MemoryBuffer>
715 ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) {
716   assert(FileMgr);
717   auto Buffer = FileMgr->getBufferForFile(Filename, UserFilesAreVolatile);
718   if (Buffer)
719     return std::move(*Buffer);
720   if (ErrorStr)
721     *ErrorStr = Buffer.getError().message();
722   return nullptr;
723 }
724 
725 /// Configure the diagnostics object for use with ASTUnit.
726 void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
727                              ASTUnit &AST, bool CaptureDiagnostics) {
728   assert(Diags.get() && "no DiagnosticsEngine was provided");
729   if (CaptureDiagnostics)
730     Diags->setClient(new StoredDiagnosticConsumer(&AST.StoredDiagnostics, nullptr));
731 }
732 
733 std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
734     const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
735     WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
736     const FileSystemOptions &FileSystemOpts, bool UseDebugInfo,
737     bool OnlyLocalDecls, ArrayRef<RemappedFile> RemappedFiles,
738     bool CaptureDiagnostics, bool AllowPCHWithCompilerErrors,
739     bool UserFilesAreVolatile) {
740   std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
741 
742   // Recover resources if we crash before exiting this method.
743   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
744     ASTUnitCleanup(AST.get());
745   llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
746     llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
747     DiagCleanup(Diags.get());
748 
749   ConfigureDiags(Diags, *AST, CaptureDiagnostics);
750 
751   AST->LangOpts = std::make_shared<LangOptions>();
752   AST->OnlyLocalDecls = OnlyLocalDecls;
753   AST->CaptureDiagnostics = CaptureDiagnostics;
754   AST->Diagnostics = Diags;
755   IntrusiveRefCntPtr<vfs::FileSystem> VFS = 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->PCMCache = new MemoryBufferCache;
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(), *AST->PCMCache, 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(PP, AST->Ctx.get(), PCHContainerRdr, {},
795                               /*isysroot=*/"",
796                               /*DisableValidation=*/disableValid,
797                               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<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<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<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       case BuildPreambleError::PreambleIsEmpty:
1367         // Try again next time.
1368         PreambleRebuildCounter = 1;
1369         return nullptr;
1370       case BuildPreambleError::CouldntCreateTargetInfo:
1371       case BuildPreambleError::BeginSourceFileFailed:
1372       case BuildPreambleError::CouldntEmitPCH:
1373         // These erros are more likely to repeat, retry after some period.
1374         PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1375         return nullptr;
1376       }
1377       llvm_unreachable("unexpected BuildPreambleError");
1378     }
1379   }
1380 
1381   assert(Preamble && "Preamble wasn't built");
1382 
1383   TopLevelDecls.clear();
1384   TopLevelDeclsInPreamble = Callbacks.takeTopLevelDeclIDs();
1385   PreambleTopLevelHashValue = Callbacks.getHash();
1386 
1387   NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1388 
1389   checkAndRemoveNonDriverDiags(NewPreambleDiags);
1390   StoredDiagnostics = std::move(NewPreambleDiags);
1391   PreambleDiagnostics = std::move(NewPreambleDiagsStandalone);
1392 
1393   // If the hash of top-level entities differs from the hash of the top-level
1394   // entities the last time we rebuilt the preamble, clear out the completion
1395   // cache.
1396   if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1397     CompletionCacheTopLevelHashValue = 0;
1398     PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1399   }
1400 
1401   return MainFileBuffer;
1402 }
1403 
1404 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1405   assert(Preamble && "Should only be called when preamble was built");
1406 
1407   std::vector<Decl *> Resolved;
1408   Resolved.reserve(TopLevelDeclsInPreamble.size());
1409   ExternalASTSource &Source = *getASTContext().getExternalSource();
1410   for (const auto TopLevelDecl : TopLevelDeclsInPreamble) {
1411     // Resolve the declaration ID to an actual declaration, possibly
1412     // deserializing the declaration in the process.
1413     if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1414       Resolved.push_back(D);
1415   }
1416   TopLevelDeclsInPreamble.clear();
1417   TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1418 }
1419 
1420 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1421   // Steal the created target, context, and preprocessor if they have been
1422   // created.
1423   assert(CI.hasInvocation() && "missing invocation");
1424   LangOpts = CI.getInvocation().LangOpts;
1425   TheSema = CI.takeSema();
1426   Consumer = CI.takeASTConsumer();
1427   if (CI.hasASTContext())
1428     Ctx = &CI.getASTContext();
1429   if (CI.hasPreprocessor())
1430     PP = CI.getPreprocessorPtr();
1431   CI.setSourceManager(nullptr);
1432   CI.setFileManager(nullptr);
1433   if (CI.hasTarget())
1434     Target = &CI.getTarget();
1435   Reader = CI.getModuleManager();
1436   HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1437 }
1438 
1439 StringRef ASTUnit::getMainFileName() const {
1440   if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1441     const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1442     if (Input.isFile())
1443       return Input.getFile();
1444     else
1445       return Input.getBuffer()->getBufferIdentifier();
1446   }
1447 
1448   if (SourceMgr) {
1449     if (const FileEntry *
1450           FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1451       return FE->getName();
1452   }
1453 
1454   return {};
1455 }
1456 
1457 StringRef ASTUnit::getASTFileName() const {
1458   if (!isMainFileAST())
1459     return {};
1460 
1461   serialization::ModuleFile &
1462     Mod = Reader->getModuleManager().getPrimaryModule();
1463   return Mod.FileName;
1464 }
1465 
1466 std::unique_ptr<ASTUnit>
1467 ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
1468                 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1469                 bool CaptureDiagnostics, bool UserFilesAreVolatile) {
1470   std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1471   ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1472   IntrusiveRefCntPtr<vfs::FileSystem> VFS =
1473       createVFSFromCompilerInvocation(*CI, *Diags);
1474   AST->Diagnostics = Diags;
1475   AST->FileSystemOpts = CI->getFileSystemOpts();
1476   AST->Invocation = std::move(CI);
1477   AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1478   AST->UserFilesAreVolatile = UserFilesAreVolatile;
1479   AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1480                                      UserFilesAreVolatile);
1481   AST->PCMCache = new MemoryBufferCache;
1482 
1483   return AST;
1484 }
1485 
1486 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
1487     std::shared_ptr<CompilerInvocation> CI,
1488     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1489     IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FrontendAction *Action,
1490     ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
1491     bool OnlyLocalDecls, bool CaptureDiagnostics,
1492     unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults,
1493     bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
1494     std::unique_ptr<ASTUnit> *ErrAST) {
1495   assert(CI && "A CompilerInvocation is required");
1496 
1497   std::unique_ptr<ASTUnit> OwnAST;
1498   ASTUnit *AST = Unit;
1499   if (!AST) {
1500     // Create the AST unit.
1501     OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile);
1502     AST = OwnAST.get();
1503     if (!AST)
1504       return nullptr;
1505   }
1506 
1507   if (!ResourceFilesPath.empty()) {
1508     // Override the resources path.
1509     CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1510   }
1511   AST->OnlyLocalDecls = OnlyLocalDecls;
1512   AST->CaptureDiagnostics = CaptureDiagnostics;
1513   if (PrecompilePreambleAfterNParses > 0)
1514     AST->PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1515   AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1516   AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1517   AST->IncludeBriefCommentsInCodeCompletion
1518     = IncludeBriefCommentsInCodeCompletion;
1519 
1520   // Recover resources if we crash before exiting this method.
1521   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1522     ASTUnitCleanup(OwnAST.get());
1523   llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1524     llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1525     DiagCleanup(Diags.get());
1526 
1527   // We'll manage file buffers ourselves.
1528   CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1529   CI->getFrontendOpts().DisableFree = false;
1530   ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1531 
1532   // Create the compiler instance to use for building the AST.
1533   std::unique_ptr<CompilerInstance> Clang(
1534       new CompilerInstance(std::move(PCHContainerOps)));
1535 
1536   // Recover resources if we crash before exiting this method.
1537   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1538     CICleanup(Clang.get());
1539 
1540   Clang->setInvocation(std::move(CI));
1541   AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1542 
1543   // Set up diagnostics, capturing any diagnostics that would
1544   // otherwise be dropped.
1545   Clang->setDiagnostics(&AST->getDiagnostics());
1546 
1547   // Create the target instance.
1548   Clang->setTarget(TargetInfo::CreateTargetInfo(
1549       Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1550   if (!Clang->hasTarget())
1551     return nullptr;
1552 
1553   // Inform the target of the language options.
1554   //
1555   // FIXME: We shouldn't need to do this, the target should be immutable once
1556   // created. This complexity should be lifted elsewhere.
1557   Clang->getTarget().adjust(Clang->getLangOpts());
1558 
1559   assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1560          "Invocation must have exactly one source file!");
1561   assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1562              InputKind::Source &&
1563          "FIXME: AST inputs not yet supported here!");
1564   assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1565              InputKind::LLVM_IR &&
1566          "IR inputs not support here!");
1567 
1568   // Configure the various subsystems.
1569   AST->TheSema.reset();
1570   AST->Ctx = nullptr;
1571   AST->PP = nullptr;
1572   AST->Reader = nullptr;
1573 
1574   // Create a file manager object to provide access to and cache the filesystem.
1575   Clang->setFileManager(&AST->getFileManager());
1576 
1577   // Create the source manager.
1578   Clang->setSourceManager(&AST->getSourceManager());
1579 
1580   FrontendAction *Act = Action;
1581 
1582   std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
1583   if (!Act) {
1584     TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1585     Act = TrackerAct.get();
1586   }
1587 
1588   // Recover resources if we crash before exiting this method.
1589   llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1590     ActCleanup(TrackerAct.get());
1591 
1592   if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1593     AST->transferASTDataFromCompilerInstance(*Clang);
1594     if (OwnAST && ErrAST)
1595       ErrAST->swap(OwnAST);
1596 
1597     return nullptr;
1598   }
1599 
1600   if (Persistent && !TrackerAct) {
1601     Clang->getPreprocessor().addPPCallbacks(
1602         llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
1603                                            AST->getCurrentTopLevelHashValue()));
1604     std::vector<std::unique_ptr<ASTConsumer>> Consumers;
1605     if (Clang->hasASTConsumer())
1606       Consumers.push_back(Clang->takeASTConsumer());
1607     Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>(
1608         *AST, AST->getCurrentTopLevelHashValue()));
1609     Clang->setASTConsumer(
1610         llvm::make_unique<MultiplexConsumer>(std::move(Consumers)));
1611   }
1612   if (!Act->Execute()) {
1613     AST->transferASTDataFromCompilerInstance(*Clang);
1614     if (OwnAST && ErrAST)
1615       ErrAST->swap(OwnAST);
1616 
1617     return nullptr;
1618   }
1619 
1620   // Steal the created target, context, and preprocessor.
1621   AST->transferASTDataFromCompilerInstance(*Clang);
1622 
1623   Act->EndSourceFile();
1624 
1625   if (OwnAST)
1626     return OwnAST.release();
1627   else
1628     return AST;
1629 }
1630 
1631 bool ASTUnit::LoadFromCompilerInvocation(
1632     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1633     unsigned PrecompilePreambleAfterNParses,
1634     IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
1635   if (!Invocation)
1636     return true;
1637 
1638   assert(VFS && "VFS is null");
1639 
1640   // We'll manage file buffers ourselves.
1641   Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1642   Invocation->getFrontendOpts().DisableFree = false;
1643   getDiagnostics().Reset();
1644   ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1645 
1646   std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1647   if (PrecompilePreambleAfterNParses > 0) {
1648     PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1649     OverrideMainBuffer =
1650         getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1651     getDiagnostics().Reset();
1652     ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1653   }
1654 
1655   SimpleTimer ParsingTimer(WantTiming);
1656   ParsingTimer.setOutput("Parsing " + getMainFileName());
1657 
1658   // Recover resources if we crash before exiting this method.
1659   llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1660     MemBufferCleanup(OverrideMainBuffer.get());
1661 
1662   return Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1663 }
1664 
1665 std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
1666     std::shared_ptr<CompilerInvocation> CI,
1667     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1668     IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
1669     bool OnlyLocalDecls, bool CaptureDiagnostics,
1670     unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1671     bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1672     bool UserFilesAreVolatile) {
1673   // Create the AST unit.
1674   std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1675   ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1676   AST->Diagnostics = Diags;
1677   AST->OnlyLocalDecls = OnlyLocalDecls;
1678   AST->CaptureDiagnostics = CaptureDiagnostics;
1679   AST->TUKind = TUKind;
1680   AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1681   AST->IncludeBriefCommentsInCodeCompletion
1682     = IncludeBriefCommentsInCodeCompletion;
1683   AST->Invocation = std::move(CI);
1684   AST->FileSystemOpts = FileMgr->getFileSystemOpts();
1685   AST->FileMgr = FileMgr;
1686   AST->UserFilesAreVolatile = UserFilesAreVolatile;
1687 
1688   // Recover resources if we crash before exiting this method.
1689   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1690     ASTUnitCleanup(AST.get());
1691   llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1692     llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1693     DiagCleanup(Diags.get());
1694 
1695   if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1696                                       PrecompilePreambleAfterNParses,
1697                                       AST->FileMgr->getVirtualFileSystem()))
1698     return nullptr;
1699   return AST;
1700 }
1701 
1702 ASTUnit *ASTUnit::LoadFromCommandLine(
1703     const char **ArgBegin, const char **ArgEnd,
1704     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1705     IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
1706     bool OnlyLocalDecls, bool CaptureDiagnostics,
1707     ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
1708     unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1709     bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1710     bool AllowPCHWithCompilerErrors, SkipFunctionBodiesScope SkipFunctionBodies,
1711     bool SingleFileParse, bool UserFilesAreVolatile, bool ForSerialization,
1712     llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST,
1713     IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
1714   assert(Diags.get() && "no DiagnosticsEngine was provided");
1715 
1716   SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1717 
1718   std::shared_ptr<CompilerInvocation> CI;
1719 
1720   {
1721     CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1722                                       &StoredDiagnostics, nullptr);
1723 
1724     CI = createInvocationFromCommandLine(
1725         llvm::makeArrayRef(ArgBegin, ArgEnd), Diags, VFS);
1726     if (!CI)
1727       return nullptr;
1728   }
1729 
1730   // Override any files that need remapping
1731   for (const auto &RemappedFile : RemappedFiles) {
1732     CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1733                                               RemappedFile.second);
1734   }
1735   PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1736   PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1737   PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1738   PPOpts.SingleFileParseMode = SingleFileParse;
1739 
1740   // Override the resources path.
1741   CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1742 
1743   CI->getFrontendOpts().SkipFunctionBodies =
1744       SkipFunctionBodies == SkipFunctionBodiesScope::PreambleAndMainFile;
1745 
1746   if (ModuleFormat)
1747     CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue();
1748 
1749   // Create the AST unit.
1750   std::unique_ptr<ASTUnit> AST;
1751   AST.reset(new ASTUnit(false));
1752   AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1753   AST->StoredDiagnostics.swap(StoredDiagnostics);
1754   ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1755   AST->Diagnostics = Diags;
1756   AST->FileSystemOpts = CI->getFileSystemOpts();
1757   if (!VFS)
1758     VFS = vfs::getRealFileSystem();
1759   VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS);
1760   AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1761   AST->PCMCache = new MemoryBufferCache;
1762   AST->OnlyLocalDecls = OnlyLocalDecls;
1763   AST->CaptureDiagnostics = CaptureDiagnostics;
1764   AST->TUKind = TUKind;
1765   AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1766   AST->IncludeBriefCommentsInCodeCompletion
1767     = IncludeBriefCommentsInCodeCompletion;
1768   AST->UserFilesAreVolatile = UserFilesAreVolatile;
1769   AST->Invocation = CI;
1770   AST->SkipFunctionBodies = SkipFunctionBodies;
1771   if (ForSerialization)
1772     AST->WriterData.reset(new ASTWriterData(*AST->PCMCache));
1773   // Zero out now to ease cleanup during crash recovery.
1774   CI = nullptr;
1775   Diags = nullptr;
1776 
1777   // Recover resources if we crash before exiting this method.
1778   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1779     ASTUnitCleanup(AST.get());
1780 
1781   if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1782                                       PrecompilePreambleAfterNParses,
1783                                       VFS)) {
1784     // Some error occurred, if caller wants to examine diagnostics, pass it the
1785     // ASTUnit.
1786     if (ErrAST) {
1787       AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
1788       ErrAST->swap(AST);
1789     }
1790     return nullptr;
1791   }
1792 
1793   return AST.release();
1794 }
1795 
1796 bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1797                       ArrayRef<RemappedFile> RemappedFiles,
1798                       IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
1799   if (!Invocation)
1800     return true;
1801 
1802   if (!VFS) {
1803     assert(FileMgr && "FileMgr is null on Reparse call");
1804     VFS = FileMgr->getVirtualFileSystem();
1805   }
1806 
1807   clearFileLevelDecls();
1808 
1809   SimpleTimer ParsingTimer(WantTiming);
1810   ParsingTimer.setOutput("Reparsing " + getMainFileName());
1811 
1812   // Remap files.
1813   PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1814   for (const auto &RB : PPOpts.RemappedFileBuffers)
1815     delete RB.second;
1816 
1817   Invocation->getPreprocessorOpts().clearRemappedFiles();
1818   for (const auto &RemappedFile : RemappedFiles) {
1819     Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1820                                                       RemappedFile.second);
1821   }
1822 
1823   // If we have a preamble file lying around, or if we might try to
1824   // build a precompiled preamble, do so now.
1825   std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1826   if (Preamble || PreambleRebuildCounter > 0)
1827     OverrideMainBuffer =
1828         getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1829 
1830   // Clear out the diagnostics state.
1831   FileMgr.reset();
1832   getDiagnostics().Reset();
1833   ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1834   if (OverrideMainBuffer)
1835     getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1836 
1837   // Parse the sources
1838   bool Result =
1839       Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1840 
1841   // If we're caching global code-completion results, and the top-level
1842   // declarations have changed, clear out the code-completion cache.
1843   if (!Result && ShouldCacheCodeCompletionResults &&
1844       CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1845     CacheCodeCompletionResults();
1846 
1847   // We now need to clear out the completion info related to this translation
1848   // unit; it'll be recreated if necessary.
1849   CCTUInfo.reset();
1850 
1851   return Result;
1852 }
1853 
1854 void ASTUnit::ResetForParse() {
1855   SavedMainFileBuffer.reset();
1856 
1857   SourceMgr.reset();
1858   TheSema.reset();
1859   Ctx.reset();
1860   PP.reset();
1861   Reader.reset();
1862 
1863   TopLevelDecls.clear();
1864   clearFileLevelDecls();
1865 }
1866 
1867 //----------------------------------------------------------------------------//
1868 // Code completion
1869 //----------------------------------------------------------------------------//
1870 
1871 namespace {
1872 
1873   /// Code completion consumer that combines the cached code-completion
1874   /// results from an ASTUnit with the code-completion results provided to it,
1875   /// then passes the result on to
1876   class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1877     uint64_t NormalContexts;
1878     ASTUnit &AST;
1879     CodeCompleteConsumer &Next;
1880 
1881   public:
1882     AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1883                                   const CodeCompleteOptions &CodeCompleteOpts)
1884         : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
1885           AST(AST), Next(Next) {
1886       // Compute the set of contexts in which we will look when we don't have
1887       // any information about the specific context.
1888       NormalContexts
1889         = (1LL << CodeCompletionContext::CCC_TopLevel)
1890         | (1LL << CodeCompletionContext::CCC_ObjCInterface)
1891         | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
1892         | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
1893         | (1LL << CodeCompletionContext::CCC_Statement)
1894         | (1LL << CodeCompletionContext::CCC_Expression)
1895         | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
1896         | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
1897         | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
1898         | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
1899         | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
1900         | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
1901         | (1LL << CodeCompletionContext::CCC_Recovery);
1902 
1903       if (AST.getASTContext().getLangOpts().CPlusPlus)
1904         NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
1905                        |  (1LL << CodeCompletionContext::CCC_UnionTag)
1906                        |  (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
1907     }
1908 
1909     void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
1910                                     CodeCompletionResult *Results,
1911                                     unsigned NumResults) override;
1912 
1913     void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1914                                    OverloadCandidate *Candidates,
1915                                    unsigned NumCandidates) override {
1916       Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1917     }
1918 
1919     CodeCompletionAllocator &getAllocator() override {
1920       return Next.getAllocator();
1921     }
1922 
1923     CodeCompletionTUInfo &getCodeCompletionTUInfo() override {
1924       return Next.getCodeCompletionTUInfo();
1925     }
1926   };
1927 
1928 } // namespace
1929 
1930 /// Helper function that computes which global names are hidden by the
1931 /// local code-completion results.
1932 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1933                                  CodeCompletionResult *Results,
1934                                  unsigned NumResults,
1935                                  ASTContext &Ctx,
1936                           llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1937   bool OnlyTagNames = false;
1938   switch (Context.getKind()) {
1939   case CodeCompletionContext::CCC_Recovery:
1940   case CodeCompletionContext::CCC_TopLevel:
1941   case CodeCompletionContext::CCC_ObjCInterface:
1942   case CodeCompletionContext::CCC_ObjCImplementation:
1943   case CodeCompletionContext::CCC_ObjCIvarList:
1944   case CodeCompletionContext::CCC_ClassStructUnion:
1945   case CodeCompletionContext::CCC_Statement:
1946   case CodeCompletionContext::CCC_Expression:
1947   case CodeCompletionContext::CCC_ObjCMessageReceiver:
1948   case CodeCompletionContext::CCC_DotMemberAccess:
1949   case CodeCompletionContext::CCC_ArrowMemberAccess:
1950   case CodeCompletionContext::CCC_ObjCPropertyAccess:
1951   case CodeCompletionContext::CCC_Namespace:
1952   case CodeCompletionContext::CCC_Type:
1953   case CodeCompletionContext::CCC_Name:
1954   case CodeCompletionContext::CCC_PotentiallyQualifiedName:
1955   case CodeCompletionContext::CCC_ParenthesizedExpression:
1956   case CodeCompletionContext::CCC_ObjCInterfaceName:
1957     break;
1958 
1959   case CodeCompletionContext::CCC_EnumTag:
1960   case CodeCompletionContext::CCC_UnionTag:
1961   case CodeCompletionContext::CCC_ClassOrStructTag:
1962     OnlyTagNames = true;
1963     break;
1964 
1965   case CodeCompletionContext::CCC_ObjCProtocolName:
1966   case CodeCompletionContext::CCC_MacroName:
1967   case CodeCompletionContext::CCC_MacroNameUse:
1968   case CodeCompletionContext::CCC_PreprocessorExpression:
1969   case CodeCompletionContext::CCC_PreprocessorDirective:
1970   case CodeCompletionContext::CCC_NaturalLanguage:
1971   case CodeCompletionContext::CCC_SelectorName:
1972   case CodeCompletionContext::CCC_TypeQualifiers:
1973   case CodeCompletionContext::CCC_Other:
1974   case CodeCompletionContext::CCC_OtherWithMacros:
1975   case CodeCompletionContext::CCC_ObjCInstanceMessage:
1976   case CodeCompletionContext::CCC_ObjCClassMessage:
1977   case CodeCompletionContext::CCC_ObjCCategoryName:
1978     // We're looking for nothing, or we're looking for names that cannot
1979     // be hidden.
1980     return;
1981   }
1982 
1983   using Result = CodeCompletionResult;
1984   for (unsigned I = 0; I != NumResults; ++I) {
1985     if (Results[I].Kind != Result::RK_Declaration)
1986       continue;
1987 
1988     unsigned IDNS
1989       = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
1990 
1991     bool Hiding = false;
1992     if (OnlyTagNames)
1993       Hiding = (IDNS & Decl::IDNS_Tag);
1994     else {
1995       unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
1996                              Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
1997                              Decl::IDNS_NonMemberOperator);
1998       if (Ctx.getLangOpts().CPlusPlus)
1999         HiddenIDNS |= Decl::IDNS_Tag;
2000       Hiding = (IDNS & HiddenIDNS);
2001     }
2002 
2003     if (!Hiding)
2004       continue;
2005 
2006     DeclarationName Name = Results[I].Declaration->getDeclName();
2007     if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2008       HiddenNames.insert(Identifier->getName());
2009     else
2010       HiddenNames.insert(Name.getAsString());
2011   }
2012 }
2013 
2014 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2015                                             CodeCompletionContext Context,
2016                                             CodeCompletionResult *Results,
2017                                             unsigned NumResults) {
2018   // Merge the results we were given with the results we cached.
2019   bool AddedResult = false;
2020   uint64_t InContexts =
2021       Context.getKind() == CodeCompletionContext::CCC_Recovery
2022         ? NormalContexts : (1LL << Context.getKind());
2023   // Contains the set of names that are hidden by "local" completion results.
2024   llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2025   using Result = CodeCompletionResult;
2026   SmallVector<Result, 8> AllResults;
2027   for (ASTUnit::cached_completion_iterator
2028             C = AST.cached_completion_begin(),
2029          CEnd = AST.cached_completion_end();
2030        C != CEnd; ++C) {
2031     // If the context we are in matches any of the contexts we are
2032     // interested in, we'll add this result.
2033     if ((C->ShowInContexts & InContexts) == 0)
2034       continue;
2035 
2036     // If we haven't added any results previously, do so now.
2037     if (!AddedResult) {
2038       CalculateHiddenNames(Context, Results, NumResults, S.Context,
2039                            HiddenNames);
2040       AllResults.insert(AllResults.end(), Results, Results + NumResults);
2041       AddedResult = true;
2042     }
2043 
2044     // Determine whether this global completion result is hidden by a local
2045     // completion result. If so, skip it.
2046     if (C->Kind != CXCursor_MacroDefinition &&
2047         HiddenNames.count(C->Completion->getTypedText()))
2048       continue;
2049 
2050     // Adjust priority based on similar type classes.
2051     unsigned Priority = C->Priority;
2052     CodeCompletionString *Completion = C->Completion;
2053     if (!Context.getPreferredType().isNull()) {
2054       if (C->Kind == CXCursor_MacroDefinition) {
2055         Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2056                                          S.getLangOpts(),
2057                                Context.getPreferredType()->isAnyPointerType());
2058       } else if (C->Type) {
2059         CanQualType Expected
2060           = S.Context.getCanonicalType(
2061                                Context.getPreferredType().getUnqualifiedType());
2062         SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2063         if (ExpectedSTC == C->TypeClass) {
2064           // We know this type is similar; check for an exact match.
2065           llvm::StringMap<unsigned> &CachedCompletionTypes
2066             = AST.getCachedCompletionTypes();
2067           llvm::StringMap<unsigned>::iterator Pos
2068             = CachedCompletionTypes.find(QualType(Expected).getAsString());
2069           if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2070             Priority /= CCF_ExactTypeMatch;
2071           else
2072             Priority /= CCF_SimilarTypeMatch;
2073         }
2074       }
2075     }
2076 
2077     // Adjust the completion string, if required.
2078     if (C->Kind == CXCursor_MacroDefinition &&
2079         Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2080       // Create a new code-completion string that just contains the
2081       // macro name, without its arguments.
2082       CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2083                                     CCP_CodePattern, C->Availability);
2084       Builder.AddTypedTextChunk(C->Completion->getTypedText());
2085       Priority = CCP_CodePattern;
2086       Completion = Builder.TakeString();
2087     }
2088 
2089     AllResults.push_back(Result(Completion, Priority, C->Kind,
2090                                 C->Availability));
2091   }
2092 
2093   // If we did not add any cached completion results, just forward the
2094   // results we were given to the next consumer.
2095   if (!AddedResult) {
2096     Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2097     return;
2098   }
2099 
2100   Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2101                                   AllResults.size());
2102 }
2103 
2104 void ASTUnit::CodeComplete(
2105     StringRef File, unsigned Line, unsigned Column,
2106     ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
2107     bool IncludeCodePatterns, bool IncludeBriefComments,
2108     CodeCompleteConsumer &Consumer,
2109     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2110     DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr,
2111     FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2112     SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2113   if (!Invocation)
2114     return;
2115 
2116   SimpleTimer CompletionTimer(WantTiming);
2117   CompletionTimer.setOutput("Code completion @ " + File + ":" +
2118                             Twine(Line) + ":" + Twine(Column));
2119 
2120   auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
2121 
2122   FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2123   CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2124   PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2125 
2126   CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2127                                    CachedCompletionResults.empty();
2128   CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2129   CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2130   CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2131   CodeCompleteOpts.LoadExternal = Consumer.loadExternal();
2132   CodeCompleteOpts.IncludeFixIts = Consumer.includeFixIts();
2133 
2134   assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2135 
2136   FrontendOpts.CodeCompletionAt.FileName = File;
2137   FrontendOpts.CodeCompletionAt.Line = Line;
2138   FrontendOpts.CodeCompletionAt.Column = Column;
2139 
2140   // Set the language options appropriately.
2141   LangOpts = *CCInvocation->getLangOpts();
2142 
2143   // Spell-checking and warnings are wasteful during code-completion.
2144   LangOpts.SpellChecking = false;
2145   CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
2146 
2147   std::unique_ptr<CompilerInstance> Clang(
2148       new CompilerInstance(PCHContainerOps));
2149 
2150   // Recover resources if we crash before exiting this method.
2151   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2152     CICleanup(Clang.get());
2153 
2154   auto &Inv = *CCInvocation;
2155   Clang->setInvocation(std::move(CCInvocation));
2156   OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2157 
2158   // Set up diagnostics, capturing any diagnostics produced.
2159   Clang->setDiagnostics(&Diag);
2160   CaptureDroppedDiagnostics Capture(true,
2161                                     Clang->getDiagnostics(),
2162                                     &StoredDiagnostics, nullptr);
2163   ProcessWarningOptions(Diag, Inv.getDiagnosticOpts());
2164 
2165   // Create the target instance.
2166   Clang->setTarget(TargetInfo::CreateTargetInfo(
2167       Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
2168   if (!Clang->hasTarget()) {
2169     Clang->setInvocation(nullptr);
2170     return;
2171   }
2172 
2173   // Inform the target of the language options.
2174   //
2175   // FIXME: We shouldn't need to do this, the target should be immutable once
2176   // created. This complexity should be lifted elsewhere.
2177   Clang->getTarget().adjust(Clang->getLangOpts());
2178 
2179   assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2180          "Invocation must have exactly one source file!");
2181   assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
2182              InputKind::Source &&
2183          "FIXME: AST inputs not yet supported here!");
2184   assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
2185              InputKind::LLVM_IR &&
2186          "IR inputs not support here!");
2187 
2188   // Use the source and file managers that we were given.
2189   Clang->setFileManager(&FileMgr);
2190   Clang->setSourceManager(&SourceMgr);
2191 
2192   // Remap files.
2193   PreprocessorOpts.clearRemappedFiles();
2194   PreprocessorOpts.RetainRemappedFileBuffers = true;
2195   for (const auto &RemappedFile : RemappedFiles) {
2196     PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second);
2197     OwnedBuffers.push_back(RemappedFile.second);
2198   }
2199 
2200   // Use the code completion consumer we were given, but adding any cached
2201   // code-completion results.
2202   AugmentedCodeCompleteConsumer *AugmentedConsumer
2203     = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2204   Clang->setCodeCompletionConsumer(AugmentedConsumer);
2205 
2206   // If we have a precompiled preamble, try to use it. We only allow
2207   // the use of the precompiled preamble if we're if the completion
2208   // point is within the main file, after the end of the precompiled
2209   // preamble.
2210   std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2211   if (Preamble) {
2212     std::string CompleteFilePath(File);
2213 
2214     auto VFS = FileMgr.getVirtualFileSystem();
2215     auto CompleteFileStatus = VFS->status(CompleteFilePath);
2216     if (CompleteFileStatus) {
2217       llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID();
2218 
2219       std::string MainPath(OriginalSourceFile);
2220       auto MainStatus = VFS->status(MainPath);
2221       if (MainStatus) {
2222         llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID();
2223         if (CompleteFileID == MainID && Line > 1)
2224           OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2225               PCHContainerOps, Inv, VFS, false, Line - 1);
2226       }
2227     }
2228   }
2229 
2230   // If the main file has been overridden due to the use of a preamble,
2231   // make that override happen and introduce the preamble.
2232   if (OverrideMainBuffer) {
2233     assert(Preamble &&
2234            "No preamble was built, but OverrideMainBuffer is not null");
2235 
2236     auto VFS = FileMgr.getVirtualFileSystem();
2237     Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS,
2238                                   OverrideMainBuffer.get());
2239     // FIXME: there is no way to update VFS if it was changed by
2240     // AddImplicitPreamble as FileMgr is accepted as a parameter by this method.
2241     // We use on-disk preambles instead and rely on FileMgr's VFS to ensure the
2242     // PCH files are always readable.
2243     OwnedBuffers.push_back(OverrideMainBuffer.release());
2244   } else {
2245     PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2246     PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2247   }
2248 
2249   // Disable the preprocessing record if modules are not enabled.
2250   if (!Clang->getLangOpts().Modules)
2251     PreprocessorOpts.DetailedRecord = false;
2252 
2253   std::unique_ptr<SyntaxOnlyAction> Act;
2254   Act.reset(new SyntaxOnlyAction);
2255   if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2256     Act->Execute();
2257     Act->EndSourceFile();
2258   }
2259 }
2260 
2261 bool ASTUnit::Save(StringRef File) {
2262   if (HadModuleLoaderFatalFailure)
2263     return true;
2264 
2265   // Write to a temporary file and later rename it to the actual file, to avoid
2266   // possible race conditions.
2267   SmallString<128> TempPath;
2268   TempPath = File;
2269   TempPath += "-%%%%%%%%";
2270   int fd;
2271   if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath))
2272     return true;
2273 
2274   // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2275   // unconditionally create a stat cache when we parse the file?
2276   llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2277 
2278   serialize(Out);
2279   Out.close();
2280   if (Out.has_error()) {
2281     Out.clear_error();
2282     return true;
2283   }
2284 
2285   if (llvm::sys::fs::rename(TempPath, File)) {
2286     llvm::sys::fs::remove(TempPath);
2287     return true;
2288   }
2289 
2290   return false;
2291 }
2292 
2293 static bool serializeUnit(ASTWriter &Writer,
2294                           SmallVectorImpl<char> &Buffer,
2295                           Sema &S,
2296                           bool hasErrors,
2297                           raw_ostream &OS) {
2298   Writer.WriteAST(S, std::string(), nullptr, "", hasErrors);
2299 
2300   // Write the generated bitstream to "Out".
2301   if (!Buffer.empty())
2302     OS.write(Buffer.data(), Buffer.size());
2303 
2304   return false;
2305 }
2306 
2307 bool ASTUnit::serialize(raw_ostream &OS) {
2308   // For serialization we are lenient if the errors were only warn-as-error kind.
2309   bool hasErrors = getDiagnostics().hasUncompilableErrorOccurred();
2310 
2311   if (WriterData)
2312     return serializeUnit(WriterData->Writer, WriterData->Buffer,
2313                          getSema(), hasErrors, OS);
2314 
2315   SmallString<128> Buffer;
2316   llvm::BitstreamWriter Stream(Buffer);
2317   MemoryBufferCache PCMCache;
2318   ASTWriter Writer(Stream, Buffer, PCMCache, {});
2319   return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2320 }
2321 
2322 using SLocRemap = ContinuousRangeMap<unsigned, int, 2>;
2323 
2324 void ASTUnit::TranslateStoredDiagnostics(
2325                           FileManager &FileMgr,
2326                           SourceManager &SrcMgr,
2327                           const SmallVectorImpl<StandaloneDiagnostic> &Diags,
2328                           SmallVectorImpl<StoredDiagnostic> &Out) {
2329   // Map the standalone diagnostic into the new source manager. We also need to
2330   // remap all the locations to the new view. This includes the diag location,
2331   // any associated source ranges, and the source ranges of associated fix-its.
2332   // FIXME: There should be a cleaner way to do this.
2333   SmallVector<StoredDiagnostic, 4> Result;
2334   Result.reserve(Diags.size());
2335 
2336   for (const auto &SD : Diags) {
2337     // Rebuild the StoredDiagnostic.
2338     if (SD.Filename.empty())
2339       continue;
2340     const FileEntry *FE = FileMgr.getFile(SD.Filename);
2341     if (!FE)
2342       continue;
2343     SourceLocation FileLoc;
2344     auto ItFileID = PreambleSrcLocCache.find(SD.Filename);
2345     if (ItFileID == PreambleSrcLocCache.end()) {
2346       FileID FID = SrcMgr.translateFile(FE);
2347       FileLoc = SrcMgr.getLocForStartOfFile(FID);
2348       PreambleSrcLocCache[SD.Filename] = FileLoc;
2349     } else {
2350       FileLoc = ItFileID->getValue();
2351     }
2352 
2353     if (FileLoc.isInvalid())
2354       continue;
2355     SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset);
2356     FullSourceLoc Loc(L, SrcMgr);
2357 
2358     SmallVector<CharSourceRange, 4> Ranges;
2359     Ranges.reserve(SD.Ranges.size());
2360     for (const auto &Range : SD.Ranges) {
2361       SourceLocation BL = FileLoc.getLocWithOffset(Range.first);
2362       SourceLocation EL = FileLoc.getLocWithOffset(Range.second);
2363       Ranges.push_back(CharSourceRange::getCharRange(BL, EL));
2364     }
2365 
2366     SmallVector<FixItHint, 2> FixIts;
2367     FixIts.reserve(SD.FixIts.size());
2368     for (const auto &FixIt : SD.FixIts) {
2369       FixIts.push_back(FixItHint());
2370       FixItHint &FH = FixIts.back();
2371       FH.CodeToInsert = FixIt.CodeToInsert;
2372       SourceLocation BL = FileLoc.getLocWithOffset(FixIt.RemoveRange.first);
2373       SourceLocation EL = FileLoc.getLocWithOffset(FixIt.RemoveRange.second);
2374       FH.RemoveRange = CharSourceRange::getCharRange(BL, EL);
2375     }
2376 
2377     Result.push_back(StoredDiagnostic(SD.Level, SD.ID,
2378                                       SD.Message, Loc, Ranges, FixIts));
2379   }
2380   Result.swap(Out);
2381 }
2382 
2383 void ASTUnit::addFileLevelDecl(Decl *D) {
2384   assert(D);
2385 
2386   // We only care about local declarations.
2387   if (D->isFromASTFile())
2388     return;
2389 
2390   SourceManager &SM = *SourceMgr;
2391   SourceLocation Loc = D->getLocation();
2392   if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2393     return;
2394 
2395   // We only keep track of the file-level declarations of each file.
2396   if (!D->getLexicalDeclContext()->isFileContext())
2397     return;
2398 
2399   SourceLocation FileLoc = SM.getFileLoc(Loc);
2400   assert(SM.isLocalSourceLocation(FileLoc));
2401   FileID FID;
2402   unsigned Offset;
2403   std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2404   if (FID.isInvalid())
2405     return;
2406 
2407   LocDeclsTy *&Decls = FileDecls[FID];
2408   if (!Decls)
2409     Decls = new LocDeclsTy();
2410 
2411   std::pair<unsigned, Decl *> LocDecl(Offset, D);
2412 
2413   if (Decls->empty() || Decls->back().first <= Offset) {
2414     Decls->push_back(LocDecl);
2415     return;
2416   }
2417 
2418   LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
2419                                             LocDecl, llvm::less_first());
2420 
2421   Decls->insert(I, LocDecl);
2422 }
2423 
2424 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2425                                   SmallVectorImpl<Decl *> &Decls) {
2426   if (File.isInvalid())
2427     return;
2428 
2429   if (SourceMgr->isLoadedFileID(File)) {
2430     assert(Ctx->getExternalSource() && "No external source!");
2431     return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2432                                                          Decls);
2433   }
2434 
2435   FileDeclsTy::iterator I = FileDecls.find(File);
2436   if (I == FileDecls.end())
2437     return;
2438 
2439   LocDeclsTy &LocDecls = *I->second;
2440   if (LocDecls.empty())
2441     return;
2442 
2443   LocDeclsTy::iterator BeginIt =
2444       std::lower_bound(LocDecls.begin(), LocDecls.end(),
2445                        std::make_pair(Offset, (Decl *)nullptr),
2446                        llvm::less_first());
2447   if (BeginIt != LocDecls.begin())
2448     --BeginIt;
2449 
2450   // If we are pointing at a top-level decl inside an objc container, we need
2451   // to backtrack until we find it otherwise we will fail to report that the
2452   // region overlaps with an objc container.
2453   while (BeginIt != LocDecls.begin() &&
2454          BeginIt->second->isTopLevelDeclInObjCContainer())
2455     --BeginIt;
2456 
2457   LocDeclsTy::iterator EndIt = std::upper_bound(
2458       LocDecls.begin(), LocDecls.end(),
2459       std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first());
2460   if (EndIt != LocDecls.end())
2461     ++EndIt;
2462 
2463   for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2464     Decls.push_back(DIt->second);
2465 }
2466 
2467 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2468                                     unsigned Line, unsigned Col) const {
2469   const SourceManager &SM = getSourceManager();
2470   SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2471   return SM.getMacroArgExpandedLocation(Loc);
2472 }
2473 
2474 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2475                                     unsigned Offset) const {
2476   const SourceManager &SM = getSourceManager();
2477   SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2478   return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2479 }
2480 
2481 /// If \arg Loc is a loaded location from the preamble, returns
2482 /// the corresponding local location of the main file, otherwise it returns
2483 /// \arg Loc.
2484 SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) const {
2485   FileID PreambleID;
2486   if (SourceMgr)
2487     PreambleID = SourceMgr->getPreambleFileID();
2488 
2489   if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2490     return Loc;
2491 
2492   unsigned Offs;
2493   if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble->getBounds().Size) {
2494     SourceLocation FileLoc
2495         = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2496     return FileLoc.getLocWithOffset(Offs);
2497   }
2498 
2499   return Loc;
2500 }
2501 
2502 /// If \arg Loc is a local location of the main file but inside the
2503 /// preamble chunk, returns the corresponding loaded location from the
2504 /// preamble, otherwise it returns \arg Loc.
2505 SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) const {
2506   FileID PreambleID;
2507   if (SourceMgr)
2508     PreambleID = SourceMgr->getPreambleFileID();
2509 
2510   if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2511     return Loc;
2512 
2513   unsigned Offs;
2514   if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2515       Offs < Preamble->getBounds().Size) {
2516     SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2517     return FileLoc.getLocWithOffset(Offs);
2518   }
2519 
2520   return Loc;
2521 }
2522 
2523 bool ASTUnit::isInPreambleFileID(SourceLocation Loc) const {
2524   FileID FID;
2525   if (SourceMgr)
2526     FID = SourceMgr->getPreambleFileID();
2527 
2528   if (Loc.isInvalid() || FID.isInvalid())
2529     return false;
2530 
2531   return SourceMgr->isInFileID(Loc, FID);
2532 }
2533 
2534 bool ASTUnit::isInMainFileID(SourceLocation Loc) const {
2535   FileID FID;
2536   if (SourceMgr)
2537     FID = SourceMgr->getMainFileID();
2538 
2539   if (Loc.isInvalid() || FID.isInvalid())
2540     return false;
2541 
2542   return SourceMgr->isInFileID(Loc, FID);
2543 }
2544 
2545 SourceLocation ASTUnit::getEndOfPreambleFileID() const {
2546   FileID FID;
2547   if (SourceMgr)
2548     FID = SourceMgr->getPreambleFileID();
2549 
2550   if (FID.isInvalid())
2551     return {};
2552 
2553   return SourceMgr->getLocForEndOfFile(FID);
2554 }
2555 
2556 SourceLocation ASTUnit::getStartOfMainFileID() const {
2557   FileID FID;
2558   if (SourceMgr)
2559     FID = SourceMgr->getMainFileID();
2560 
2561   if (FID.isInvalid())
2562     return {};
2563 
2564   return SourceMgr->getLocForStartOfFile(FID);
2565 }
2566 
2567 llvm::iterator_range<PreprocessingRecord::iterator>
2568 ASTUnit::getLocalPreprocessingEntities() const {
2569   if (isMainFileAST()) {
2570     serialization::ModuleFile &
2571       Mod = Reader->getModuleManager().getPrimaryModule();
2572     return Reader->getModulePreprocessedEntities(Mod);
2573   }
2574 
2575   if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2576     return llvm::make_range(PPRec->local_begin(), PPRec->local_end());
2577 
2578   return llvm::make_range(PreprocessingRecord::iterator(),
2579                           PreprocessingRecord::iterator());
2580 }
2581 
2582 bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2583   if (isMainFileAST()) {
2584     serialization::ModuleFile &
2585       Mod = Reader->getModuleManager().getPrimaryModule();
2586     for (const auto *D : Reader->getModuleFileLevelDecls(Mod)) {
2587       if (!Fn(context, D))
2588         return false;
2589     }
2590 
2591     return true;
2592   }
2593 
2594   for (ASTUnit::top_level_iterator TL = top_level_begin(),
2595                                 TLEnd = top_level_end();
2596          TL != TLEnd; ++TL) {
2597     if (!Fn(context, *TL))
2598       return false;
2599   }
2600 
2601   return true;
2602 }
2603 
2604 const FileEntry *ASTUnit::getPCHFile() {
2605   if (!Reader)
2606     return nullptr;
2607 
2608   serialization::ModuleFile *Mod = nullptr;
2609   Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) {
2610     switch (M.Kind) {
2611     case serialization::MK_ImplicitModule:
2612     case serialization::MK_ExplicitModule:
2613     case serialization::MK_PrebuiltModule:
2614       return true; // skip dependencies.
2615     case serialization::MK_PCH:
2616       Mod = &M;
2617       return true; // found it.
2618     case serialization::MK_Preamble:
2619       return false; // look in dependencies.
2620     case serialization::MK_MainFile:
2621       return false; // look in dependencies.
2622     }
2623 
2624     return true;
2625   });
2626   if (Mod)
2627     return Mod->File;
2628 
2629   return nullptr;
2630 }
2631 
2632 bool ASTUnit::isModuleFile() const {
2633   return isMainFileAST() && getLangOpts().isCompilingModule();
2634 }
2635 
2636 InputKind ASTUnit::getInputKind() const {
2637   auto &LangOpts = getLangOpts();
2638 
2639   InputKind::Language Lang;
2640   if (LangOpts.OpenCL)
2641     Lang = InputKind::OpenCL;
2642   else if (LangOpts.CUDA)
2643     Lang = InputKind::CUDA;
2644   else if (LangOpts.RenderScript)
2645     Lang = InputKind::RenderScript;
2646   else if (LangOpts.CPlusPlus)
2647     Lang = LangOpts.ObjC1 ? InputKind::ObjCXX : InputKind::CXX;
2648   else
2649     Lang = LangOpts.ObjC1 ? InputKind::ObjC : InputKind::C;
2650 
2651   InputKind::Format Fmt = InputKind::Source;
2652   if (LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap)
2653     Fmt = InputKind::ModuleMap;
2654 
2655   // We don't know if input was preprocessed. Assume not.
2656   bool PP = false;
2657 
2658   return InputKind(Lang, Fmt, PP);
2659 }
2660 
2661 #ifndef NDEBUG
2662 ASTUnit::ConcurrencyState::ConcurrencyState() {
2663   Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2664 }
2665 
2666 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2667   delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2668 }
2669 
2670 void ASTUnit::ConcurrencyState::start() {
2671   bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2672   assert(acquired && "Concurrent access to ASTUnit!");
2673 }
2674 
2675 void ASTUnit::ConcurrencyState::finish() {
2676   static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2677 }
2678 
2679 #else // NDEBUG
2680 
2681 ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = nullptr; }
2682 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2683 void ASTUnit::ConcurrencyState::start() {}
2684 void ASTUnit::ConcurrencyState::finish() {}
2685 
2686 #endif // NDEBUG
2687