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