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