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