clang  8.0.0svn
ASTUnit.cpp
Go to the documentation of this file.
1 //===- ASTUnit.cpp - ASTUnit utility --------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // ASTUnit Implementation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclGroup.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/Type.h"
28 #include "clang/AST/TypeOrdering.h"
29 #include "clang/Basic/Diagnostic.h"
32 #include "clang/Basic/LLVM.h"
35 #include "clang/Basic/Module.h"
38 #include "clang/Basic/TargetInfo.h"
49 #include "clang/Frontend/Utils.h"
50 #include "clang/Lex/HeaderSearch.h"
52 #include "clang/Lex/Lexer.h"
53 #include "clang/Lex/PPCallbacks.h"
55 #include "clang/Lex/Preprocessor.h"
57 #include "clang/Lex/Token.h"
60 #include "clang/Sema/Sema.h"
66 #include "llvm/ADT/ArrayRef.h"
67 #include "llvm/ADT/DenseMap.h"
68 #include "llvm/ADT/IntrusiveRefCntPtr.h"
69 #include "llvm/ADT/None.h"
70 #include "llvm/ADT/Optional.h"
71 #include "llvm/ADT/STLExtras.h"
72 #include "llvm/ADT/SmallString.h"
73 #include "llvm/ADT/SmallVector.h"
74 #include "llvm/ADT/StringMap.h"
75 #include "llvm/ADT/StringRef.h"
76 #include "llvm/ADT/StringSet.h"
77 #include "llvm/ADT/Twine.h"
78 #include "llvm/ADT/iterator_range.h"
79 #include "llvm/Bitcode/BitstreamWriter.h"
80 #include "llvm/Support/Allocator.h"
81 #include "llvm/Support/Casting.h"
82 #include "llvm/Support/CrashRecoveryContext.h"
83 #include "llvm/Support/DJB.h"
84 #include "llvm/Support/ErrorHandling.h"
85 #include "llvm/Support/ErrorOr.h"
86 #include "llvm/Support/FileSystem.h"
87 #include "llvm/Support/MemoryBuffer.h"
88 #include "llvm/Support/Mutex.h"
89 #include "llvm/Support/Timer.h"
90 #include "llvm/Support/VirtualFileSystem.h"
91 #include "llvm/Support/raw_ostream.h"
92 #include <algorithm>
93 #include <atomic>
94 #include <cassert>
95 #include <cstdint>
96 #include <cstdio>
97 #include <cstdlib>
98 #include <memory>
99 #include <string>
100 #include <tuple>
101 #include <utility>
102 #include <vector>
103 
104 using namespace clang;
105 
106 using llvm::TimeRecord;
107 
108 namespace {
109 
110  class SimpleTimer {
111  bool WantTiming;
112  TimeRecord Start;
113  std::string Output;
114 
115  public:
116  explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
117  if (WantTiming)
118  Start = TimeRecord::getCurrentTime();
119  }
120 
121  ~SimpleTimer() {
122  if (WantTiming) {
123  TimeRecord Elapsed = TimeRecord::getCurrentTime();
124  Elapsed -= Start;
125  llvm::errs() << Output << ':';
126  Elapsed.print(Elapsed, llvm::errs());
127  llvm::errs() << '\n';
128  }
129  }
130 
131  void setOutput(const Twine &Output) {
132  if (WantTiming)
133  this->Output = Output.str();
134  }
135  };
136 
137 } // namespace
138 
139 template <class T>
140 static std::unique_ptr<T> valueOrNull(llvm::ErrorOr<std::unique_ptr<T>> Val) {
141  if (!Val)
142  return nullptr;
143  return std::move(*Val);
144 }
145 
146 template <class T>
147 static bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
148  if (!Val)
149  return false;
150  Output = std::move(*Val);
151  return true;
152 }
153 
154 /// Get a source buffer for \p MainFilePath, handling all file-to-file
155 /// and file-to-buffer remappings inside \p Invocation.
156 static std::unique_ptr<llvm::MemoryBuffer>
158  llvm::vfs::FileSystem *VFS,
159  StringRef FilePath, bool isVolatile) {
160  const auto &PreprocessorOpts = Invocation.getPreprocessorOpts();
161 
162  // Try to determine if the main file has been remapped, either from the
163  // command line (to another file) or directly through the compiler
164  // invocation (to a memory buffer).
165  llvm::MemoryBuffer *Buffer = nullptr;
166  std::unique_ptr<llvm::MemoryBuffer> BufferOwner;
167  auto FileStatus = VFS->status(FilePath);
168  if (FileStatus) {
169  llvm::sys::fs::UniqueID MainFileID = FileStatus->getUniqueID();
170 
171  // Check whether there is a file-file remapping of the main file
172  for (const auto &RF : PreprocessorOpts.RemappedFiles) {
173  std::string MPath(RF.first);
174  auto MPathStatus = VFS->status(MPath);
175  if (MPathStatus) {
176  llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
177  if (MainFileID == MID) {
178  // We found a remapping. Try to load the resulting, remapped source.
179  BufferOwner = valueOrNull(VFS->getBufferForFile(RF.second, -1, true, isVolatile));
180  if (!BufferOwner)
181  return nullptr;
182  }
183  }
184  }
185 
186  // Check whether there is a file-buffer remapping. It supercedes the
187  // file-file remapping.
188  for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
189  std::string MPath(RB.first);
190  auto MPathStatus = VFS->status(MPath);
191  if (MPathStatus) {
192  llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
193  if (MainFileID == MID) {
194  // We found a remapping.
195  BufferOwner.reset();
196  Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
197  }
198  }
199  }
200  }
201 
202  // If the main source file was not remapped, load it now.
203  if (!Buffer && !BufferOwner) {
204  BufferOwner = valueOrNull(VFS->getBufferForFile(FilePath, -1, true, isVolatile));
205  if (!BufferOwner)
206  return nullptr;
207  }
208 
209  if (BufferOwner)
210  return BufferOwner;
211  if (!Buffer)
212  return nullptr;
213  return llvm::MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(), FilePath);
214 }
215 
218  llvm::BitstreamWriter Stream;
220 
222  : Stream(Buffer), Writer(Stream, Buffer, PCMCache, {}) {}
223 };
224 
225 void ASTUnit::clearFileLevelDecls() {
226  llvm::DeleteContainerSeconds(FileDecls);
227 }
228 
229 /// After failing to build a precompiled preamble (due to
230 /// errors in the source that occurs in the preamble), the number of
231 /// reparses during which we'll skip even trying to precompile the
232 /// preamble.
234 
235 /// Tracks the number of ASTUnit objects that are currently active.
236 ///
237 /// Used for debugging purposes only.
238 static std::atomic<unsigned> ActiveASTUnitObjects;
239 
240 ASTUnit::ASTUnit(bool _MainFileIsAST)
241  : MainFileIsAST(_MainFileIsAST), WantTiming(getenv("LIBCLANG_TIMING")),
242  ShouldCacheCodeCompletionResults(false),
243  IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
244  UnsafeToFree(false) {
245  if (getenv("LIBCLANG_OBJTRACKING"))
246  fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects);
247 }
248 
249 ASTUnit::~ASTUnit() {
250  // If we loaded from an AST file, balance out the BeginSourceFile call.
251  if (MainFileIsAST && getDiagnostics().getClient()) {
253  }
254 
255  clearFileLevelDecls();
256 
257  // Free the buffers associated with remapped files. We are required to
258  // perform this operation here because we explicitly request that the
259  // compiler instance *not* free these buffers for each invocation of the
260  // parser.
261  if (Invocation && OwnsRemappedFileBuffers) {
262  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
263  for (const auto &RB : PPOpts.RemappedFileBuffers)
264  delete RB.second;
265  }
266 
267  ClearCachedCompletionResults();
268 
269  if (getenv("LIBCLANG_OBJTRACKING"))
270  fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects);
271 }
272 
273 void ASTUnit::setPreprocessor(std::shared_ptr<Preprocessor> PP) {
274  this->PP = std::move(PP);
275 }
276 
278  assert(getDiagnostics().getClient() && Ctx &&
279  "Bad context for source file");
280  getDiagnostics().getClient()->BeginSourceFile(Ctx->getLangOpts(), PP.get());
281 }
282 
283 /// Determine the set of code-completion contexts in which this
284 /// declaration should be shown.
285 static unsigned getDeclShowContexts(const NamedDecl *ND,
286  const LangOptions &LangOpts,
287  bool &IsNestedNameSpecifier) {
288  IsNestedNameSpecifier = false;
289 
290  if (isa<UsingShadowDecl>(ND))
291  ND = ND->getUnderlyingDecl();
292  if (!ND)
293  return 0;
294 
295  uint64_t Contexts = 0;
296  if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
297  isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND) ||
298  isa<TypeAliasTemplateDecl>(ND)) {
299  // Types can appear in these contexts.
300  if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
301  Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
307 
308  // In C++, types can appear in expressions contexts (for functional casts).
309  if (LangOpts.CPlusPlus)
310  Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
311 
312  // In Objective-C, message sends can send interfaces. In Objective-C++,
313  // all types are available due to functional casts.
314  if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
316 
317  // In Objective-C, you can only be a subclass of another Objective-C class
318  if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) {
319  // Objective-C interfaces can be used in a class property expression.
320  if (ID->getDefinition())
321  Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
323  }
324 
325  // Deal with tag names.
326  if (isa<EnumDecl>(ND)) {
327  Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
328 
329  // Part of the nested-name-specifier in C++0x.
330  if (LangOpts.CPlusPlus11)
331  IsNestedNameSpecifier = true;
332  } else if (const auto *Record = dyn_cast<RecordDecl>(ND)) {
333  if (Record->isUnion())
334  Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
335  else
336  Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
337 
338  if (LangOpts.CPlusPlus)
339  IsNestedNameSpecifier = true;
340  } else if (isa<ClassTemplateDecl>(ND))
341  IsNestedNameSpecifier = true;
342  } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
343  // Values can appear in these contexts.
344  Contexts = (1LL << CodeCompletionContext::CCC_Statement)
348  } else if (isa<ObjCProtocolDecl>(ND)) {
350  } else if (isa<ObjCCategoryDecl>(ND)) {
352  } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
353  Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
354 
355  // Part of the nested-name-specifier.
356  IsNestedNameSpecifier = true;
357  }
358 
359  return Contexts;
360 }
361 
362 void ASTUnit::CacheCodeCompletionResults() {
363  if (!TheSema)
364  return;
365 
366  SimpleTimer Timer(WantTiming);
367  Timer.setOutput("Cache global code completions for " + getMainFileName());
368 
369  // Clear out the previous results.
370  ClearCachedCompletionResults();
371 
372  // Gather the set of global code completions.
374  SmallVector<Result, 8> Results;
375  CachedCompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
376  CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
377  TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
378  CCTUInfo, Results);
379 
380  // Translate global code completions into cached completions.
381  llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
383 
384  for (auto &R : Results) {
385  switch (R.Kind) {
386  case Result::RK_Declaration: {
387  bool IsNestedNameSpecifier = false;
388  CachedCodeCompletionResult CachedResult;
389  CachedResult.Completion = R.CreateCodeCompletionString(
390  *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
391  IncludeBriefCommentsInCodeCompletion);
392  CachedResult.ShowInContexts = getDeclShowContexts(
393  R.Declaration, Ctx->getLangOpts(), IsNestedNameSpecifier);
394  CachedResult.Priority = R.Priority;
395  CachedResult.Kind = R.CursorKind;
396  CachedResult.Availability = R.Availability;
397 
398  // Keep track of the type of this completion in an ASTContext-agnostic
399  // way.
400  QualType UsageType = getDeclUsageType(*Ctx, R.Declaration);
401  if (UsageType.isNull()) {
402  CachedResult.TypeClass = STC_Void;
403  CachedResult.Type = 0;
404  } else {
405  CanQualType CanUsageType
406  = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
407  CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
408 
409  // Determine whether we have already seen this type. If so, we save
410  // ourselves the work of formatting the type string by using the
411  // temporary, CanQualType-based hash table to find the associated value.
412  unsigned &TypeValue = CompletionTypes[CanUsageType];
413  if (TypeValue == 0) {
414  TypeValue = CompletionTypes.size();
415  CachedCompletionTypes[QualType(CanUsageType).getAsString()]
416  = TypeValue;
417  }
418 
419  CachedResult.Type = TypeValue;
420  }
421 
422  CachedCompletionResults.push_back(CachedResult);
423 
424  /// Handle nested-name-specifiers in C++.
425  if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier &&
426  !R.StartsNestedNameSpecifier) {
427  // The contexts in which a nested-name-specifier can appear in C++.
428  uint64_t NNSContexts
441 
442  if (isa<NamespaceDecl>(R.Declaration) ||
443  isa<NamespaceAliasDecl>(R.Declaration))
444  NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
445 
446  if (unsigned 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->PCMCache = new MemoryBufferCache;
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(), *AST->PCMCache, 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(PP, AST->Ctx.get(), PCHContainerRdr, {},
795  /*isysroot=*/"",
796  /*DisableValidation=*/disableValid,
797  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  auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
1082  if (OverrideMainBuffer) {
1083  assert(Preamble &&
1084  "No preamble was built, but OverrideMainBuffer is not null");
1085  IntrusiveRefCntPtr<llvm::vfs::FileSystem> OldVFS = VFS;
1086  Preamble->AddImplicitPreamble(*CCInvocation, VFS, OverrideMainBuffer.get());
1087  if (OldVFS != VFS && FileMgr) {
1088  assert(OldVFS == FileMgr->getVirtualFileSystem() &&
1089  "VFS passed to Parse and VFS in FileMgr are different");
1090  FileMgr = new FileManager(FileMgr->getFileSystemOpts(), VFS);
1091  }
1092  }
1093 
1094  // Create the compiler instance to use for building the AST.
1095  std::unique_ptr<CompilerInstance> Clang(
1096  new CompilerInstance(std::move(PCHContainerOps)));
1097  if (FileMgr && VFS) {
1098  assert(VFS == FileMgr->getVirtualFileSystem() &&
1099  "VFS passed to Parse and VFS in FileMgr are different");
1100  } else if (VFS) {
1101  Clang->setVirtualFileSystem(VFS);
1102  }
1103 
1104  // Recover resources if we crash before exiting this method.
1105  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1106  CICleanup(Clang.get());
1107 
1108  Clang->setInvocation(CCInvocation);
1109  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1110 
1111  // Set up diagnostics, capturing any diagnostics that would
1112  // otherwise be dropped.
1113  Clang->setDiagnostics(&getDiagnostics());
1114 
1115  // Create the target instance.
1116  Clang->setTarget(TargetInfo::CreateTargetInfo(
1117  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1118  if (!Clang->hasTarget())
1119  return true;
1120 
1121  // Inform the target of the language options.
1122  //
1123  // FIXME: We shouldn't need to do this, the target should be immutable once
1124  // created. This complexity should be lifted elsewhere.
1125  Clang->getTarget().adjust(Clang->getLangOpts());
1126 
1127  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1128  "Invocation must have exactly one source file!");
1129  assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1131  "FIXME: AST inputs not yet supported here!");
1132  assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1134  "IR inputs not support here!");
1135 
1136  // Configure the various subsystems.
1137  LangOpts = Clang->getInvocation().LangOpts;
1138  FileSystemOpts = Clang->getFileSystemOpts();
1139  if (!FileMgr) {
1140  Clang->createFileManager();
1141  FileMgr = &Clang->getFileManager();
1142  }
1143 
1144  ResetForParse();
1145 
1146  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1147  UserFilesAreVolatile);
1148  if (!OverrideMainBuffer) {
1149  checkAndRemoveNonDriverDiags(StoredDiagnostics);
1150  TopLevelDeclsInPreamble.clear();
1151  }
1152 
1153  // Create a file manager object to provide access to and cache the filesystem.
1154  Clang->setFileManager(&getFileManager());
1155 
1156  // Create the source manager.
1157  Clang->setSourceManager(&getSourceManager());
1158 
1159  // If the main file has been overridden due to the use of a preamble,
1160  // make that override happen and introduce the preamble.
1161  if (OverrideMainBuffer) {
1162  // The stored diagnostic has the old source manager in it; update
1163  // the locations to refer into the new source manager. Since we've
1164  // been careful to make sure that the source manager's state
1165  // before and after are identical, so that we can reuse the source
1166  // location itself.
1167  checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1168 
1169  // Keep track of the override buffer;
1170  SavedMainFileBuffer = std::move(OverrideMainBuffer);
1171  }
1172 
1173  std::unique_ptr<TopLevelDeclTrackerAction> Act(
1174  new TopLevelDeclTrackerAction(*this));
1175 
1176  // Recover resources if we crash before exiting this method.
1177  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1178  ActCleanup(Act.get());
1179 
1180  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1181  goto error;
1182 
1183  if (SavedMainFileBuffer)
1184  TranslateStoredDiagnostics(getFileManager(), getSourceManager(),
1185  PreambleDiagnostics, StoredDiagnostics);
1186  else
1187  PreambleSrcLocCache.clear();
1188 
1189  if (!Act->Execute())
1190  goto error;
1191 
1192  transferASTDataFromCompilerInstance(*Clang);
1193 
1194  Act->EndSourceFile();
1195 
1196  FailedParseDiagnostics.clear();
1197 
1198  return false;
1199 
1200 error:
1201  // Remove the overridden buffer we used for the preamble.
1202  SavedMainFileBuffer = nullptr;
1203 
1204  // Keep the ownership of the data in the ASTUnit because the client may
1205  // want to see the diagnostics.
1206  transferASTDataFromCompilerInstance(*Clang);
1207  FailedParseDiagnostics.swap(StoredDiagnostics);
1208  StoredDiagnostics.clear();
1209  NumStoredDiagnosticsFromDriver = 0;
1210  return true;
1211 }
1212 
1213 static std::pair<unsigned, unsigned>
1215  const LangOptions &LangOpts) {
1216  CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts);
1217  unsigned Offset = SM.getFileOffset(FileRange.getBegin());
1218  unsigned EndOffset = SM.getFileOffset(FileRange.getEnd());
1219  return std::make_pair(Offset, EndOffset);
1220 }
1221 
1223  const LangOptions &LangOpts,
1224  const FixItHint &InFix) {
1225  ASTUnit::StandaloneFixIt OutFix;
1226  OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts);
1228  LangOpts);
1229  OutFix.CodeToInsert = InFix.CodeToInsert;
1231  return OutFix;
1232 }
1233 
1236  const StoredDiagnostic &InDiag) {
1238  OutDiag.ID = InDiag.getID();
1239  OutDiag.Level = InDiag.getLevel();
1240  OutDiag.Message = InDiag.getMessage();
1241  OutDiag.LocOffset = 0;
1242  if (InDiag.getLocation().isInvalid())
1243  return OutDiag;
1244  const SourceManager &SM = InDiag.getLocation().getManager();
1245  SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation());
1246  OutDiag.Filename = SM.getFilename(FileLoc);
1247  if (OutDiag.Filename.empty())
1248  return OutDiag;
1249  OutDiag.LocOffset = SM.getFileOffset(FileLoc);
1250  for (const auto &Range : InDiag.getRanges())
1251  OutDiag.Ranges.push_back(makeStandaloneRange(Range, SM, LangOpts));
1252  for (const auto &FixIt : InDiag.getFixIts())
1253  OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, FixIt));
1254 
1255  return OutDiag;
1256 }
1257 
1258 /// Attempt to build or re-use a precompiled preamble when (re-)parsing
1259 /// the source file.
1260 ///
1261 /// This routine will compute the preamble of the main source file. If a
1262 /// non-trivial preamble is found, it will precompile that preamble into a
1263 /// precompiled header so that the precompiled preamble can be used to reduce
1264 /// reparsing time. If a precompiled preamble has already been constructed,
1265 /// this routine will determine if it is still valid and, if so, avoid
1266 /// rebuilding the precompiled preamble.
1267 ///
1268 /// \param AllowRebuild When true (the default), this routine is
1269 /// allowed to rebuild the precompiled preamble if it is found to be
1270 /// out-of-date.
1271 ///
1272 /// \param MaxLines When non-zero, the maximum number of lines that
1273 /// can occur within the preamble.
1274 ///
1275 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1276 /// buffer that should be used in place of the main file when doing so.
1277 /// Otherwise, returns a NULL pointer.
1278 std::unique_ptr<llvm::MemoryBuffer>
1279 ASTUnit::getMainBufferWithPrecompiledPreamble(
1280  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1281  CompilerInvocation &PreambleInvocationIn,
1282  IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool AllowRebuild,
1283  unsigned MaxLines) {
1284  auto MainFilePath =
1285  PreambleInvocationIn.getFrontendOpts().Inputs[0].getFile();
1286  std::unique_ptr<llvm::MemoryBuffer> MainFileBuffer =
1287  getBufferForFileHandlingRemapping(PreambleInvocationIn, VFS.get(),
1288  MainFilePath, UserFilesAreVolatile);
1289  if (!MainFileBuffer)
1290  return nullptr;
1291 
1292  PreambleBounds Bounds =
1293  ComputePreambleBounds(*PreambleInvocationIn.getLangOpts(),
1294  MainFileBuffer.get(), MaxLines);
1295  if (!Bounds.Size)
1296  return nullptr;
1297 
1298  if (Preamble) {
1299  if (Preamble->CanReuse(PreambleInvocationIn, MainFileBuffer.get(), Bounds,
1300  VFS.get())) {
1301  // Okay! We can re-use the precompiled preamble.
1302 
1303  // Set the state of the diagnostic object to mimic its state
1304  // after parsing the preamble.
1305  getDiagnostics().Reset();
1307  PreambleInvocationIn.getDiagnosticOpts());
1308  getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1309 
1310  PreambleRebuildCounter = 1;
1311  return MainFileBuffer;
1312  } else {
1313  Preamble.reset();
1314  PreambleDiagnostics.clear();
1315  TopLevelDeclsInPreamble.clear();
1316  PreambleSrcLocCache.clear();
1317  PreambleRebuildCounter = 1;
1318  }
1319  }
1320 
1321  // If the preamble rebuild counter > 1, it's because we previously
1322  // failed to build a preamble and we're not yet ready to try
1323  // again. Decrement the counter and return a failure.
1324  if (PreambleRebuildCounter > 1) {
1325  --PreambleRebuildCounter;
1326  return nullptr;
1327  }
1328 
1329  assert(!Preamble && "No Preamble should be stored at that point");
1330  // If we aren't allowed to rebuild the precompiled preamble, just
1331  // return now.
1332  if (!AllowRebuild)
1333  return nullptr;
1334 
1335  SmallVector<StandaloneDiagnostic, 4> NewPreambleDiagsStandalone;
1336  SmallVector<StoredDiagnostic, 4> NewPreambleDiags;
1337  ASTUnitPreambleCallbacks Callbacks;
1338  {
1340  if (CaptureDiagnostics)
1341  Capture.emplace(/*RequestCapture=*/true, *Diagnostics, &NewPreambleDiags,
1342  &NewPreambleDiagsStandalone);
1343 
1344  // We did not previously compute a preamble, or it can't be reused anyway.
1345  SimpleTimer PreambleTimer(WantTiming);
1346  PreambleTimer.setOutput("Precompiling preamble");
1347 
1348  const bool PreviousSkipFunctionBodies =
1349  PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies;
1350  if (SkipFunctionBodies == SkipFunctionBodiesScope::Preamble)
1351  PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies = true;
1352 
1353  llvm::ErrorOr<PrecompiledPreamble> NewPreamble = PrecompiledPreamble::Build(
1354  PreambleInvocationIn, MainFileBuffer.get(), Bounds, *Diagnostics, VFS,
1355  PCHContainerOps, /*StoreInMemory=*/false, Callbacks);
1356 
1357  PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies =
1358  PreviousSkipFunctionBodies;
1359 
1360  if (NewPreamble) {
1361  Preamble = std::move(*NewPreamble);
1362  PreambleRebuildCounter = 1;
1363  } else {
1364  switch (static_cast<BuildPreambleError>(NewPreamble.getError().value())) {
1366  // Try again next time.
1367  PreambleRebuildCounter = 1;
1368  return nullptr;
1372  // These erros are more likely to repeat, retry after some period.
1373  PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1374  return nullptr;
1375  }
1376  llvm_unreachable("unexpected BuildPreambleError");
1377  }
1378  }
1379 
1380  assert(Preamble && "Preamble wasn't built");
1381 
1382  TopLevelDecls.clear();
1383  TopLevelDeclsInPreamble = Callbacks.takeTopLevelDeclIDs();
1384  PreambleTopLevelHashValue = Callbacks.getHash();
1385 
1386  NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1387 
1388  checkAndRemoveNonDriverDiags(NewPreambleDiags);
1389  StoredDiagnostics = std::move(NewPreambleDiags);
1390  PreambleDiagnostics = std::move(NewPreambleDiagsStandalone);
1391 
1392  // If the hash of top-level entities differs from the hash of the top-level
1393  // entities the last time we rebuilt the preamble, clear out the completion
1394  // cache.
1395  if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1396  CompletionCacheTopLevelHashValue = 0;
1397  PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1398  }
1399 
1400  return MainFileBuffer;
1401 }
1402 
1403 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1404  assert(Preamble && "Should only be called when preamble was built");
1405 
1406  std::vector<Decl *> Resolved;
1407  Resolved.reserve(TopLevelDeclsInPreamble.size());
1409  for (const auto TopLevelDecl : TopLevelDeclsInPreamble) {
1410  // Resolve the declaration ID to an actual declaration, possibly
1411  // deserializing the declaration in the process.
1412  if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1413  Resolved.push_back(D);
1414  }
1415  TopLevelDeclsInPreamble.clear();
1416  TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1417 }
1418 
1419 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1420  // Steal the created target, context, and preprocessor if they have been
1421  // created.
1422  assert(CI.hasInvocation() && "missing invocation");
1423  LangOpts = CI.getInvocation().LangOpts;
1424  TheSema = CI.takeSema();
1425  Consumer = CI.takeASTConsumer();
1426  if (CI.hasASTContext())
1427  Ctx = &CI.getASTContext();
1428  if (CI.hasPreprocessor())
1429  PP = CI.getPreprocessorPtr();
1430  CI.setSourceManager(nullptr);
1431  CI.setFileManager(nullptr);
1432  if (CI.hasTarget())
1433  Target = &CI.getTarget();
1434  Reader = CI.getModuleManager();
1435  HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1436 }
1437 
1438 StringRef ASTUnit::getMainFileName() const {
1439  if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1440  const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1441  if (Input.isFile())
1442  return Input.getFile();
1443  else
1444  return Input.getBuffer()->getBufferIdentifier();
1445  }
1446 
1447  if (SourceMgr) {
1448  if (const FileEntry *
1449  FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1450  return FE->getName();
1451  }
1452 
1453  return {};
1454 }
1455 
1456 StringRef ASTUnit::getASTFileName() const {
1457  if (!isMainFileAST())
1458  return {};
1459 
1461  Mod = Reader->getModuleManager().getPrimaryModule();
1462  return Mod.FileName;
1463 }
1464 
1465 std::unique_ptr<ASTUnit>
1466 ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
1468  bool CaptureDiagnostics, bool UserFilesAreVolatile) {
1469  std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1470  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1472  createVFSFromCompilerInvocation(*CI, *Diags);
1473  AST->Diagnostics = Diags;
1474  AST->FileSystemOpts = CI->getFileSystemOpts();
1475  AST->Invocation = std::move(CI);
1476  AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1477  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1478  AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1479  UserFilesAreVolatile);
1480  AST->PCMCache = new MemoryBufferCache;
1481 
1482  return AST;
1483 }
1484 
1486  std::shared_ptr<CompilerInvocation> CI,
1487  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1489  ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
1490  bool OnlyLocalDecls, bool CaptureDiagnostics,
1491  unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults,
1492  bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
1493  std::unique_ptr<ASTUnit> *ErrAST) {
1494  assert(CI && "A CompilerInvocation is required");
1495 
1496  std::unique_ptr<ASTUnit> OwnAST;
1497  ASTUnit *AST = Unit;
1498  if (!AST) {
1499  // Create the AST unit.
1500  OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile);
1501  AST = OwnAST.get();
1502  if (!AST)
1503  return nullptr;
1504  }
1505 
1506  if (!ResourceFilesPath.empty()) {
1507  // Override the resources path.
1508  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1509  }
1510  AST->OnlyLocalDecls = OnlyLocalDecls;
1511  AST->CaptureDiagnostics = CaptureDiagnostics;
1512  if (PrecompilePreambleAfterNParses > 0)
1513  AST->PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1514  AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1515  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1516  AST->IncludeBriefCommentsInCodeCompletion
1517  = IncludeBriefCommentsInCodeCompletion;
1518 
1519  // Recover resources if we crash before exiting this method.
1520  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1521  ASTUnitCleanup(OwnAST.get());
1522  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1523  llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1524  DiagCleanup(Diags.get());
1525 
1526  // We'll manage file buffers ourselves.
1527  CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1528  CI->getFrontendOpts().DisableFree = false;
1529  ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1530 
1531  // Create the compiler instance to use for building the AST.
1532  std::unique_ptr<CompilerInstance> Clang(
1533  new CompilerInstance(std::move(PCHContainerOps)));
1534 
1535  // Recover resources if we crash before exiting this method.
1536  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1537  CICleanup(Clang.get());
1538 
1539  Clang->setInvocation(std::move(CI));
1540  AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1541 
1542  // Set up diagnostics, capturing any diagnostics that would
1543  // otherwise be dropped.
1544  Clang->setDiagnostics(&AST->getDiagnostics());
1545 
1546  // Create the target instance.
1547  Clang->setTarget(TargetInfo::CreateTargetInfo(
1548  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1549  if (!Clang->hasTarget())
1550  return nullptr;
1551 
1552  // Inform the target of the language options.
1553  //
1554  // FIXME: We shouldn't need to do this, the target should be immutable once
1555  // created. This complexity should be lifted elsewhere.
1556  Clang->getTarget().adjust(Clang->getLangOpts());
1557 
1558  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1559  "Invocation must have exactly one source file!");
1560  assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1562  "FIXME: AST inputs not yet supported here!");
1563  assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1565  "IR inputs not support here!");
1566 
1567  // Configure the various subsystems.
1568  AST->TheSema.reset();
1569  AST->Ctx = nullptr;
1570  AST->PP = nullptr;
1571  AST->Reader = nullptr;
1572 
1573  // Create a file manager object to provide access to and cache the filesystem.
1574  Clang->setFileManager(&AST->getFileManager());
1575 
1576  // Create the source manager.
1577  Clang->setSourceManager(&AST->getSourceManager());
1578 
1579  FrontendAction *Act = Action;
1580 
1581  std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
1582  if (!Act) {
1583  TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1584  Act = TrackerAct.get();
1585  }
1586 
1587  // Recover resources if we crash before exiting this method.
1588  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1589  ActCleanup(TrackerAct.get());
1590 
1591  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1592  AST->transferASTDataFromCompilerInstance(*Clang);
1593  if (OwnAST && ErrAST)
1594  ErrAST->swap(OwnAST);
1595 
1596  return nullptr;
1597  }
1598 
1599  if (Persistent && !TrackerAct) {
1600  Clang->getPreprocessor().addPPCallbacks(
1601  llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
1602  AST->getCurrentTopLevelHashValue()));
1603  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
1604  if (Clang->hasASTConsumer())
1605  Consumers.push_back(Clang->takeASTConsumer());
1606  Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>(
1607  *AST, AST->getCurrentTopLevelHashValue()));
1608  Clang->setASTConsumer(
1609  llvm::make_unique<MultiplexConsumer>(std::move(Consumers)));
1610  }
1611  if (!Act->Execute()) {
1612  AST->transferASTDataFromCompilerInstance(*Clang);
1613  if (OwnAST && ErrAST)
1614  ErrAST->swap(OwnAST);
1615 
1616  return nullptr;
1617  }
1618 
1619  // Steal the created target, context, and preprocessor.
1620  AST->transferASTDataFromCompilerInstance(*Clang);
1621 
1622  Act->EndSourceFile();
1623 
1624  if (OwnAST)
1625  return OwnAST.release();
1626  else
1627  return AST;
1628 }
1629 
1630 bool ASTUnit::LoadFromCompilerInvocation(
1631  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1632  unsigned PrecompilePreambleAfterNParses,
1634  if (!Invocation)
1635  return true;
1636 
1637  assert(VFS && "VFS is null");
1638 
1639  // We'll manage file buffers ourselves.
1640  Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1641  Invocation->getFrontendOpts().DisableFree = false;
1642  getDiagnostics().Reset();
1643  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1644 
1645  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1646  if (PrecompilePreambleAfterNParses > 0) {
1647  PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1648  OverrideMainBuffer =
1649  getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1650  getDiagnostics().Reset();
1651  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1652  }
1653 
1654  SimpleTimer ParsingTimer(WantTiming);
1655  ParsingTimer.setOutput("Parsing " + getMainFileName());
1656 
1657  // Recover resources if we crash before exiting this method.
1658  llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1659  MemBufferCleanup(OverrideMainBuffer.get());
1660 
1661  return Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1662 }
1663 
1664 std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
1665  std::shared_ptr<CompilerInvocation> CI,
1666  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1668  bool OnlyLocalDecls, bool CaptureDiagnostics,
1669  unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1670  bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1671  bool UserFilesAreVolatile) {
1672  // Create the AST unit.
1673  std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1674  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1675  AST->Diagnostics = Diags;
1676  AST->OnlyLocalDecls = OnlyLocalDecls;
1677  AST->CaptureDiagnostics = CaptureDiagnostics;
1678  AST->TUKind = TUKind;
1679  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1680  AST->IncludeBriefCommentsInCodeCompletion
1681  = IncludeBriefCommentsInCodeCompletion;
1682  AST->Invocation = std::move(CI);
1683  AST->FileSystemOpts = FileMgr->getFileSystemOpts();
1684  AST->FileMgr = FileMgr;
1685  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1686 
1687  // Recover resources if we crash before exiting this method.
1688  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1689  ASTUnitCleanup(AST.get());
1690  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1691  llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1692  DiagCleanup(Diags.get());
1693 
1694  if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1695  PrecompilePreambleAfterNParses,
1696  AST->FileMgr->getVirtualFileSystem()))
1697  return nullptr;
1698  return AST;
1699 }
1700 
1702  const char **ArgBegin, const char **ArgEnd,
1703  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1704  IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
1705  bool OnlyLocalDecls, bool CaptureDiagnostics,
1706  ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
1707  unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1708  bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1709  bool AllowPCHWithCompilerErrors, SkipFunctionBodiesScope SkipFunctionBodies,
1710  bool SingleFileParse, bool UserFilesAreVolatile, bool ForSerialization,
1711  llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST,
1713  assert(Diags.get() && "no DiagnosticsEngine was provided");
1714 
1715  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1716 
1717  std::shared_ptr<CompilerInvocation> CI;
1718 
1719  {
1720  CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1721  &StoredDiagnostics, nullptr);
1722 
1724  llvm::makeArrayRef(ArgBegin, ArgEnd), Diags, VFS);
1725  if (!CI)
1726  return nullptr;
1727  }
1728 
1729  // Override any files that need remapping
1730  for (const auto &RemappedFile : RemappedFiles) {
1731  CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1732  RemappedFile.second);
1733  }
1734  PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1735  PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1736  PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1737  PPOpts.SingleFileParseMode = SingleFileParse;
1738 
1739  // Override the resources path.
1740  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1741 
1742  CI->getFrontendOpts().SkipFunctionBodies =
1743  SkipFunctionBodies == SkipFunctionBodiesScope::PreambleAndMainFile;
1744 
1745  if (ModuleFormat)
1746  CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue();
1747 
1748  // Create the AST unit.
1749  std::unique_ptr<ASTUnit> AST;
1750  AST.reset(new ASTUnit(false));
1751  AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1752  AST->StoredDiagnostics.swap(StoredDiagnostics);
1753  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1754  AST->Diagnostics = Diags;
1755  AST->FileSystemOpts = CI->getFileSystemOpts();
1756  if (!VFS)
1757  VFS = llvm::vfs::getRealFileSystem();
1758  VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS);
1759  AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1760  AST->PCMCache = new MemoryBufferCache;
1761  AST->OnlyLocalDecls = OnlyLocalDecls;
1762  AST->CaptureDiagnostics = CaptureDiagnostics;
1763  AST->TUKind = TUKind;
1764  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1765  AST->IncludeBriefCommentsInCodeCompletion
1766  = IncludeBriefCommentsInCodeCompletion;
1767  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1768  AST->Invocation = CI;
1769  AST->SkipFunctionBodies = SkipFunctionBodies;
1770  if (ForSerialization)
1771  AST->WriterData.reset(new ASTWriterData(*AST->PCMCache));
1772  // Zero out now to ease cleanup during crash recovery.
1773  CI = nullptr;
1774  Diags = nullptr;
1775 
1776  // Recover resources if we crash before exiting this method.
1777  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1778  ASTUnitCleanup(AST.get());
1779 
1780  if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1781  PrecompilePreambleAfterNParses,
1782  VFS)) {
1783  // Some error occurred, if caller wants to examine diagnostics, pass it the
1784  // ASTUnit.
1785  if (ErrAST) {
1786  AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
1787  ErrAST->swap(AST);
1788  }
1789  return nullptr;
1790  }
1791 
1792  return AST.release();
1793 }
1794 
1795 bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1796  ArrayRef<RemappedFile> RemappedFiles,
1798  if (!Invocation)
1799  return true;
1800 
1801  if (!VFS) {
1802  assert(FileMgr && "FileMgr is null on Reparse call");
1803  VFS = FileMgr->getVirtualFileSystem();
1804  }
1805 
1806  clearFileLevelDecls();
1807 
1808  SimpleTimer ParsingTimer(WantTiming);
1809  ParsingTimer.setOutput("Reparsing " + getMainFileName());
1810 
1811  // Remap files.
1812  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1813  for (const auto &RB : PPOpts.RemappedFileBuffers)
1814  delete RB.second;
1815 
1816  Invocation->getPreprocessorOpts().clearRemappedFiles();
1817  for (const auto &RemappedFile : RemappedFiles) {
1818  Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1819  RemappedFile.second);
1820  }
1821 
1822  // If we have a preamble file lying around, or if we might try to
1823  // build a precompiled preamble, do so now.
1824  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1825  if (Preamble || PreambleRebuildCounter > 0)
1826  OverrideMainBuffer =
1827  getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1828 
1829  // Clear out the diagnostics state.
1830  FileMgr.reset();
1831  getDiagnostics().Reset();
1832  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1833  if (OverrideMainBuffer)
1834  getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1835 
1836  // Parse the sources
1837  bool Result =
1838  Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1839 
1840  // If we're caching global code-completion results, and the top-level
1841  // declarations have changed, clear out the code-completion cache.
1842  if (!Result && ShouldCacheCodeCompletionResults &&
1843  CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1844  CacheCodeCompletionResults();
1845 
1846  // We now need to clear out the completion info related to this translation
1847  // unit; it'll be recreated if necessary.
1848  CCTUInfo.reset();
1849 
1850  return Result;
1851 }
1852 
1854  SavedMainFileBuffer.reset();
1855 
1856  SourceMgr.reset();
1857  TheSema.reset();
1858  Ctx.reset();
1859  PP.reset();
1860  Reader.reset();
1861 
1862  TopLevelDecls.clear();
1863  clearFileLevelDecls();
1864 }
1865 
1866 //----------------------------------------------------------------------------//
1867 // Code completion
1868 //----------------------------------------------------------------------------//
1869 
1870 namespace {
1871 
1872  /// Code completion consumer that combines the cached code-completion
1873  /// results from an ASTUnit with the code-completion results provided to it,
1874  /// then passes the result on to
1875  class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1876  uint64_t NormalContexts;
1877  ASTUnit &AST;
1878  CodeCompleteConsumer &Next;
1879 
1880  public:
1881  AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1882  const CodeCompleteOptions &CodeCompleteOpts)
1883  : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
1884  AST(AST), Next(Next) {
1885  // Compute the set of contexts in which we will look when we don't have
1886  // any information about the specific context.
1887  NormalContexts
1901 
1902  if (AST.getASTContext().getLangOpts().CPlusPlus)
1903  NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
1906  }
1907 
1908  void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
1909  CodeCompletionResult *Results,
1910  unsigned NumResults) override;
1911 
1912  void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1913  OverloadCandidate *Candidates,
1914  unsigned NumCandidates,
1915  SourceLocation OpenParLoc) override {
1916  Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates,
1917  OpenParLoc);
1918  }
1919 
1920  CodeCompletionAllocator &getAllocator() override {
1921  return Next.getAllocator();
1922  }
1923 
1925  return Next.getCodeCompletionTUInfo();
1926  }
1927  };
1928 
1929 } // namespace
1930 
1931 /// Helper function that computes which global names are hidden by the
1932 /// local code-completion results.
1933 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1934  CodeCompletionResult *Results,
1935  unsigned NumResults,
1936  ASTContext &Ctx,
1937  llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1938  bool OnlyTagNames = false;
1939  switch (Context.getKind()) {
1958  break;
1959 
1963  OnlyTagNames = true;
1964  break;
1965 
1980  // We're looking for nothing, or we're looking for names that cannot
1981  // be hidden.
1982  return;
1983  }
1984 
1985  using Result = CodeCompletionResult;
1986  for (unsigned I = 0; I != NumResults; ++I) {
1987  if (Results[I].Kind != Result::RK_Declaration)
1988  continue;
1989 
1990  unsigned IDNS
1992 
1993  bool Hiding = false;
1994  if (OnlyTagNames)
1995  Hiding = (IDNS & Decl::IDNS_Tag);
1996  else {
1997  unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2000  if (Ctx.getLangOpts().CPlusPlus)
2001  HiddenIDNS |= Decl::IDNS_Tag;
2002  Hiding = (IDNS & HiddenIDNS);
2003  }
2004 
2005  if (!Hiding)
2006  continue;
2007 
2008  DeclarationName Name = Results[I].Declaration->getDeclName();
2010  HiddenNames.insert(Identifier->getName());
2011  else
2012  HiddenNames.insert(Name.getAsString());
2013  }
2014 }
2015 
2016 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2017  CodeCompletionContext Context,
2018  CodeCompletionResult *Results,
2019  unsigned NumResults) {
2020  // Merge the results we were given with the results we cached.
2021  bool AddedResult = false;
2022  uint64_t InContexts =
2024  ? NormalContexts : (1LL << Context.getKind());
2025  // Contains the set of names that are hidden by "local" completion results.
2026  llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2027  using Result = CodeCompletionResult;
2028  SmallVector<Result, 8> AllResults;
2030  C = AST.cached_completion_begin(),
2031  CEnd = AST.cached_completion_end();
2032  C != CEnd; ++C) {
2033  // If the context we are in matches any of the contexts we are
2034  // interested in, we'll add this result.
2035  if ((C->ShowInContexts & InContexts) == 0)
2036  continue;
2037 
2038  // If we haven't added any results previously, do so now.
2039  if (!AddedResult) {
2040  CalculateHiddenNames(Context, Results, NumResults, S.Context,
2041  HiddenNames);
2042  AllResults.insert(AllResults.end(), Results, Results + NumResults);
2043  AddedResult = true;
2044  }
2045 
2046  // Determine whether this global completion result is hidden by a local
2047  // completion result. If so, skip it.
2048  if (C->Kind != CXCursor_MacroDefinition &&
2049  HiddenNames.count(C->Completion->getTypedText()))
2050  continue;
2051 
2052  // Adjust priority based on similar type classes.
2053  unsigned Priority = C->Priority;
2054  CodeCompletionString *Completion = C->Completion;
2055  if (!Context.getPreferredType().isNull()) {
2056  if (C->Kind == CXCursor_MacroDefinition) {
2057  Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2058  S.getLangOpts(),
2059  Context.getPreferredType()->isAnyPointerType());
2060  } else if (C->Type) {
2063  Context.getPreferredType().getUnqualifiedType());
2064  SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2065  if (ExpectedSTC == C->TypeClass) {
2066  // We know this type is similar; check for an exact match.
2067  llvm::StringMap<unsigned> &CachedCompletionTypes
2068  = AST.getCachedCompletionTypes();
2069  llvm::StringMap<unsigned>::iterator Pos
2070  = CachedCompletionTypes.find(QualType(Expected).getAsString());
2071  if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2072  Priority /= CCF_ExactTypeMatch;
2073  else
2074  Priority /= CCF_SimilarTypeMatch;
2075  }
2076  }
2077  }
2078 
2079  // Adjust the completion string, if required.
2080  if (C->Kind == CXCursor_MacroDefinition &&
2082  // Create a new code-completion string that just contains the
2083  // macro name, without its arguments.
2084  CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2085  CCP_CodePattern, C->Availability);
2086  Builder.AddTypedTextChunk(C->Completion->getTypedText());
2087  Priority = CCP_CodePattern;
2088  Completion = Builder.TakeString();
2089  }
2090 
2091  AllResults.push_back(Result(Completion, Priority, C->Kind,
2092  C->Availability));
2093  }
2094 
2095  // If we did not add any cached completion results, just forward the
2096  // results we were given to the next consumer.
2097  if (!AddedResult) {
2098  Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2099  return;
2100  }
2101 
2102  Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2103  AllResults.size());
2104 }
2105 
2107  StringRef File, unsigned Line, unsigned Column,
2108  ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
2109  bool IncludeCodePatterns, bool IncludeBriefComments,
2110  CodeCompleteConsumer &Consumer,
2111  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2112  DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr,
2113  FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2115  if (!Invocation)
2116  return;
2117 
2118  SimpleTimer CompletionTimer(WantTiming);
2119  CompletionTimer.setOutput("Code completion @ " + File + ":" +
2120  Twine(Line) + ":" + Twine(Column));
2121 
2122  auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
2123 
2124  FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2125  CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2126  PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2127 
2128  CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2129  CachedCompletionResults.empty();
2130  CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2131  CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2132  CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2133  CodeCompleteOpts.LoadExternal = Consumer.loadExternal();
2134  CodeCompleteOpts.IncludeFixIts = Consumer.includeFixIts();
2135 
2136  assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2137 
2138  FrontendOpts.CodeCompletionAt.FileName = File;
2139  FrontendOpts.CodeCompletionAt.Line = Line;
2140  FrontendOpts.CodeCompletionAt.Column = Column;
2141 
2142  // Set the language options appropriately.
2143  LangOpts = *CCInvocation->getLangOpts();
2144 
2145  // Spell-checking and warnings are wasteful during code-completion.
2146  LangOpts.SpellChecking = false;
2147  CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
2148 
2149  std::unique_ptr<CompilerInstance> Clang(
2150  new CompilerInstance(PCHContainerOps));
2151 
2152  // Recover resources if we crash before exiting this method.
2153  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2154  CICleanup(Clang.get());
2155 
2156  auto &Inv = *CCInvocation;
2157  Clang->setInvocation(std::move(CCInvocation));
2158  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2159 
2160  // Set up diagnostics, capturing any diagnostics produced.
2161  Clang->setDiagnostics(&Diag);
2162  CaptureDroppedDiagnostics Capture(true,
2163  Clang->getDiagnostics(),
2164  &StoredDiagnostics, nullptr);
2165  ProcessWarningOptions(Diag, Inv.getDiagnosticOpts());
2166 
2167  // Create the target instance.
2168  Clang->setTarget(TargetInfo::CreateTargetInfo(
2169  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
2170  if (!Clang->hasTarget()) {
2171  Clang->setInvocation(nullptr);
2172  return;
2173  }
2174 
2175  // Inform the target of the language options.
2176  //
2177  // FIXME: We shouldn't need to do this, the target should be immutable once
2178  // created. This complexity should be lifted elsewhere.
2179  Clang->getTarget().adjust(Clang->getLangOpts());
2180 
2181  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2182  "Invocation must have exactly one source file!");
2183  assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
2185  "FIXME: AST inputs not yet supported here!");
2186  assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
2188  "IR inputs not support here!");
2189 
2190  // Use the source and file managers that we were given.
2191  Clang->setFileManager(&FileMgr);
2192  Clang->setSourceManager(&SourceMgr);
2193 
2194  // Remap files.
2195  PreprocessorOpts.clearRemappedFiles();
2196  PreprocessorOpts.RetainRemappedFileBuffers = true;
2197  for (const auto &RemappedFile : RemappedFiles) {
2198  PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second);
2199  OwnedBuffers.push_back(RemappedFile.second);
2200  }
2201 
2202  // Use the code completion consumer we were given, but adding any cached
2203  // code-completion results.
2204  AugmentedCodeCompleteConsumer *AugmentedConsumer
2205  = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2206  Clang->setCodeCompletionConsumer(AugmentedConsumer);
2207 
2208  // If we have a precompiled preamble, try to use it. We only allow
2209  // the use of the precompiled preamble if we're if the completion
2210  // point is within the main file, after the end of the precompiled
2211  // preamble.
2212  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2213  if (Preamble) {
2214  std::string CompleteFilePath(File);
2215 
2216  auto VFS = FileMgr.getVirtualFileSystem();
2217  auto CompleteFileStatus = VFS->status(CompleteFilePath);
2218  if (CompleteFileStatus) {
2219  llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID();
2220 
2221  std::string MainPath(OriginalSourceFile);
2222  auto MainStatus = VFS->status(MainPath);
2223  if (MainStatus) {
2224  llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID();
2225  if (CompleteFileID == MainID && Line > 1)
2226  OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2227  PCHContainerOps, Inv, VFS, false, Line - 1);
2228  }
2229  }
2230  }
2231 
2232  // If the main file has been overridden due to the use of a preamble,
2233  // make that override happen and introduce the preamble.
2234  if (OverrideMainBuffer) {
2235  assert(Preamble &&
2236  "No preamble was built, but OverrideMainBuffer is not null");
2237 
2238  auto VFS = FileMgr.getVirtualFileSystem();
2239  Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS,
2240  OverrideMainBuffer.get());
2241  // FIXME: there is no way to update VFS if it was changed by
2242  // AddImplicitPreamble as FileMgr is accepted as a parameter by this method.
2243  // We use on-disk preambles instead and rely on FileMgr's VFS to ensure the
2244  // PCH files are always readable.
2245  OwnedBuffers.push_back(OverrideMainBuffer.release());
2246  } else {
2247  PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2248  PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2249  }
2250 
2251  // Disable the preprocessing record if modules are not enabled.
2252  if (!Clang->getLangOpts().Modules)
2253  PreprocessorOpts.DetailedRecord = false;
2254 
2255  std::unique_ptr<SyntaxOnlyAction> Act;
2256  Act.reset(new SyntaxOnlyAction);
2257  if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2258  Act->Execute();
2259  Act->EndSourceFile();
2260  }
2261 }
2262 
2263 bool ASTUnit::Save(StringRef File) {
2264  if (HadModuleLoaderFatalFailure)
2265  return true;
2266 
2267  // Write to a temporary file and later rename it to the actual file, to avoid
2268  // possible race conditions.
2269  SmallString<128> TempPath;
2270  TempPath = File;
2271  TempPath += "-%%%%%%%%";
2272  int fd;
2273  if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath))
2274  return true;
2275 
2276  // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2277  // unconditionally create a stat cache when we parse the file?
2278  llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2279 
2280  serialize(Out);
2281  Out.close();
2282  if (Out.has_error()) {
2283  Out.clear_error();
2284  return true;
2285  }
2286 
2287  if (llvm::sys::fs::rename(TempPath, File)) {
2288  llvm::sys::fs::remove(TempPath);
2289  return true;
2290  }
2291 
2292  return false;
2293 }
2294 
2295 static bool serializeUnit(ASTWriter &Writer,
2296  SmallVectorImpl<char> &Buffer,
2297  Sema &S,
2298  bool hasErrors,
2299  raw_ostream &OS) {
2300  Writer.WriteAST(S, std::string(), nullptr, "", hasErrors);
2301 
2302  // Write the generated bitstream to "Out".
2303  if (!Buffer.empty())
2304  OS.write(Buffer.data(), Buffer.size());
2305 
2306  return false;
2307 }
2308 
2309 bool ASTUnit::serialize(raw_ostream &OS) {
2310  // For serialization we are lenient if the errors were only warn-as-error kind.
2311  bool hasErrors = getDiagnostics().hasUncompilableErrorOccurred();
2312 
2313  if (WriterData)
2314  return serializeUnit(WriterData->Writer, WriterData->Buffer,
2315  getSema(), hasErrors, OS);
2316 
2317  SmallString<128> Buffer;
2318  llvm::BitstreamWriter Stream(Buffer);
2319  MemoryBufferCache PCMCache;
2320  ASTWriter Writer(Stream, Buffer, PCMCache, {});
2321  return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2322 }
2323 
2325 
2326 void ASTUnit::TranslateStoredDiagnostics(
2327  FileManager &FileMgr,
2328  SourceManager &SrcMgr,
2331  // Map the standalone diagnostic into the new source manager. We also need to
2332  // remap all the locations to the new view. This includes the diag location,
2333  // any associated source ranges, and the source ranges of associated fix-its.
2334  // FIXME: There should be a cleaner way to do this.
2336  Result.reserve(Diags.size());
2337 
2338  for (const auto &SD : Diags) {
2339  // Rebuild the StoredDiagnostic.
2340  if (SD.Filename.empty())
2341  continue;
2342  const FileEntry *FE = FileMgr.getFile(SD.Filename);
2343  if (!FE)
2344  continue;
2345  SourceLocation FileLoc;
2346  auto ItFileID = PreambleSrcLocCache.find(SD.Filename);
2347  if (ItFileID == PreambleSrcLocCache.end()) {
2348  FileID FID = SrcMgr.translateFile(FE);
2349  FileLoc = SrcMgr.getLocForStartOfFile(FID);
2350  PreambleSrcLocCache[SD.Filename] = FileLoc;
2351  } else {
2352  FileLoc = ItFileID->getValue();
2353  }
2354 
2355  if (FileLoc.isInvalid())
2356  continue;
2357  SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset);
2358  FullSourceLoc Loc(L, SrcMgr);
2359 
2361  Ranges.reserve(SD.Ranges.size());
2362  for (const auto &Range : SD.Ranges) {
2363  SourceLocation BL = FileLoc.getLocWithOffset(Range.first);
2364  SourceLocation EL = FileLoc.getLocWithOffset(Range.second);
2365  Ranges.push_back(CharSourceRange::getCharRange(BL, EL));
2366  }
2367 
2369  FixIts.reserve(SD.FixIts.size());
2370  for (const auto &FixIt : SD.FixIts) {
2371  FixIts.push_back(FixItHint());
2372  FixItHint &FH = FixIts.back();
2373  FH.CodeToInsert = FixIt.CodeToInsert;
2374  SourceLocation BL = FileLoc.getLocWithOffset(FixIt.RemoveRange.first);
2375  SourceLocation EL = FileLoc.getLocWithOffset(FixIt.RemoveRange.second);
2377  }
2378 
2379  Result.push_back(StoredDiagnostic(SD.Level, SD.ID,
2380  SD.Message, Loc, Ranges, FixIts));
2381  }
2382  Result.swap(Out);
2383 }
2384 
2386  assert(D);
2387 
2388  // We only care about local declarations.
2389  if (D->isFromASTFile())
2390  return;
2391 
2392  SourceManager &SM = *SourceMgr;
2393  SourceLocation Loc = D->getLocation();
2394  if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2395  return;
2396 
2397  // We only keep track of the file-level declarations of each file.
2398  if (!D->getLexicalDeclContext()->isFileContext())
2399  return;
2400 
2401  SourceLocation FileLoc = SM.getFileLoc(Loc);
2402  assert(SM.isLocalSourceLocation(FileLoc));
2403  FileID FID;
2404  unsigned Offset;
2405  std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2406  if (FID.isInvalid())
2407  return;
2408 
2409  LocDeclsTy *&Decls = FileDecls[FID];
2410  if (!Decls)
2411  Decls = new LocDeclsTy();
2412 
2413  std::pair<unsigned, Decl *> LocDecl(Offset, D);
2414 
2415  if (Decls->empty() || Decls->back().first <= Offset) {
2416  Decls->push_back(LocDecl);
2417  return;
2418  }
2419 
2420  LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
2421  LocDecl, llvm::less_first());
2422 
2423  Decls->insert(I, LocDecl);
2424 }
2425 
2426 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2427  SmallVectorImpl<Decl *> &Decls) {
2428  if (File.isInvalid())
2429  return;
2430 
2431  if (SourceMgr->isLoadedFileID(File)) {
2432  assert(Ctx->getExternalSource() && "No external source!");
2433  return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2434  Decls);
2435  }
2436 
2437  FileDeclsTy::iterator I = FileDecls.find(File);
2438  if (I == FileDecls.end())
2439  return;
2440 
2441  LocDeclsTy &LocDecls = *I->second;
2442  if (LocDecls.empty())
2443  return;
2444 
2445  LocDeclsTy::iterator BeginIt =
2446  std::lower_bound(LocDecls.begin(), LocDecls.end(),
2447  std::make_pair(Offset, (Decl *)nullptr),
2448  llvm::less_first());
2449  if (BeginIt != LocDecls.begin())
2450  --BeginIt;
2451 
2452  // If we are pointing at a top-level decl inside an objc container, we need
2453  // to backtrack until we find it otherwise we will fail to report that the
2454  // region overlaps with an objc container.
2455  while (BeginIt != LocDecls.begin() &&
2456  BeginIt->second->isTopLevelDeclInObjCContainer())
2457  --BeginIt;
2458 
2459  LocDeclsTy::iterator EndIt = std::upper_bound(
2460  LocDecls.begin(), LocDecls.end(),
2461  std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first());
2462  if (EndIt != LocDecls.end())
2463  ++EndIt;
2464 
2465  for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2466  Decls.push_back(DIt->second);
2467 }
2468 
2470  unsigned Line, unsigned Col) const {
2471  const SourceManager &SM = getSourceManager();
2472  SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2473  return SM.getMacroArgExpandedLocation(Loc);
2474 }
2475 
2477  unsigned Offset) const {
2478  const SourceManager &SM = getSourceManager();
2479  SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2480  return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2481 }
2482 
2483 /// If \arg Loc is a loaded location from the preamble, returns
2484 /// the corresponding local location of the main file, otherwise it returns
2485 /// \arg Loc.
2487  FileID PreambleID;
2488  if (SourceMgr)
2489  PreambleID = SourceMgr->getPreambleFileID();
2490 
2491  if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2492  return Loc;
2493 
2494  unsigned Offs;
2495  if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble->getBounds().Size) {
2496  SourceLocation FileLoc
2497  = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2498  return FileLoc.getLocWithOffset(Offs);
2499  }
2500 
2501  return Loc;
2502 }
2503 
2504 /// If \arg Loc is a local location of the main file but inside the
2505 /// preamble chunk, returns the corresponding loaded location from the
2506 /// preamble, otherwise it returns \arg Loc.
2508  FileID PreambleID;
2509  if (SourceMgr)
2510  PreambleID = SourceMgr->getPreambleFileID();
2511 
2512  if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2513  return Loc;
2514 
2515  unsigned Offs;
2516  if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2517  Offs < Preamble->getBounds().Size) {
2518  SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2519  return FileLoc.getLocWithOffset(Offs);
2520  }
2521 
2522  return Loc;
2523 }
2524 
2526  FileID FID;
2527  if (SourceMgr)
2528  FID = SourceMgr->getPreambleFileID();
2529 
2530  if (Loc.isInvalid() || FID.isInvalid())
2531  return false;
2532 
2533  return SourceMgr->isInFileID(Loc, FID);
2534 }
2535 
2537  FileID FID;
2538  if (SourceMgr)
2539  FID = SourceMgr->getMainFileID();
2540 
2541  if (Loc.isInvalid() || FID.isInvalid())
2542  return false;
2543 
2544  return SourceMgr->isInFileID(Loc, FID);
2545 }
2546 
2548  FileID FID;
2549  if (SourceMgr)
2550  FID = SourceMgr->getPreambleFileID();
2551 
2552  if (FID.isInvalid())
2553  return {};
2554 
2555  return SourceMgr->getLocForEndOfFile(FID);
2556 }
2557 
2559  FileID FID;
2560  if (SourceMgr)
2561  FID = SourceMgr->getMainFileID();
2562 
2563  if (FID.isInvalid())
2564  return {};
2565 
2566  return SourceMgr->getLocForStartOfFile(FID);
2567 }
2568 
2569 llvm::iterator_range<PreprocessingRecord::iterator>
2571  if (isMainFileAST()) {
2573  Mod = Reader->getModuleManager().getPrimaryModule();
2574  return Reader->getModulePreprocessedEntities(Mod);
2575  }
2576 
2577  if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2578  return llvm::make_range(PPRec->local_begin(), PPRec->local_end());
2579 
2580  return llvm::make_range(PreprocessingRecord::iterator(),
2582 }
2583 
2585  if (isMainFileAST()) {
2587  Mod = Reader->getModuleManager().getPrimaryModule();
2588  for (const auto *D : Reader->getModuleFileLevelDecls(Mod)) {
2589  if (!Fn(context, D))
2590  return false;
2591  }
2592 
2593  return true;
2594  }
2595 
2597  TLEnd = top_level_end();
2598  TL != TLEnd; ++TL) {
2599  if (!Fn(context, *TL))
2600  return false;
2601  }
2602 
2603  return true;
2604 }
2605 
2607  if (!Reader)
2608  return nullptr;
2609 
2610  serialization::ModuleFile *Mod = nullptr;
2611  Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) {
2612  switch (M.Kind) {
2613  case serialization::MK_ImplicitModule:
2614  case serialization::MK_ExplicitModule:
2615  case serialization::MK_PrebuiltModule:
2616  return true; // skip dependencies.
2617  case serialization::MK_PCH:
2618  Mod = &M;
2619  return true; // found it.
2620  case serialization::MK_Preamble:
2621  return false; // look in dependencies.
2622  case serialization::MK_MainFile:
2623  return false; // look in dependencies.
2624  }
2625 
2626  return true;
2627  });
2628  if (Mod)
2629  return Mod->File;
2630 
2631  return nullptr;
2632 }
2633 
2636 }
2637 
2639  auto &LangOpts = getLangOpts();
2640 
2641  InputKind::Language Lang;
2642  if (LangOpts.OpenCL)
2643  Lang = InputKind::OpenCL;
2644  else if (LangOpts.CUDA)
2645  Lang = InputKind::CUDA;
2646  else if (LangOpts.RenderScript)
2647  Lang = InputKind::RenderScript;
2648  else if (LangOpts.CPlusPlus)
2649  Lang = LangOpts.ObjC1 ? InputKind::ObjCXX : InputKind::CXX;
2650  else
2651  Lang = LangOpts.ObjC1 ? InputKind::ObjC : InputKind::C;
2652 
2654  if (LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap)
2655  Fmt = InputKind::ModuleMap;
2656 
2657  // We don't know if input was preprocessed. Assume not.
2658  bool PP = false;
2659 
2660  return InputKind(Lang, Fmt, PP);
2661 }
2662 
2663 #ifndef NDEBUG
2664 ASTUnit::ConcurrencyState::ConcurrencyState() {
2665  Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2666 }
2667 
2668 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2669  delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2670 }
2671 
2672 void ASTUnit::ConcurrencyState::start() {
2673  bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2674  assert(acquired && "Concurrent access to ASTUnit!");
2675 }
2676 
2677 void ASTUnit::ConcurrencyState::finish() {
2678  static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2679 }
2680 
2681 #else // NDEBUG
2682 
2683 ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = nullptr; }
2684 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2685 void ASTUnit::ConcurrencyState::start() {}
2686 void ASTUnit::ConcurrencyState::finish() {}
2687 
2688 #endif // NDEBUG
An unknown context, in which we are recovering from a parsing error and don&#39;t know which completions ...
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
Defines the clang::ASTContext interface.
Describes the bounds (start, size) of the preamble and a flag required by PreprocessorOptions::Precom...
Definition: Lexer.h:59
unsigned getID() const
Definition: Diagnostic.h:1457
void ResetForParse()
Free data that will be re-generated on the next parse.
Definition: ASTUnit.cpp:1853
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:1627
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:509
A (possibly-)qualified type.
Definition: Type.h:642
virtual Decl * GetExternalDecl(uint32_t ID)
Resolve a declaration ID into a declaration, potentially building a new declaration.
static std::unique_ptr< T > valueOrNull(llvm::ErrorOr< std::unique_ptr< T >> Val)
Definition: ASTUnit.cpp:140
unsigned IncludeBriefComments
Show brief documentation comments in code completion results.
bool RemappedFilesKeepOriginalName
True if the SourceManager should report the original file name for contents of files that were remapp...
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:117
const LangOptions & getLangOpts() const
Definition: ASTUnit.h:456
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:34
std::pair< unsigned, unsigned > InsertFromRange
Definition: ASTUnit.h:91
void addTopLevelDecl(Decl *D)
Add a new top-level declaration.
Definition: ASTUnit.h:522
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...
unsigned IncludeGlobals
Show top-level decls in code completion results.
unsigned getMacroUsagePriority(StringRef MacroName, const LangOptions &LangOpts, bool PreferredTypeIsPointer=false)
Determine the priority to be given to a macro code completion result with the given name...
Code completion where an Objective-C class message is expected.
C Language Family Type Representation.
Defines the SourceManager interface.
Code completion within a type-qualifier list.
Load everything, including Sema.
Definition: ASTUnit.h:671
Abstract base class for actions which can be performed by the frontend.
Defines the clang::Module class, which describes a module in the source code.
bool isLocalSourceLocation(SourceLocation Loc) const
Returns true if Loc did not come from a PCH/Module.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
ModuleKind Kind
The type of this module.
Definition: Module.h:120
const FileManager & getFileManager() const
Definition: ASTUnit.h:471
Defines the C++ template declaration subclasses.
const unsigned DefaultPreambleRebuildInterval
After failing to build a precompiled preamble (due to errors in the source that occurs in the preambl...
Definition: ASTUnit.cpp:233
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Definition: Diagnostic.h:1436
StringRef getASTFileName() const
If this ASTUnit came from an AST file, returns the filename for it.
Definition: ASTUnit.cpp:1456
std::string CodeToInsert
The actual code to insert at the insertion location, as a string.
Definition: Diagnostic.h:78
An unspecified code-completion context.
bool isCompilingModule() const
Are we compiling a module interface (.cppm or module map)?
Definition: LangOptions.h:230
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:404
Code completion occurred on the right-hand side of a member access expression using the arrow operato...
Code completion occurred after the "enum" keyword, to indicate an enumeration name.
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
virtual void EndSourceFile()
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: Diagnostic.h:1519
bool isOutputBinary() const
Determine whether the output of this consumer is binary.
Options for controlling the target.
Definition: TargetOptions.h:26
Manage memory buffers across multiple users.
SourceLocation mapLocationToPreamble(SourceLocation Loc) const
If Loc is a local location of the main file but inside the preamble chunk, returns the corresponding ...
Definition: ASTUnit.cpp:2507
void addRemappedFile(StringRef From, StringRef To)
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1487
NamedDecl * getUnderlyingDecl()
Looks through UsingDecls and ObjCCompatibleAliasDecls for the underlying named decl.
Definition: Decl.h:431
static void checkAndSanitizeDiags(SmallVectorImpl< StoredDiagnostic > &StoredDiagnostics, SourceManager &SM)
Definition: ASTUnit.cpp: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:38
bool hasSourceManager() const
Definition: Diagnostic.h:1327
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Definition: PPCallbacks.h:36
Parse and apply any fixits to the source.
static std::unique_ptr< ASTUnit > create(std::shared_ptr< CompilerInvocation > CI, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, bool CaptureDiagnostics, bool UserFilesAreVolatile)
Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
Definition: ASTUnit.cpp:1466
Types, declared with &#39;struct foo&#39;, typedefs, etc.
Definition: DeclBase.h:131
std::vector< std::pair< unsigned, unsigned > > Ranges
Definition: ASTUnit.h:102
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:297
One of these records is kept for each identifier that is lexed.
CodeCompletionString * TakeString()
Take the resulting completion string.
cached_completion_iterator cached_completion_end()
Definition: ASTUnit.h:613
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
Definition: Diagnostic.cpp:92
SourceLocation getBegin() const
Iteration over the preprocessed entities.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
Utility class for loading a ASTContext from an AST file.
Definition: ASTUnit.h:87
bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input)
Prepare the action for processing the input file Input.
SkipFunctionBodiesScope
Enumerates the available scopes for skipping function bodies.
Definition: ASTUnit.h:84
A "string" used to describe how code completion can be performed for an entity.
static std::atomic< unsigned > ActiveASTUnitObjects
Tracks the number of ASTUnit objects that are currently active.
Definition: ASTUnit.cpp:238
SourceLocation translateFileLineCol(const FileEntry *SourceFile, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
static bool moveOnNoError(llvm::ErrorOr< T > Val, T &Output)
Definition: ASTUnit.cpp:147
Kind getKind() const
Retrieve the kind of code-completion context.
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
bool isInPreambleFileID(SourceLocation Loc) const
Definition: ASTUnit.cpp:2525
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:667
std::shared_ptr< Preprocessor > getPreprocessorPtr()
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
Compiling a module from a module map.
Definition: LangOptions.h:74
Describes a module or submodule.
Definition: Module.h:65
StringRef getMainFileName() const
Definition: ASTUnit.cpp:1438
Languages that the frontend can parse and compile.
unsigned IncludeCodePatterns
Show code patterns in code completion results.
An allocator used specifically for the purpose of code completion.
DiagnosticsEngine::Level Level
Definition: ASTUnit.h:98
bool isInvalid() const
Load the AST, but do not restore Sema state.
Definition: ASTUnit.h:668
FileID translateFile(const FileEntry *SourceFile) const
Get the FileID for the given file.
void setPreprocessor(std::shared_ptr< Preprocessor > pp)
Definition: ASTUnit.cpp:273
std::unique_ptr< CompilerInvocation > createInvocationFromCommandLine(ArrayRef< const char *> Args, IntrusiveRefCntPtr< DiagnosticsEngine > Diags=IntrusiveRefCntPtr< DiagnosticsEngine >(), IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
createInvocationFromCommandLine - Construct a compiler invocation object for a command line argument ...
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
Namespaces, declared with &#39;namespace foo {}&#39;.
Definition: DeclBase.h:141
uint32_t Offset
Definition: CacheTokens.cpp:43
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
void Initialize(const TargetInfo &Target, const TargetInfo *AuxTarget=nullptr)
Initialize the preprocessor using information about the target.
const FileEntry * getPCHFile()
Get the PCH file if one was included.
Definition: ASTUnit.cpp:2606
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:2309
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
std::string ResourceDir
The directory which holds the compiler resource files (builtin includes, etc.).
virtual CodeCompletionTUInfo & getCodeCompletionTUInfo()=0
Code completion occurred within an Objective-C implementation or category implementation.
ArrayRef< CharSourceRange > getRanges() const
Definition: Diagnostic.h:1470
Defines the Diagnostic-related interfaces.
bool RetainRemappedFileBuffers
Whether the compiler instance should retain (i.e., not free) the buffers associated with remapped fil...
unsigned SkipFunctionBodies
Skip over function bodies to speed up parsing in cases you do not need them (e.g. ...
void addFileLevelDecl(Decl *D)
Add a new local file-level declaration.
Definition: ASTUnit.cpp:2385
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:1213
static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag)
Definition: ASTUnit.cpp:1041
StringRef getMessage() const
Definition: Diagnostic.h:1460
void Reset()
Reset the state of the diagnostic object to its initial configuration.
Definition: Diagnostic.cpp:114
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
The AST file itself appears corrupted.
Definition: ASTReader.h:387
Ordinary names.
Definition: DeclBase.h:145
virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, SourceLocation OpenParLoc)
std::vector< std::pair< std::string, llvm::MemoryBuffer * > > RemappedFileBuffers
The set of file-to-buffer remappings, which take existing files on the system (the first part of each...
Encapsulates the information needed to find the file referenced by a #include or #include_next, (sub-)framework lookup, etc.
Definition: HeaderSearch.h:148
FrontendOptions & getFrontendOpts()
A set of callbacks to gather useful information while building a preamble.
bool DetailedRecord
Whether we should maintain a detailed record of all macro definitions and expansions.
Code completion where an Objective-C category name is expected.
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
static void checkAndRemoveNonDriverDiags(SmallVectorImpl< StoredDiagnostic > &StoredDiags)
Definition: ASTUnit.cpp:1046
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:278
Code completion occurred within a "top-level" completion context, e.g., at namespace or global scope...
static void AddDefinedMacroToHash(const Token &MacroNameTok, unsigned &Hash)
Add the given macro to the hash of all top-level entities.
Definition: ASTUnit.cpp:848
This declaration is a C++ operator declared in a non-class context.
Definition: DeclBase.h:169
StringRef Filename
Definition: Format.cpp:1620
bool isModuleFile() const
Returns true if the ASTUnit was constructed from a serialized module file.
Definition: ASTUnit.cpp:2634
IntrusiveRefCntPtr< ASTReader > getModuleManager() const
Code completion occurred where a protocol name is expected.
Allows QualTypes to be sorted and hence used in maps and sets.
CommentOptions CommentOpts
Options for parsing comments.
Definition: LangOptions.h:203
QualType getPreferredType() const
Retrieve the type that this expression would prefer to have, e.g., if the expression is a variable in...
llvm::MemoryBuffer * getBuffer() const
CodeCompletionTUInfo & getCodeCompletionTUInfo()
Definition: ASTUnit.h:311
IntrusiveRefCntPtr< ASTReader > getASTReader() const
Definition: ASTUnit.cpp:697
Defines the clang::LangOptions interface.
bool isMainFileAST() const
Definition: ASTUnit.h:419
Code completion occurred where a new name is expected.
Represents a character-granular source range.
DiagnosticsEngine::Level getLevel() const
Definition: Diagnostic.h:1458
llvm::StringRef getAsString(SyncScope S)
Definition: SyncScope.h:51
std::pair< unsigned, unsigned > RemoveRange
Definition: ASTUnit.h:90
static void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash)
Add the given declaration to the hash of all top-level entities.
Definition: ASTUnit.cpp:871
unsigned & getCurrentTopLevelHashValue()
Retrieve a reference to the current top-level name hash value.
Definition: ASTUnit.h:538
bool hasUncompilableErrorOccurred() const
Errors that actually prevent compilation, not those that are upgraded from a warning by -Werror...
Definition: Diagnostic.h:750
const AnnotatedLine * Line
ArrayRef< FixItHint > getFixIts() const
Definition: Diagnostic.h:1480
StringRef getFile() const
std::vector< StandaloneFixIt > FixIts
Definition: ASTUnit.h:103
std::string getAsString() const
Retrieve the human-readable string for this name.
Defines the clang::Preprocessor interface.
const NamedDecl * Declaration
When Kind == RK_Declaration or RK_Pattern, the declaration we are referring to.
bool isFileContext() const
Definition: DeclBase.h:1817
comments::CommandTraits & getCommentCommandTraits() const
Definition: ASTContext.h:884
const SourceManager & getManager() const
DeclContext * getDeclContext()
Definition: DeclBase.h:427
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
Information about a module that has been loaded by the ASTReader.
Definition: Module.h:108
IdentifierInfo * getAsIdentifierInfo() const
Retrieve the IdentifierInfo * stored in this declaration name, or null if this declaration name isn&#39;t...
Captures a result of code completion.
Code completion occurred where a new name is expected and a qualified name is permissible.
static unsigned getDeclShowContexts(const NamedDecl *ND, const LangOptions &LangOpts, bool &IsNestedNameSpecifier)
Determine the set of code-completion contexts in which this declaration should be shown...
Definition: ASTUnit.cpp:285
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:123
bool BeforePreviousInsertions
Definition: Diagnostic.h:80
Code completion occurred in a context where natural language is expected, e.g., a comment or string l...
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, const std::shared_ptr< TargetOptions > &Opts)
Construct a target for the given options.
Definition: Targets.cpp:596
PreambleBounds ComputePreambleBounds(const LangOptions &LangOpts, llvm::MemoryBuffer *Buffer, unsigned MaxLines)
Runs lexer to compute suggested preamble bounds.
SmallString< 128 > Buffer
Definition: ASTUnit.cpp:217
An input file for the front end.
static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM, const LangOptions &LangOpts, const FixItHint &InFix)
Definition: ASTUnit.cpp:1222
top_level_iterator top_level_begin()
Definition: ASTUnit.h:497
The result type of a method or function.
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:707
const SourceManager & SM
Definition: Format.cpp:1490
The client can&#39;t handle any AST loading failures.
Definition: ASTReader.h:1508
The AST file was missing.
Definition: ASTReader.h:390
CharSourceRange InsertFromRange
Code in the specific range that should be inserted in the insertion location.
Definition: Diagnostic.h:74
unsigned getNumWarnings() const
Definition: Diagnostic.h:760
std::unique_ptr< Sema > takeSema()
static CharSourceRange getCharRange(SourceRange R)
The context in which code completion occurred, so that the code-completion consumer can process the r...
bool AllowPCHWithCompilerErrors
When true, a PCH with compiler errors will not be rejected.
CharSourceRange RemoveRange
Code that should be replaced to correct the error.
Definition: Diagnostic.h:70
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
Abstract interface for external sources of AST nodes.
static void CalculateHiddenNames(const CodeCompletionContext &Context, CodeCompletionResult *Results, unsigned NumResults, ASTContext &Ctx, llvm::StringSet< llvm::BumpPtrAllocator > &HiddenNames)
Helper function that computes which global names are hidden by the local code-completion results...
Definition: ASTUnit.cpp:1933
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
The control block was read successfully.
Definition: ASTReader.h:384
std::pair< std::string, llvm::MemoryBuffer * > RemappedFile
A mapping from a file name to the memory buffer that stores the remapped contents of that file...
Definition: ASTUnit.h:655
Code completion occurred within a class, struct, or union.
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
Definition: MacroInfo.h:291
#define false
Definition: stdbool.h:33
Kind
SourceLocation mapLocationFromPreamble(SourceLocation Loc) const
If Loc is a loaded location from the preamble, returns the corresponding local location of the main f...
Definition: ASTUnit.cpp:2486
OverloadCandidate - A single candidate in an overload set (C++ 13.3).
Definition: Overload.h:730
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
Divide by this factor when a code-completion result&#39;s type exactly matches the type we expect...
bool Execute()
Set the source manager&#39;s main input file, and run the action.
bool isInMainFileID(SourceLocation Loc) const
Definition: ASTUnit.cpp:2536
Members, declared with object declarations within tag definitions.
Definition: DeclBase.h:137
FileSystemOptions & getFileSystemOpts()
Returns the current file system options.
Definition: FileManager.h:223
ParsedSourceLocation CodeCompletionAt
If given, enable code completion at the provided location.
virtual TranslationUnitKind getTranslationUnitKind()
For AST-based actions, the kind of translation unit we&#39;re handling.
ASTWriterData(MemoryBufferCache &PCMCache)
Definition: ASTUnit.cpp:221
llvm::StringMap< unsigned > & getCachedCompletionTypes()
Retrieve the mapping from formatted type names to unique type identifiers.
Definition: ASTUnit.h:301
LLVM IR: we accept this so that we can run the optimizer on it, and compile it to assembly or object ...
std::vector< FrontendInputFile > Inputs
The input files and their types.
Sema & getSema() const
Definition: ASTUnit.h:451
void CodeComplete(StringRef File, unsigned Line, unsigned Column, ArrayRef< RemappedFile > RemappedFiles, bool IncludeMacros, bool IncludeCodePatterns, bool IncludeBriefComments, CodeCompleteConsumer &Consumer, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, FileManager &FileMgr, SmallVectorImpl< StoredDiagnostic > &StoredDiagnostics, SmallVectorImpl< const llvm::MemoryBuffer *> &OwnedBuffers)
Perform code completion at the given file, line, and column within this translation unit...
Definition: ASTUnit.cpp:2106
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:177
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:60
The kind of a file that we&#39;ve been handed as an input.
Code completion where the name of an Objective-C class is expected.
std::vector< CachedCodeCompletionResult >::iterator cached_completion_iterator
Definition: ASTUnit.h:607
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, bool ReportDiags=true)
ProcessWarningOptions - Initialize the diagnostic client and process the warning options specified on...
Definition: Warnings.cpp:44
Abstract base class to use for AST consumer-based frontend actions.
SourceLocation getFileLoc(SourceLocation Loc) const
Given Loc, if it is a macro location return the expansion location or the spelling location...
Code completion occurred within an Objective-C interface, protocol, or category interface.
Defines the clang::TargetOptions class.
DiagnosticOptions & getDiagnosticOpts() const
static std::pair< unsigned, unsigned > makeStandaloneRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Definition: ASTUnit.cpp:1214
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:1701
bool isAnyPointerType() const
Definition: Type.h:6274
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
Definition: DeclBase.h:695
virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info)
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
Definition: Diagnostic.cpp:525
void setListener(std::unique_ptr< ASTReaderListener > Listener)
Set the AST callbacks listener.
Definition: ASTReader.h:1575
void InitBuiltinTypes(const TargetInfo &Target, const TargetInfo *AuxTarget=nullptr)
Initialize built-in types.
SourceManager & getSourceManager() const
Definition: Diagnostic.h:1328
SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T)
Determine the simplified type class of the given canonical type.
llvm::BitstreamWriter Stream
Definition: ASTUnit.cpp:218
TranslationUnitKind getTranslationUnitKind() const
Determine what kind of translation unit this AST represents.
Definition: ASTUnit.h:648
void setNumWarnings(unsigned NumWarnings)
Definition: Diagnostic.h:762
The AST file was writtten with a different language/target configuration.
Definition: ASTReader.h:401
bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn)
Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declara...
Definition: ASTUnit.cpp:2584
File is a PCH file treated as the actual main file.
Definition: Module.h:57
static ASTUnit * LoadFromCompilerInvocationAction(std::shared_ptr< CompilerInvocation > CI, std::shared_ptr< PCHContainerOperations > PCHContainerOps, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, FrontendAction *Action=nullptr, ASTUnit *Unit=nullptr, bool Persistent=true, StringRef ResourceFilesPath=StringRef(), bool OnlyLocalDecls=false, bool CaptureDiagnostics=false, unsigned PrecompilePreambleAfterNParses=0, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool UserFilesAreVolatile=false, std::unique_ptr< ASTUnit > *ErrAST=nullptr)
Create an ASTUnit from a source file, via a CompilerInvocation object, by invoking the optionally pro...
Definition: ASTUnit.cpp:1485
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:1235
Priority for a nested-name-specifier.
Dataflow Directional Tag Classes.
virtual llvm::StringRef getFormat() const =0
Equivalent to the format passed to -fmodule-format=.
bool isValid() const
Return true if this is a valid SourceLocation object.
static std::unique_ptr< llvm::MemoryBuffer > getBufferForFileHandlingRemapping(const CompilerInvocation &Invocation, llvm::vfs::FileSystem *VFS, StringRef FilePath, bool isVolatile)
Get a source buffer for MainFilePath, handling all file-to-file and file-to-buffer remappings inside ...
Definition: ASTUnit.cpp:157
Code completion occurred where an macro is being defined.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1261
PreprocessorOptions & getPreprocessorOpts()
static CharSourceRange makeFileCharRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Accepts a range and returns a character range with file locations.
Definition: Lexer.cpp:868
Code completion occurred after the "struct" or "class" keyword, to indicate a struct or class name...
llvm::iterator_range< PreprocessingRecord::iterator > getLocalPreprocessingEntities() const
Returns an iterator range for the local preprocessing entities of the local Preprocessor, if this is a parsed source file, or the loaded preprocessing entities of the primary module if this is an AST file.
Definition: ASTUnit.cpp:2570
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:355
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
const FullSourceLoc & getLocation() const
Definition: Diagnostic.h:1459
Code completion occurred after the "union" keyword, to indicate a union name.
Code completion occurred where a macro name is expected (without any arguments, in the case of a func...
bool(*)(void *context, const Decl *D) DeclVisitorFn
Type for a function iterating over a number of declarations.
Definition: ASTUnit.h:629
bool includeFixIts() const
Whether to include completion items with small fix-its, e.g.
A builder class used to construct new code-completion strings.
Code completion where an Objective-C instance message is expected.
The name of a declaration.
ASTMutationListener * getASTMutationListener()
Definition: ASTUnit.cpp:701
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:975
void enableSourceFileDiagnostics()
Enable source-range based diagnostic messages.
Definition: ASTUnit.cpp:277
bool SingleFileParseMode
When enabled, preprocessor is in a mode for parsing a single file only.
Helper class for holding the data necessary to invoke the compiler.
const HeaderSearchOptions & getHeaderSearchOpts() const
Definition: ASTUnit.h:461
Divide by this factor when a code-completion result&#39;s type is similar to the type we expect (e...
Language
The language for the input, used to select and validate the language standard and possible actions...
SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const
If Loc points inside a function macro argument, the returned location will be the macro location in w...
top_level_iterator top_level_end()
Definition: ASTUnit.h:504
A map from continuous integer ranges to some value, with a very specialized interface.
Tags, declared with &#39;struct foo;&#39; and referenced with &#39;struct foo&#39;.
Definition: DeclBase.h:126
FrontendOptions - Options for controlling the behavior of the frontend.
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any...
Definition: ASTContext.h:1089
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:2426
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:2263
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:1795
cached_completion_iterator cached_completion_begin()
Definition: ASTUnit.h:609
Abstract interface for callback invocations by the ASTReader.
Definition: ASTReader.h:127
SourceLocation getEndOfPreambleFileID() const
Definition: ASTUnit.cpp:2547
TargetInfo & getTarget() const
CodeCompleteOptions CodeCompleteOpts
unsigned Size
Size of the preamble in bytes.
Definition: Lexer.h:61
SourceLocation getEnd() const
const ASTContext & getASTContext() const
Definition: ASTUnit.h:434
Defines the PPCallbacks interface.
Keeps track of options that affect how file operations are performed.
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2259
static bool serializeUnit(ASTWriter &Writer, SmallVectorImpl< char > &Buffer, Sema &S, bool hasErrors, raw_ostream &OS)
Definition: ASTUnit.cpp:2295
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:6126
const DiagnosticsEngine & getDiagnostics() const
Definition: ASTUnit.h:424
Code completion inside the filename part of a #include directive.
virtual CodeCompletionAllocator & getAllocator()=0
Retrieve the allocator that will be used to allocate code completion strings.
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:824
const SourceManager & getSourceManager() const
Definition: ASTUnit.h:427
Priority for a code pattern.
An unspecified code-completion context where we should also add macro completions.
unsigned IncludeFixIts
Include results after corrections (small fix-its), e.g.
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:152
ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile, Module *WritingModule, StringRef isysroot, bool hasErrors=false)
Write a precompiled header for the given semantic analysis.
Definition: ASTWriter.cpp:4596
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:329
DeclContext * getLookupParent()
Find the parent context of this context that will be used for unqualified name lookup.
Definition: DeclBase.cpp:1050
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:104
serialization::DeclID getDeclID(const Decl *D)
Determine the declaration ID of an already-emitted declaration.
Definition: ASTWriter.cpp:5542
bool hadModuleLoaderFatalFailure() const
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1315
std::pair< unsigned, bool > PrecompiledPreambleBytes
If non-zero, the implicit PCH include is actually a precompiled preamble that covers this number of b...
Defines the clang::TargetInfo interface.
The AST file is out-of-date relative to its input files, and needs to be regenerated.
Definition: ASTReader.h:394
A SourceLocation and its associated SourceManager.
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
The translation unit is a complete translation unit.
Definition: LangOptions.h:331
SourceLocation getStartOfMainFileID() const
Definition: ASTUnit.cpp:2558
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
std::unique_ptr< ASTConsumer > takeASTConsumer()
takeASTConsumer - Remove the current AST consumer and give ownership to the caller.
unsigned LoadExternal
Hint whether to load data from the external AST to provide full results.
The AST file was written by a different version of Clang.
Definition: ASTReader.h:397
Code completion occurred on the right-hand side of a member access expression using the dot operator...
SimplifiedTypeClass
A simplified classification of types used when determining "similar" types for code completion...
Code completion occurred where a type name is expected.
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:66
unsigned IncludeMacros
Show macros in code completion results.
ASTContext & Context
Definition: Sema.h:320
This represents a decl that may have a name.
Definition: Decl.h:248
bool isTranslationUnit() const
Definition: DeclBase.h:1822
InputKind getInputKind() const
Determine the input kind this AST unit represents.
Definition: ASTUnit.cpp:2638
virtual void BeginSourceFile(const LangOptions &LangOpts, const Preprocessor *PP=nullptr)
Callback to inform the diagnostic client that processing of a source file is beginning.
Definition: Diagnostic.h:1511
const FileEntry * File
The file entry for the module file.
Definition: Module.h:164
const LangOptions & getLangOpts() const
Definition: ASTContext.h:716
SourceLocation getLocation(const FileEntry *File, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
Definition: ASTUnit.cpp:2469
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Definition: Preprocessor.h:924
This class handles loading and caching of source files into memory.
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystem() const
Definition: FileManager.h:226
SourceLocation getLocation() const
Definition: DeclBase.h:418
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:127
std::vector< Decl * >::iterator top_level_iterator
Definition: ASTUnit.h:495