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