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