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