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