clang  9.0.0svn
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"
33 #include "clang/Basic/Module.h"
36 #include "clang/Basic/TargetInfo.h"
46 #include "clang/Frontend/Utils.h"
47 #include "clang/Lex/HeaderSearch.h"
49 #include "clang/Lex/Lexer.h"
50 #include "clang/Lex/PPCallbacks.h"
52 #include "clang/Lex/Preprocessor.h"
54 #include "clang/Lex/Token.h"
57 #include "clang/Sema/Sema.h"
65 #include "llvm/ADT/ArrayRef.h"
66 #include "llvm/ADT/DenseMap.h"
67 #include "llvm/ADT/IntrusiveRefCntPtr.h"
68 #include "llvm/ADT/None.h"
69 #include "llvm/ADT/Optional.h"
70 #include "llvm/ADT/STLExtras.h"
71 #include "llvm/ADT/SmallString.h"
72 #include "llvm/ADT/SmallVector.h"
73 #include "llvm/ADT/StringMap.h"
74 #include "llvm/ADT/StringRef.h"
75 #include "llvm/ADT/StringSet.h"
76 #include "llvm/ADT/Twine.h"
77 #include "llvm/ADT/iterator_range.h"
78 #include "llvm/Bitstream/BitstreamWriter.h"
79 #include "llvm/Support/Allocator.h"
80 #include "llvm/Support/Casting.h"
81 #include "llvm/Support/CrashRecoveryContext.h"
82 #include "llvm/Support/DJB.h"
83 #include "llvm/Support/ErrorHandling.h"
84 #include "llvm/Support/ErrorOr.h"
85 #include "llvm/Support/FileSystem.h"
86 #include "llvm/Support/MemoryBuffer.h"
87 #include "llvm/Support/Mutex.h"
88 #include "llvm/Support/Timer.h"
89 #include "llvm/Support/VirtualFileSystem.h"
90 #include "llvm/Support/raw_ostream.h"
91 #include <algorithm>
92 #include <atomic>
93 #include <cassert>
94 #include <cstdint>
95 #include <cstdio>
96 #include <cstdlib>
97 #include <memory>
98 #include <string>
99 #include <tuple>
100 #include <utility>
101 #include <vector>
102 
103 using namespace clang;
104 
105 using llvm::TimeRecord;
106 
107 namespace {
108 
109  class SimpleTimer {
110  bool WantTiming;
111  TimeRecord Start;
112  std::string Output;
113 
114  public:
115  explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
116  if (WantTiming)
117  Start = TimeRecord::getCurrentTime();
118  }
119 
120  ~SimpleTimer() {
121  if (WantTiming) {
122  TimeRecord Elapsed = TimeRecord::getCurrentTime();
123  Elapsed -= Start;
124  llvm::errs() << Output << ':';
125  Elapsed.print(Elapsed, llvm::errs());
126  llvm::errs() << '\n';
127  }
128  }
129 
130  void setOutput(const Twine &Output) {
131  if (WantTiming)
132  this->Output = Output.str();
133  }
134  };
135 
136 } // namespace
137 
138 template <class T>
139 static std::unique_ptr<T> valueOrNull(llvm::ErrorOr<std::unique_ptr<T>> Val) {
140  if (!Val)
141  return nullptr;
142  return std::move(*Val);
143 }
144 
145 template <class T>
146 static bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
147  if (!Val)
148  return false;
149  Output = std::move(*Val);
150  return true;
151 }
152 
153 /// Get a source buffer for \p MainFilePath, handling all file-to-file
154 /// and file-to-buffer remappings inside \p Invocation.
155 static std::unique_ptr<llvm::MemoryBuffer>
157  llvm::vfs::FileSystem *VFS,
158  StringRef FilePath, bool isVolatile) {
159  const auto &PreprocessorOpts = Invocation.getPreprocessorOpts();
160 
161  // Try to determine if the main file has been remapped, either from the
162  // command line (to another file) or directly through the compiler
163  // invocation (to a memory buffer).
164  llvm::MemoryBuffer *Buffer = nullptr;
165  std::unique_ptr<llvm::MemoryBuffer> BufferOwner;
166  auto FileStatus = VFS->status(FilePath);
167  if (FileStatus) {
168  llvm::sys::fs::UniqueID MainFileID = FileStatus->getUniqueID();
169 
170  // Check whether there is a file-file remapping of the main file
171  for (const auto &RF : PreprocessorOpts.RemappedFiles) {
172  std::string MPath(RF.first);
173  auto MPathStatus = VFS->status(MPath);
174  if (MPathStatus) {
175  llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
176  if (MainFileID == MID) {
177  // We found a remapping. Try to load the resulting, remapped source.
178  BufferOwner = valueOrNull(VFS->getBufferForFile(RF.second, -1, true, isVolatile));
179  if (!BufferOwner)
180  return nullptr;
181  }
182  }
183  }
184 
185  // Check whether there is a file-buffer remapping. It supercedes the
186  // file-file remapping.
187  for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
188  std::string MPath(RB.first);
189  auto MPathStatus = VFS->status(MPath);
190  if (MPathStatus) {
191  llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
192  if (MainFileID == MID) {
193  // We found a remapping.
194  BufferOwner.reset();
195  Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
196  }
197  }
198  }
199  }
200 
201  // If the main source file was not remapped, load it now.
202  if (!Buffer && !BufferOwner) {
203  BufferOwner = valueOrNull(VFS->getBufferForFile(FilePath, -1, true, isVolatile));
204  if (!BufferOwner)
205  return nullptr;
206  }
207 
208  if (BufferOwner)
209  return BufferOwner;
210  if (!Buffer)
211  return nullptr;
212  return llvm::MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(), FilePath);
213 }
214 
217  llvm::BitstreamWriter Stream;
219 
221  : Stream(Buffer), Writer(Stream, Buffer, ModuleCache, {}) {}
222 };
223 
224 void ASTUnit::clearFileLevelDecls() {
225  llvm::DeleteContainerSeconds(FileDecls);
226 }
227 
228 /// After failing to build a precompiled preamble (due to
229 /// errors in the source that occurs in the preamble), the number of
230 /// reparses during which we'll skip even trying to precompile the
231 /// preamble.
233 
234 /// Tracks the number of ASTUnit objects that are currently active.
235 ///
236 /// Used for debugging purposes only.
237 static std::atomic<unsigned> ActiveASTUnitObjects;
238 
239 ASTUnit::ASTUnit(bool _MainFileIsAST)
240  : MainFileIsAST(_MainFileIsAST), WantTiming(getenv("LIBCLANG_TIMING")),
241  ShouldCacheCodeCompletionResults(false),
242  IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
243  UnsafeToFree(false) {
244  if (getenv("LIBCLANG_OBJTRACKING"))
245  fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects);
246 }
247 
248 ASTUnit::~ASTUnit() {
249  // If we loaded from an AST file, balance out the BeginSourceFile call.
250  if (MainFileIsAST && getDiagnostics().getClient()) {
252  }
253 
254  clearFileLevelDecls();
255 
256  // Free the buffers associated with remapped files. We are required to
257  // perform this operation here because we explicitly request that the
258  // compiler instance *not* free these buffers for each invocation of the
259  // parser.
260  if (Invocation && OwnsRemappedFileBuffers) {
261  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
262  for (const auto &RB : PPOpts.RemappedFileBuffers)
263  delete RB.second;
264  }
265 
266  ClearCachedCompletionResults();
267 
268  if (getenv("LIBCLANG_OBJTRACKING"))
269  fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects);
270 }
271 
272 void ASTUnit::setPreprocessor(std::shared_ptr<Preprocessor> PP) {
273  this->PP = std::move(PP);
274 }
275 
277  assert(getDiagnostics().getClient() && Ctx &&
278  "Bad context for source file");
279  getDiagnostics().getClient()->BeginSourceFile(Ctx->getLangOpts(), PP.get());
280 }
281 
282 /// Determine the set of code-completion contexts in which this
283 /// declaration should be shown.
284 static uint64_t getDeclShowContexts(const NamedDecl *ND,
285  const LangOptions &LangOpts,
286  bool &IsNestedNameSpecifier) {
287  IsNestedNameSpecifier = false;
288 
289  if (isa<UsingShadowDecl>(ND))
290  ND = ND->getUnderlyingDecl();
291  if (!ND)
292  return 0;
293 
294  uint64_t Contexts = 0;
295  if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
296  isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND) ||
297  isa<TypeAliasTemplateDecl>(ND)) {
298  // Types can appear in these contexts.
299  if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
300  Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
306 
307  // In C++, types can appear in expressions contexts (for functional casts).
308  if (LangOpts.CPlusPlus)
309  Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
310 
311  // In Objective-C, message sends can send interfaces. In Objective-C++,
312  // all types are available due to functional casts.
313  if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
315 
316  // In Objective-C, you can only be a subclass of another Objective-C class
317  if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) {
318  // Objective-C interfaces can be used in a class property expression.
319  if (ID->getDefinition())
320  Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
322  }
323 
324  // Deal with tag names.
325  if (isa<EnumDecl>(ND)) {
326  Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
327 
328  // Part of the nested-name-specifier in C++0x.
329  if (LangOpts.CPlusPlus11)
330  IsNestedNameSpecifier = true;
331  } else if (const auto *Record = dyn_cast<RecordDecl>(ND)) {
332  if (Record->isUnion())
333  Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
334  else
335  Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
336 
337  if (LangOpts.CPlusPlus)
338  IsNestedNameSpecifier = true;
339  } else if (isa<ClassTemplateDecl>(ND))
340  IsNestedNameSpecifier = true;
341  } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
342  // Values can appear in these contexts.
343  Contexts = (1LL << CodeCompletionContext::CCC_Statement)
347  } else if (isa<ObjCProtocolDecl>(ND)) {
349  } else if (isa<ObjCCategoryDecl>(ND)) {
351  } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
352  Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
353 
354  // Part of the nested-name-specifier.
355  IsNestedNameSpecifier = true;
356  }
357 
358  return Contexts;
359 }
360 
361 void ASTUnit::CacheCodeCompletionResults() {
362  if (!TheSema)
363  return;
364 
365  SimpleTimer Timer(WantTiming);
366  Timer.setOutput("Cache global code completions for " + getMainFileName());
367 
368  // Clear out the previous results.
369  ClearCachedCompletionResults();
370 
371  // Gather the set of global code completions.
373  SmallVector<Result, 8> Results;
374  CachedCompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
375  CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
376  TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
377  CCTUInfo, Results);
378 
379  // Translate global code completions into cached completions.
380  llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
382 
383  for (auto &R : Results) {
384  switch (R.Kind) {
385  case Result::RK_Declaration: {
386  bool IsNestedNameSpecifier = false;
387  CachedCodeCompletionResult CachedResult;
388  CachedResult.Completion = R.CreateCodeCompletionString(
389  *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
390  IncludeBriefCommentsInCodeCompletion);
391  CachedResult.ShowInContexts = getDeclShowContexts(
392  R.Declaration, Ctx->getLangOpts(), IsNestedNameSpecifier);
393  CachedResult.Priority = R.Priority;
394  CachedResult.Kind = R.CursorKind;
395  CachedResult.Availability = R.Availability;
396 
397  // Keep track of the type of this completion in an ASTContext-agnostic
398  // way.
399  QualType UsageType = getDeclUsageType(*Ctx, R.Declaration);
400  if (UsageType.isNull()) {
401  CachedResult.TypeClass = STC_Void;
402  CachedResult.Type = 0;
403  } else {
404  CanQualType CanUsageType
405  = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
406  CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
407 
408  // Determine whether we have already seen this type. If so, we save
409  // ourselves the work of formatting the type string by using the
410  // temporary, CanQualType-based hash table to find the associated value.
411  unsigned &TypeValue = CompletionTypes[CanUsageType];
412  if (TypeValue == 0) {
413  TypeValue = CompletionTypes.size();
414  CachedCompletionTypes[QualType(CanUsageType).getAsString()]
415  = TypeValue;
416  }
417 
418  CachedResult.Type = TypeValue;
419  }
420 
421  CachedCompletionResults.push_back(CachedResult);
422 
423  /// Handle nested-name-specifiers in C++.
424  if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier &&
425  !R.StartsNestedNameSpecifier) {
426  // The contexts in which a nested-name-specifier can appear in C++.
427  uint64_t NNSContexts
441 
442  if (isa<NamespaceDecl>(R.Declaration) ||
443  isa<NamespaceAliasDecl>(R.Declaration))
444  NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
445 
446  if (uint64_t RemainingContexts
447  = NNSContexts & ~CachedResult.ShowInContexts) {
448  // If there any contexts where this completion can be a
449  // nested-name-specifier but isn't already an option, create a
450  // nested-name-specifier completion.
451  R.StartsNestedNameSpecifier = true;
452  CachedResult.Completion = R.CreateCodeCompletionString(
453  *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
454  IncludeBriefCommentsInCodeCompletion);
455  CachedResult.ShowInContexts = RemainingContexts;
456  CachedResult.Priority = CCP_NestedNameSpecifier;
457  CachedResult.TypeClass = STC_Void;
458  CachedResult.Type = 0;
459  CachedCompletionResults.push_back(CachedResult);
460  }
461  }
462  break;
463  }
464 
465  case Result::RK_Keyword:
466  case Result::RK_Pattern:
467  // Ignore keywords and patterns; we don't care, since they are so
468  // easily regenerated.
469  break;
470 
471  case Result::RK_Macro: {
472  CachedCodeCompletionResult CachedResult;
473  CachedResult.Completion = R.CreateCodeCompletionString(
474  *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
475  IncludeBriefCommentsInCodeCompletion);
476  CachedResult.ShowInContexts
489 
490  CachedResult.Priority = R.Priority;
491  CachedResult.Kind = R.CursorKind;
492  CachedResult.Availability = R.Availability;
493  CachedResult.TypeClass = STC_Void;
494  CachedResult.Type = 0;
495  CachedCompletionResults.push_back(CachedResult);
496  break;
497  }
498  }
499  }
500 
501  // Save the current top-level hash value.
502  CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
503 }
504 
505 void ASTUnit::ClearCachedCompletionResults() {
506  CachedCompletionResults.clear();
507  CachedCompletionTypes.clear();
508  CachedCompletionAllocator = nullptr;
509 }
510 
511 namespace {
512 
513 /// Gathers information from ASTReader that will be used to initialize
514 /// a Preprocessor.
515 class ASTInfoCollector : public ASTReaderListener {
516  Preprocessor &PP;
517  ASTContext *Context;
518  HeaderSearchOptions &HSOpts;
519  PreprocessorOptions &PPOpts;
520  LangOptions &LangOpt;
521  std::shared_ptr<TargetOptions> &TargetOpts;
522  IntrusiveRefCntPtr<TargetInfo> &Target;
523  unsigned &Counter;
524  bool InitializedLanguage = false;
525 
526 public:
527  ASTInfoCollector(Preprocessor &PP, ASTContext *Context,
528  HeaderSearchOptions &HSOpts, PreprocessorOptions &PPOpts,
529  LangOptions &LangOpt,
530  std::shared_ptr<TargetOptions> &TargetOpts,
531  IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter)
532  : PP(PP), Context(Context), HSOpts(HSOpts), PPOpts(PPOpts),
533  LangOpt(LangOpt), TargetOpts(TargetOpts), Target(Target),
534  Counter(Counter) {}
535 
536  bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
537  bool AllowCompatibleDifferences) override {
538  if (InitializedLanguage)
539  return false;
540 
541  LangOpt = LangOpts;
542  InitializedLanguage = true;
543 
544  updated();
545  return false;
546  }
547 
548  bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
549  StringRef SpecificModuleCachePath,
550  bool Complain) override {
551  this->HSOpts = HSOpts;
552  return false;
553  }
554 
555  bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain,
556  std::string &SuggestedPredefines) override {
557  this->PPOpts = PPOpts;
558  return false;
559  }
560 
561  bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
562  bool AllowCompatibleDifferences) override {
563  // If we've already initialized the target, don't do it again.
564  if (Target)
565  return false;
566 
567  this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts);
568  Target =
569  TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts);
570 
571  updated();
572  return false;
573  }
574 
575  void ReadCounter(const serialization::ModuleFile &M,
576  unsigned Value) override {
577  Counter = Value;
578  }
579 
580 private:
581  void updated() {
582  if (!Target || !InitializedLanguage)
583  return;
584 
585  // Inform the target of the language options.
586  //
587  // FIXME: We shouldn't need to do this, the target should be immutable once
588  // created. This complexity should be lifted elsewhere.
589  Target->adjust(LangOpt);
590 
591  // Initialize the preprocessor.
592  PP.Initialize(*Target);
593 
594  if (!Context)
595  return;
596 
597  // Initialize the ASTContext
598  Context->InitBuiltinTypes(*Target);
599 
600  // Adjust printing policy based on language options.
601  Context->setPrintingPolicy(PrintingPolicy(LangOpt));
602 
603  // We didn't have access to the comment options when the ASTContext was
604  // constructed, so register them now.
606  LangOpt.CommentOpts);
607  }
608 };
609 
610 /// Diagnostic consumer that saves each diagnostic it is given.
611 class FilterAndStoreDiagnosticConsumer : public DiagnosticConsumer {
614  bool CaptureNonErrorsFromIncludes = true;
615  const LangOptions *LangOpts = nullptr;
616  SourceManager *SourceMgr = nullptr;
617 
618 public:
619  FilterAndStoreDiagnosticConsumer(
622  bool CaptureNonErrorsFromIncludes)
623  : StoredDiags(StoredDiags), StandaloneDiags(StandaloneDiags),
624  CaptureNonErrorsFromIncludes(CaptureNonErrorsFromIncludes) {
625  assert((StoredDiags || StandaloneDiags) &&
626  "No output collections were passed to StoredDiagnosticConsumer.");
627  }
628 
629  void BeginSourceFile(const LangOptions &LangOpts,
630  const Preprocessor *PP = nullptr) override {
631  this->LangOpts = &LangOpts;
632  if (PP)
633  SourceMgr = &PP->getSourceManager();
634  }
635 
636  void HandleDiagnostic(DiagnosticsEngine::Level Level,
637  const Diagnostic &Info) override;
638 };
639 
640 /// RAII object that optionally captures and filters diagnostics, if
641 /// there is no diagnostic client to capture them already.
642 class CaptureDroppedDiagnostics {
643  DiagnosticsEngine &Diags;
644  FilterAndStoreDiagnosticConsumer Client;
645  DiagnosticConsumer *PreviousClient = nullptr;
646  std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
647 
648 public:
649  CaptureDroppedDiagnostics(
650  CaptureDiagsKind CaptureDiagnostics, DiagnosticsEngine &Diags,
653  : Diags(Diags),
654  Client(StoredDiags, StandaloneDiags,
655  CaptureDiagnostics !=
657  if (CaptureDiagnostics != CaptureDiagsKind::None ||
658  Diags.getClient() == nullptr) {
659  OwningPreviousClient = Diags.takeClient();
660  PreviousClient = Diags.getClient();
661  Diags.setClient(&Client, false);
662  }
663  }
664 
665  ~CaptureDroppedDiagnostics() {
666  if (Diags.getClient() == &Client)
667  Diags.setClient(PreviousClient, !!OwningPreviousClient.release());
668  }
669 };
670 
671 } // namespace
672 
674 makeStandaloneDiagnostic(const LangOptions &LangOpts,
675  const StoredDiagnostic &InDiag);
676 
677 static bool isInMainFile(const clang::Diagnostic &D) {
678  if (!D.hasSourceManager() || !D.getLocation().isValid())
679  return false;
680 
681  auto &M = D.getSourceManager();
682  return M.isWrittenInMainFile(M.getExpansionLoc(D.getLocation()));
683 }
684 
685 void FilterAndStoreDiagnosticConsumer::HandleDiagnostic(
687  // Default implementation (Warnings/errors count).
689 
690  // Only record the diagnostic if it's part of the source manager we know
691  // about. This effectively drops diagnostics from modules we're building.
692  // FIXME: In the long run, ee don't want to drop source managers from modules.
693  if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr) {
694  if (!CaptureNonErrorsFromIncludes && Level <= DiagnosticsEngine::Warning &&
695  !isInMainFile(Info)) {
696  return;
697  }
698 
699  StoredDiagnostic *ResultDiag = nullptr;
700  if (StoredDiags) {
701  StoredDiags->emplace_back(Level, Info);
702  ResultDiag = &StoredDiags->back();
703  }
704 
705  if (StandaloneDiags) {
707  if (!ResultDiag) {
708  StoredDiag.emplace(Level, Info);
709  ResultDiag = StoredDiag.getPointer();
710  }
711  StandaloneDiags->push_back(
712  makeStandaloneDiagnostic(*LangOpts, *ResultDiag));
713  }
714  }
715 }
716 
718  return Reader;
719 }
720 
722  if (WriterData)
723  return &WriterData->Writer;
724  return nullptr;
725 }
726 
728  if (WriterData)
729  return &WriterData->Writer;
730  return nullptr;
731 }
732 
733 std::unique_ptr<llvm::MemoryBuffer>
734 ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) {
735  assert(FileMgr);
736  auto Buffer = FileMgr->getBufferForFile(Filename, UserFilesAreVolatile);
737  if (Buffer)
738  return std::move(*Buffer);
739  if (ErrorStr)
740  *ErrorStr = Buffer.getError().message();
741  return nullptr;
742 }
743 
744 /// Configure the diagnostics object for use with ASTUnit.
745 void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
746  ASTUnit &AST,
747  CaptureDiagsKind CaptureDiagnostics) {
748  assert(Diags.get() && "no DiagnosticsEngine was provided");
749  if (CaptureDiagnostics != CaptureDiagsKind::None)
750  Diags->setClient(new FilterAndStoreDiagnosticConsumer(
751  &AST.StoredDiagnostics, nullptr,
753 }
754 
755 std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
756  const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
758  const FileSystemOptions &FileSystemOpts, bool UseDebugInfo,
759  bool OnlyLocalDecls, ArrayRef<RemappedFile> RemappedFiles,
760  CaptureDiagsKind CaptureDiagnostics, bool AllowPCHWithCompilerErrors,
761  bool UserFilesAreVolatile) {
762  std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
763 
764  // Recover resources if we crash before exiting this method.
765  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
766  ASTUnitCleanup(AST.get());
767  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
768  llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
769  DiagCleanup(Diags.get());
770 
771  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
772 
773  AST->LangOpts = std::make_shared<LangOptions>();
774  AST->OnlyLocalDecls = OnlyLocalDecls;
775  AST->CaptureDiagnostics = CaptureDiagnostics;
776  AST->Diagnostics = Diags;
778  llvm::vfs::getRealFileSystem();
779  AST->FileMgr = new FileManager(FileSystemOpts, VFS);
780  AST->UserFilesAreVolatile = UserFilesAreVolatile;
781  AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
782  AST->getFileManager(),
783  UserFilesAreVolatile);
784  AST->ModuleCache = new InMemoryModuleCache;
785  AST->HSOpts = std::make_shared<HeaderSearchOptions>();
786  AST->HSOpts->ModuleFormat = PCHContainerRdr.getFormat();
787  AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
788  AST->getSourceManager(),
789  AST->getDiagnostics(),
790  AST->getLangOpts(),
791  /*Target=*/nullptr));
792  AST->PPOpts = std::make_shared<PreprocessorOptions>();
793 
794  for (const auto &RemappedFile : RemappedFiles)
795  AST->PPOpts->addRemappedFile(RemappedFile.first, RemappedFile.second);
796 
797  // Gather Info for preprocessor construction later on.
798 
799  HeaderSearch &HeaderInfo = *AST->HeaderInfo;
800  unsigned Counter;
801 
802  AST->PP = std::make_shared<Preprocessor>(
803  AST->PPOpts, AST->getDiagnostics(), *AST->LangOpts,
804  AST->getSourceManager(), HeaderInfo, AST->ModuleLoader,
805  /*IILookup=*/nullptr,
806  /*OwnsHeaderSearch=*/false);
807  Preprocessor &PP = *AST->PP;
808 
809  if (ToLoad >= LoadASTOnly)
810  AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(),
811  PP.getIdentifierTable(), PP.getSelectorTable(),
812  PP.getBuiltinInfo());
813 
814  bool disableValid = false;
815  if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
816  disableValid = true;
817  AST->Reader = new ASTReader(
818  PP, *AST->ModuleCache, AST->Ctx.get(), PCHContainerRdr, {},
819  /*isysroot=*/"",
820  /*DisableValidation=*/disableValid, AllowPCHWithCompilerErrors);
821 
822  AST->Reader->setListener(llvm::make_unique<ASTInfoCollector>(
823  *AST->PP, AST->Ctx.get(), *AST->HSOpts, *AST->PPOpts, *AST->LangOpts,
824  AST->TargetOpts, AST->Target, Counter));
825 
826  // Attach the AST reader to the AST context as an external AST
827  // source, so that declarations will be deserialized from the
828  // AST file as needed.
829  // We need the external source to be set up before we read the AST, because
830  // eagerly-deserialized declarations may use it.
831  if (AST->Ctx)
832  AST->Ctx->setExternalSource(AST->Reader);
833 
834  switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile,
836  case ASTReader::Success:
837  break;
838 
839  case ASTReader::Failure:
840  case ASTReader::Missing:
845  AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
846  return nullptr;
847  }
848 
849  AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile();
850 
851  PP.setCounterValue(Counter);
852 
853  // Create an AST consumer, even though it isn't used.
854  if (ToLoad >= LoadASTOnly)
855  AST->Consumer.reset(new ASTConsumer);
856 
857  // Create a semantic analysis object and tell the AST reader about it.
858  if (ToLoad >= LoadEverything) {
859  AST->TheSema.reset(new Sema(PP, *AST->Ctx, *AST->Consumer));
860  AST->TheSema->Initialize();
861  AST->Reader->InitializeSema(*AST->TheSema);
862  }
863 
864  // Tell the diagnostic client that we have started a source file.
865  AST->getDiagnostics().getClient()->BeginSourceFile(PP.getLangOpts(), &PP);
866 
867  return AST;
868 }
869 
870 /// Add the given macro to the hash of all top-level entities.
871 static void AddDefinedMacroToHash(const Token &MacroNameTok, unsigned &Hash) {
872  Hash = llvm::djbHash(MacroNameTok.getIdentifierInfo()->getName(), Hash);
873 }
874 
875 namespace {
876 
877 /// Preprocessor callback class that updates a hash value with the names
878 /// of all macros that have been defined by the translation unit.
879 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
880  unsigned &Hash;
881 
882 public:
883  explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) {}
884 
885  void MacroDefined(const Token &MacroNameTok,
886  const MacroDirective *MD) override {
887  AddDefinedMacroToHash(MacroNameTok, Hash);
888  }
889 };
890 
891 } // namespace
892 
893 /// Add the given declaration to the hash of all top-level entities.
894 static void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
895  if (!D)
896  return;
897 
898  DeclContext *DC = D->getDeclContext();
899  if (!DC)
900  return;
901 
902  if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
903  return;
904 
905  if (const auto *ND = dyn_cast<NamedDecl>(D)) {
906  if (const auto *EnumD = dyn_cast<EnumDecl>(D)) {
907  // For an unscoped enum include the enumerators in the hash since they
908  // enter the top-level namespace.
909  if (!EnumD->isScoped()) {
910  for (const auto *EI : EnumD->enumerators()) {
911  if (EI->getIdentifier())
912  Hash = llvm::djbHash(EI->getIdentifier()->getName(), Hash);
913  }
914  }
915  }
916 
917  if (ND->getIdentifier())
918  Hash = llvm::djbHash(ND->getIdentifier()->getName(), Hash);
919  else if (DeclarationName Name = ND->getDeclName()) {
920  std::string NameStr = Name.getAsString();
921  Hash = llvm::djbHash(NameStr, Hash);
922  }
923  return;
924  }
925 
926  if (const auto *ImportD = dyn_cast<ImportDecl>(D)) {
927  if (const Module *Mod = ImportD->getImportedModule()) {
928  std::string ModName = Mod->getFullModuleName();
929  Hash = llvm::djbHash(ModName, Hash);
930  }
931  return;
932  }
933 }
934 
935 namespace {
936 
937 class TopLevelDeclTrackerConsumer : public ASTConsumer {
938  ASTUnit &Unit;
939  unsigned &Hash;
940 
941 public:
942  TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
943  : Unit(_Unit), Hash(Hash) {
944  Hash = 0;
945  }
946 
947  void handleTopLevelDecl(Decl *D) {
948  if (!D)
949  return;
950 
951  // FIXME: Currently ObjC method declarations are incorrectly being
952  // reported as top-level declarations, even though their DeclContext
953  // is the containing ObjC @interface/@implementation. This is a
954  // fundamental problem in the parser right now.
955  if (isa<ObjCMethodDecl>(D))
956  return;
957 
959  Unit.addTopLevelDecl(D);
960 
961  handleFileLevelDecl(D);
962  }
963 
964  void handleFileLevelDecl(Decl *D) {
965  Unit.addFileLevelDecl(D);
966  if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
967  for (auto *I : NSD->decls())
968  handleFileLevelDecl(I);
969  }
970  }
971 
972  bool HandleTopLevelDecl(DeclGroupRef D) override {
973  for (auto *TopLevelDecl : D)
974  handleTopLevelDecl(TopLevelDecl);
975  return true;
976  }
977 
978  // We're not interested in "interesting" decls.
979  void HandleInterestingDecl(DeclGroupRef) override {}
980 
981  void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
982  for (auto *TopLevelDecl : D)
983  handleTopLevelDecl(TopLevelDecl);
984  }
985 
986  ASTMutationListener *GetASTMutationListener() override {
987  return Unit.getASTMutationListener();
988  }
989 
990  ASTDeserializationListener *GetASTDeserializationListener() override {
991  return Unit.getDeserializationListener();
992  }
993 };
994 
995 class TopLevelDeclTrackerAction : public ASTFrontendAction {
996 public:
997  ASTUnit &Unit;
998 
999  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
1000  StringRef InFile) override {
1002  llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
1003  Unit.getCurrentTopLevelHashValue()));
1004  return llvm::make_unique<TopLevelDeclTrackerConsumer>(
1005  Unit, Unit.getCurrentTopLevelHashValue());
1006  }
1007 
1008 public:
1009  TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
1010 
1011  bool hasCodeCompletionSupport() const override { return false; }
1012 
1014  return Unit.getTranslationUnitKind();
1015  }
1016 };
1017 
1018 class ASTUnitPreambleCallbacks : public PreambleCallbacks {
1019 public:
1020  unsigned getHash() const { return Hash; }
1021 
1022  std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
1023 
1024  std::vector<serialization::DeclID> takeTopLevelDeclIDs() {
1025  return std::move(TopLevelDeclIDs);
1026  }
1027 
1028  void AfterPCHEmitted(ASTWriter &Writer) override {
1029  TopLevelDeclIDs.reserve(TopLevelDecls.size());
1030  for (const auto *D : TopLevelDecls) {
1031  // Invalid top-level decls may not have been serialized.
1032  if (D->isInvalidDecl())
1033  continue;
1034  TopLevelDeclIDs.push_back(Writer.getDeclID(D));
1035  }
1036  }
1037 
1038  void HandleTopLevelDecl(DeclGroupRef DG) override {
1039  for (auto *D : DG) {
1040  // FIXME: Currently ObjC method declarations are incorrectly being
1041  // reported as top-level declarations, even though their DeclContext
1042  // is the containing ObjC @interface/@implementation. This is a
1043  // fundamental problem in the parser right now.
1044  if (isa<ObjCMethodDecl>(D))
1045  continue;
1047  TopLevelDecls.push_back(D);
1048  }
1049  }
1050 
1051  std::unique_ptr<PPCallbacks> createPPCallbacks() override {
1052  return llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(Hash);
1053  }
1054 
1055 private:
1056  unsigned Hash = 0;
1057  std::vector<Decl *> TopLevelDecls;
1058  std::vector<serialization::DeclID> TopLevelDeclIDs;
1060 };
1061 
1062 } // namespace
1063 
1064 static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
1065  return StoredDiag.getLocation().isValid();
1066 }
1067 
1068 static void
1070  // Get rid of stored diagnostics except the ones from the driver which do not
1071  // have a source location.
1072  StoredDiags.erase(
1073  std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
1074  StoredDiags.end());
1075 }
1076 
1078  StoredDiagnostics,
1079  SourceManager &SM) {
1080  // The stored diagnostic has the old source manager in it; update
1081  // the locations to refer into the new source manager. Since we've
1082  // been careful to make sure that the source manager's state
1083  // before and after are identical, so that we can reuse the source
1084  // location itself.
1085  for (auto &SD : StoredDiagnostics) {
1086  if (SD.getLocation().isValid()) {
1087  FullSourceLoc Loc(SD.getLocation(), SM);
1088  SD.setLocation(Loc);
1089  }
1090  }
1091 }
1092 
1093 /// Parse the source file into a translation unit using the given compiler
1094 /// invocation, replacing the current translation unit.
1095 ///
1096 /// \returns True if a failure occurred that causes the ASTUnit not to
1097 /// contain any translation-unit information, false otherwise.
1098 bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1099  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
1101  if (!Invocation)
1102  return true;
1103 
1104  if (VFS && FileMgr)
1105  assert(VFS == &FileMgr->getVirtualFileSystem() &&
1106  "VFS passed to Parse and VFS in FileMgr are different");
1107 
1108  auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
1109  if (OverrideMainBuffer) {
1110  assert(Preamble &&
1111  "No preamble was built, but OverrideMainBuffer is not null");
1112  Preamble->AddImplicitPreamble(*CCInvocation, VFS, OverrideMainBuffer.get());
1113  // VFS may have changed...
1114  }
1115 
1116  // Create the compiler instance to use for building the AST.
1117  std::unique_ptr<CompilerInstance> Clang(
1118  new CompilerInstance(std::move(PCHContainerOps)));
1119 
1120  // Ensure that Clang has a FileManager with the right VFS, which may have
1121  // changed above in AddImplicitPreamble. If VFS is nullptr, rely on
1122  // createFileManager to create one.
1123  if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS)
1124  Clang->setFileManager(&*FileMgr);
1125  else
1126  FileMgr = Clang->createFileManager(std::move(VFS));
1127 
1128  // Recover resources if we crash before exiting this method.
1129  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1130  CICleanup(Clang.get());
1131 
1132  Clang->setInvocation(CCInvocation);
1133  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1134 
1135  // Set up diagnostics, capturing any diagnostics that would
1136  // otherwise be dropped.
1137  Clang->setDiagnostics(&getDiagnostics());
1138 
1139  // Create the target instance.
1140  Clang->setTarget(TargetInfo::CreateTargetInfo(
1141  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1142  if (!Clang->hasTarget())
1143  return true;
1144 
1145  // Inform the target of the language options.
1146  //
1147  // FIXME: We shouldn't need to do this, the target should be immutable once
1148  // created. This complexity should be lifted elsewhere.
1149  Clang->getTarget().adjust(Clang->getLangOpts());
1150 
1151  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1152  "Invocation must have exactly one source file!");
1153  assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1155  "FIXME: AST inputs not yet supported here!");
1156  assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1158  "IR inputs not support here!");
1159 
1160  // Configure the various subsystems.
1161  LangOpts = Clang->getInvocation().LangOpts;
1162  FileSystemOpts = Clang->getFileSystemOpts();
1163 
1164  ResetForParse();
1165 
1166  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1167  UserFilesAreVolatile);
1168  if (!OverrideMainBuffer) {
1169  checkAndRemoveNonDriverDiags(StoredDiagnostics);
1170  TopLevelDeclsInPreamble.clear();
1171  }
1172 
1173  // Create a file manager object to provide access to and cache the filesystem.
1174  Clang->setFileManager(&getFileManager());
1175 
1176  // Create the source manager.
1177  Clang->setSourceManager(&getSourceManager());
1178 
1179  // If the main file has been overridden due to the use of a preamble,
1180  // make that override happen and introduce the preamble.
1181  if (OverrideMainBuffer) {
1182  // The stored diagnostic has the old source manager in it; update
1183  // the locations to refer into the new source manager. Since we've
1184  // been careful to make sure that the source manager's state
1185  // before and after are identical, so that we can reuse the source
1186  // location itself.
1187  checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1188 
1189  // Keep track of the override buffer;
1190  SavedMainFileBuffer = std::move(OverrideMainBuffer);
1191  }
1192 
1193  std::unique_ptr<TopLevelDeclTrackerAction> Act(
1194  new TopLevelDeclTrackerAction(*this));
1195 
1196  // Recover resources if we crash before exiting this method.
1197  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1198  ActCleanup(Act.get());
1199 
1200  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1201  goto error;
1202 
1203  if (SavedMainFileBuffer)
1204  TranslateStoredDiagnostics(getFileManager(), getSourceManager(),
1205  PreambleDiagnostics, StoredDiagnostics);
1206  else
1207  PreambleSrcLocCache.clear();
1208 
1209  if (llvm::Error Err = Act->Execute()) {
1210  consumeError(std::move(Err)); // FIXME this drops errors on the floor.
1211  goto error;
1212  }
1213 
1214  transferASTDataFromCompilerInstance(*Clang);
1215 
1216  Act->EndSourceFile();
1217 
1218  FailedParseDiagnostics.clear();
1219 
1220  return false;
1221 
1222 error:
1223  // Remove the overridden buffer we used for the preamble.
1224  SavedMainFileBuffer = nullptr;
1225 
1226  // Keep the ownership of the data in the ASTUnit because the client may
1227  // want to see the diagnostics.
1228  transferASTDataFromCompilerInstance(*Clang);
1229  FailedParseDiagnostics.swap(StoredDiagnostics);
1230  StoredDiagnostics.clear();
1231  NumStoredDiagnosticsFromDriver = 0;
1232  return true;
1233 }
1234 
1235 static std::pair<unsigned, unsigned>
1237  const LangOptions &LangOpts) {
1238  CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts);
1239  unsigned Offset = SM.getFileOffset(FileRange.getBegin());
1240  unsigned EndOffset = SM.getFileOffset(FileRange.getEnd());
1241  return std::make_pair(Offset, EndOffset);
1242 }
1243 
1245  const LangOptions &LangOpts,
1246  const FixItHint &InFix) {
1247  ASTUnit::StandaloneFixIt OutFix;
1248  OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts);
1250  LangOpts);
1251  OutFix.CodeToInsert = InFix.CodeToInsert;
1253  return OutFix;
1254 }
1255 
1258  const StoredDiagnostic &InDiag) {
1260  OutDiag.ID = InDiag.getID();
1261  OutDiag.Level = InDiag.getLevel();
1262  OutDiag.Message = InDiag.getMessage();
1263  OutDiag.LocOffset = 0;
1264  if (InDiag.getLocation().isInvalid())
1265  return OutDiag;
1266  const SourceManager &SM = InDiag.getLocation().getManager();
1267  SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation());
1268  OutDiag.Filename = SM.getFilename(FileLoc);
1269  if (OutDiag.Filename.empty())
1270  return OutDiag;
1271  OutDiag.LocOffset = SM.getFileOffset(FileLoc);
1272  for (const auto &Range : InDiag.getRanges())
1273  OutDiag.Ranges.push_back(makeStandaloneRange(Range, SM, LangOpts));
1274  for (const auto &FixIt : InDiag.getFixIts())
1275  OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, FixIt));
1276 
1277  return OutDiag;
1278 }
1279 
1280 /// Attempt to build or re-use a precompiled preamble when (re-)parsing
1281 /// the source file.
1282 ///
1283 /// This routine will compute the preamble of the main source file. If a
1284 /// non-trivial preamble is found, it will precompile that preamble into a
1285 /// precompiled header so that the precompiled preamble can be used to reduce
1286 /// reparsing time. If a precompiled preamble has already been constructed,
1287 /// this routine will determine if it is still valid and, if so, avoid
1288 /// rebuilding the precompiled preamble.
1289 ///
1290 /// \param AllowRebuild When true (the default), this routine is
1291 /// allowed to rebuild the precompiled preamble if it is found to be
1292 /// out-of-date.
1293 ///
1294 /// \param MaxLines When non-zero, the maximum number of lines that
1295 /// can occur within the preamble.
1296 ///
1297 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1298 /// buffer that should be used in place of the main file when doing so.
1299 /// Otherwise, returns a NULL pointer.
1300 std::unique_ptr<llvm::MemoryBuffer>
1301 ASTUnit::getMainBufferWithPrecompiledPreamble(
1302  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1303  CompilerInvocation &PreambleInvocationIn,
1304  IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool AllowRebuild,
1305  unsigned MaxLines) {
1306  auto MainFilePath =
1307  PreambleInvocationIn.getFrontendOpts().Inputs[0].getFile();
1308  std::unique_ptr<llvm::MemoryBuffer> MainFileBuffer =
1309  getBufferForFileHandlingRemapping(PreambleInvocationIn, VFS.get(),
1310  MainFilePath, UserFilesAreVolatile);
1311  if (!MainFileBuffer)
1312  return nullptr;
1313 
1314  PreambleBounds Bounds =
1315  ComputePreambleBounds(*PreambleInvocationIn.getLangOpts(),
1316  MainFileBuffer.get(), MaxLines);
1317  if (!Bounds.Size)
1318  return nullptr;
1319 
1320  if (Preamble) {
1321  if (Preamble->CanReuse(PreambleInvocationIn, MainFileBuffer.get(), Bounds,
1322  VFS.get())) {
1323  // Okay! We can re-use the precompiled preamble.
1324 
1325  // Set the state of the diagnostic object to mimic its state
1326  // after parsing the preamble.
1327  getDiagnostics().Reset();
1329  PreambleInvocationIn.getDiagnosticOpts());
1330  getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1331 
1332  PreambleRebuildCountdown = 1;
1333  return MainFileBuffer;
1334  } else {
1335  Preamble.reset();
1336  PreambleDiagnostics.clear();
1337  TopLevelDeclsInPreamble.clear();
1338  PreambleSrcLocCache.clear();
1339  PreambleRebuildCountdown = 1;
1340  }
1341  }
1342 
1343  // If the preamble rebuild counter > 1, it's because we previously
1344  // failed to build a preamble and we're not yet ready to try
1345  // again. Decrement the counter and return a failure.
1346  if (PreambleRebuildCountdown > 1) {
1347  --PreambleRebuildCountdown;
1348  return nullptr;
1349  }
1350 
1351  assert(!Preamble && "No Preamble should be stored at that point");
1352  // If we aren't allowed to rebuild the precompiled preamble, just
1353  // return now.
1354  if (!AllowRebuild)
1355  return nullptr;
1356 
1357  ++PreambleCounter;
1358 
1359  SmallVector<StandaloneDiagnostic, 4> NewPreambleDiagsStandalone;
1360  SmallVector<StoredDiagnostic, 4> NewPreambleDiags;
1361  ASTUnitPreambleCallbacks Callbacks;
1362  {
1364  if (CaptureDiagnostics != CaptureDiagsKind::None)
1365  Capture.emplace(CaptureDiagnostics, *Diagnostics, &NewPreambleDiags,
1366  &NewPreambleDiagsStandalone);
1367 
1368  // We did not previously compute a preamble, or it can't be reused anyway.
1369  SimpleTimer PreambleTimer(WantTiming);
1370  PreambleTimer.setOutput("Precompiling preamble");
1371 
1372  const bool PreviousSkipFunctionBodies =
1373  PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies;
1374  if (SkipFunctionBodies == SkipFunctionBodiesScope::Preamble)
1375  PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies = true;
1376 
1377  llvm::ErrorOr<PrecompiledPreamble> NewPreamble = PrecompiledPreamble::Build(
1378  PreambleInvocationIn, MainFileBuffer.get(), Bounds, *Diagnostics, VFS,
1379  PCHContainerOps, /*StoreInMemory=*/false, Callbacks);
1380 
1381  PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies =
1382  PreviousSkipFunctionBodies;
1383 
1384  if (NewPreamble) {
1385  Preamble = std::move(*NewPreamble);
1386  PreambleRebuildCountdown = 1;
1387  } else {
1388  switch (static_cast<BuildPreambleError>(NewPreamble.getError().value())) {
1390  // Try again next time.
1391  PreambleRebuildCountdown = 1;
1392  return nullptr;
1397  // These erros are more likely to repeat, retry after some period.
1398  PreambleRebuildCountdown = DefaultPreambleRebuildInterval;
1399  return nullptr;
1400  }
1401  llvm_unreachable("unexpected BuildPreambleError");
1402  }
1403  }
1404 
1405  assert(Preamble && "Preamble wasn't built");
1406 
1407  TopLevelDecls.clear();
1408  TopLevelDeclsInPreamble = Callbacks.takeTopLevelDeclIDs();
1409  PreambleTopLevelHashValue = Callbacks.getHash();
1410 
1411  NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1412 
1413  checkAndRemoveNonDriverDiags(NewPreambleDiags);
1414  StoredDiagnostics = std::move(NewPreambleDiags);
1415  PreambleDiagnostics = std::move(NewPreambleDiagsStandalone);
1416 
1417  // If the hash of top-level entities differs from the hash of the top-level
1418  // entities the last time we rebuilt the preamble, clear out the completion
1419  // cache.
1420  if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1421  CompletionCacheTopLevelHashValue = 0;
1422  PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1423  }
1424 
1425  return MainFileBuffer;
1426 }
1427 
1428 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1429  assert(Preamble && "Should only be called when preamble was built");
1430 
1431  std::vector<Decl *> Resolved;
1432  Resolved.reserve(TopLevelDeclsInPreamble.size());
1434  for (const auto TopLevelDecl : TopLevelDeclsInPreamble) {
1435  // Resolve the declaration ID to an actual declaration, possibly
1436  // deserializing the declaration in the process.
1437  if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1438  Resolved.push_back(D);
1439  }
1440  TopLevelDeclsInPreamble.clear();
1441  TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1442 }
1443 
1444 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1445  // Steal the created target, context, and preprocessor if they have been
1446  // created.
1447  assert(CI.hasInvocation() && "missing invocation");
1448  LangOpts = CI.getInvocation().LangOpts;
1449  TheSema = CI.takeSema();
1450  Consumer = CI.takeASTConsumer();
1451  if (CI.hasASTContext())
1452  Ctx = &CI.getASTContext();
1453  if (CI.hasPreprocessor())
1454  PP = CI.getPreprocessorPtr();
1455  CI.setSourceManager(nullptr);
1456  CI.setFileManager(nullptr);
1457  if (CI.hasTarget())
1458  Target = &CI.getTarget();
1459  Reader = CI.getModuleManager();
1460  HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1461 }
1462 
1463 StringRef ASTUnit::getMainFileName() const {
1464  if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1465  const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1466  if (Input.isFile())
1467  return Input.getFile();
1468  else
1469  return Input.getBuffer()->getBufferIdentifier();
1470  }
1471 
1472  if (SourceMgr) {
1473  if (const FileEntry *
1474  FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1475  return FE->getName();
1476  }
1477 
1478  return {};
1479 }
1480 
1481 StringRef ASTUnit::getASTFileName() const {
1482  if (!isMainFileAST())
1483  return {};
1484 
1486  Mod = Reader->getModuleManager().getPrimaryModule();
1487  return Mod.FileName;
1488 }
1489 
1490 std::unique_ptr<ASTUnit>
1491 ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
1493  CaptureDiagsKind CaptureDiagnostics,
1494  bool UserFilesAreVolatile) {
1495  std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1496  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1498  createVFSFromCompilerInvocation(*CI, *Diags);
1499  AST->Diagnostics = Diags;
1500  AST->FileSystemOpts = CI->getFileSystemOpts();
1501  AST->Invocation = std::move(CI);
1502  AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1503  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1504  AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1505  UserFilesAreVolatile);
1506  AST->ModuleCache = new InMemoryModuleCache;
1507 
1508  return AST;
1509 }
1510 
1512  std::shared_ptr<CompilerInvocation> CI,
1513  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1515  ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
1516  bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
1517  unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults,
1518  bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
1519  std::unique_ptr<ASTUnit> *ErrAST) {
1520  assert(CI && "A CompilerInvocation is required");
1521 
1522  std::unique_ptr<ASTUnit> OwnAST;
1523  ASTUnit *AST = Unit;
1524  if (!AST) {
1525  // Create the AST unit.
1526  OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile);
1527  AST = OwnAST.get();
1528  if (!AST)
1529  return nullptr;
1530  }
1531 
1532  if (!ResourceFilesPath.empty()) {
1533  // Override the resources path.
1534  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1535  }
1536  AST->OnlyLocalDecls = OnlyLocalDecls;
1537  AST->CaptureDiagnostics = CaptureDiagnostics;
1538  if (PrecompilePreambleAfterNParses > 0)
1539  AST->PreambleRebuildCountdown = PrecompilePreambleAfterNParses;
1540  AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1541  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1542  AST->IncludeBriefCommentsInCodeCompletion
1543  = IncludeBriefCommentsInCodeCompletion;
1544 
1545  // Recover resources if we crash before exiting this method.
1546  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1547  ASTUnitCleanup(OwnAST.get());
1548  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1549  llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1550  DiagCleanup(Diags.get());
1551 
1552  // We'll manage file buffers ourselves.
1553  CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1554  CI->getFrontendOpts().DisableFree = false;
1555  ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1556 
1557  // Create the compiler instance to use for building the AST.
1558  std::unique_ptr<CompilerInstance> Clang(
1559  new CompilerInstance(std::move(PCHContainerOps)));
1560 
1561  // Recover resources if we crash before exiting this method.
1562  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1563  CICleanup(Clang.get());
1564 
1565  Clang->setInvocation(std::move(CI));
1566  AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1567 
1568  // Set up diagnostics, capturing any diagnostics that would
1569  // otherwise be dropped.
1570  Clang->setDiagnostics(&AST->getDiagnostics());
1571 
1572  // Create the target instance.
1573  Clang->setTarget(TargetInfo::CreateTargetInfo(
1574  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1575  if (!Clang->hasTarget())
1576  return nullptr;
1577 
1578  // Inform the target of the language options.
1579  //
1580  // FIXME: We shouldn't need to do this, the target should be immutable once
1581  // created. This complexity should be lifted elsewhere.
1582  Clang->getTarget().adjust(Clang->getLangOpts());
1583 
1584  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1585  "Invocation must have exactly one source file!");
1586  assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1588  "FIXME: AST inputs not yet supported here!");
1589  assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1591  "IR inputs not support here!");
1592 
1593  // Configure the various subsystems.
1594  AST->TheSema.reset();
1595  AST->Ctx = nullptr;
1596  AST->PP = nullptr;
1597  AST->Reader = nullptr;
1598 
1599  // Create a file manager object to provide access to and cache the filesystem.
1600  Clang->setFileManager(&AST->getFileManager());
1601 
1602  // Create the source manager.
1603  Clang->setSourceManager(&AST->getSourceManager());
1604 
1605  FrontendAction *Act = Action;
1606 
1607  std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
1608  if (!Act) {
1609  TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1610  Act = TrackerAct.get();
1611  }
1612 
1613  // Recover resources if we crash before exiting this method.
1614  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1615  ActCleanup(TrackerAct.get());
1616 
1617  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1618  AST->transferASTDataFromCompilerInstance(*Clang);
1619  if (OwnAST && ErrAST)
1620  ErrAST->swap(OwnAST);
1621 
1622  return nullptr;
1623  }
1624 
1625  if (Persistent && !TrackerAct) {
1626  Clang->getPreprocessor().addPPCallbacks(
1627  llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
1628  AST->getCurrentTopLevelHashValue()));
1629  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
1630  if (Clang->hasASTConsumer())
1631  Consumers.push_back(Clang->takeASTConsumer());
1632  Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>(
1633  *AST, AST->getCurrentTopLevelHashValue()));
1634  Clang->setASTConsumer(
1635  llvm::make_unique<MultiplexConsumer>(std::move(Consumers)));
1636  }
1637  if (llvm::Error Err = Act->Execute()) {
1638  consumeError(std::move(Err)); // FIXME this drops errors on the floor.
1639  AST->transferASTDataFromCompilerInstance(*Clang);
1640  if (OwnAST && ErrAST)
1641  ErrAST->swap(OwnAST);
1642 
1643  return nullptr;
1644  }
1645 
1646  // Steal the created target, context, and preprocessor.
1647  AST->transferASTDataFromCompilerInstance(*Clang);
1648 
1649  Act->EndSourceFile();
1650 
1651  if (OwnAST)
1652  return OwnAST.release();
1653  else
1654  return AST;
1655 }
1656 
1657 bool ASTUnit::LoadFromCompilerInvocation(
1658  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1659  unsigned PrecompilePreambleAfterNParses,
1661  if (!Invocation)
1662  return true;
1663 
1664  assert(VFS && "VFS is null");
1665 
1666  // We'll manage file buffers ourselves.
1667  Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1668  Invocation->getFrontendOpts().DisableFree = false;
1669  getDiagnostics().Reset();
1670  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1671 
1672  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1673  if (PrecompilePreambleAfterNParses > 0) {
1674  PreambleRebuildCountdown = PrecompilePreambleAfterNParses;
1675  OverrideMainBuffer =
1676  getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1677  getDiagnostics().Reset();
1678  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1679  }
1680 
1681  SimpleTimer ParsingTimer(WantTiming);
1682  ParsingTimer.setOutput("Parsing " + getMainFileName());
1683 
1684  // Recover resources if we crash before exiting this method.
1685  llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1686  MemBufferCleanup(OverrideMainBuffer.get());
1687 
1688  return Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1689 }
1690 
1691 std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
1692  std::shared_ptr<CompilerInvocation> CI,
1693  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1695  bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
1696  unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1697  bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1698  bool UserFilesAreVolatile) {
1699  // Create the AST unit.
1700  std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1701  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1702  AST->Diagnostics = Diags;
1703  AST->OnlyLocalDecls = OnlyLocalDecls;
1704  AST->CaptureDiagnostics = CaptureDiagnostics;
1705  AST->TUKind = TUKind;
1706  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1707  AST->IncludeBriefCommentsInCodeCompletion
1708  = IncludeBriefCommentsInCodeCompletion;
1709  AST->Invocation = std::move(CI);
1710  AST->FileSystemOpts = FileMgr->getFileSystemOpts();
1711  AST->FileMgr = FileMgr;
1712  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1713 
1714  // Recover resources if we crash before exiting this method.
1715  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1716  ASTUnitCleanup(AST.get());
1717  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1718  llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1719  DiagCleanup(Diags.get());
1720 
1721  if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1722  PrecompilePreambleAfterNParses,
1723  &AST->FileMgr->getVirtualFileSystem()))
1724  return nullptr;
1725  return AST;
1726 }
1727 
1729  const char **ArgBegin, const char **ArgEnd,
1730  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1731  IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
1732  bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
1733  ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
1734  unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1735  bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1736  bool AllowPCHWithCompilerErrors, SkipFunctionBodiesScope SkipFunctionBodies,
1737  bool SingleFileParse, bool UserFilesAreVolatile, bool ForSerialization,
1738  llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST,
1740  assert(Diags.get() && "no DiagnosticsEngine was provided");
1741 
1742  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1743 
1744  std::shared_ptr<CompilerInvocation> CI;
1745 
1746  {
1747  CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1748  &StoredDiagnostics, nullptr);
1749 
1751  llvm::makeArrayRef(ArgBegin, ArgEnd), Diags, VFS);
1752  if (!CI)
1753  return nullptr;
1754  }
1755 
1756  // Override any files that need remapping
1757  for (const auto &RemappedFile : RemappedFiles) {
1758  CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1759  RemappedFile.second);
1760  }
1761  PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1762  PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1763  PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1764  PPOpts.SingleFileParseMode = SingleFileParse;
1765 
1766  // Override the resources path.
1767  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1768 
1769  CI->getFrontendOpts().SkipFunctionBodies =
1770  SkipFunctionBodies == SkipFunctionBodiesScope::PreambleAndMainFile;
1771 
1772  if (ModuleFormat)
1773  CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue();
1774 
1775  // Create the AST unit.
1776  std::unique_ptr<ASTUnit> AST;
1777  AST.reset(new ASTUnit(false));
1778  AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1779  AST->StoredDiagnostics.swap(StoredDiagnostics);
1780  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1781  AST->Diagnostics = Diags;
1782  AST->FileSystemOpts = CI->getFileSystemOpts();
1783  if (!VFS)
1784  VFS = llvm::vfs::getRealFileSystem();
1785  VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS);
1786  AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1787  AST->ModuleCache = new InMemoryModuleCache;
1788  AST->OnlyLocalDecls = OnlyLocalDecls;
1789  AST->CaptureDiagnostics = CaptureDiagnostics;
1790  AST->TUKind = TUKind;
1791  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1792  AST->IncludeBriefCommentsInCodeCompletion
1793  = IncludeBriefCommentsInCodeCompletion;
1794  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1795  AST->Invocation = CI;
1796  AST->SkipFunctionBodies = SkipFunctionBodies;
1797  if (ForSerialization)
1798  AST->WriterData.reset(new ASTWriterData(*AST->ModuleCache));
1799  // Zero out now to ease cleanup during crash recovery.
1800  CI = nullptr;
1801  Diags = nullptr;
1802 
1803  // Recover resources if we crash before exiting this method.
1804  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1805  ASTUnitCleanup(AST.get());
1806 
1807  if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1808  PrecompilePreambleAfterNParses,
1809  VFS)) {
1810  // Some error occurred, if caller wants to examine diagnostics, pass it the
1811  // ASTUnit.
1812  if (ErrAST) {
1813  AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
1814  ErrAST->swap(AST);
1815  }
1816  return nullptr;
1817  }
1818 
1819  return AST.release();
1820 }
1821 
1822 bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1823  ArrayRef<RemappedFile> RemappedFiles,
1825  if (!Invocation)
1826  return true;
1827 
1828  if (!VFS) {
1829  assert(FileMgr && "FileMgr is null on Reparse call");
1830  VFS = &FileMgr->getVirtualFileSystem();
1831  }
1832 
1833  clearFileLevelDecls();
1834 
1835  SimpleTimer ParsingTimer(WantTiming);
1836  ParsingTimer.setOutput("Reparsing " + getMainFileName());
1837 
1838  // Remap files.
1839  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1840  for (const auto &RB : PPOpts.RemappedFileBuffers)
1841  delete RB.second;
1842 
1843  Invocation->getPreprocessorOpts().clearRemappedFiles();
1844  for (const auto &RemappedFile : RemappedFiles) {
1845  Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1846  RemappedFile.second);
1847  }
1848 
1849  // If we have a preamble file lying around, or if we might try to
1850  // build a precompiled preamble, do so now.
1851  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1852  if (Preamble || PreambleRebuildCountdown > 0)
1853  OverrideMainBuffer =
1854  getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1855 
1856  // Clear out the diagnostics state.
1857  FileMgr.reset();
1858  getDiagnostics().Reset();
1859  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1860  if (OverrideMainBuffer)
1861  getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1862 
1863  // Parse the sources
1864  bool Result =
1865  Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1866 
1867  // If we're caching global code-completion results, and the top-level
1868  // declarations have changed, clear out the code-completion cache.
1869  if (!Result && ShouldCacheCodeCompletionResults &&
1870  CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1871  CacheCodeCompletionResults();
1872 
1873  // We now need to clear out the completion info related to this translation
1874  // unit; it'll be recreated if necessary.
1875  CCTUInfo.reset();
1876 
1877  return Result;
1878 }
1879 
1881  SavedMainFileBuffer.reset();
1882 
1883  SourceMgr.reset();
1884  TheSema.reset();
1885  Ctx.reset();
1886  PP.reset();
1887  Reader.reset();
1888 
1889  TopLevelDecls.clear();
1890  clearFileLevelDecls();
1891 }
1892 
1893 //----------------------------------------------------------------------------//
1894 // Code completion
1895 //----------------------------------------------------------------------------//
1896 
1897 namespace {
1898 
1899  /// Code completion consumer that combines the cached code-completion
1900  /// results from an ASTUnit with the code-completion results provided to it,
1901  /// then passes the result on to
1902  class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1903  uint64_t NormalContexts;
1904  ASTUnit &AST;
1905  CodeCompleteConsumer &Next;
1906 
1907  public:
1908  AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1909  const CodeCompleteOptions &CodeCompleteOpts)
1910  : CodeCompleteConsumer(CodeCompleteOpts), AST(AST), Next(Next) {
1911  // Compute the set of contexts in which we will look when we don't have
1912  // any information about the specific context.
1913  NormalContexts
1927 
1928  if (AST.getASTContext().getLangOpts().CPlusPlus)
1929  NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
1932  }
1933 
1934  void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
1935  CodeCompletionResult *Results,
1936  unsigned NumResults) override;
1937 
1938  void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1939  OverloadCandidate *Candidates,
1940  unsigned NumCandidates,
1941  SourceLocation OpenParLoc) override {
1942  Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates,
1943  OpenParLoc);
1944  }
1945 
1946  CodeCompletionAllocator &getAllocator() override {
1947  return Next.getAllocator();
1948  }
1949 
1951  return Next.getCodeCompletionTUInfo();
1952  }
1953  };
1954 
1955 } // namespace
1956 
1957 /// Helper function that computes which global names are hidden by the
1958 /// local code-completion results.
1959 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1960  CodeCompletionResult *Results,
1961  unsigned NumResults,
1962  ASTContext &Ctx,
1963  llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1964  bool OnlyTagNames = false;
1965  switch (Context.getKind()) {
1984  break;
1985 
1989  OnlyTagNames = true;
1990  break;
1991 
2007  // We're looking for nothing, or we're looking for names that cannot
2008  // be hidden.
2009  return;
2010  }
2011 
2012  using Result = CodeCompletionResult;
2013  for (unsigned I = 0; I != NumResults; ++I) {
2014  if (Results[I].Kind != Result::RK_Declaration)
2015  continue;
2016 
2017  unsigned IDNS
2019 
2020  bool Hiding = false;
2021  if (OnlyTagNames)
2022  Hiding = (IDNS & Decl::IDNS_Tag);
2023  else {
2024  unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2027  if (Ctx.getLangOpts().CPlusPlus)
2028  HiddenIDNS |= Decl::IDNS_Tag;
2029  Hiding = (IDNS & HiddenIDNS);
2030  }
2031 
2032  if (!Hiding)
2033  continue;
2034 
2035  DeclarationName Name = Results[I].Declaration->getDeclName();
2037  HiddenNames.insert(Identifier->getName());
2038  else
2039  HiddenNames.insert(Name.getAsString());
2040  }
2041 }
2042 
2043 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2044  CodeCompletionContext Context,
2045  CodeCompletionResult *Results,
2046  unsigned NumResults) {
2047  // Merge the results we were given with the results we cached.
2048  bool AddedResult = false;
2049  uint64_t InContexts =
2051  ? NormalContexts : (1LL << Context.getKind());
2052  // Contains the set of names that are hidden by "local" completion results.
2053  llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2054  using Result = CodeCompletionResult;
2055  SmallVector<Result, 8> AllResults;
2057  C = AST.cached_completion_begin(),
2058  CEnd = AST.cached_completion_end();
2059  C != CEnd; ++C) {
2060  // If the context we are in matches any of the contexts we are
2061  // interested in, we'll add this result.
2062  if ((C->ShowInContexts & InContexts) == 0)
2063  continue;
2064 
2065  // If we haven't added any results previously, do so now.
2066  if (!AddedResult) {
2067  CalculateHiddenNames(Context, Results, NumResults, S.Context,
2068  HiddenNames);
2069  AllResults.insert(AllResults.end(), Results, Results + NumResults);
2070  AddedResult = true;
2071  }
2072 
2073  // Determine whether this global completion result is hidden by a local
2074  // completion result. If so, skip it.
2075  if (C->Kind != CXCursor_MacroDefinition &&
2076  HiddenNames.count(C->Completion->getTypedText()))
2077  continue;
2078 
2079  // Adjust priority based on similar type classes.
2080  unsigned Priority = C->Priority;
2081  CodeCompletionString *Completion = C->Completion;
2082  if (!Context.getPreferredType().isNull()) {
2083  if (C->Kind == CXCursor_MacroDefinition) {
2084  Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2085  S.getLangOpts(),
2086  Context.getPreferredType()->isAnyPointerType());
2087  } else if (C->Type) {
2090  Context.getPreferredType().getUnqualifiedType());
2091  SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2092  if (ExpectedSTC == C->TypeClass) {
2093  // We know this type is similar; check for an exact match.
2094  llvm::StringMap<unsigned> &CachedCompletionTypes
2095  = AST.getCachedCompletionTypes();
2096  llvm::StringMap<unsigned>::iterator Pos
2097  = CachedCompletionTypes.find(QualType(Expected).getAsString());
2098  if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2099  Priority /= CCF_ExactTypeMatch;
2100  else
2101  Priority /= CCF_SimilarTypeMatch;
2102  }
2103  }
2104  }
2105 
2106  // Adjust the completion string, if required.
2107  if (C->Kind == CXCursor_MacroDefinition &&
2109  // Create a new code-completion string that just contains the
2110  // macro name, without its arguments.
2111  CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2112  CCP_CodePattern, C->Availability);
2113  Builder.AddTypedTextChunk(C->Completion->getTypedText());
2114  Priority = CCP_CodePattern;
2115  Completion = Builder.TakeString();
2116  }
2117 
2118  AllResults.push_back(Result(Completion, Priority, C->Kind,
2119  C->Availability));
2120  }
2121 
2122  // If we did not add any cached completion results, just forward the
2123  // results we were given to the next consumer.
2124  if (!AddedResult) {
2125  Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2126  return;
2127  }
2128 
2129  Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2130  AllResults.size());
2131 }
2132 
2134  StringRef File, unsigned Line, unsigned Column,
2135  ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
2136  bool IncludeCodePatterns, bool IncludeBriefComments,
2137  CodeCompleteConsumer &Consumer,
2138  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2139  DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr,
2140  FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2142  if (!Invocation)
2143  return;
2144 
2145  SimpleTimer CompletionTimer(WantTiming);
2146  CompletionTimer.setOutput("Code completion @ " + File + ":" +
2147  Twine(Line) + ":" + Twine(Column));
2148 
2149  auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
2150 
2151  FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2152  CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2153  PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2154 
2155  CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2156  CachedCompletionResults.empty();
2157  CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2158  CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2159  CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2160  CodeCompleteOpts.LoadExternal = Consumer.loadExternal();
2161  CodeCompleteOpts.IncludeFixIts = Consumer.includeFixIts();
2162 
2163  assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2164 
2165  FrontendOpts.CodeCompletionAt.FileName = File;
2166  FrontendOpts.CodeCompletionAt.Line = Line;
2167  FrontendOpts.CodeCompletionAt.Column = Column;
2168 
2169  // Set the language options appropriately.
2170  LangOpts = *CCInvocation->getLangOpts();
2171 
2172  // Spell-checking and warnings are wasteful during code-completion.
2173  LangOpts.SpellChecking = false;
2174  CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
2175 
2176  std::unique_ptr<CompilerInstance> Clang(
2177  new CompilerInstance(PCHContainerOps));
2178 
2179  // Recover resources if we crash before exiting this method.
2180  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2181  CICleanup(Clang.get());
2182 
2183  auto &Inv = *CCInvocation;
2184  Clang->setInvocation(std::move(CCInvocation));
2185  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2186 
2187  // Set up diagnostics, capturing any diagnostics produced.
2188  Clang->setDiagnostics(&Diag);
2189  CaptureDroppedDiagnostics Capture(CaptureDiagsKind::All,
2190  Clang->getDiagnostics(),
2191  &StoredDiagnostics, nullptr);
2192  ProcessWarningOptions(Diag, Inv.getDiagnosticOpts());
2193 
2194  // Create the target instance.
2195  Clang->setTarget(TargetInfo::CreateTargetInfo(
2196  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
2197  if (!Clang->hasTarget()) {
2198  Clang->setInvocation(nullptr);
2199  return;
2200  }
2201 
2202  // Inform the target of the language options.
2203  //
2204  // FIXME: We shouldn't need to do this, the target should be immutable once
2205  // created. This complexity should be lifted elsewhere.
2206  Clang->getTarget().adjust(Clang->getLangOpts());
2207 
2208  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2209  "Invocation must have exactly one source file!");
2210  assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
2212  "FIXME: AST inputs not yet supported here!");
2213  assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
2215  "IR inputs not support here!");
2216 
2217  // Use the source and file managers that we were given.
2218  Clang->setFileManager(&FileMgr);
2219  Clang->setSourceManager(&SourceMgr);
2220 
2221  // Remap files.
2222  PreprocessorOpts.clearRemappedFiles();
2223  PreprocessorOpts.RetainRemappedFileBuffers = true;
2224  for (const auto &RemappedFile : RemappedFiles) {
2225  PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second);
2226  OwnedBuffers.push_back(RemappedFile.second);
2227  }
2228 
2229  // Use the code completion consumer we were given, but adding any cached
2230  // code-completion results.
2231  AugmentedCodeCompleteConsumer *AugmentedConsumer
2232  = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2233  Clang->setCodeCompletionConsumer(AugmentedConsumer);
2234 
2235  // If we have a precompiled preamble, try to use it. We only allow
2236  // the use of the precompiled preamble if we're if the completion
2237  // point is within the main file, after the end of the precompiled
2238  // preamble.
2239  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2240  if (Preamble) {
2241  std::string CompleteFilePath(File);
2242 
2243  auto &VFS = FileMgr.getVirtualFileSystem();
2244  auto CompleteFileStatus = VFS.status(CompleteFilePath);
2245  if (CompleteFileStatus) {
2246  llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID();
2247 
2248  std::string MainPath(OriginalSourceFile);
2249  auto MainStatus = VFS.status(MainPath);
2250  if (MainStatus) {
2251  llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID();
2252  if (CompleteFileID == MainID && Line > 1)
2253  OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2254  PCHContainerOps, Inv, &VFS, false, Line - 1);
2255  }
2256  }
2257  }
2258 
2259  // If the main file has been overridden due to the use of a preamble,
2260  // make that override happen and introduce the preamble.
2261  if (OverrideMainBuffer) {
2262  assert(Preamble &&
2263  "No preamble was built, but OverrideMainBuffer is not null");
2264 
2266  &FileMgr.getVirtualFileSystem();
2267  Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS,
2268  OverrideMainBuffer.get());
2269  // FIXME: there is no way to update VFS if it was changed by
2270  // AddImplicitPreamble as FileMgr is accepted as a parameter by this method.
2271  // We use on-disk preambles instead and rely on FileMgr's VFS to ensure the
2272  // PCH files are always readable.
2273  OwnedBuffers.push_back(OverrideMainBuffer.release());
2274  } else {
2275  PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2276  PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2277  }
2278 
2279  // Disable the preprocessing record if modules are not enabled.
2280  if (!Clang->getLangOpts().Modules)
2281  PreprocessorOpts.DetailedRecord = false;
2282 
2283  std::unique_ptr<SyntaxOnlyAction> Act;
2284  Act.reset(new SyntaxOnlyAction);
2285  if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2286  if (llvm::Error Err = Act->Execute()) {
2287  consumeError(std::move(Err)); // FIXME this drops errors on the floor.
2288  }
2289  Act->EndSourceFile();
2290  }
2291 }
2292 
2293 bool ASTUnit::Save(StringRef File) {
2294  if (HadModuleLoaderFatalFailure)
2295  return true;
2296 
2297  // Write to a temporary file and later rename it to the actual file, to avoid
2298  // possible race conditions.
2299  SmallString<128> TempPath;
2300  TempPath = File;
2301  TempPath += "-%%%%%%%%";
2302  int fd;
2303  if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath))
2304  return true;
2305 
2306  // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2307  // unconditionally create a stat cache when we parse the file?
2308  llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2309 
2310  serialize(Out);
2311  Out.close();
2312  if (Out.has_error()) {
2313  Out.clear_error();
2314  return true;
2315  }
2316 
2317  if (llvm::sys::fs::rename(TempPath, File)) {
2318  llvm::sys::fs::remove(TempPath);
2319  return true;
2320  }
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  const FileEntry *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  LocDeclsTy *&Decls = FileDecls[FID];
2440  if (!Decls)
2441  Decls = new 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  InputKind::Language Lang;
2672  if (LangOpts.OpenCL)
2673  Lang = InputKind::OpenCL;
2674  else if (LangOpts.CUDA)
2675  Lang = InputKind::CUDA;
2676  else if (LangOpts.RenderScript)
2677  Lang = InputKind::RenderScript;
2678  else if (LangOpts.CPlusPlus)
2679  Lang = LangOpts.ObjC ? InputKind::ObjCXX : InputKind::CXX;
2680  else
2681  Lang = LangOpts.ObjC ? InputKind::ObjC : InputKind::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 llvm::sys::MutexImpl(/*recursive=*/true);
2696 }
2697 
2698 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2699  delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2700 }
2701 
2702 void ASTUnit::ConcurrencyState::start() {
2703  bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2704  assert(acquired && "Concurrent access to ASTUnit!");
2705 }
2706 
2707 void ASTUnit::ConcurrencyState::finish() {
2708  static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
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:1466
void ResetForParse()
Free data that will be re-generated on the next parse.
Definition: ASTUnit.cpp:1880
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:1718
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:510
A (possibly-)qualified type.
Definition: Type.h:643
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:139
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:116
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.
Defines the clang::Module class, which describes a module in the source code.
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:88
Divide by this factor when a code-completion result&#39;s type exactly matches the type we expect...
ModuleKind Kind
The type of this module.
Definition: Module.h:119
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:232
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Definition: Diagnostic.h:1445
StringRef getASTFileName() const
If this ASTUnit came from an AST file, returns the filename for it.
Definition: ASTUnit.cpp:1481
std::string CodeToInsert
The actual code to insert at the insertion location, as a string.
Definition: Diagnostic.h:78
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:1491
bool isCompilingModule() const
Are we compiling a module interface (.cppm or module map)?
Definition: LangOptions.h:263
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:518
unsigned getIdentifierNamespace() const
Definition: DeclBase.h:799
Code completion occurred where an Objective-C message receiver is expected.
ASTContext & getASTContext() const
The AST file has errors.
Definition: ASTReader.h:403
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:1528
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:1496
NamedDecl * getUnderlyingDecl()
Looks through UsingDecls and ObjCCompatibleAliasDecls for the underlying named decl.
Definition: Decl.h:431
static void checkAndSanitizeDiags(SmallVectorImpl< StoredDiagnostic > &StoredDiagnostics, SourceManager &SM)
Definition: ASTUnit.cpp:1077
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:37
bool hasSourceManager() const
Definition: Diagnostic.h:1336
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:132
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, 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:1728
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:297
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:99
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:154
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:237
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:146
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:734
void setPrintingPolicy(const clang::PrintingPolicy &Policy)
Definition: ASTContext.h:661
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:49
Compiling a module from a module map.
Definition: LangOptions.h:78
Describes a module or submodule.
Definition: Module.h:64
StringRef getMainFileName() const
Definition: ASTUnit.cpp:1463
Languages that the frontend can parse and compile.
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:272
std::unique_ptr< CompilerInvocation > createInvocationFromCommandLine(ArrayRef< const char *> Args, IntrusiveRefCntPtr< DiagnosticsEngine > Diags=IntrusiveRefCntPtr< DiagnosticsEngine >(), IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
createInvocationFromCommandLine - Construct a compiler invocation object for a command line argument ...
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:142
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:755
const SourceLocation & getLocation() const
Definition: Diagnostic.h:1335
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
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:149
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:1479
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:727
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
Definition: DeclBase.h:828
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
const LangOptions & getLangOpts() const
Definition: Sema.h:1285
static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag)
Definition: ASTUnit.cpp:1064
StringRef getMessage() const
Definition: Diagnostic.h:1469
void Reset()
Reset the state of the diagnostic object to its initial configuration.
Definition: Diagnostic.cpp:121
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
The AST file itself appears corrupted.
Definition: ASTReader.h:386
Ordinary names.
Definition: DeclBase.h:146
virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, SourceLocation OpenParLoc)
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.
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
static void checkAndRemoveNonDriverDiags(SmallVectorImpl< StoredDiagnostic > &StoredDiags)
Definition: ASTUnit.cpp:1069
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:328
Code completion occurred within a "top-level" completion context, e.g., at namespace or global scope...
static void AddDefinedMacroToHash(const Token &MacroNameTok, unsigned &Hash)
Add the given macro to the hash of all top-level entities.
Definition: ASTUnit.cpp:871
This declaration is a C++ operator declared in a non-class context.
Definition: DeclBase.h:170
StringRef Filename
Definition: Format.cpp:1711
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:4644
bool isModuleFile() const
Returns true if the ASTUnit was constructed from a serialized module file.
Definition: ASTUnit.cpp:2664
Divide by this factor when a code-completion result&#39;s type is similar to the type we expect (e...
IntrusiveRefCntPtr< ASTReader > getModuleManager() const
unsigned Offset
Definition: Format.cpp:1713
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:236
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:717
Defines the clang::LangOptions interface.
bool isMainFileAST() const
Definition: ASTUnit.h:424
Represents a character-granular source range.
DiagnosticsEngine::Level getLevel() const
Definition: Diagnostic.h:1467
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:894
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:753
const AnnotatedLine * Line
ArrayRef< FixItHint > getFixIts() const
Definition: Diagnostic.h:1489
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:1849
comments::CommandTraits & getCommentCommandTraits() const
Definition: ASTContext.h:878
const SourceManager & getManager() const
DeclContext * getDeclContext()
Definition: DeclBase.h:438
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...
ASTEdit remove(RangeSelector S)
Removes the source selected by S.
Definition: Transformer.h:232
Information about a module that has been loaded by the ASTReader.
Definition: Module.h:107
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: Module.h:122
bool BeforePreviousInsertions
Definition: Diagnostic.h:80
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:602
PreambleBounds ComputePreambleBounds(const LangOptions &LangOpts, llvm::MemoryBuffer *Buffer, unsigned MaxLines)
Runs lexer to compute suggested preamble bounds.
SmallString< 128 > Buffer
Definition: ASTUnit.cpp:216
An input file for the front end.
static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM, const LangOptions &LangOpts, const FixItHint &InFix)
Definition: ASTUnit.cpp:1244
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:708
const SourceManager & SM
Definition: Format.cpp:1572
The client can&#39;t handle any AST loading failures.
Definition: ASTReader.h:1505
The AST file was missing.
Definition: ASTReader.h:389
CharSourceRange InsertFromRange
Code in the specific range that should be inserted in the insertion location.
Definition: Diagnostic.h:74
unsigned getNumWarnings() const
Definition: Diagnostic.h:763
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:70
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:1959
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
The control block was read successfully.
Definition: ASTReader.h:383
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:739
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:138
FileSystemOptions & getFileSystemOpts()
Returns the current file system options.
Definition: FileManager.h:221
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
LLVM IR: we accept this so that we can run the optimizer on it, and compile it to assembly or object ...
std::vector< FrontendInputFile > Inputs
The input files and their types.
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:2133
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:179
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
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:43
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:1236
bool isAnyPointerType() const
Definition: Type.h:6388
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
Definition: DeclBase.h:702
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:531
void setListener(std::unique_ptr< ASTReaderListener > Listener)
Set the AST callbacks listener.
Definition: ASTReader.h:1572
void InitBuiltinTypes(const TargetInfo &Target, const TargetInfo *AuxTarget=nullptr)
Initialize built-in types.
SourceManager & getSourceManager() const
Definition: Diagnostic.h:1337
SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T)
Determine the simplified type class of the given canonical type.
llvm::BitstreamWriter Stream
Definition: ASTUnit.cpp:217
TranslationUnitKind getTranslationUnitKind() const
Determine what kind of translation unit this AST represents.
Definition: ASTUnit.h:655
void setNumWarnings(unsigned NumWarnings)
Definition: Diagnostic.h:765
The AST file was writtten with a different language/target configuration.
Definition: ASTReader.h:400
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: Module.h:56
void registerCommentOptions(const CommentOptions &CommentOptions)
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:1257
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:156
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:1271
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:284
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:866
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:354
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
const FullSourceLoc & getLocation() const
Definition: Diagnostic.h:1468
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:721
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:976
void enableSourceFileDiagnostics()
Enable source-range based diagnostic messages.
Definition: ASTUnit.cpp:276
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:1511
Language
The language for the input, used to select and validate the language standard and possible actions...
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:127
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:1081
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:2293
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:1822
cached_completion_iterator cached_completion_begin()
Definition: ASTUnit.h:616
Abstract interface for callback invocations by the ASTReader.
Definition: ASTReader.h:126
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:220
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:2280
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.
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:6222
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:900
Priority for a code pattern.
static bool isInMainFile(const clang::Diagnostic &D)
Definition: ASTUnit.cpp:677
const SourceManager & getSourceManager() const
Definition: ASTUnit.h:432
An unspecified code-completion context where we should also add macro completions.
Priority for a nested-name-specifier.
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:152
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:362
DeclContext * getLookupParent()
Find the parent context of this context that will be used for unqualified name lookup.
Definition: DeclBase.cpp:1039
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:103
serialization::DeclID getDeclID(const Decl *D)
Determine the declaration ID of an already-emitted declaration.
Definition: ASTWriter.cpp:5657
bool hadModuleLoaderFatalFailure() const
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1324
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:393
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:364
SourceLocation getStartOfMainFileID() const
Definition: ASTUnit.cpp:2588
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:396
Code completion occurred on the right-hand side of a member access expression using the dot operator...
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:66
unsigned IncludeMacros
Show macros in code completion results.
ASTContext & Context
Definition: Sema.h:374
This represents a decl that may have a name.
Definition: Decl.h:248
bool isTranslationUnit() const
Definition: DeclBase.h:1854
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:1520
const FileEntry * File
The file entry for the module file.
Definition: Module.h:163
const LangOptions & getLangOpts() const
Definition: ASTContext.h:710
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
llvm::vfs::FileSystem & getVirtualFileSystem() const
Definition: FileManager.h:224
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Definition: Preprocessor.h:995
This class handles loading and caching of source files into memory.
SourceLocation getLocation() const
Definition: DeclBase.h:429
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:124
std::vector< Decl * >::iterator top_level_iterator
Definition: ASTUnit.h:500