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