clang  3.7.0
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"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/AST/StmtVisitor.h"
19 #include "clang/AST/TypeOrdering.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/TargetInfo.h"
29 #include "clang/Frontend/Utils.h"
30 #include "clang/Lex/HeaderSearch.h"
31 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Sema/Sema.h"
36 #include "llvm/ADT/ArrayRef.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/ADT/StringSet.h"
39 #include "llvm/Support/CrashRecoveryContext.h"
40 #include "llvm/Support/Host.h"
41 #include "llvm/Support/MemoryBuffer.h"
42 #include "llvm/Support/Mutex.h"
43 #include "llvm/Support/MutexGuard.h"
44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/Timer.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include <atomic>
48 #include <cstdio>
49 #include <cstdlib>
50 using namespace clang;
51 
52 using llvm::TimeRecord;
53 
54 namespace {
55  class SimpleTimer {
56  bool WantTiming;
57  TimeRecord Start;
58  std::string Output;
59 
60  public:
61  explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
62  if (WantTiming)
63  Start = TimeRecord::getCurrentTime();
64  }
65 
66  void setOutput(const Twine &Output) {
67  if (WantTiming)
68  this->Output = Output.str();
69  }
70 
71  ~SimpleTimer() {
72  if (WantTiming) {
73  TimeRecord Elapsed = TimeRecord::getCurrentTime();
74  Elapsed -= Start;
75  llvm::errs() << Output << ':';
76  Elapsed.print(Elapsed, llvm::errs());
77  llvm::errs() << '\n';
78  }
79  }
80  };
81 
82  struct OnDiskData {
83  /// \brief The file in which the precompiled preamble is stored.
84  std::string PreambleFile;
85 
86  /// \brief Temporary files that should be removed when the ASTUnit is
87  /// destroyed.
88  SmallVector<std::string, 4> TemporaryFiles;
89 
90  /// \brief Erase temporary files.
91  void CleanTemporaryFiles();
92 
93  /// \brief Erase the preamble file.
94  void CleanPreambleFile();
95 
96  /// \brief Erase temporary files and the preamble file.
97  void Cleanup();
98  };
99 }
100 
101 static llvm::sys::SmartMutex<false> &getOnDiskMutex() {
102  static llvm::sys::SmartMutex<false> M(/* recursive = */ true);
103  return M;
104 }
105 
106 static void cleanupOnDiskMapAtExit();
107 
108 typedef llvm::DenseMap<const ASTUnit *,
109  std::unique_ptr<OnDiskData>> OnDiskDataMap;
111  static OnDiskDataMap M;
112  static bool hasRegisteredAtExit = false;
113  if (!hasRegisteredAtExit) {
114  hasRegisteredAtExit = true;
115  atexit(cleanupOnDiskMapAtExit);
116  }
117  return M;
118 }
119 
120 static void cleanupOnDiskMapAtExit() {
121  // Use the mutex because there can be an alive thread destroying an ASTUnit.
122  llvm::MutexGuard Guard(getOnDiskMutex());
123  for (const auto &I : getOnDiskDataMap()) {
124  // We don't worry about freeing the memory associated with OnDiskDataMap.
125  // All we care about is erasing stale files.
126  I.second->Cleanup();
127  }
128 }
129 
130 static OnDiskData &getOnDiskData(const ASTUnit *AU) {
131  // We require the mutex since we are modifying the structure of the
132  // DenseMap.
133  llvm::MutexGuard Guard(getOnDiskMutex());
135  auto &D = M[AU];
136  if (!D)
137  D = llvm::make_unique<OnDiskData>();
138  return *D;
139 }
140 
141 static void erasePreambleFile(const ASTUnit *AU) {
142  getOnDiskData(AU).CleanPreambleFile();
143 }
144 
145 static void removeOnDiskEntry(const ASTUnit *AU) {
146  // We require the mutex since we are modifying the structure of the
147  // DenseMap.
148  llvm::MutexGuard Guard(getOnDiskMutex());
150  OnDiskDataMap::iterator I = M.find(AU);
151  if (I != M.end()) {
152  I->second->Cleanup();
153  M.erase(I);
154  }
155 }
156 
157 static void setPreambleFile(const ASTUnit *AU, StringRef preambleFile) {
158  getOnDiskData(AU).PreambleFile = preambleFile;
159 }
160 
161 static const std::string &getPreambleFile(const ASTUnit *AU) {
162  return getOnDiskData(AU).PreambleFile;
163 }
164 
165 void OnDiskData::CleanTemporaryFiles() {
166  for (StringRef File : TemporaryFiles)
167  llvm::sys::fs::remove(File);
168  TemporaryFiles.clear();
169 }
170 
171 void OnDiskData::CleanPreambleFile() {
172  if (!PreambleFile.empty()) {
173  llvm::sys::fs::remove(PreambleFile);
174  PreambleFile.clear();
175  }
176 }
177 
178 void OnDiskData::Cleanup() {
179  CleanTemporaryFiles();
180  CleanPreambleFile();
181 }
182 
185  llvm::BitstreamWriter Stream;
187 
189 };
190 
191 void ASTUnit::clearFileLevelDecls() {
192  llvm::DeleteContainerSeconds(FileDecls);
193 }
194 
195 void ASTUnit::CleanTemporaryFiles() {
196  getOnDiskData(this).CleanTemporaryFiles();
197 }
198 
199 void ASTUnit::addTemporaryFile(StringRef TempFile) {
200  getOnDiskData(this).TemporaryFiles.push_back(TempFile);
201 }
202 
203 /// \brief After failing to build a precompiled preamble (due to
204 /// errors in the source that occurs in the preamble), the number of
205 /// reparses during which we'll skip even trying to precompile the
206 /// preamble.
208 
209 /// \brief Tracks the number of ASTUnit objects that are currently active.
210 ///
211 /// Used for debugging purposes only.
212 static std::atomic<unsigned> ActiveASTUnitObjects;
213 
214 ASTUnit::ASTUnit(bool _MainFileIsAST)
215  : Reader(nullptr), HadModuleLoaderFatalFailure(false),
216  OnlyLocalDecls(false), CaptureDiagnostics(false),
217  MainFileIsAST(_MainFileIsAST),
218  TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")),
219  OwnsRemappedFileBuffers(true),
220  NumStoredDiagnosticsFromDriver(0),
221  PreambleRebuildCounter(0),
222  NumWarningsInPreamble(0),
223  ShouldCacheCodeCompletionResults(false),
224  IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
225  CompletionCacheTopLevelHashValue(0),
226  PreambleTopLevelHashValue(0),
227  CurrentTopLevelHashValue(0),
228  UnsafeToFree(false) {
229  if (getenv("LIBCLANG_OBJTRACKING"))
230  fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects);
231 }
232 
233 ASTUnit::~ASTUnit() {
234  // If we loaded from an AST file, balance out the BeginSourceFile call.
235  if (MainFileIsAST && getDiagnostics().getClient()) {
237  }
238 
239  clearFileLevelDecls();
240 
241  // Clean up the temporary files and the preamble file.
242  removeOnDiskEntry(this);
243 
244  // Free the buffers associated with remapped files. We are required to
245  // perform this operation here because we explicitly request that the
246  // compiler instance *not* free these buffers for each invocation of the
247  // parser.
248  if (Invocation.get() && OwnsRemappedFileBuffers) {
249  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
250  for (const auto &RB : PPOpts.RemappedFileBuffers)
251  delete RB.second;
252  }
253 
254  ClearCachedCompletionResults();
255 
256  if (getenv("LIBCLANG_OBJTRACKING"))
257  fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects);
258 }
259 
261 
262 /// \brief Determine the set of code-completion contexts in which this
263 /// declaration should be shown.
264 static unsigned getDeclShowContexts(const NamedDecl *ND,
265  const LangOptions &LangOpts,
266  bool &IsNestedNameSpecifier) {
267  IsNestedNameSpecifier = false;
268 
269  if (isa<UsingShadowDecl>(ND))
270  ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
271  if (!ND)
272  return 0;
273 
274  uint64_t Contexts = 0;
275  if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
276  isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
277  // Types can appear in these contexts.
278  if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
279  Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
285 
286  // In C++, types can appear in expressions contexts (for functional casts).
287  if (LangOpts.CPlusPlus)
288  Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
289 
290  // In Objective-C, message sends can send interfaces. In Objective-C++,
291  // all types are available due to functional casts.
292  if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
294 
295  // In Objective-C, you can only be a subclass of another Objective-C class
296  if (isa<ObjCInterfaceDecl>(ND))
298 
299  // Deal with tag names.
300  if (isa<EnumDecl>(ND)) {
301  Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
302 
303  // Part of the nested-name-specifier in C++0x.
304  if (LangOpts.CPlusPlus11)
305  IsNestedNameSpecifier = true;
306  } else if (const RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
307  if (Record->isUnion())
308  Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
309  else
310  Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
311 
312  if (LangOpts.CPlusPlus)
313  IsNestedNameSpecifier = true;
314  } else if (isa<ClassTemplateDecl>(ND))
315  IsNestedNameSpecifier = true;
316  } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
317  // Values can appear in these contexts.
318  Contexts = (1LL << CodeCompletionContext::CCC_Statement)
322  } else if (isa<ObjCProtocolDecl>(ND)) {
324  } else if (isa<ObjCCategoryDecl>(ND)) {
326  } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
327  Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
328 
329  // Part of the nested-name-specifier.
330  IsNestedNameSpecifier = true;
331  }
332 
333  return Contexts;
334 }
335 
336 void ASTUnit::CacheCodeCompletionResults() {
337  if (!TheSema)
338  return;
339 
340  SimpleTimer Timer(WantTiming);
341  Timer.setOutput("Cache global code completions for " + getMainFileName());
342 
343  // Clear out the previous results.
344  ClearCachedCompletionResults();
345 
346  // Gather the set of global code completions.
348  SmallVector<Result, 8> Results;
349  CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
350  CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
351  TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
352  CCTUInfo, Results);
353 
354  // Translate global code completions into cached completions.
355  llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
357 
358  for (Result &R : Results) {
359  switch (R.Kind) {
360  case Result::RK_Declaration: {
361  bool IsNestedNameSpecifier = false;
362  CachedCodeCompletionResult CachedResult;
363  CachedResult.Completion = R.CreateCodeCompletionString(
364  *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
365  IncludeBriefCommentsInCodeCompletion);
366  CachedResult.ShowInContexts = getDeclShowContexts(
367  R.Declaration, Ctx->getLangOpts(), IsNestedNameSpecifier);
368  CachedResult.Priority = R.Priority;
369  CachedResult.Kind = R.CursorKind;
370  CachedResult.Availability = R.Availability;
371 
372  // Keep track of the type of this completion in an ASTContext-agnostic
373  // way.
374  QualType UsageType = getDeclUsageType(*Ctx, R.Declaration);
375  if (UsageType.isNull()) {
376  CachedResult.TypeClass = STC_Void;
377  CachedResult.Type = 0;
378  } else {
379  CanQualType CanUsageType
380  = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
381  CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
382 
383  // Determine whether we have already seen this type. If so, we save
384  // ourselves the work of formatting the type string by using the
385  // temporary, CanQualType-based hash table to find the associated value.
386  unsigned &TypeValue = CompletionTypes[CanUsageType];
387  if (TypeValue == 0) {
388  TypeValue = CompletionTypes.size();
389  CachedCompletionTypes[QualType(CanUsageType).getAsString()]
390  = TypeValue;
391  }
392 
393  CachedResult.Type = TypeValue;
394  }
395 
396  CachedCompletionResults.push_back(CachedResult);
397 
398  /// Handle nested-name-specifiers in C++.
399  if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier &&
400  !R.StartsNestedNameSpecifier) {
401  // The contexts in which a nested-name-specifier can appear in C++.
402  uint64_t NNSContexts
415 
416  if (isa<NamespaceDecl>(R.Declaration) ||
417  isa<NamespaceAliasDecl>(R.Declaration))
418  NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
419 
420  if (unsigned RemainingContexts
421  = NNSContexts & ~CachedResult.ShowInContexts) {
422  // If there any contexts where this completion can be a
423  // nested-name-specifier but isn't already an option, create a
424  // nested-name-specifier completion.
425  R.StartsNestedNameSpecifier = true;
426  CachedResult.Completion = R.CreateCodeCompletionString(
427  *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
428  IncludeBriefCommentsInCodeCompletion);
429  CachedResult.ShowInContexts = RemainingContexts;
430  CachedResult.Priority = CCP_NestedNameSpecifier;
431  CachedResult.TypeClass = STC_Void;
432  CachedResult.Type = 0;
433  CachedCompletionResults.push_back(CachedResult);
434  }
435  }
436  break;
437  }
438 
439  case Result::RK_Keyword:
440  case Result::RK_Pattern:
441  // Ignore keywords and patterns; we don't care, since they are so
442  // easily regenerated.
443  break;
444 
445  case Result::RK_Macro: {
446  CachedCodeCompletionResult CachedResult;
447  CachedResult.Completion = R.CreateCodeCompletionString(
448  *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
449  IncludeBriefCommentsInCodeCompletion);
450  CachedResult.ShowInContexts
463 
464  CachedResult.Priority = R.Priority;
465  CachedResult.Kind = R.CursorKind;
466  CachedResult.Availability = R.Availability;
467  CachedResult.TypeClass = STC_Void;
468  CachedResult.Type = 0;
469  CachedCompletionResults.push_back(CachedResult);
470  break;
471  }
472  }
473  }
474 
475  // Save the current top-level hash value.
476  CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
477 }
478 
479 void ASTUnit::ClearCachedCompletionResults() {
480  CachedCompletionResults.clear();
481  CachedCompletionTypes.clear();
482  CachedCompletionAllocator = nullptr;
483 }
484 
485 namespace {
486 
487 /// \brief Gathers information from ASTReader that will be used to initialize
488 /// a Preprocessor.
489 class ASTInfoCollector : public ASTReaderListener {
490  Preprocessor &PP;
492  LangOptions &LangOpt;
493  std::shared_ptr<TargetOptions> &TargetOpts;
495  unsigned &Counter;
496 
497  bool InitializedLanguage;
498 public:
499  ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt,
500  std::shared_ptr<TargetOptions> &TargetOpts,
501  IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter)
502  : PP(PP), Context(Context), LangOpt(LangOpt), TargetOpts(TargetOpts),
503  Target(Target), Counter(Counter), InitializedLanguage(false) {}
504 
505  bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
506  bool AllowCompatibleDifferences) override {
507  if (InitializedLanguage)
508  return false;
509 
510  LangOpt = LangOpts;
511  InitializedLanguage = true;
512 
513  updated();
514  return false;
515  }
516 
517  bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
518  bool AllowCompatibleDifferences) override {
519  // If we've already initialized the target, don't do it again.
520  if (Target)
521  return false;
522 
523  this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts);
524  Target =
525  TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts);
526 
527  updated();
528  return false;
529  }
530 
531  void ReadCounter(const serialization::ModuleFile &M,
532  unsigned Value) override {
533  Counter = Value;
534  }
535 
536 private:
537  void updated() {
538  if (!Target || !InitializedLanguage)
539  return;
540 
541  // Inform the target of the language options.
542  //
543  // FIXME: We shouldn't need to do this, the target should be immutable once
544  // created. This complexity should be lifted elsewhere.
545  Target->adjust(LangOpt);
546 
547  // Initialize the preprocessor.
548  PP.Initialize(*Target);
549 
550  // Initialize the ASTContext
551  Context.InitBuiltinTypes(*Target);
552 
553  // We didn't have access to the comment options when the ASTContext was
554  // constructed, so register them now.
556  LangOpt.CommentOpts);
557  }
558 };
559 
560  /// \brief Diagnostic consumer that saves each diagnostic it is given.
561 class StoredDiagnosticConsumer : public DiagnosticConsumer {
564 
565 public:
566  explicit StoredDiagnosticConsumer(
568  : StoredDiags(StoredDiags), SourceMgr(nullptr) {}
569 
570  void BeginSourceFile(const LangOptions &LangOpts,
571  const Preprocessor *PP = nullptr) override {
572  if (PP)
573  SourceMgr = &PP->getSourceManager();
574  }
575 
576  void HandleDiagnostic(DiagnosticsEngine::Level Level,
577  const Diagnostic &Info) override;
578 };
579 
580 /// \brief RAII object that optionally captures diagnostics, if
581 /// there is no diagnostic client to capture them already.
582 class CaptureDroppedDiagnostics {
583  DiagnosticsEngine &Diags;
584  StoredDiagnosticConsumer Client;
585  DiagnosticConsumer *PreviousClient;
586  std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
587 
588 public:
589  CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
591  : Diags(Diags), Client(StoredDiags), PreviousClient(nullptr)
592  {
593  if (RequestCapture || Diags.getClient() == nullptr) {
594  OwningPreviousClient = Diags.takeClient();
595  PreviousClient = Diags.getClient();
596  Diags.setClient(&Client, false);
597  }
598  }
599 
600  ~CaptureDroppedDiagnostics() {
601  if (Diags.getClient() == &Client)
602  Diags.setClient(PreviousClient, !!OwningPreviousClient.release());
603  }
604 };
605 
606 } // anonymous namespace
607 
608 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
609  const Diagnostic &Info) {
610  // Default implementation (Warnings/errors count).
612 
613  // Only record the diagnostic if it's part of the source manager we know
614  // about. This effectively drops diagnostics from modules we're building.
615  // FIXME: In the long run, ee don't want to drop source managers from modules.
616  if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr)
617  StoredDiags.emplace_back(Level, Info);
618 }
619 
621  if (WriterData)
622  return &WriterData->Writer;
623  return nullptr;
624 }
625 
627  if (WriterData)
628  return &WriterData->Writer;
629  return nullptr;
630 }
631 
632 std::unique_ptr<llvm::MemoryBuffer>
633 ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) {
634  assert(FileMgr);
635  auto Buffer = FileMgr->getBufferForFile(Filename);
636  if (Buffer)
637  return std::move(*Buffer);
638  if (ErrorStr)
639  *ErrorStr = Buffer.getError().message();
640  return nullptr;
641 }
642 
643 /// \brief Configure the diagnostics object for use with ASTUnit.
644 void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
645  ASTUnit &AST, bool CaptureDiagnostics) {
646  assert(Diags.get() && "no DiagnosticsEngine was provided");
647  if (CaptureDiagnostics)
648  Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics));
649 }
650 
651 std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
652  const std::string &Filename,
653  const PCHContainerReader &PCHContainerRdr,
655  const FileSystemOptions &FileSystemOpts, bool OnlyLocalDecls,
656  ArrayRef<RemappedFile> RemappedFiles, bool CaptureDiagnostics,
657  bool AllowPCHWithCompilerErrors, bool UserFilesAreVolatile) {
658  std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
659 
660  // Recover resources if we crash before exiting this method.
661  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
662  ASTUnitCleanup(AST.get());
663  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
664  llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
665  DiagCleanup(Diags.get());
666 
667  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
668 
669  AST->OnlyLocalDecls = OnlyLocalDecls;
670  AST->CaptureDiagnostics = CaptureDiagnostics;
671  AST->Diagnostics = Diags;
673  AST->FileMgr = new FileManager(FileSystemOpts, VFS);
674  AST->UserFilesAreVolatile = UserFilesAreVolatile;
675  AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
676  AST->getFileManager(),
677  UserFilesAreVolatile);
678  AST->HSOpts = new HeaderSearchOptions();
679  AST->HSOpts->ModuleFormat = PCHContainerRdr.getFormat();
680  AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
681  AST->getSourceManager(),
682  AST->getDiagnostics(),
683  AST->ASTFileLangOpts,
684  /*Target=*/nullptr));
685 
687 
688  for (const auto &RemappedFile : RemappedFiles)
689  PPOpts->addRemappedFile(RemappedFile.first, RemappedFile.second);
690 
691  // Gather Info for preprocessor construction later on.
692 
693  HeaderSearch &HeaderInfo = *AST->HeaderInfo;
694  unsigned Counter;
695 
696  AST->PP =
697  new Preprocessor(PPOpts, AST->getDiagnostics(), AST->ASTFileLangOpts,
698  AST->getSourceManager(), HeaderInfo, *AST,
699  /*IILookup=*/nullptr,
700  /*OwnsHeaderSearch=*/false);
701  Preprocessor &PP = *AST->PP;
702 
703  AST->Ctx = new ASTContext(AST->ASTFileLangOpts, AST->getSourceManager(),
704  PP.getIdentifierTable(), PP.getSelectorTable(),
705  PP.getBuiltinInfo());
706  ASTContext &Context = *AST->Ctx;
707 
708  bool disableValid = false;
709  if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
710  disableValid = true;
711  AST->Reader = new ASTReader(PP, Context, PCHContainerRdr,
712  /*isysroot=*/"",
713  /*DisableValidation=*/disableValid,
714  AllowPCHWithCompilerErrors);
715 
716  AST->Reader->setListener(llvm::make_unique<ASTInfoCollector>(
717  *AST->PP, Context, AST->ASTFileLangOpts, AST->TargetOpts, AST->Target,
718  Counter));
719 
720  // Attach the AST reader to the AST context as an external AST
721  // source, so that declarations will be deserialized from the
722  // AST file as needed.
723  // We need the external source to be set up before we read the AST, because
724  // eagerly-deserialized declarations may use it.
725  Context.setExternalSource(AST->Reader);
726 
727  switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile,
729  case ASTReader::Success:
730  break;
731 
732  case ASTReader::Failure:
733  case ASTReader::Missing:
738  AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
739  return nullptr;
740  }
741 
742  AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile();
743 
744  PP.setCounterValue(Counter);
745 
746  // Create an AST consumer, even though it isn't used.
747  AST->Consumer.reset(new ASTConsumer);
748 
749  // Create a semantic analysis object and tell the AST reader about it.
750  AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
751  AST->TheSema->Initialize();
752  AST->Reader->InitializeSema(*AST->TheSema);
753 
754  // Tell the diagnostic client that we have started a source file.
755  AST->getDiagnostics().getClient()->BeginSourceFile(Context.getLangOpts(),&PP);
756 
757  return AST;
758 }
759 
760 namespace {
761 
762 /// \brief Preprocessor callback class that updates a hash value with the names
763 /// of all macros that have been defined by the translation unit.
764 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
765  unsigned &Hash;
766 
767 public:
768  explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
769 
770  void MacroDefined(const Token &MacroNameTok,
771  const MacroDirective *MD) override {
772  Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
773  }
774 };
775 
776 /// \brief Add the given declaration to the hash of all top-level entities.
777 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
778  if (!D)
779  return;
780 
781  DeclContext *DC = D->getDeclContext();
782  if (!DC)
783  return;
784 
785  if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
786  return;
787 
788  if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
789  if (EnumDecl *EnumD = dyn_cast<EnumDecl>(D)) {
790  // For an unscoped enum include the enumerators in the hash since they
791  // enter the top-level namespace.
792  if (!EnumD->isScoped()) {
793  for (const auto *EI : EnumD->enumerators()) {
794  if (EI->getIdentifier())
795  Hash = llvm::HashString(EI->getIdentifier()->getName(), Hash);
796  }
797  }
798  }
799 
800  if (ND->getIdentifier())
801  Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
802  else if (DeclarationName Name = ND->getDeclName()) {
803  std::string NameStr = Name.getAsString();
804  Hash = llvm::HashString(NameStr, Hash);
805  }
806  return;
807  }
808 
809  if (ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) {
810  if (Module *Mod = ImportD->getImportedModule()) {
811  std::string ModName = Mod->getFullModuleName();
812  Hash = llvm::HashString(ModName, Hash);
813  }
814  return;
815  }
816 }
817 
818 class TopLevelDeclTrackerConsumer : public ASTConsumer {
819  ASTUnit &Unit;
820  unsigned &Hash;
821 
822 public:
823  TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
824  : Unit(_Unit), Hash(Hash) {
825  Hash = 0;
826  }
827 
828  void handleTopLevelDecl(Decl *D) {
829  if (!D)
830  return;
831 
832  // FIXME: Currently ObjC method declarations are incorrectly being
833  // reported as top-level declarations, even though their DeclContext
834  // is the containing ObjC @interface/@implementation. This is a
835  // fundamental problem in the parser right now.
836  if (isa<ObjCMethodDecl>(D))
837  return;
838 
839  AddTopLevelDeclarationToHash(D, Hash);
840  Unit.addTopLevelDecl(D);
841 
842  handleFileLevelDecl(D);
843  }
844 
845  void handleFileLevelDecl(Decl *D) {
846  Unit.addFileLevelDecl(D);
847  if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) {
848  for (auto *I : NSD->decls())
849  handleFileLevelDecl(I);
850  }
851  }
852 
853  bool HandleTopLevelDecl(DeclGroupRef D) override {
854  for (Decl *TopLevelDecl : D)
855  handleTopLevelDecl(TopLevelDecl);
856  return true;
857  }
858 
859  // We're not interested in "interesting" decls.
860  void HandleInterestingDecl(DeclGroupRef) override {}
861 
862  void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
863  for (Decl *TopLevelDecl : D)
864  handleTopLevelDecl(TopLevelDecl);
865  }
866 
867  ASTMutationListener *GetASTMutationListener() override {
868  return Unit.getASTMutationListener();
869  }
870 
871  ASTDeserializationListener *GetASTDeserializationListener() override {
872  return Unit.getDeserializationListener();
873  }
874 };
875 
876 class TopLevelDeclTrackerAction : public ASTFrontendAction {
877 public:
878  ASTUnit &Unit;
879 
880  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
881  StringRef InFile) override {
883  llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
884  Unit.getCurrentTopLevelHashValue()));
885  return llvm::make_unique<TopLevelDeclTrackerConsumer>(
886  Unit, Unit.getCurrentTopLevelHashValue());
887  }
888 
889 public:
890  TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
891 
892  bool hasCodeCompletionSupport() const override { return false; }
893  TranslationUnitKind getTranslationUnitKind() override {
894  return Unit.getTranslationUnitKind();
895  }
896 };
897 
898 class PrecompilePreambleAction : public ASTFrontendAction {
899  ASTUnit &Unit;
900  bool HasEmittedPreamblePCH;
901 
902 public:
903  explicit PrecompilePreambleAction(ASTUnit &Unit)
904  : Unit(Unit), HasEmittedPreamblePCH(false) {}
905 
906  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
907  StringRef InFile) override;
908  bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
909  void setHasEmittedPreamblePCH() { HasEmittedPreamblePCH = true; }
910  bool shouldEraseOutputFiles() override { return !hasEmittedPreamblePCH(); }
911 
912  bool hasCodeCompletionSupport() const override { return false; }
913  bool hasASTFileSupport() const override { return false; }
914  TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; }
915 };
916 
917 class PrecompilePreambleConsumer : public PCHGenerator {
918  ASTUnit &Unit;
919  unsigned &Hash;
920  std::vector<Decl *> TopLevelDecls;
921  PrecompilePreambleAction *Action;
922  raw_ostream *Out;
923 
924 public:
925  PrecompilePreambleConsumer(ASTUnit &Unit, PrecompilePreambleAction *Action,
926  const Preprocessor &PP, StringRef isysroot,
927  raw_ostream *Out)
928  : PCHGenerator(PP, "", nullptr, isysroot, std::make_shared<PCHBuffer>(),
929  /*AllowASTWithErrors=*/true),
930  Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()), Action(Action),
931  Out(Out) {
932  Hash = 0;
933  }
934 
935  bool HandleTopLevelDecl(DeclGroupRef DG) override {
936  for (Decl *D : DG) {
937  // FIXME: Currently ObjC method declarations are incorrectly being
938  // reported as top-level declarations, even though their DeclContext
939  // is the containing ObjC @interface/@implementation. This is a
940  // fundamental problem in the parser right now.
941  if (isa<ObjCMethodDecl>(D))
942  continue;
943  AddTopLevelDeclarationToHash(D, Hash);
944  TopLevelDecls.push_back(D);
945  }
946  return true;
947  }
948 
949  void HandleTranslationUnit(ASTContext &Ctx) override {
951  if (hasEmittedPCH()) {
952  // Write the generated bitstream to "Out".
953  *Out << getPCH();
954  // Make sure it hits disk now.
955  Out->flush();
956  // Free the buffer.
958  getPCH() = std::move(Empty);
959 
960  // Translate the top-level declarations we captured during
961  // parsing into declaration IDs in the precompiled
962  // preamble. This will allow us to deserialize those top-level
963  // declarations when requested.
964  for (Decl *D : TopLevelDecls) {
965  // Invalid top-level decls may not have been serialized.
966  if (D->isInvalidDecl())
967  continue;
968  Unit.addTopLevelDeclFromPreamble(getWriter().getDeclID(D));
969  }
970 
971  Action->setHasEmittedPreamblePCH();
972  }
973  }
974 };
975 
976 }
977 
978 std::unique_ptr<ASTConsumer>
979 PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
980  StringRef InFile) {
981  std::string Sysroot;
982  std::string OutputFile;
984  CI, InFile, Sysroot, OutputFile);
985  if (!OS)
986  return nullptr;
987 
989  Sysroot.clear();
990 
992  llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
993  Unit.getCurrentTopLevelHashValue()));
994  return llvm::make_unique<PrecompilePreambleConsumer>(
995  Unit, this, CI.getPreprocessor(), Sysroot, OS);
996 }
997 
998 static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
999  return StoredDiag.getLocation().isValid();
1000 }
1001 
1002 static void
1004  // Get rid of stored diagnostics except the ones from the driver which do not
1005  // have a source location.
1006  StoredDiags.erase(
1007  std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
1008  StoredDiags.end());
1009 }
1010 
1012  StoredDiagnostics,
1013  SourceManager &SM) {
1014  // The stored diagnostic has the old source manager in it; update
1015  // the locations to refer into the new source manager. Since we've
1016  // been careful to make sure that the source manager's state
1017  // before and after are identical, so that we can reuse the source
1018  // location itself.
1019  for (StoredDiagnostic &SD : StoredDiagnostics) {
1020  if (SD.getLocation().isValid()) {
1021  FullSourceLoc Loc(SD.getLocation(), SM);
1022  SD.setLocation(Loc);
1023  }
1024  }
1025 }
1026 
1027 /// Parse the source file into a translation unit using the given compiler
1028 /// invocation, replacing the current translation unit.
1029 ///
1030 /// \returns True if a failure occurred that causes the ASTUnit not to
1031 /// contain any translation-unit information, false otherwise.
1032 bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1033  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer) {
1034  SavedMainFileBuffer.reset();
1035 
1036  if (!Invocation)
1037  return true;
1038 
1039  // Create the compiler instance to use for building the AST.
1040  std::unique_ptr<CompilerInstance> Clang(
1041  new CompilerInstance(PCHContainerOps));
1042 
1043  // Recover resources if we crash before exiting this method.
1044  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1045  CICleanup(Clang.get());
1046 
1048  CCInvocation(new CompilerInvocation(*Invocation));
1049 
1050  Clang->setInvocation(CCInvocation.get());
1051  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1052 
1053  // Set up diagnostics, capturing any diagnostics that would
1054  // otherwise be dropped.
1055  Clang->setDiagnostics(&getDiagnostics());
1056 
1057  // Create the target instance.
1058  Clang->setTarget(TargetInfo::CreateTargetInfo(
1059  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1060  if (!Clang->hasTarget())
1061  return true;
1062 
1063  // Inform the target of the language options.
1064  //
1065  // FIXME: We shouldn't need to do this, the target should be immutable once
1066  // created. This complexity should be lifted elsewhere.
1067  Clang->getTarget().adjust(Clang->getLangOpts());
1068 
1069  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1070  "Invocation must have exactly one source file!");
1071  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1072  "FIXME: AST inputs not yet supported here!");
1073  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1074  "IR inputs not support here!");
1075 
1076  // Configure the various subsystems.
1077  LangOpts = Clang->getInvocation().LangOpts;
1078  FileSystemOpts = Clang->getFileSystemOpts();
1080  createVFSFromCompilerInvocation(Clang->getInvocation(), getDiagnostics());
1081  if (!VFS)
1082  return true;
1083  FileMgr = new FileManager(FileSystemOpts, VFS);
1084  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1085  UserFilesAreVolatile);
1086  TheSema.reset();
1087  Ctx = nullptr;
1088  PP = nullptr;
1089  Reader = nullptr;
1090 
1091  // Clear out old caches and data.
1092  TopLevelDecls.clear();
1093  clearFileLevelDecls();
1094  CleanTemporaryFiles();
1095 
1096  if (!OverrideMainBuffer) {
1097  checkAndRemoveNonDriverDiags(StoredDiagnostics);
1098  TopLevelDeclsInPreamble.clear();
1099  }
1100 
1101  // Create a file manager object to provide access to and cache the filesystem.
1102  Clang->setFileManager(&getFileManager());
1103 
1104  // Create the source manager.
1105  Clang->setSourceManager(&getSourceManager());
1106 
1107  // If the main file has been overridden due to the use of a preamble,
1108  // make that override happen and introduce the preamble.
1109  PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
1110  if (OverrideMainBuffer) {
1111  PreprocessorOpts.addRemappedFile(OriginalSourceFile,
1112  OverrideMainBuffer.get());
1113  PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
1114  PreprocessorOpts.PrecompiledPreambleBytes.second
1115  = PreambleEndsAtStartOfLine;
1116  PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
1117  PreprocessorOpts.DisablePCHValidation = true;
1118 
1119  // The stored diagnostic has the old source manager in it; update
1120  // the locations to refer into the new source manager. Since we've
1121  // been careful to make sure that the source manager's state
1122  // before and after are identical, so that we can reuse the source
1123  // location itself.
1124  checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1125 
1126  // Keep track of the override buffer;
1127  SavedMainFileBuffer = std::move(OverrideMainBuffer);
1128  }
1129 
1130  std::unique_ptr<TopLevelDeclTrackerAction> Act(
1131  new TopLevelDeclTrackerAction(*this));
1132 
1133  // Recover resources if we crash before exiting this method.
1134  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1135  ActCleanup(Act.get());
1136 
1137  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1138  goto error;
1139 
1140  if (SavedMainFileBuffer) {
1141  std::string ModName = getPreambleFile(this);
1142  TranslateStoredDiagnostics(getFileManager(), getSourceManager(),
1143  PreambleDiagnostics, StoredDiagnostics);
1144  }
1145 
1146  if (!Act->Execute())
1147  goto error;
1148 
1149  transferASTDataFromCompilerInstance(*Clang);
1150 
1151  Act->EndSourceFile();
1152 
1153  FailedParseDiagnostics.clear();
1154 
1155  return false;
1156 
1157 error:
1158  // Remove the overridden buffer we used for the preamble.
1159  SavedMainFileBuffer = nullptr;
1160 
1161  // Keep the ownership of the data in the ASTUnit because the client may
1162  // want to see the diagnostics.
1163  transferASTDataFromCompilerInstance(*Clang);
1164  FailedParseDiagnostics.swap(StoredDiagnostics);
1165  StoredDiagnostics.clear();
1166  NumStoredDiagnosticsFromDriver = 0;
1167  return true;
1168 }
1169 
1170 /// \brief Simple function to retrieve a path for a preamble precompiled header.
1171 static std::string GetPreamblePCHPath() {
1172  // FIXME: This is a hack so that we can override the preamble file during
1173  // crash-recovery testing, which is the only case where the preamble files
1174  // are not necessarily cleaned up.
1175  const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1176  if (TmpFile)
1177  return TmpFile;
1178 
1179  SmallString<128> Path;
1180  llvm::sys::fs::createTemporaryFile("preamble", "pch", Path);
1181 
1182  return Path.str();
1183 }
1184 
1185 /// \brief Compute the preamble for the main file, providing the source buffer
1186 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1187 /// that describes the preamble.
1188 ASTUnit::ComputedPreamble
1189 ASTUnit::ComputePreamble(CompilerInvocation &Invocation, unsigned MaxLines) {
1190  FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1191  PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1192 
1193  // Try to determine if the main file has been remapped, either from the
1194  // command line (to another file) or directly through the compiler invocation
1195  // (to a memory buffer).
1196  llvm::MemoryBuffer *Buffer = nullptr;
1197  std::unique_ptr<llvm::MemoryBuffer> BufferOwner;
1198  std::string MainFilePath(FrontendOpts.Inputs[0].getFile());
1199  llvm::sys::fs::UniqueID MainFileID;
1200  if (!llvm::sys::fs::getUniqueID(MainFilePath, MainFileID)) {
1201  // Check whether there is a file-file remapping of the main file
1202  for (const auto &RF : PreprocessorOpts.RemappedFiles) {
1203  std::string MPath(RF.first);
1204  llvm::sys::fs::UniqueID MID;
1205  if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1206  if (MainFileID == MID) {
1207  // We found a remapping. Try to load the resulting, remapped source.
1208  BufferOwner = getBufferForFile(RF.second);
1209  if (!BufferOwner)
1210  return ComputedPreamble(nullptr, nullptr, 0, true);
1211  }
1212  }
1213  }
1214 
1215  // Check whether there is a file-buffer remapping. It supercedes the
1216  // file-file remapping.
1217  for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
1218  std::string MPath(RB.first);
1219  llvm::sys::fs::UniqueID MID;
1220  if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1221  if (MainFileID == MID) {
1222  // We found a remapping.
1223  BufferOwner.reset();
1224  Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
1225  }
1226  }
1227  }
1228  }
1229 
1230  // If the main source file was not remapped, load it now.
1231  if (!Buffer && !BufferOwner) {
1232  BufferOwner = getBufferForFile(FrontendOpts.Inputs[0].getFile());
1233  if (!BufferOwner)
1234  return ComputedPreamble(nullptr, nullptr, 0, true);
1235  }
1236 
1237  if (!Buffer)
1238  Buffer = BufferOwner.get();
1239  auto Pre = Lexer::ComputePreamble(Buffer->getBuffer(),
1240  *Invocation.getLangOpts(), MaxLines);
1241  return ComputedPreamble(Buffer, std::move(BufferOwner), Pre.first,
1242  Pre.second);
1243 }
1244 
1246 ASTUnit::PreambleFileHash::createForFile(off_t Size, time_t ModTime) {
1248  Result.Size = Size;
1249  Result.ModTime = ModTime;
1250  memset(Result.MD5, 0, sizeof(Result.MD5));
1251  return Result;
1252 }
1253 
1255  const llvm::MemoryBuffer *Buffer) {
1257  Result.Size = Buffer->getBufferSize();
1258  Result.ModTime = 0;
1259 
1260  llvm::MD5 MD5Ctx;
1261  MD5Ctx.update(Buffer->getBuffer().data());
1262  MD5Ctx.final(Result.MD5);
1263 
1264  return Result;
1265 }
1266 
1267 namespace clang {
1269  const ASTUnit::PreambleFileHash &RHS) {
1270  return LHS.Size == RHS.Size && LHS.ModTime == RHS.ModTime &&
1271  memcmp(LHS.MD5, RHS.MD5, sizeof(LHS.MD5)) == 0;
1272 }
1273 } // namespace clang
1274 
1275 static std::pair<unsigned, unsigned>
1277  const LangOptions &LangOpts) {
1278  CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts);
1279  unsigned Offset = SM.getFileOffset(FileRange.getBegin());
1280  unsigned EndOffset = SM.getFileOffset(FileRange.getEnd());
1281  return std::make_pair(Offset, EndOffset);
1282 }
1283 
1285  const LangOptions &LangOpts,
1286  const FixItHint &InFix) {
1287  ASTUnit::StandaloneFixIt OutFix;
1288  OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts);
1290  LangOpts);
1291  OutFix.CodeToInsert = InFix.CodeToInsert;
1293  return OutFix;
1294 }
1295 
1298  const StoredDiagnostic &InDiag) {
1300  OutDiag.ID = InDiag.getID();
1301  OutDiag.Level = InDiag.getLevel();
1302  OutDiag.Message = InDiag.getMessage();
1303  OutDiag.LocOffset = 0;
1304  if (InDiag.getLocation().isInvalid())
1305  return OutDiag;
1306  const SourceManager &SM = InDiag.getLocation().getManager();
1307  SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation());
1308  OutDiag.Filename = SM.getFilename(FileLoc);
1309  if (OutDiag.Filename.empty())
1310  return OutDiag;
1311  OutDiag.LocOffset = SM.getFileOffset(FileLoc);
1312  for (const CharSourceRange &Range : InDiag.getRanges())
1313  OutDiag.Ranges.push_back(makeStandaloneRange(Range, SM, LangOpts));
1314  for (const FixItHint &FixIt : InDiag.getFixIts())
1315  OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, FixIt));
1316 
1317  return OutDiag;
1318 }
1319 
1320 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1321 /// the source file.
1322 ///
1323 /// This routine will compute the preamble of the main source file. If a
1324 /// non-trivial preamble is found, it will precompile that preamble into a
1325 /// precompiled header so that the precompiled preamble can be used to reduce
1326 /// reparsing time. If a precompiled preamble has already been constructed,
1327 /// this routine will determine if it is still valid and, if so, avoid
1328 /// rebuilding the precompiled preamble.
1329 ///
1330 /// \param AllowRebuild When true (the default), this routine is
1331 /// allowed to rebuild the precompiled preamble if it is found to be
1332 /// out-of-date.
1333 ///
1334 /// \param MaxLines When non-zero, the maximum number of lines that
1335 /// can occur within the preamble.
1336 ///
1337 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1338 /// buffer that should be used in place of the main file when doing so.
1339 /// Otherwise, returns a NULL pointer.
1340 std::unique_ptr<llvm::MemoryBuffer>
1341 ASTUnit::getMainBufferWithPrecompiledPreamble(
1342  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1343  const CompilerInvocation &PreambleInvocationIn, bool AllowRebuild,
1344  unsigned MaxLines) {
1345 
1347  PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
1348  FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
1349  PreprocessorOptions &PreprocessorOpts
1350  = PreambleInvocation->getPreprocessorOpts();
1351 
1352  ComputedPreamble NewPreamble = ComputePreamble(*PreambleInvocation, MaxLines);
1353 
1354  if (!NewPreamble.Size) {
1355  // We couldn't find a preamble in the main source. Clear out the current
1356  // preamble, if we have one. It's obviously no good any more.
1357  Preamble.clear();
1358  erasePreambleFile(this);
1359 
1360  // The next time we actually see a preamble, precompile it.
1361  PreambleRebuildCounter = 1;
1362  return nullptr;
1363  }
1364 
1365  if (!Preamble.empty()) {
1366  // We've previously computed a preamble. Check whether we have the same
1367  // preamble now that we did before, and that there's enough space in
1368  // the main-file buffer within the precompiled preamble to fit the
1369  // new main file.
1370  if (Preamble.size() == NewPreamble.Size &&
1371  PreambleEndsAtStartOfLine == NewPreamble.PreambleEndsAtStartOfLine &&
1372  memcmp(Preamble.getBufferStart(), NewPreamble.Buffer->getBufferStart(),
1373  NewPreamble.Size) == 0) {
1374  // The preamble has not changed. We may be able to re-use the precompiled
1375  // preamble.
1376 
1377  // Check that none of the files used by the preamble have changed.
1378  bool AnyFileChanged = false;
1379 
1380  // First, make a record of those files that have been overridden via
1381  // remapping or unsaved_files.
1382  llvm::StringMap<PreambleFileHash> OverriddenFiles;
1383  for (const auto &R : PreprocessorOpts.RemappedFiles) {
1384  if (AnyFileChanged)
1385  break;
1386 
1387  vfs::Status Status;
1388  if (FileMgr->getNoncachedStatValue(R.second, Status)) {
1389  // If we can't stat the file we're remapping to, assume that something
1390  // horrible happened.
1391  AnyFileChanged = true;
1392  break;
1393  }
1394 
1395  OverriddenFiles[R.first] = PreambleFileHash::createForFile(
1396  Status.getSize(), Status.getLastModificationTime().toEpochTime());
1397  }
1398 
1399  for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
1400  if (AnyFileChanged)
1401  break;
1402  OverriddenFiles[RB.first] =
1404  }
1405 
1406  // Check whether anything has changed.
1407  for (llvm::StringMap<PreambleFileHash>::iterator
1408  F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1409  !AnyFileChanged && F != FEnd;
1410  ++F) {
1411  llvm::StringMap<PreambleFileHash>::iterator Overridden
1412  = OverriddenFiles.find(F->first());
1413  if (Overridden != OverriddenFiles.end()) {
1414  // This file was remapped; check whether the newly-mapped file
1415  // matches up with the previous mapping.
1416  if (Overridden->second != F->second)
1417  AnyFileChanged = true;
1418  continue;
1419  }
1420 
1421  // The file was not remapped; check whether it has changed on disk.
1422  vfs::Status Status;
1423  if (FileMgr->getNoncachedStatValue(F->first(), Status)) {
1424  // If we can't stat the file, assume that something horrible happened.
1425  AnyFileChanged = true;
1426  } else if (Status.getSize() != uint64_t(F->second.Size) ||
1427  Status.getLastModificationTime().toEpochTime() !=
1428  uint64_t(F->second.ModTime))
1429  AnyFileChanged = true;
1430  }
1431 
1432  if (!AnyFileChanged) {
1433  // Okay! We can re-use the precompiled preamble.
1434 
1435  // Set the state of the diagnostic object to mimic its state
1436  // after parsing the preamble.
1437  getDiagnostics().Reset();
1439  PreambleInvocation->getDiagnosticOpts());
1440  getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1441 
1442  return llvm::MemoryBuffer::getMemBufferCopy(
1443  NewPreamble.Buffer->getBuffer(), FrontendOpts.Inputs[0].getFile());
1444  }
1445  }
1446 
1447  // If we aren't allowed to rebuild the precompiled preamble, just
1448  // return now.
1449  if (!AllowRebuild)
1450  return nullptr;
1451 
1452  // We can't reuse the previously-computed preamble. Build a new one.
1453  Preamble.clear();
1454  PreambleDiagnostics.clear();
1455  erasePreambleFile(this);
1456  PreambleRebuildCounter = 1;
1457  } else if (!AllowRebuild) {
1458  // We aren't allowed to rebuild the precompiled preamble; just
1459  // return now.
1460  return nullptr;
1461  }
1462 
1463  // If the preamble rebuild counter > 1, it's because we previously
1464  // failed to build a preamble and we're not yet ready to try
1465  // again. Decrement the counter and return a failure.
1466  if (PreambleRebuildCounter > 1) {
1467  --PreambleRebuildCounter;
1468  return nullptr;
1469  }
1470 
1471  // Create a temporary file for the precompiled preamble. In rare
1472  // circumstances, this can fail.
1473  std::string PreamblePCHPath = GetPreamblePCHPath();
1474  if (PreamblePCHPath.empty()) {
1475  // Try again next time.
1476  PreambleRebuildCounter = 1;
1477  return nullptr;
1478  }
1479 
1480  // We did not previously compute a preamble, or it can't be reused anyway.
1481  SimpleTimer PreambleTimer(WantTiming);
1482  PreambleTimer.setOutput("Precompiling preamble");
1483 
1484  // Save the preamble text for later; we'll need to compare against it for
1485  // subsequent reparses.
1486  StringRef MainFilename = FrontendOpts.Inputs[0].getFile();
1487  Preamble.assign(FileMgr->getFile(MainFilename),
1488  NewPreamble.Buffer->getBufferStart(),
1489  NewPreamble.Buffer->getBufferStart() + NewPreamble.Size);
1490  PreambleEndsAtStartOfLine = NewPreamble.PreambleEndsAtStartOfLine;
1491 
1492  PreambleBuffer = llvm::MemoryBuffer::getMemBufferCopy(
1493  NewPreamble.Buffer->getBuffer().slice(0, Preamble.size()), MainFilename);
1494 
1495  // Remap the main source file to the preamble buffer.
1496  StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
1497  PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer.get());
1498 
1499  // Tell the compiler invocation to generate a temporary precompiled header.
1500  FrontendOpts.ProgramAction = frontend::GeneratePCH;
1501  // FIXME: Generate the precompiled header into memory?
1502  FrontendOpts.OutputFile = PreamblePCHPath;
1503  PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1504  PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1505 
1506  // Create the compiler instance to use for building the precompiled preamble.
1507  std::unique_ptr<CompilerInstance> Clang(
1508  new CompilerInstance(PCHContainerOps));
1509 
1510  // Recover resources if we crash before exiting this method.
1511  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1512  CICleanup(Clang.get());
1513 
1514  Clang->setInvocation(&*PreambleInvocation);
1515  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1516 
1517  // Set up diagnostics, capturing all of the diagnostics produced.
1518  Clang->setDiagnostics(&getDiagnostics());
1519 
1520  // Create the target instance.
1521  Clang->setTarget(TargetInfo::CreateTargetInfo(
1522  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1523  if (!Clang->hasTarget()) {
1524  llvm::sys::fs::remove(FrontendOpts.OutputFile);
1525  Preamble.clear();
1526  PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1527  PreprocessorOpts.RemappedFileBuffers.pop_back();
1528  return nullptr;
1529  }
1530 
1531  // Inform the target of the language options.
1532  //
1533  // FIXME: We shouldn't need to do this, the target should be immutable once
1534  // created. This complexity should be lifted elsewhere.
1535  Clang->getTarget().adjust(Clang->getLangOpts());
1536 
1537  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1538  "Invocation must have exactly one source file!");
1539  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1540  "FIXME: AST inputs not yet supported here!");
1541  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1542  "IR inputs not support here!");
1543 
1544  // Clear out old caches and data.
1545  getDiagnostics().Reset();
1546  ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1547  checkAndRemoveNonDriverDiags(StoredDiagnostics);
1548  TopLevelDecls.clear();
1549  TopLevelDeclsInPreamble.clear();
1550  PreambleDiagnostics.clear();
1551 
1553  createVFSFromCompilerInvocation(Clang->getInvocation(), getDiagnostics());
1554  if (!VFS)
1555  return nullptr;
1556 
1557  // Create a file manager object to provide access to and cache the filesystem.
1558  Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
1559 
1560  // Create the source manager.
1561  Clang->setSourceManager(new SourceManager(getDiagnostics(),
1562  Clang->getFileManager()));
1563 
1564  auto PreambleDepCollector = std::make_shared<DependencyCollector>();
1565  Clang->addDependencyCollector(PreambleDepCollector);
1566 
1567  std::unique_ptr<PrecompilePreambleAction> Act;
1568  Act.reset(new PrecompilePreambleAction(*this));
1569  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1570  llvm::sys::fs::remove(FrontendOpts.OutputFile);
1571  Preamble.clear();
1572  PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1573  PreprocessorOpts.RemappedFileBuffers.pop_back();
1574  return nullptr;
1575  }
1576 
1577  Act->Execute();
1578 
1579  // Transfer any diagnostics generated when parsing the preamble into the set
1580  // of preamble diagnostics.
1582  E = stored_diag_end();
1583  I != E; ++I)
1584  PreambleDiagnostics.push_back(
1585  makeStandaloneDiagnostic(Clang->getLangOpts(), *I));
1586 
1587  Act->EndSourceFile();
1588 
1589  checkAndRemoveNonDriverDiags(StoredDiagnostics);
1590 
1591  if (!Act->hasEmittedPreamblePCH()) {
1592  // The preamble PCH failed (e.g. there was a module loading fatal error),
1593  // so no precompiled header was generated. Forget that we even tried.
1594  // FIXME: Should we leave a note for ourselves to try again?
1595  llvm::sys::fs::remove(FrontendOpts.OutputFile);
1596  Preamble.clear();
1597  TopLevelDeclsInPreamble.clear();
1598  PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1599  PreprocessorOpts.RemappedFileBuffers.pop_back();
1600  return nullptr;
1601  }
1602 
1603  // Keep track of the preamble we precompiled.
1604  setPreambleFile(this, FrontendOpts.OutputFile);
1605  NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1606 
1607  // Keep track of all of the files that the source manager knows about,
1608  // so we can verify whether they have changed or not.
1609  FilesInPreamble.clear();
1610  SourceManager &SourceMgr = Clang->getSourceManager();
1611  for (auto &Filename : PreambleDepCollector->getDependencies()) {
1612  const FileEntry *File = Clang->getFileManager().getFile(Filename);
1613  if (!File || File == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))
1614  continue;
1615  if (time_t ModTime = File->getModificationTime()) {
1616  FilesInPreamble[File->getName()] = PreambleFileHash::createForFile(
1617  File->getSize(), ModTime);
1618  } else {
1619  llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File);
1620  FilesInPreamble[File->getName()] =
1622  }
1623  }
1624 
1625  PreambleRebuildCounter = 1;
1626  PreprocessorOpts.RemappedFileBuffers.pop_back();
1627 
1628  // If the hash of top-level entities differs from the hash of the top-level
1629  // entities the last time we rebuilt the preamble, clear out the completion
1630  // cache.
1631  if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1632  CompletionCacheTopLevelHashValue = 0;
1633  PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1634  }
1635 
1636  return llvm::MemoryBuffer::getMemBufferCopy(NewPreamble.Buffer->getBuffer(),
1637  MainFilename);
1638 }
1639 
1640 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1641  std::vector<Decl *> Resolved;
1642  Resolved.reserve(TopLevelDeclsInPreamble.size());
1644  for (serialization::DeclID TopLevelDecl : TopLevelDeclsInPreamble) {
1645  // Resolve the declaration ID to an actual declaration, possibly
1646  // deserializing the declaration in the process.
1647  if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1648  Resolved.push_back(D);
1649  }
1650  TopLevelDeclsInPreamble.clear();
1651  TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1652 }
1653 
1654 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1655  // Steal the created target, context, and preprocessor if they have been
1656  // created.
1657  assert(CI.hasInvocation() && "missing invocation");
1658  LangOpts = CI.getInvocation().LangOpts;
1659  TheSema = CI.takeSema();
1660  Consumer = CI.takeASTConsumer();
1661  if (CI.hasASTContext())
1662  Ctx = &CI.getASTContext();
1663  if (CI.hasPreprocessor())
1664  PP = &CI.getPreprocessor();
1665  CI.setSourceManager(nullptr);
1666  CI.setFileManager(nullptr);
1667  if (CI.hasTarget())
1668  Target = &CI.getTarget();
1669  Reader = CI.getModuleManager();
1670  HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1671 }
1672 
1673 StringRef ASTUnit::getMainFileName() const {
1674  if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1675  const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1676  if (Input.isFile())
1677  return Input.getFile();
1678  else
1679  return Input.getBuffer()->getBufferIdentifier();
1680  }
1681 
1682  if (SourceMgr) {
1683  if (const FileEntry *
1684  FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1685  return FE->getName();
1686  }
1687 
1688  return StringRef();
1689 }
1690 
1691 StringRef ASTUnit::getASTFileName() const {
1692  if (!isMainFileAST())
1693  return StringRef();
1694 
1696  Mod = Reader->getModuleManager().getPrimaryModule();
1697  return Mod.FileName;
1698 }
1699 
1702  bool CaptureDiagnostics,
1703  bool UserFilesAreVolatile) {
1704  std::unique_ptr<ASTUnit> AST;
1705  AST.reset(new ASTUnit(false));
1706  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1707  AST->Diagnostics = Diags;
1708  AST->Invocation = CI;
1709  AST->FileSystemOpts = CI->getFileSystemOpts();
1711  createVFSFromCompilerInvocation(*CI, *Diags);
1712  if (!VFS)
1713  return nullptr;
1714  AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1715  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1716  AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1717  UserFilesAreVolatile);
1718 
1719  return AST.release();
1720 }
1721 
1723  CompilerInvocation *CI,
1724  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1726  ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
1727  bool OnlyLocalDecls, bool CaptureDiagnostics, bool PrecompilePreamble,
1728  bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1729  bool UserFilesAreVolatile, std::unique_ptr<ASTUnit> *ErrAST) {
1730  assert(CI && "A CompilerInvocation is required");
1731 
1732  std::unique_ptr<ASTUnit> OwnAST;
1733  ASTUnit *AST = Unit;
1734  if (!AST) {
1735  // Create the AST unit.
1736  OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile));
1737  AST = OwnAST.get();
1738  if (!AST)
1739  return nullptr;
1740  }
1741 
1742  if (!ResourceFilesPath.empty()) {
1743  // Override the resources path.
1744  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1745  }
1746  AST->OnlyLocalDecls = OnlyLocalDecls;
1747  AST->CaptureDiagnostics = CaptureDiagnostics;
1748  if (PrecompilePreamble)
1749  AST->PreambleRebuildCounter = 2;
1750  AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1751  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1752  AST->IncludeBriefCommentsInCodeCompletion
1753  = IncludeBriefCommentsInCodeCompletion;
1754 
1755  // Recover resources if we crash before exiting this method.
1756  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1757  ASTUnitCleanup(OwnAST.get());
1758  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1759  llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1760  DiagCleanup(Diags.get());
1761 
1762  // We'll manage file buffers ourselves.
1764  CI->getFrontendOpts().DisableFree = false;
1766 
1767  // Create the compiler instance to use for building the AST.
1768  std::unique_ptr<CompilerInstance> Clang(
1769  new CompilerInstance(PCHContainerOps));
1770 
1771  // Recover resources if we crash before exiting this method.
1772  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1773  CICleanup(Clang.get());
1774 
1775  Clang->setInvocation(CI);
1776  AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1777 
1778  // Set up diagnostics, capturing any diagnostics that would
1779  // otherwise be dropped.
1780  Clang->setDiagnostics(&AST->getDiagnostics());
1781 
1782  // Create the target instance.
1783  Clang->setTarget(TargetInfo::CreateTargetInfo(
1784  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1785  if (!Clang->hasTarget())
1786  return nullptr;
1787 
1788  // Inform the target of the language options.
1789  //
1790  // FIXME: We shouldn't need to do this, the target should be immutable once
1791  // created. This complexity should be lifted elsewhere.
1792  Clang->getTarget().adjust(Clang->getLangOpts());
1793 
1794  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1795  "Invocation must have exactly one source file!");
1796  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1797  "FIXME: AST inputs not yet supported here!");
1798  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1799  "IR inputs not supported here!");
1800 
1801  // Configure the various subsystems.
1802  AST->TheSema.reset();
1803  AST->Ctx = nullptr;
1804  AST->PP = nullptr;
1805  AST->Reader = nullptr;
1806 
1807  // Create a file manager object to provide access to and cache the filesystem.
1808  Clang->setFileManager(&AST->getFileManager());
1809 
1810  // Create the source manager.
1811  Clang->setSourceManager(&AST->getSourceManager());
1812 
1813  ASTFrontendAction *Act = Action;
1814 
1815  std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
1816  if (!Act) {
1817  TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1818  Act = TrackerAct.get();
1819  }
1820 
1821  // Recover resources if we crash before exiting this method.
1822  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1823  ActCleanup(TrackerAct.get());
1824 
1825  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1826  AST->transferASTDataFromCompilerInstance(*Clang);
1827  if (OwnAST && ErrAST)
1828  ErrAST->swap(OwnAST);
1829 
1830  return nullptr;
1831  }
1832 
1833  if (Persistent && !TrackerAct) {
1834  Clang->getPreprocessor().addPPCallbacks(
1835  llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
1836  AST->getCurrentTopLevelHashValue()));
1837  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
1838  if (Clang->hasASTConsumer())
1839  Consumers.push_back(Clang->takeASTConsumer());
1840  Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>(
1841  *AST, AST->getCurrentTopLevelHashValue()));
1842  Clang->setASTConsumer(
1843  llvm::make_unique<MultiplexConsumer>(std::move(Consumers)));
1844  }
1845  if (!Act->Execute()) {
1846  AST->transferASTDataFromCompilerInstance(*Clang);
1847  if (OwnAST && ErrAST)
1848  ErrAST->swap(OwnAST);
1849 
1850  return nullptr;
1851  }
1852 
1853  // Steal the created target, context, and preprocessor.
1854  AST->transferASTDataFromCompilerInstance(*Clang);
1855 
1856  Act->EndSourceFile();
1857 
1858  if (OwnAST)
1859  return OwnAST.release();
1860  else
1861  return AST;
1862 }
1863 
1864 bool ASTUnit::LoadFromCompilerInvocation(
1865  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1866  bool PrecompilePreamble) {
1867  if (!Invocation)
1868  return true;
1869 
1870  // We'll manage file buffers ourselves.
1871  Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1872  Invocation->getFrontendOpts().DisableFree = false;
1874 
1875  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1876  if (PrecompilePreamble) {
1877  PreambleRebuildCounter = 2;
1878  OverrideMainBuffer =
1879  getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation);
1880  }
1881 
1882  SimpleTimer ParsingTimer(WantTiming);
1883  ParsingTimer.setOutput("Parsing " + getMainFileName());
1884 
1885  // Recover resources if we crash before exiting this method.
1886  llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1887  MemBufferCleanup(OverrideMainBuffer.get());
1888 
1889  return Parse(PCHContainerOps, std::move(OverrideMainBuffer));
1890 }
1891 
1892 std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
1893  CompilerInvocation *CI,
1894  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1895  IntrusiveRefCntPtr<DiagnosticsEngine> Diags, bool OnlyLocalDecls,
1896  bool CaptureDiagnostics, bool PrecompilePreamble,
1897  TranslationUnitKind TUKind, bool CacheCodeCompletionResults,
1898  bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile) {
1899  // Create the AST unit.
1900  std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1901  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1902  AST->Diagnostics = Diags;
1903  AST->OnlyLocalDecls = OnlyLocalDecls;
1904  AST->CaptureDiagnostics = CaptureDiagnostics;
1905  AST->TUKind = TUKind;
1906  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1907  AST->IncludeBriefCommentsInCodeCompletion
1908  = IncludeBriefCommentsInCodeCompletion;
1909  AST->Invocation = CI;
1910  AST->FileSystemOpts = CI->getFileSystemOpts();
1912  createVFSFromCompilerInvocation(*CI, *Diags);
1913  if (!VFS)
1914  return nullptr;
1915  AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1916  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1917 
1918  // Recover resources if we crash before exiting this method.
1919  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1920  ASTUnitCleanup(AST.get());
1921  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1922  llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1923  DiagCleanup(Diags.get());
1924 
1925  if (AST->LoadFromCompilerInvocation(PCHContainerOps, PrecompilePreamble))
1926  return nullptr;
1927  return AST;
1928 }
1929 
1931  const char **ArgBegin, const char **ArgEnd,
1932  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1933  IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
1934  bool OnlyLocalDecls, bool CaptureDiagnostics,
1935  ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
1936  bool PrecompilePreamble, TranslationUnitKind TUKind,
1937  bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1938  bool AllowPCHWithCompilerErrors, bool SkipFunctionBodies,
1939  bool UserFilesAreVolatile, bool ForSerialization,
1940  std::unique_ptr<ASTUnit> *ErrAST) {
1941  assert(Diags.get() && "no DiagnosticsEngine was provided");
1942 
1943  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1944 
1946 
1947  {
1948 
1949  CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1950  StoredDiagnostics);
1951 
1953  llvm::makeArrayRef(ArgBegin, ArgEnd),
1954  Diags);
1955  if (!CI)
1956  return nullptr;
1957  }
1958 
1959  // Override any files that need remapping
1960  for (const auto &RemappedFile : RemappedFiles) {
1961  CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1962  RemappedFile.second);
1963  }
1964  PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1965  PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1966  PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1967 
1968  // Override the resources path.
1969  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1970 
1971  CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
1972 
1973  // Create the AST unit.
1974  std::unique_ptr<ASTUnit> AST;
1975  AST.reset(new ASTUnit(false));
1976  ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1977  AST->Diagnostics = Diags;
1978  AST->FileSystemOpts = CI->getFileSystemOpts();
1980  createVFSFromCompilerInvocation(*CI, *Diags);
1981  if (!VFS)
1982  return nullptr;
1983  AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1984  AST->OnlyLocalDecls = OnlyLocalDecls;
1985  AST->CaptureDiagnostics = CaptureDiagnostics;
1986  AST->TUKind = TUKind;
1987  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1988  AST->IncludeBriefCommentsInCodeCompletion
1989  = IncludeBriefCommentsInCodeCompletion;
1990  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1991  AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1992  AST->StoredDiagnostics.swap(StoredDiagnostics);
1993  AST->Invocation = CI;
1994  if (ForSerialization)
1995  AST->WriterData.reset(new ASTWriterData());
1996  // Zero out now to ease cleanup during crash recovery.
1997  CI = nullptr;
1998  Diags = nullptr;
1999 
2000  // Recover resources if we crash before exiting this method.
2001  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
2002  ASTUnitCleanup(AST.get());
2003 
2004  if (AST->LoadFromCompilerInvocation(PCHContainerOps, PrecompilePreamble)) {
2005  // Some error occurred, if caller wants to examine diagnostics, pass it the
2006  // ASTUnit.
2007  if (ErrAST) {
2008  AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
2009  ErrAST->swap(AST);
2010  }
2011  return nullptr;
2012  }
2013 
2014  return AST.release();
2015 }
2016 
2017 bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2018  ArrayRef<RemappedFile> RemappedFiles) {
2019  if (!Invocation)
2020  return true;
2021 
2022  clearFileLevelDecls();
2023 
2024  SimpleTimer ParsingTimer(WantTiming);
2025  ParsingTimer.setOutput("Reparsing " + getMainFileName());
2026 
2027  // Remap files.
2028  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
2029  for (const auto &RB : PPOpts.RemappedFileBuffers)
2030  delete RB.second;
2031 
2032  Invocation->getPreprocessorOpts().clearRemappedFiles();
2033  for (const auto &RemappedFile : RemappedFiles) {
2034  Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
2035  RemappedFile.second);
2036  }
2037 
2038  // If we have a preamble file lying around, or if we might try to
2039  // build a precompiled preamble, do so now.
2040  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2041  if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
2042  OverrideMainBuffer =
2043  getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation);
2044 
2045  // Clear out the diagnostics state.
2046  getDiagnostics().Reset();
2048  if (OverrideMainBuffer)
2049  getDiagnostics().setNumWarnings(NumWarningsInPreamble);
2050 
2051  // Parse the sources
2052  bool Result = Parse(PCHContainerOps, std::move(OverrideMainBuffer));
2053 
2054  // If we're caching global code-completion results, and the top-level
2055  // declarations have changed, clear out the code-completion cache.
2056  if (!Result && ShouldCacheCodeCompletionResults &&
2057  CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
2058  CacheCodeCompletionResults();
2059 
2060  // We now need to clear out the completion info related to this translation
2061  // unit; it'll be recreated if necessary.
2062  CCTUInfo.reset();
2063 
2064  return Result;
2065 }
2066 
2067 //----------------------------------------------------------------------------//
2068 // Code completion
2069 //----------------------------------------------------------------------------//
2070 
2071 namespace {
2072  /// \brief Code completion consumer that combines the cached code-completion
2073  /// results from an ASTUnit with the code-completion results provided to it,
2074  /// then passes the result on to
2075  class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
2076  uint64_t NormalContexts;
2077  ASTUnit &AST;
2079 
2080  public:
2081  AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
2082  const CodeCompleteOptions &CodeCompleteOpts)
2083  : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
2084  AST(AST), Next(Next)
2085  {
2086  // Compute the set of contexts in which we will look when we don't have
2087  // any information about the specific context.
2088  NormalContexts
2102 
2103  if (AST.getASTContext().getLangOpts().CPlusPlus)
2104  NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
2107  }
2108 
2109  void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
2110  CodeCompletionResult *Results,
2111  unsigned NumResults) override;
2112 
2113  void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
2114  OverloadCandidate *Candidates,
2115  unsigned NumCandidates) override {
2116  Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
2117  }
2118 
2119  CodeCompletionAllocator &getAllocator() override {
2120  return Next.getAllocator();
2121  }
2122 
2124  return Next.getCodeCompletionTUInfo();
2125  }
2126  };
2127 }
2128 
2129 /// \brief Helper function that computes which global names are hidden by the
2130 /// local code-completion results.
2132  CodeCompletionResult *Results,
2133  unsigned NumResults,
2134  ASTContext &Ctx,
2135  llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
2136  bool OnlyTagNames = false;
2137  switch (Context.getKind()) {
2156  break;
2157 
2161  OnlyTagNames = true;
2162  break;
2163 
2177  // We're looking for nothing, or we're looking for names that cannot
2178  // be hidden.
2179  return;
2180  }
2181 
2182  typedef CodeCompletionResult Result;
2183  for (unsigned I = 0; I != NumResults; ++I) {
2184  if (Results[I].Kind != Result::RK_Declaration)
2185  continue;
2186 
2187  unsigned IDNS
2189 
2190  bool Hiding = false;
2191  if (OnlyTagNames)
2192  Hiding = (IDNS & Decl::IDNS_Tag);
2193  else {
2194  unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2197  if (Ctx.getLangOpts().CPlusPlus)
2198  HiddenIDNS |= Decl::IDNS_Tag;
2199  Hiding = (IDNS & HiddenIDNS);
2200  }
2201 
2202  if (!Hiding)
2203  continue;
2204 
2205  DeclarationName Name = Results[I].Declaration->getDeclName();
2206  if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2207  HiddenNames.insert(Identifier->getName());
2208  else
2209  HiddenNames.insert(Name.getAsString());
2210  }
2211 }
2212 
2213 
2214 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2216  CodeCompletionResult *Results,
2217  unsigned NumResults) {
2218  // Merge the results we were given with the results we cached.
2219  bool AddedResult = false;
2220  uint64_t InContexts =
2222  ? NormalContexts : (1LL << Context.getKind());
2223  // Contains the set of names that are hidden by "local" completion results.
2224  llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2225  typedef CodeCompletionResult Result;
2226  SmallVector<Result, 8> AllResults;
2228  C = AST.cached_completion_begin(),
2229  CEnd = AST.cached_completion_end();
2230  C != CEnd; ++C) {
2231  // If the context we are in matches any of the contexts we are
2232  // interested in, we'll add this result.
2233  if ((C->ShowInContexts & InContexts) == 0)
2234  continue;
2235 
2236  // If we haven't added any results previously, do so now.
2237  if (!AddedResult) {
2238  CalculateHiddenNames(Context, Results, NumResults, S.Context,
2239  HiddenNames);
2240  AllResults.insert(AllResults.end(), Results, Results + NumResults);
2241  AddedResult = true;
2242  }
2243 
2244  // Determine whether this global completion result is hidden by a local
2245  // completion result. If so, skip it.
2246  if (C->Kind != CXCursor_MacroDefinition &&
2247  HiddenNames.count(C->Completion->getTypedText()))
2248  continue;
2249 
2250  // Adjust priority based on similar type classes.
2251  unsigned Priority = C->Priority;
2252  CodeCompletionString *Completion = C->Completion;
2253  if (!Context.getPreferredType().isNull()) {
2254  if (C->Kind == CXCursor_MacroDefinition) {
2255  Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2256  S.getLangOpts(),
2257  Context.getPreferredType()->isAnyPointerType());
2258  } else if (C->Type) {
2259  CanQualType Expected
2261  Context.getPreferredType().getUnqualifiedType());
2262  SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2263  if (ExpectedSTC == C->TypeClass) {
2264  // We know this type is similar; check for an exact match.
2265  llvm::StringMap<unsigned> &CachedCompletionTypes
2266  = AST.getCachedCompletionTypes();
2267  llvm::StringMap<unsigned>::iterator Pos
2268  = CachedCompletionTypes.find(QualType(Expected).getAsString());
2269  if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2270  Priority /= CCF_ExactTypeMatch;
2271  else
2272  Priority /= CCF_SimilarTypeMatch;
2273  }
2274  }
2275  }
2276 
2277  // Adjust the completion string, if required.
2278  if (C->Kind == CXCursor_MacroDefinition &&
2280  // Create a new code-completion string that just contains the
2281  // macro name, without its arguments.
2283  CCP_CodePattern, C->Availability);
2284  Builder.AddTypedTextChunk(C->Completion->getTypedText());
2285  Priority = CCP_CodePattern;
2286  Completion = Builder.TakeString();
2287  }
2288 
2289  AllResults.push_back(Result(Completion, Priority, C->Kind,
2290  C->Availability));
2291  }
2292 
2293  // If we did not add any cached completion results, just forward the
2294  // results we were given to the next consumer.
2295  if (!AddedResult) {
2296  Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2297  return;
2298  }
2299 
2300  Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2301  AllResults.size());
2302 }
2303 
2305  StringRef File, unsigned Line, unsigned Column,
2306  ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
2307  bool IncludeCodePatterns, bool IncludeBriefComments,
2308  CodeCompleteConsumer &Consumer,
2309  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2310  DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr,
2311  FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2313  if (!Invocation)
2314  return;
2315 
2316  SimpleTimer CompletionTimer(WantTiming);
2317  CompletionTimer.setOutput("Code completion @ " + File + ":" +
2318  Twine(Line) + ":" + Twine(Column));
2319 
2321  CCInvocation(new CompilerInvocation(*Invocation));
2322 
2323  FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2324  CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2325  PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2326 
2327  CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2328  CachedCompletionResults.empty();
2329  CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2330  CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2331  CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2332 
2333  assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2334 
2335  FrontendOpts.CodeCompletionAt.FileName = File;
2336  FrontendOpts.CodeCompletionAt.Line = Line;
2337  FrontendOpts.CodeCompletionAt.Column = Column;
2338 
2339  // Set the language options appropriately.
2340  LangOpts = *CCInvocation->getLangOpts();
2341 
2342  // Spell-checking and warnings are wasteful during code-completion.
2343  LangOpts.SpellChecking = false;
2344  CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
2345 
2346  std::unique_ptr<CompilerInstance> Clang(
2347  new CompilerInstance(PCHContainerOps));
2348 
2349  // Recover resources if we crash before exiting this method.
2350  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2351  CICleanup(Clang.get());
2352 
2353  Clang->setInvocation(&*CCInvocation);
2354  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2355 
2356  // Set up diagnostics, capturing any diagnostics produced.
2357  Clang->setDiagnostics(&Diag);
2358  CaptureDroppedDiagnostics Capture(true,
2359  Clang->getDiagnostics(),
2360  StoredDiagnostics);
2361  ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2362 
2363  // Create the target instance.
2364  Clang->setTarget(TargetInfo::CreateTargetInfo(
2365  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
2366  if (!Clang->hasTarget()) {
2367  Clang->setInvocation(nullptr);
2368  return;
2369  }
2370 
2371  // Inform the target of the language options.
2372  //
2373  // FIXME: We shouldn't need to do this, the target should be immutable once
2374  // created. This complexity should be lifted elsewhere.
2375  Clang->getTarget().adjust(Clang->getLangOpts());
2376 
2377  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2378  "Invocation must have exactly one source file!");
2379  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
2380  "FIXME: AST inputs not yet supported here!");
2381  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
2382  "IR inputs not support here!");
2383 
2384 
2385  // Use the source and file managers that we were given.
2386  Clang->setFileManager(&FileMgr);
2387  Clang->setSourceManager(&SourceMgr);
2388 
2389  // Remap files.
2390  PreprocessorOpts.clearRemappedFiles();
2391  PreprocessorOpts.RetainRemappedFileBuffers = true;
2392  for (const auto &RemappedFile : RemappedFiles) {
2393  PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second);
2394  OwnedBuffers.push_back(RemappedFile.second);
2395  }
2396 
2397  // Use the code completion consumer we were given, but adding any cached
2398  // code-completion results.
2399  AugmentedCodeCompleteConsumer *AugmentedConsumer
2400  = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2401  Clang->setCodeCompletionConsumer(AugmentedConsumer);
2402 
2403  // If we have a precompiled preamble, try to use it. We only allow
2404  // the use of the precompiled preamble if we're if the completion
2405  // point is within the main file, after the end of the precompiled
2406  // preamble.
2407  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2408  if (!getPreambleFile(this).empty()) {
2409  std::string CompleteFilePath(File);
2410  llvm::sys::fs::UniqueID CompleteFileID;
2411 
2412  if (!llvm::sys::fs::getUniqueID(CompleteFilePath, CompleteFileID)) {
2413  std::string MainPath(OriginalSourceFile);
2414  llvm::sys::fs::UniqueID MainID;
2415  if (!llvm::sys::fs::getUniqueID(MainPath, MainID)) {
2416  if (CompleteFileID == MainID && Line > 1)
2417  OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2418  PCHContainerOps, *CCInvocation, false, Line - 1);
2419  }
2420  }
2421  }
2422 
2423  // If the main file has been overridden due to the use of a preamble,
2424  // make that override happen and introduce the preamble.
2425  if (OverrideMainBuffer) {
2426  PreprocessorOpts.addRemappedFile(OriginalSourceFile,
2427  OverrideMainBuffer.get());
2428  PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2429  PreprocessorOpts.PrecompiledPreambleBytes.second
2430  = PreambleEndsAtStartOfLine;
2431  PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
2432  PreprocessorOpts.DisablePCHValidation = true;
2433 
2434  OwnedBuffers.push_back(OverrideMainBuffer.release());
2435  } else {
2436  PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2437  PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2438  }
2439 
2440  // Disable the preprocessing record if modules are not enabled.
2441  if (!Clang->getLangOpts().Modules)
2442  PreprocessorOpts.DetailedRecord = false;
2443 
2444  std::unique_ptr<SyntaxOnlyAction> Act;
2445  Act.reset(new SyntaxOnlyAction);
2446  if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2447  Act->Execute();
2448  Act->EndSourceFile();
2449  }
2450 }
2451 
2452 bool ASTUnit::Save(StringRef File) {
2453  if (HadModuleLoaderFatalFailure)
2454  return true;
2455 
2456  // Write to a temporary file and later rename it to the actual file, to avoid
2457  // possible race conditions.
2458  SmallString<128> TempPath;
2459  TempPath = File;
2460  TempPath += "-%%%%%%%%";
2461  int fd;
2462  if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath))
2463  return true;
2464 
2465  // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2466  // unconditionally create a stat cache when we parse the file?
2467  llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2468 
2469  serialize(Out);
2470  Out.close();
2471  if (Out.has_error()) {
2472  Out.clear_error();
2473  return true;
2474  }
2475 
2476  if (llvm::sys::fs::rename(TempPath, File)) {
2477  llvm::sys::fs::remove(TempPath);
2478  return true;
2479  }
2480 
2481  return false;
2482 }
2483 
2484 static bool serializeUnit(ASTWriter &Writer,
2485  SmallVectorImpl<char> &Buffer,
2486  Sema &S,
2487  bool hasErrors,
2488  raw_ostream &OS) {
2489  Writer.WriteAST(S, std::string(), nullptr, "", hasErrors);
2490 
2491  // Write the generated bitstream to "Out".
2492  if (!Buffer.empty())
2493  OS.write(Buffer.data(), Buffer.size());
2494 
2495  return false;
2496 }
2497 
2498 bool ASTUnit::serialize(raw_ostream &OS) {
2499  bool hasErrors = getDiagnostics().hasErrorOccurred();
2500 
2501  if (WriterData)
2502  return serializeUnit(WriterData->Writer, WriterData->Buffer,
2503  getSema(), hasErrors, OS);
2504 
2505  SmallString<128> Buffer;
2506  llvm::BitstreamWriter Stream(Buffer);
2507  ASTWriter Writer(Stream);
2508  return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2509 }
2510 
2512 
2513 void ASTUnit::TranslateStoredDiagnostics(
2514  FileManager &FileMgr,
2515  SourceManager &SrcMgr,
2518  // Map the standalone diagnostic into the new source manager. We also need to
2519  // remap all the locations to the new view. This includes the diag location,
2520  // any associated source ranges, and the source ranges of associated fix-its.
2521  // FIXME: There should be a cleaner way to do this.
2522 
2524  Result.reserve(Diags.size());
2525  for (const StandaloneDiagnostic &SD : Diags) {
2526  // Rebuild the StoredDiagnostic.
2527  if (SD.Filename.empty())
2528  continue;
2529  const FileEntry *FE = FileMgr.getFile(SD.Filename);
2530  if (!FE)
2531  continue;
2532  FileID FID = SrcMgr.translateFile(FE);
2533  SourceLocation FileLoc = SrcMgr.getLocForStartOfFile(FID);
2534  if (FileLoc.isInvalid())
2535  continue;
2536  SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset);
2537  FullSourceLoc Loc(L, SrcMgr);
2538 
2540  Ranges.reserve(SD.Ranges.size());
2541  for (const auto &Range : SD.Ranges) {
2542  SourceLocation BL = FileLoc.getLocWithOffset(Range.first);
2543  SourceLocation EL = FileLoc.getLocWithOffset(Range.second);
2544  Ranges.push_back(CharSourceRange::getCharRange(BL, EL));
2545  }
2546 
2548  FixIts.reserve(SD.FixIts.size());
2549  for (const StandaloneFixIt &FixIt : SD.FixIts) {
2550  FixIts.push_back(FixItHint());
2551  FixItHint &FH = FixIts.back();
2552  FH.CodeToInsert = FixIt.CodeToInsert;
2553  SourceLocation BL = FileLoc.getLocWithOffset(FixIt.RemoveRange.first);
2554  SourceLocation EL = FileLoc.getLocWithOffset(FixIt.RemoveRange.second);
2556  }
2557 
2558  Result.push_back(StoredDiagnostic(SD.Level, SD.ID,
2559  SD.Message, Loc, Ranges, FixIts));
2560  }
2561  Result.swap(Out);
2562 }
2563 
2565  assert(D);
2566 
2567  // We only care about local declarations.
2568  if (D->isFromASTFile())
2569  return;
2570 
2571  SourceManager &SM = *SourceMgr;
2572  SourceLocation Loc = D->getLocation();
2573  if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2574  return;
2575 
2576  // We only keep track of the file-level declarations of each file.
2577  if (!D->getLexicalDeclContext()->isFileContext())
2578  return;
2579 
2580  SourceLocation FileLoc = SM.getFileLoc(Loc);
2581  assert(SM.isLocalSourceLocation(FileLoc));
2582  FileID FID;
2583  unsigned Offset;
2584  std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2585  if (FID.isInvalid())
2586  return;
2587 
2588  LocDeclsTy *&Decls = FileDecls[FID];
2589  if (!Decls)
2590  Decls = new LocDeclsTy();
2591 
2592  std::pair<unsigned, Decl *> LocDecl(Offset, D);
2593 
2594  if (Decls->empty() || Decls->back().first <= Offset) {
2595  Decls->push_back(LocDecl);
2596  return;
2597  }
2598 
2599  LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
2600  LocDecl, llvm::less_first());
2601 
2602  Decls->insert(I, LocDecl);
2603 }
2604 
2605 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2606  SmallVectorImpl<Decl *> &Decls) {
2607  if (File.isInvalid())
2608  return;
2609 
2610  if (SourceMgr->isLoadedFileID(File)) {
2611  assert(Ctx->getExternalSource() && "No external source!");
2612  return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2613  Decls);
2614  }
2615 
2616  FileDeclsTy::iterator I = FileDecls.find(File);
2617  if (I == FileDecls.end())
2618  return;
2619 
2620  LocDeclsTy &LocDecls = *I->second;
2621  if (LocDecls.empty())
2622  return;
2623 
2624  LocDeclsTy::iterator BeginIt =
2625  std::lower_bound(LocDecls.begin(), LocDecls.end(),
2626  std::make_pair(Offset, (Decl *)nullptr),
2627  llvm::less_first());
2628  if (BeginIt != LocDecls.begin())
2629  --BeginIt;
2630 
2631  // If we are pointing at a top-level decl inside an objc container, we need
2632  // to backtrack until we find it otherwise we will fail to report that the
2633  // region overlaps with an objc container.
2634  while (BeginIt != LocDecls.begin() &&
2635  BeginIt->second->isTopLevelDeclInObjCContainer())
2636  --BeginIt;
2637 
2638  LocDeclsTy::iterator EndIt = std::upper_bound(
2639  LocDecls.begin(), LocDecls.end(),
2640  std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first());
2641  if (EndIt != LocDecls.end())
2642  ++EndIt;
2643 
2644  for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2645  Decls.push_back(DIt->second);
2646 }
2647 
2649  unsigned Line, unsigned Col) const {
2650  const SourceManager &SM = getSourceManager();
2651  SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2652  return SM.getMacroArgExpandedLocation(Loc);
2653 }
2654 
2656  unsigned Offset) const {
2657  const SourceManager &SM = getSourceManager();
2658  SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2659  return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2660 }
2661 
2662 /// \brief If \arg Loc is a loaded location from the preamble, returns
2663 /// the corresponding local location of the main file, otherwise it returns
2664 /// \arg Loc.
2666  FileID PreambleID;
2667  if (SourceMgr)
2668  PreambleID = SourceMgr->getPreambleFileID();
2669 
2670  if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2671  return Loc;
2672 
2673  unsigned Offs;
2674  if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
2675  SourceLocation FileLoc
2676  = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2677  return FileLoc.getLocWithOffset(Offs);
2678  }
2679 
2680  return Loc;
2681 }
2682 
2683 /// \brief If \arg Loc is a local location of the main file but inside the
2684 /// preamble chunk, returns the corresponding loaded location from the
2685 /// preamble, otherwise it returns \arg Loc.
2687  FileID PreambleID;
2688  if (SourceMgr)
2689  PreambleID = SourceMgr->getPreambleFileID();
2690 
2691  if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2692  return Loc;
2693 
2694  unsigned Offs;
2695  if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2696  Offs < Preamble.size()) {
2697  SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2698  return FileLoc.getLocWithOffset(Offs);
2699  }
2700 
2701  return Loc;
2702 }
2703 
2705  FileID FID;
2706  if (SourceMgr)
2707  FID = SourceMgr->getPreambleFileID();
2708 
2709  if (Loc.isInvalid() || FID.isInvalid())
2710  return false;
2711 
2712  return SourceMgr->isInFileID(Loc, FID);
2713 }
2714 
2716  FileID FID;
2717  if (SourceMgr)
2718  FID = SourceMgr->getMainFileID();
2719 
2720  if (Loc.isInvalid() || FID.isInvalid())
2721  return false;
2722 
2723  return SourceMgr->isInFileID(Loc, FID);
2724 }
2725 
2727  FileID FID;
2728  if (SourceMgr)
2729  FID = SourceMgr->getPreambleFileID();
2730 
2731  if (FID.isInvalid())
2732  return SourceLocation();
2733 
2734  return SourceMgr->getLocForEndOfFile(FID);
2735 }
2736 
2738  FileID FID;
2739  if (SourceMgr)
2740  FID = SourceMgr->getMainFileID();
2741 
2742  if (FID.isInvalid())
2743  return SourceLocation();
2744 
2745  return SourceMgr->getLocForStartOfFile(FID);
2746 }
2747 
2748 llvm::iterator_range<PreprocessingRecord::iterator>
2750  if (isMainFileAST()) {
2752  Mod = Reader->getModuleManager().getPrimaryModule();
2753  return Reader->getModulePreprocessedEntities(Mod);
2754  }
2755 
2756  if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2757  return llvm::make_range(PPRec->local_begin(), PPRec->local_end());
2758 
2759  return llvm::make_range(PreprocessingRecord::iterator(),
2761 }
2762 
2764  if (isMainFileAST()) {
2766  Mod = Reader->getModuleManager().getPrimaryModule();
2767  for (const Decl *D : Reader->getModuleFileLevelDecls(Mod)) {
2768  if (!Fn(context, D))
2769  return false;
2770  }
2771 
2772  return true;
2773  }
2774 
2776  TLEnd = top_level_end();
2777  TL != TLEnd; ++TL) {
2778  if (!Fn(context, *TL))
2779  return false;
2780  }
2781 
2782  return true;
2783 }
2784 
2785 namespace {
2786 struct PCHLocatorInfo {
2788  PCHLocatorInfo() : Mod(nullptr) {}
2789 };
2790 }
2791 
2792 static bool PCHLocator(serialization::ModuleFile &M, void *UserData) {
2793  PCHLocatorInfo &Info = *static_cast<PCHLocatorInfo*>(UserData);
2794  switch (M.Kind) {
2797  return true; // skip dependencies.
2798  case serialization::MK_PCH:
2799  Info.Mod = &M;
2800  return true; // found it.
2802  return false; // look in dependencies.
2804  return false; // look in dependencies.
2805  }
2806 
2807  return true;
2808 }
2809 
2811  if (!Reader)
2812  return nullptr;
2813 
2814  PCHLocatorInfo Info;
2815  Reader->getModuleManager().visit(PCHLocator, &Info);
2816  if (Info.Mod)
2817  return Info.Mod->File;
2818 
2819  return nullptr;
2820 }
2821 
2823  return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
2824 }
2825 
2826 void ASTUnit::PreambleData::countLines() const {
2827  NumLines = 0;
2828  if (empty())
2829  return;
2830 
2831  NumLines = std::count(Buffer.begin(), Buffer.end(), '\n');
2832 
2833  if (Buffer.back() != '\n')
2834  ++NumLines;
2835 }
2836 
2837 #ifndef NDEBUG
2838 ASTUnit::ConcurrencyState::ConcurrencyState() {
2839  Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2840 }
2841 
2842 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2843  delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2844 }
2845 
2846 void ASTUnit::ConcurrencyState::start() {
2847  bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2848  assert(acquired && "Concurrent access to ASTUnit!");
2849 }
2850 
2851 void ASTUnit::ConcurrencyState::finish() {
2852  static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2853 }
2854 
2855 #else // NDEBUG
2856 
2857 ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = 0; }
2858 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2859 void ASTUnit::ConcurrencyState::start() {}
2860 void ASTUnit::ConcurrencyState::finish() {}
2861 
2862 #endif
HeaderSearchOptions & getHeaderSearchOpts()
An unknown context, in which we are recovering from a parsing error and don't know which completions ...
std::string OutputFile
The output file, if any.
stored_diag_iterator stored_diag_afterDriver_begin()
Definition: ASTUnit.h:658
Defines the clang::ASTContext interface.
ASTContext & getASTContext() const
StringRef getMainFileName() const
Definition: ASTUnit.cpp:1673
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:2749
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
CompilerInvocation & getInvocation()
unsigned getNumWarnings() const
Definition: Diagnostic.h:587
bool isInMainFileID(SourceLocation Loc)
Definition: ASTUnit.cpp:2715
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:368
virtual Decl * GetExternalDecl(uint32_t ID)
Resolve a declaration ID into a declaration, potentially building a new declaration.
SourceLocation getBegin() const
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:115
bool isLoadedFileID(FileID FID) const
Returns true if FID came from a PCH/Module.
std::pair< unsigned, unsigned > InsertFromRange
Definition: ASTUnit.h:72
static std::pair< unsigned, bool > ComputePreamble(StringRef Buffer, const LangOptions &LangOpts, unsigned MaxLines=0)
Compute the preamble of the given file.
Definition: Lexer.cpp:537
bool operator==(CanQual< T > x, CanQual< U > y)
Code completion for a selector, as in an @selector expression.
void EndSourceFile()
Perform any per-file post processing, deallocate per-file objects, and run statistics and output file...
const LangOptions & getLangOpts() const
Definition: Sema.h:1019
unsigned getMacroUsagePriority(StringRef MacroName, const LangOptions &LangOpts, bool PreferredTypeIsPointer=false)
Determine the priority to be given to a macro code completion result with the given name...
Code completion where an Objective-C class message is expected.
const DiagnosticsEngine & getDiagnostics() const
Definition: ASTUnit.h:499
Sema & getSema() const
Definition: ASTUnit.h:515
IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
Code completion within a type-qualifier list.
StringRef getMessage() const
Definition: Diagnostic.h:1283
SourceLocation getEndOfPreambleFileID()
Definition: ASTUnit.cpp:2726
ModuleKind Kind
The type of this module.
const unsigned DefaultPreambleRebuildInterval
After failing to build a precompiled preamble (due to errors in the source that occurs in the preambl...
Definition: ASTUnit.cpp:207
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Definition: Diagnostic.h:1258
The translation unit is a prefix to a translation unit, and is not complete.
Definition: LangOptions.h:168
static const std::string & getPreambleFile(const ASTUnit *AU)
Definition: ASTUnit.cpp:161
bool hasErrorOccurred() const
Definition: Diagnostic.h:573
const SourceManager & getManager() const
std::string getAsString() const
Definition: Type.h:897
IdentifierInfo * getAsIdentifierInfo() const
const FullSourceLoc & getLocation() const
Definition: Diagnostic.h:1282
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: ASTBitCodes.h:62
std::string CodeToInsert
The actual code to insert at the insertion location, as a string.
Definition: Diagnostic.h:64
An unspecified code-completion context.
NamespaceDecl - Represent a C++ namespace.
Definition: Decl.h:400
Allocator for a cached set of global code completions.
std::shared_ptr< LangOptions > LangOpts
Options controlling the language variant.
llvm::sys::TimeValue getLastModificationTime() const
std::unique_ptr< DiagnosticConsumer > takeClient()
Return the current diagnostic client along with ownership of that client.
Definition: Diagnostic.h:376
Code completion occurred where an Objective-C message receiver is expected.
void assign(const FileEntry *F, const char *begin, const char *end)
Definition: ASTUnit.h:196
static OnDiskDataMap & getOnDiskDataMap()
Definition: ASTUnit.cpp:110
The AST file has errors.
Definition: ASTReader.h:333
Code completion occurred on the right-hand side of a member access expression using the arrow operato...
Code completion occurred after the "enum" keyword, to indicate an enumeration name.
TargetInfo & getTarget() const
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
bool(* DeclVisitorFn)(void *context, const Decl *D)
Type for a function iterating over a number of declarations.
Definition: ASTUnit.h:687
virtual void EndSourceFile()
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: Diagnostic.h:1342
Data used to determine if a file used in the preamble has been changed.
Definition: ASTUnit.h:230
Options for controlling the target.
Definition: TargetOptions.h:24
void addRemappedFile(StringRef From, StringRef To)
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1309
NamedDecl * getUnderlyingDecl()
Looks through UsingDecls and ObjCCompatibleAliasDecls for the underlying named decl.
Definition: Decl.h:318
bool isModuleFile()
Returns true if the ASTUnit was constructed from a serialized module file.
Definition: ASTUnit.cpp:2822
static void checkAndSanitizeDiags(SmallVectorImpl< StoredDiagnostic > &StoredDiagnostics, SourceManager &SM)
Definition: ASTUnit.cpp:1011
Code completion occurred within the instance variable list of an Objective-C interface, implementation, or category implementation.
off_t getSize() const
Definition: FileManager.h:86
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Definition: PPCallbacks.h:38
Parse and apply any fixits to the source.
static PreambleFileHash createForMemoryBuffer(const llvm::MemoryBuffer *Buffer)
Definition: ASTUnit.cpp:1254
static ASTUnit * create(CompilerInvocation *CI, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, bool CaptureDiagnostics, bool UserFilesAreVolatile)
Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
Definition: ASTUnit.cpp:1700
std::vector< std::pair< unsigned, unsigned > > Ranges
Definition: ASTUnit.h:83
comments::CommandTraits & getCommentCommandTraits() const
Definition: ASTContext.h:697
cached_completion_iterator cached_completion_end()
Definition: ASTUnit.h:671
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
Definition: Diagnostic.cpp:92
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:89
Utility class for loading a ASTContext from an AST file.
Definition: ASTUnit.h:68
bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input)
Prepare the action for processing the input file Input.
A "string" used to describe how code completion can be performed for an entity.
void HandleTranslationUnit(ASTContext &Ctx) override
Definition: GeneratePCH.cpp:39
bool isAnyPointerType() const
Definition: Type.h:5235
static std::atomic< unsigned > ActiveASTUnitObjects
Tracks the number of ASTUnit objects that are currently active.
Definition: ASTUnit.cpp:212
unsigned getIdentifierNamespace() const
Definition: DeclBase.h:671
bool isTranslationUnit() const
Definition: DeclBase.h:1243
static std::unique_ptr< ASTUnit > LoadFromASTFile(const std::string &Filename, const PCHContainerReader &PCHContainerRdr, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, const FileSystemOptions &FileSystemOpts, 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:651
unsigned DetailedRecord
Whether we should maintain a detailed record of all macro definitions and expansions.
FrontendAction * Action
Definition: Tooling.cpp:168
std::unique_ptr< llvm::MemoryBuffer > getBufferForFile(StringRef Filename, std::string *ErrorStr=nullptr)
Definition: ASTUnit.cpp:633
llvm::MemoryBuffer * getBuffer() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Describes a module or submodule.
Definition: Basic/Module.h:49
const SourceManager & getSourceManager() const
Definition: ASTUnit.h:502
virtual 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. Length can be 0 to indicate a ...
An allocator used specifically for the purpose of code completion.
DiagnosticsEngine::Level Level
Definition: ASTUnit.h:79
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
const LangOptions & getLangOpts() const
Definition: ASTContext.h:533
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
SmallVector< CharSourceRange, 8 > Ranges
Definition: Format.cpp:1554
FrontendOptions & getFrontendOpts()
uint32_t Offset
Definition: CacheTokens.cpp:43
const FileEntry * getPCHFile()
Get the PCH file if one was included.
Definition: ASTUnit.cpp:2810
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:2498
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
virtual CodeCompletionTUInfo & getCodeCompletionTUInfo()=0
Code completion occurred within an Objective-C implementation or category implementation.
unsigned getID() const
Definition: Diagnostic.h:1280
bool RetainRemappedFileBuffers
Whether the compiler instance should retain (i.e., not free) the buffers associated with remapped fil...
SourceLocation getFileLoc(SourceLocation Loc) const
Given Loc, if it is a macro location return the expansion location or the spelling location...
IntrusiveRefCntPtr< ASTReader > getModuleManager() const
void addFileLevelDecl(Decl *D)
Add a new local file-level declaration.
Definition: ASTUnit.cpp:2564
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:626
DeclContext * getLexicalDeclContext()
Definition: DeclBase.h:697
The result of a status operation.
std::string getAsString() const
getNameAsString - Retrieve the human-readable string for this name.
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag)
Definition: ASTUnit.cpp:998
void Reset()
Reset the state of the diagnostic object to its initial configuration.
Definition: Diagnostic.cpp:114
SourceLocation translateFileLineCol(const FileEntry *SourceFile, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
The AST file itself appears corrupted.
Definition: ASTReader.h:321
bool DisablePCHValidation
When true, disables most of the normal validation performed on precompiled headers.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
QualType getPreferredType() const
Retrieve the type that this expression would prefer to have, e.g., if the expression is a variable in...
bool isInvalid() const
void 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:4063
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:151
FrontendOptions & getFrontendOpts()
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:1003
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:258
Code completion occurred within a "top-level" completion context, e.g., at namespace or global scope...
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
Divide by this factor when a code-completion result's type exactly matches the type we expect...
std::string CurrentModule
The name of the current module.
Definition: LangOptions.h:96
StringRef getASTFileName() const
If this ASTUnit came from an AST file, returns the filename for it.
Definition: ASTUnit.cpp:1691
SourceLocation getLocForEndOfFile(FileID FID) const
Return the source location corresponding to the last byte of the specified file.
void setPreprocessor(Preprocessor *pp)
Definition: ASTUnit.cpp:260
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
ASTContext * Context
SourceManager & SM
bool isInFileID(SourceLocation Loc, FileID FID, unsigned *RelativeOffset=nullptr) const
Given a specific FileID, returns true if Loc is inside that FileID chunk and sets relative offset (of...
const FileManager & getFileManager() const
Definition: ASTUnit.h:525
Code completion occurred where a protocol name is expected.
SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const
If Loc points inside a function macro argument, the returned location will be the macro location in w...
bool hadModuleLoaderFatalFailure() const
Allows QualTypes to be sorted and hence used in maps and sets.
static void setPreambleFile(const ASTUnit *AU, StringRef preambleFile)
Definition: ASTUnit.cpp:157
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. Length can be 0 to indicate a ...
Definition: ASTUnit.cpp:2605
CodeCompletionTUInfo & getCodeCompletionTUInfo()
Definition: ASTUnit.h:371
StringRef getName() const
Return the actual identifier string.
Code completion occurred where a new name is expected.
Represents a character-granular source range.
std::pair< unsigned, unsigned > RemoveRange
Definition: ASTUnit.h:71
off_t Size
All files have size set.
Definition: ASTUnit.h:232
SourceLocation getEnd() const
unsigned & getCurrentTopLevelHashValue()
Retrieve a reference to the current top-level name hash value.
Definition: ASTUnit.h:601
static void erasePreambleFile(const ASTUnit *AU)
Definition: ASTUnit.cpp:141
unsigned IncludeCodePatterns
Show code patterns in code completion results.
SourceManager & SourceMgr
Definition: Format.cpp:1205
virtual void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, CodeCompletionResult *Results, unsigned NumResults)
Process the finalized code-completion results.
std::vector< StandaloneFixIt > FixIts
Definition: ASTUnit.h:84
Defines the clang::Preprocessor interface.
IntrusiveRefCntPtr< vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
static PreambleFileHash createForFile(off_t Size, time_t ModTime)
Definition: ASTUnit.cpp:1246
const NamedDecl * Declaration
When Kind == RK_Declaration or RK_Pattern, the declaration we are referring to. In the latter case...
FileID getPreambleFileID() const
Get the file ID for the precompiled preamble if there is one.
DeclContext * getDeclContext()
Definition: DeclBase.h:381
uint64_t getSize() const
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
stored_diag_const_iterator stored_diag_end() const
Definition: ASTUnit.h:650
Information about a module that has been loaded by the ASTReader.
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:264
static std::string GetPreamblePCHPath()
Simple function to retrieve a path for a preamble precompiled header.
Definition: ASTUnit.cpp:1171
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.
bool BeforePreviousInsertions
Definition: Diagnostic.h:66
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
Definition: DeclBase.h:624
static llvm::sys::SmartMutex< false > & getOnDiskMutex()
Definition: ASTUnit.cpp:101
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:7387
SmallString< 128 > Buffer
Definition: ASTUnit.cpp:184
An input file for the front end.
DeclarationName getDeclName() const
Definition: Decl.h:189
static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM, const LangOptions &LangOpts, const FixItHint &InFix)
Definition: ASTUnit.cpp:1284
top_level_iterator top_level_begin()
Definition: ASTUnit.h:554
The result type of a method or function.
The client can't handle any AST loading failures.
Definition: ASTReader.h:1311
The AST file was missing.
Definition: ASTReader.h:323
CharSourceRange InsertFromRange
Code in the specific range that should be inserted in the insertion location.
Definition: Diagnostic.h:60
std::unique_ptr< Sema > takeSema()
static OnDiskData & getOnDiskData(const ASTUnit *AU)
Definition: ASTUnit.cpp:130
static CharSourceRange getCharRange(SourceRange R)
Options controlling the behavior of code completion.
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. Empty for an insertion hint.
Definition: Diagnostic.h:56
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:2131
ArrayRef< FixItHint > getFixIts() const
Definition: Diagnostic.h:1302
The control block was read successfully. Aside from failures, the AST file is safe to read into the c...
Definition: ASTReader.h:319
Code completion occurred within a class, struct, or union.
size_t size() const
Definition: ASTUnit.h:204
static void cleanupOnDiskMapAtExit()
Definition: ASTUnit.cpp:120
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
Definition: MacroInfo.h:308
#define false
Definition: stdbool.h:33
Kind
OverloadCandidate - A single candidate in an overload set (C++ 13.3).
Definition: Overload.h:586
SmallVectorImpl< AnnotatedLine * >::const_iterator Next
File is a PCH file treated as the preamble.
ContinuousRangeMap< unsigned, int, 2 > SLocRemap
Definition: ASTUnit.cpp:2511
const char * getName() const
Definition: FileManager.h:84
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
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:710
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any...
Definition: ASTContext.h:864
bool Execute()
Set the source manager's main input file, and run the action.
File is a PCH file treated as such.
ParsedSourceLocation CodeCompletionAt
If given, enable code completion at the provided location.
AnnotatedLine & Line
virtual TranslationUnitKind getTranslationUnitKind()
For AST-based actions, the kind of translation unit we're handling.
std::string ImplicitPCHInclude
The implicit PCH included at the start of the translation unit, or empty.
bool isValid() const
Return true if this is a valid SourceLocation object.
llvm::StringMap< unsigned > & getCachedCompletionTypes()
Retrieve the mapping from formatted type names to unique type identifiers.
Definition: ASTUnit.h:361
std::vector< FrontendInputFile > Inputs
The input files and their types.
File is an implicitly-loaded module.
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:53
Code completion where the name of an Objective-C class is expected.
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, bool ReportDiags=true)
Definition: Warnings.cpp:44
Abstract base class to use for AST consumer-based frontend actions.
Code completion occurred within an Objective-C interface, protocol, or category interface.
virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates)
Defines the clang::TargetOptions class.
llvm::MemoryBuffer * getMemoryBufferForFile(const FileEntry *File, bool *Invalid=nullptr)
Retrieve the memory buffer associated with the given file.
static std::pair< unsigned, unsigned > makeStandaloneRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Definition: ASTUnit.cpp:1276
llvm::DenseMap< const ASTUnit *, std::unique_ptr< OnDiskData > > OnDiskDataMap
Definition: ASTUnit.cpp:109
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:398
SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T)
Determine the simplified type class of the given canonical type.
static ASTUnit * LoadFromCompilerInvocationAction(CompilerInvocation *CI, std::shared_ptr< PCHContainerOperations > PCHContainerOps, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, ASTFrontendAction *Action=nullptr, ASTUnit *Unit=nullptr, bool Persistent=true, StringRef ResourceFilesPath=StringRef(), bool OnlyLocalDecls=false, bool CaptureDiagnostics=false, bool PrecompilePreamble=false, 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:1722
llvm::BitstreamWriter Stream
Definition: ASTUnit.cpp:185
FileID getMainFileID() const
Returns the FileID of the main source file.
bool isFileContext() const
Definition: DeclBase.h:1239
void setNumWarnings(unsigned NumWarnings)
Definition: Diagnostic.h:589
The AST file was writtten with a different language/target configuration.
Definition: ASTReader.h:331
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:2763
StoredDiagnostic * stored_diag_iterator
Definition: ASTUnit.h:642
const ASTContext & getASTContext() const
Definition: ASTUnit.h:508
File is a PCH file treated as the actual main file.
bool isInPreambleFileID(SourceLocation Loc)
Definition: ASTUnit.cpp:2704
bool isLocalSourceLocation(SourceLocation Loc) const
Returns true if Loc did not come from a PCH/Module.
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition: Decl.h:3704
void registerCommentOptions(const CommentOptions &CommentOptions)
Priority for a nested-name-specifier.
virtual StringRef getFormat() const =0
Equivalent to the format passed to -fmodule-format=.
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, bool PrecompilePreamble=false, TranslationUnitKind TUKind=TU_Complete, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool AllowPCHWithCompilerErrors=false, bool SkipFunctionBodies=false, bool UserFilesAreVolatile=false, bool ForSerialization=false, std::unique_ptr< ASTUnit > *ErrAST=nullptr)
Definition: ASTUnit.cpp:1930
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...
static ASTUnit::StandaloneDiagnostic makeStandaloneDiagnostic(const LangOptions &LangOpts, const StoredDiagnostic &InDiag)
Definition: ASTUnit.cpp:1297
unsigned IncludeGlobals
Show top-level decls in code completion results.
bool Reparse(std::shared_ptr< PCHContainerOperations > PCHContainerOps, ArrayRef< RemappedFile > RemappedFiles=None)
Reparse the source files using the same command-line options that were originally used to produce thi...
Definition: ASTUnit.cpp:2017
Code completion occurred where an macro is being defined.
bool hasSourceManager() const
Definition: Diagnostic.h:1149
PreprocessorOptions & getPreprocessorOpts()
StringRef getFile() const
frontend::ActionKind ProgramAction
The frontend action to perform.
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:856
Code completion occurred after the "struct" or "class" keyword, to indicate a struct or class name...
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:302
File is an explicitly-loaded module.
CompilerInvocation * createInvocationFromCommandLine(ArrayRef< const char * > Args, IntrusiveRefCntPtr< DiagnosticsEngine > Diags=IntrusiveRefCntPtr< DiagnosticsEngine >())
Code completion occurred after the "union" keyword, to indicate a union name.
Code completion occurred where a macro name is expected (without any arguments, in the case of a func...
std::vector< Decl * >::iterator top_level_iterator
Definition: ASTUnit.h:552
A builder class used to construct new code-completion strings.
Code completion where an Objective-C instance message is expected.
ASTMutationListener * getASTMutationListener()
Definition: ASTUnit.cpp:620
Helper class for holding the data necessary to invoke the compiler.
DiagnosticOptions & getDiagnosticOpts() const
Defines the virtual file system interface vfs::FileSystem.
llvm::MD5::MD5Result MD5
Definition: ASTUnit.h:241
static bool PCHLocator(serialization::ModuleFile &M, void *UserData)
Definition: ASTUnit.cpp:2792
void addTemporaryFile(StringRef TempFile)
Add a temporary file that the ASTUnit depends on.
Definition: ASTUnit.cpp:199
top_level_iterator top_level_end()
Definition: ASTUnit.h:561
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:1855
A map from continuous integer ranges to some value, with a very specialized interface.
FrontendOptions - Options for controlling the behavior of the frontend.
FileID translateFile(const FileEntry *SourceFile) const
Get the FileID for the given file.
Code completion occurred where a statement (or declaration) is expected in a function, method, or block.
bool isMainFileAST() const
Definition: ASTUnit.h:494
SourceLocation getStartOfMainFileID()
Definition: ASTUnit.cpp:2737
Code completion occurred on the right-hand side of an Objective-C property access expression...
SmallVector< Context, 8 > Contexts
Defines the Diagnostic-related interfaces.
SourceLocation mapLocationFromPreamble(SourceLocation Loc)
If Loc is a loaded location from the preamble, returns the corresponding local location of the main f...
Definition: ASTUnit.cpp:2665
bool Save(StringRef File)
Save this translation unit to a file with the given name.
Definition: ASTUnit.cpp:2452
unsigned IncludeBriefComments
Show brief documentation comments in code completion results.
cached_completion_iterator cached_completion_begin()
Definition: ASTUnit.h:667
time_t getModificationTime() const
Definition: FileManager.h:90
Abstract interface for callback invocations by the ASTReader.
Definition: ASTReader.h:101
SourceManager & getSourceManager() const
Definition: Diagnostic.h:1150
bool isInvalid() const
enum Kind getKind() const
Retrieve the kind of code-completion context.
CodeCompleteOptions CodeCompleteOpts
ArrayRef< CharSourceRange > getRanges() const
Definition: Diagnostic.h:1292
Keeps track of options that affect how file operations are performed.
unsigned DisableFree
Disable memory freeing on exit.
Generate pre-compiled header.
static bool serializeUnit(ASTWriter &Writer, SmallVectorImpl< char > &Buffer, Sema &S, bool hasErrors, raw_ostream &OS)
Definition: ASTUnit.cpp:2484
Code completion occurred within a preprocessor expression.
Code completion occurred where an expression is expected.
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:5096
BoundNodesTreeBuilder *const Builder
virtual CodeCompletionAllocator & getAllocator()=0
Retrieve the allocator that will be used to allocate code completion strings.
SourceLocation getLocation(const FileEntry *File, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
Definition: ASTUnit.cpp:2648
An unspecified code-completion context where we should also add macro completions.
unsigned IncludeMacros
Show macros in code completion results.
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:141
std::vector< CachedCodeCompletionResult >::iterator cached_completion_iterator
Definition: ASTUnit.h:665
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:163
DeclContext * getLookupParent()
Find the parent context of this context that will be used for unqualified name lookup.
Definition: DeclBase.cpp:820
FileSystemOptions & getFileSystemOpts()
std::vector< std::pair< std::string, std::string > > RemappedFiles
The set of file remappings, which take existing files on the system (the first part of each pair) and...
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:82
DiagnosticsEngine::Level getLevel() const
Definition: Diagnostic.h:1281
const StringRef Input
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:326
A SourceLocation and its associated SourceManager.
The translation unit is a complete translation unit.
Definition: LangOptions.h:165
SourceLocation mapLocationToPreamble(SourceLocation Loc)
If Loc is a local location of the main file but inside the preamble chunk, returns the corresponding ...
Definition: ASTUnit.cpp:2686
const char * getBufferStart() const
Definition: ASTUnit.h:207
std::unique_ptr< ASTConsumer > takeASTConsumer()
The AST file was written by a different version of Clang.
Definition: ASTReader.h:328
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...
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code...
Definition: ASTWriter.h:875
Code completion occurred where a type name is expected.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:52
static raw_pwrite_stream * ComputeASTConsumerArguments(CompilerInstance &CI, StringRef InFile, std::string &Sysroot, std::string &OutputFile)
Compute the AST consumer arguments that will be used to create the PCHGenerator instance returned by ...
void InitBuiltinTypes(const TargetInfo &Target)
Initialize built-in types.
Definition: ASTContext.cpp:953
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
#define true
Definition: stdbool.h:32
SourceLocation getLocation() const
Definition: DeclBase.h:372
ASTContext & Context
Definition: Sema.h:295
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:2304
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
bool isNull() const
isNull - Return true if this QualType doesn't point to a type yet.
Definition: Type.h:633
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Definition: Preprocessor.h:773
This class handles loading and caching of source files into memory.
Divide by this factor when a code-completion result's type is similar to the type we expect (e...
Priority for a code pattern.
static void removeOnDiskEntry(const ASTUnit *AU)
Definition: ASTUnit.cpp:145
unsigned Column
Definition: Format.cpp:1202
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:96
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:177