1 //===--- ASTUnit.cpp - ASTUnit utility --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // ASTUnit Implementation.
12 //===----------------------------------------------------------------------===//
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"
22 #include "clang/Basic/TargetOptions.h"
23 #include "clang/Basic/VirtualFileSystem.h"
24 #include "clang/Frontend/CompilerInstance.h"
25 #include "clang/Frontend/FrontendActions.h"
26 #include "clang/Frontend/FrontendDiagnostic.h"
27 #include "clang/Frontend/FrontendOptions.h"
28 #include "clang/Frontend/MultiplexConsumer.h"
29 #include "clang/Frontend/Utils.h"
30 #include "clang/Lex/HeaderSearch.h"
31 #include "clang/Lex/Preprocessor.h"
32 #include "clang/Lex/PreprocessorOptions.h"
33 #include "clang/Sema/Sema.h"
34 #include "clang/Serialization/ASTReader.h"
35 #include "clang/Serialization/ASTWriter.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"
51 using namespace clang;
53 using llvm::TimeRecord;
62 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
64 Start = TimeRecord::getCurrentTime();
67 void setOutput(const Twine &Output) {
69 this->Output = Output.str();
74 TimeRecord Elapsed = TimeRecord::getCurrentTime();
76 llvm::errs() << Output << ':';
77 Elapsed.print(Elapsed, llvm::errs());
84 /// \brief The file in which the precompiled preamble is stored.
85 std::string PreambleFile;
87 /// \brief Temporary files that should be removed when the ASTUnit is
89 SmallVector<std::string, 4> TemporaryFiles;
91 /// \brief Erase temporary files.
92 void CleanTemporaryFiles();
94 /// \brief Erase the preamble file.
95 void CleanPreambleFile();
97 /// \brief Erase temporary files and the preamble file.
102 static llvm::sys::SmartMutex<false> &getOnDiskMutex() {
103 static llvm::sys::SmartMutex<false> M(/* recursive = */ true);
107 static void cleanupOnDiskMapAtExit();
109 typedef llvm::DenseMap<const ASTUnit *,
110 std::unique_ptr<OnDiskData>> OnDiskDataMap;
111 static OnDiskDataMap &getOnDiskDataMap() {
112 static OnDiskDataMap M;
113 static bool hasRegisteredAtExit = false;
114 if (!hasRegisteredAtExit) {
115 hasRegisteredAtExit = true;
116 atexit(cleanupOnDiskMapAtExit);
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.
131 static OnDiskData &getOnDiskData(const ASTUnit *AU) {
132 // We require the mutex since we are modifying the structure of the
134 llvm::MutexGuard Guard(getOnDiskMutex());
135 OnDiskDataMap &M = getOnDiskDataMap();
138 D = llvm::make_unique<OnDiskData>();
142 static void erasePreambleFile(const ASTUnit *AU) {
143 getOnDiskData(AU).CleanPreambleFile();
146 static void removeOnDiskEntry(const ASTUnit *AU) {
147 // We require the mutex since we are modifying the structure of the
149 llvm::MutexGuard Guard(getOnDiskMutex());
150 OnDiskDataMap &M = getOnDiskDataMap();
151 OnDiskDataMap::iterator I = M.find(AU);
153 I->second->Cleanup();
158 static void setPreambleFile(const ASTUnit *AU, StringRef preambleFile) {
159 getOnDiskData(AU).PreambleFile = preambleFile;
162 static const std::string &getPreambleFile(const ASTUnit *AU) {
163 return getOnDiskData(AU).PreambleFile;
166 void OnDiskData::CleanTemporaryFiles() {
167 for (StringRef File : TemporaryFiles)
168 llvm::sys::fs::remove(File);
169 TemporaryFiles.clear();
172 void OnDiskData::CleanPreambleFile() {
173 if (!PreambleFile.empty()) {
174 llvm::sys::fs::remove(PreambleFile);
175 PreambleFile.clear();
179 void OnDiskData::Cleanup() {
180 CleanTemporaryFiles();
184 struct ASTUnit::ASTWriterData {
185 SmallString<128> Buffer;
186 llvm::BitstreamWriter Stream;
189 ASTWriterData() : Stream(Buffer), Writer(Stream, { }) { }
192 void ASTUnit::clearFileLevelDecls() {
193 llvm::DeleteContainerSeconds(FileDecls);
196 void ASTUnit::CleanTemporaryFiles() {
197 getOnDiskData(this).CleanTemporaryFiles();
200 void ASTUnit::addTemporaryFile(StringRef TempFile) {
201 getOnDiskData(this).TemporaryFiles.push_back(TempFile);
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
208 const unsigned DefaultPreambleRebuildInterval = 5;
210 /// \brief Tracks the number of ASTUnit objects that are currently active.
212 /// Used for debugging purposes only.
213 static std::atomic<unsigned> ActiveASTUnitObjects;
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);
234 ASTUnit::~ASTUnit() {
235 // If we loaded from an AST file, balance out the BeginSourceFile call.
236 if (MainFileIsAST && getDiagnostics().getClient()) {
237 getDiagnostics().getClient()->EndSourceFile();
240 clearFileLevelDecls();
242 // Clean up the temporary files and the preamble file.
243 removeOnDiskEntry(this);
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
249 if (Invocation.get() && OwnsRemappedFileBuffers) {
250 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
251 for (const auto &RB : PPOpts.RemappedFileBuffers)
255 ClearCachedCompletionResults();
257 if (getenv("LIBCLANG_OBJTRACKING"))
258 fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects);
261 void ASTUnit::setPreprocessor(Preprocessor *pp) { PP = pp; }
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;
270 if (isa<UsingShadowDecl>(ND))
271 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
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)
281 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
282 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
283 | (1LL << CodeCompletionContext::CCC_Statement)
284 | (1LL << CodeCompletionContext::CCC_Type)
285 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
287 // In C++, types can appear in expressions contexts (for functional casts).
288 if (LangOpts.CPlusPlus)
289 Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
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))
294 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
296 // In Objective-C, you can only be a subclass of another Objective-C class
297 if (isa<ObjCInterfaceDecl>(ND))
298 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName);
300 // Deal with tag names.
301 if (isa<EnumDecl>(ND)) {
302 Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
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);
311 Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
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)
320 | (1LL << CodeCompletionContext::CCC_Expression)
321 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
322 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
323 } else if (isa<ObjCProtocolDecl>(ND)) {
324 Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName);
325 } else if (isa<ObjCCategoryDecl>(ND)) {
326 Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName);
327 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
328 Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
330 // Part of the nested-name-specifier.
331 IsNestedNameSpecifier = true;
337 void ASTUnit::CacheCodeCompletionResults() {
341 SimpleTimer Timer(WantTiming);
342 Timer.setOutput("Cache global code completions for " + getMainFileName());
344 // Clear out the previous results.
345 ClearCachedCompletionResults();
347 // Gather the set of global code completions.
348 typedef CodeCompletionResult Result;
349 SmallVector<Result, 8> Results;
350 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
351 CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
352 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
355 // Translate global code completions into cached completions.
356 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
357 CodeCompletionContext CCContext(CodeCompletionContext::CCC_TopLevel);
359 for (Result &R : Results) {
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;
373 // Keep track of the type of this completion in an ASTContext-agnostic
375 QualType UsageType = getDeclUsageType(*Ctx, R.Declaration);
376 if (UsageType.isNull()) {
377 CachedResult.TypeClass = STC_Void;
378 CachedResult.Type = 0;
380 CanQualType CanUsageType
381 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
382 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
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()]
394 CachedResult.Type = TypeValue;
397 CachedCompletionResults.push_back(CachedResult);
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++.
404 = (1LL << CodeCompletionContext::CCC_TopLevel)
405 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
406 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
407 | (1LL << CodeCompletionContext::CCC_Statement)
408 | (1LL << CodeCompletionContext::CCC_Expression)
409 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
410 | (1LL << CodeCompletionContext::CCC_EnumTag)
411 | (1LL << CodeCompletionContext::CCC_UnionTag)
412 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
413 | (1LL << CodeCompletionContext::CCC_Type)
414 | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName)
415 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
417 if (isa<NamespaceDecl>(R.Declaration) ||
418 isa<NamespaceAliasDecl>(R.Declaration))
419 NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
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);
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.
446 case Result::RK_Macro: {
447 CachedCodeCompletionResult CachedResult;
448 CachedResult.Completion = R.CreateCodeCompletionString(
449 *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
450 IncludeBriefCommentsInCodeCompletion);
451 CachedResult.ShowInContexts
452 = (1LL << CodeCompletionContext::CCC_TopLevel)
453 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
454 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
455 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
456 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
457 | (1LL << CodeCompletionContext::CCC_Statement)
458 | (1LL << CodeCompletionContext::CCC_Expression)
459 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
460 | (1LL << CodeCompletionContext::CCC_MacroNameUse)
461 | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
462 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
463 | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
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);
476 // Save the current top-level hash value.
477 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
480 void ASTUnit::ClearCachedCompletionResults() {
481 CachedCompletionResults.clear();
482 CachedCompletionTypes.clear();
483 CachedCompletionAllocator = nullptr;
488 /// \brief Gathers information from ASTReader that will be used to initialize
490 class ASTInfoCollector : public ASTReaderListener {
493 LangOptions &LangOpt;
494 std::shared_ptr<TargetOptions> &TargetOpts;
495 IntrusiveRefCntPtr<TargetInfo> &Target;
498 bool InitializedLanguage;
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) {}
506 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
507 bool AllowCompatibleDifferences) override {
508 if (InitializedLanguage)
512 InitializedLanguage = true;
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.
524 this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts);
526 TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts);
532 void ReadCounter(const serialization::ModuleFile &M,
533 unsigned Value) override {
539 if (!Target || !InitializedLanguage)
542 // Inform the target of the language options.
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);
548 // Initialize the preprocessor.
549 PP.Initialize(*Target);
551 // Initialize the ASTContext
552 Context.InitBuiltinTypes(*Target);
554 // We didn't have access to the comment options when the ASTContext was
555 // constructed, so register them now.
556 Context.getCommentCommandTraits().registerCommentOptions(
557 LangOpt.CommentOpts);
561 /// \brief Diagnostic consumer that saves each diagnostic it is given.
562 class StoredDiagnosticConsumer : public DiagnosticConsumer {
563 SmallVectorImpl<StoredDiagnostic> &StoredDiags;
564 SourceManager *SourceMgr;
567 explicit StoredDiagnosticConsumer(
568 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
569 : StoredDiags(StoredDiags), SourceMgr(nullptr) {}
571 void BeginSourceFile(const LangOptions &LangOpts,
572 const Preprocessor *PP = nullptr) override {
574 SourceMgr = &PP->getSourceManager();
577 void HandleDiagnostic(DiagnosticsEngine::Level Level,
578 const Diagnostic &Info) override;
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;
590 CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
591 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
592 : Diags(Diags), Client(StoredDiags), PreviousClient(nullptr)
594 if (RequestCapture || Diags.getClient() == nullptr) {
595 OwningPreviousClient = Diags.takeClient();
596 PreviousClient = Diags.getClient();
597 Diags.setClient(&Client, false);
601 ~CaptureDroppedDiagnostics() {
602 if (Diags.getClient() == &Client)
603 Diags.setClient(PreviousClient, !!OwningPreviousClient.release());
607 } // anonymous namespace
609 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
610 const Diagnostic &Info) {
611 // Default implementation (Warnings/errors count).
612 DiagnosticConsumer::HandleDiagnostic(Level, Info);
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);
621 ASTMutationListener *ASTUnit::getASTMutationListener() {
623 return &WriterData->Writer;
627 ASTDeserializationListener *ASTUnit::getDeserializationListener() {
629 return &WriterData->Writer;
633 std::unique_ptr<llvm::MemoryBuffer>
634 ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) {
636 auto Buffer = FileMgr->getBufferForFile(Filename);
638 return std::move(*Buffer);
640 *ErrorStr = Buffer.getError().message();
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));
652 std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
653 const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
654 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
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));
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());
668 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
670 AST->OnlyLocalDecls = OnlyLocalDecls;
671 AST->CaptureDiagnostics = CaptureDiagnostics;
672 AST->Diagnostics = Diags;
673 IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem();
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));
687 PreprocessorOptions *PPOpts = new PreprocessorOptions();
689 for (const auto &RemappedFile : RemappedFiles)
690 PPOpts->addRemappedFile(RemappedFile.first, RemappedFile.second);
692 // Gather Info for preprocessor construction later on.
694 HeaderSearch &HeaderInfo = *AST->HeaderInfo;
698 new Preprocessor(PPOpts, AST->getDiagnostics(), AST->ASTFileLangOpts,
699 AST->getSourceManager(), HeaderInfo, *AST,
700 /*IILookup=*/nullptr,
701 /*OwnsHeaderSearch=*/false);
702 Preprocessor &PP = *AST->PP;
704 AST->Ctx = new ASTContext(AST->ASTFileLangOpts, AST->getSourceManager(),
705 PP.getIdentifierTable(), PP.getSelectorTable(),
706 PP.getBuiltinInfo());
707 ASTContext &Context = *AST->Ctx;
709 bool disableValid = false;
710 if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
712 AST->Reader = new ASTReader(PP, Context, PCHContainerRdr, { },
714 /*DisableValidation=*/disableValid,
715 AllowPCHWithCompilerErrors);
717 AST->Reader->setListener(llvm::make_unique<ASTInfoCollector>(
718 *AST->PP, Context, AST->ASTFileLangOpts, AST->TargetOpts, AST->Target,
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);
728 switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile,
729 SourceLocation(), ASTReader::ARR_None)) {
730 case ASTReader::Success:
733 case ASTReader::Failure:
734 case ASTReader::Missing:
735 case ASTReader::OutOfDate:
736 case ASTReader::VersionMismatch:
737 case ASTReader::ConfigurationMismatch:
738 case ASTReader::HadErrors:
739 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
743 AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile();
745 PP.setCounterValue(Counter);
747 // Create an AST consumer, even though it isn't used.
748 AST->Consumer.reset(new ASTConsumer);
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);
755 // Tell the diagnostic client that we have started a source file.
756 AST->getDiagnostics().getClient()->BeginSourceFile(Context.getLangOpts(),&PP);
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 {
769 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
771 void MacroDefined(const Token &MacroNameTok,
772 const MacroDirective *MD) override {
773 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
777 /// \brief Add the given declaration to the hash of all top-level entities.
778 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
782 DeclContext *DC = D->getDeclContext();
786 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
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);
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);
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);
819 class TopLevelDeclTrackerConsumer : public ASTConsumer {
824 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
825 : Unit(_Unit), Hash(Hash) {
829 void handleTopLevelDecl(Decl *D) {
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))
840 AddTopLevelDeclarationToHash(D, Hash);
841 Unit.addTopLevelDecl(D);
843 handleFileLevelDecl(D);
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);
854 bool HandleTopLevelDecl(DeclGroupRef D) override {
855 for (Decl *TopLevelDecl : D)
856 handleTopLevelDecl(TopLevelDecl);
860 // We're not interested in "interesting" decls.
861 void HandleInterestingDecl(DeclGroupRef) override {}
863 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
864 for (Decl *TopLevelDecl : D)
865 handleTopLevelDecl(TopLevelDecl);
868 ASTMutationListener *GetASTMutationListener() override {
869 return Unit.getASTMutationListener();
872 ASTDeserializationListener *GetASTDeserializationListener() override {
873 return Unit.getDeserializationListener();
877 class TopLevelDeclTrackerAction : public ASTFrontendAction {
881 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
882 StringRef InFile) override {
883 CI.getPreprocessor().addPPCallbacks(
884 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
885 Unit.getCurrentTopLevelHashValue()));
886 return llvm::make_unique<TopLevelDeclTrackerConsumer>(
887 Unit, Unit.getCurrentTopLevelHashValue());
891 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
893 bool hasCodeCompletionSupport() const override { return false; }
894 TranslationUnitKind getTranslationUnitKind() override {
895 return Unit.getTranslationUnitKind();
899 class PrecompilePreambleAction : public ASTFrontendAction {
901 bool HasEmittedPreamblePCH;
904 explicit PrecompilePreambleAction(ASTUnit &Unit)
905 : Unit(Unit), HasEmittedPreamblePCH(false) {}
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(); }
913 bool hasCodeCompletionSupport() const override { return false; }
914 bool hasASTFileSupport() const override { return false; }
915 TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; }
918 class PrecompilePreambleConsumer : public PCHGenerator {
921 std::vector<Decl *> TopLevelDecls;
922 PrecompilePreambleAction *Action;
926 PrecompilePreambleConsumer(ASTUnit &Unit, PrecompilePreambleAction *Action,
927 const Preprocessor &PP, StringRef isysroot,
929 : PCHGenerator(PP, "", nullptr, isysroot, std::make_shared<PCHBuffer>(),
930 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>>(),
931 /*AllowASTWithErrors=*/true),
932 Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()), Action(Action),
937 bool HandleTopLevelDecl(DeclGroupRef DG) override {
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))
945 AddTopLevelDeclarationToHash(D, Hash);
946 TopLevelDecls.push_back(D);
951 void HandleTranslationUnit(ASTContext &Ctx) override {
952 PCHGenerator::HandleTranslationUnit(Ctx);
953 if (hasEmittedPCH()) {
954 // Write the generated bitstream to "Out".
956 // Make sure it hits disk now.
959 llvm::SmallVector<char, 0> Empty;
960 getPCH() = std::move(Empty);
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())
970 Unit.addTopLevelDeclFromPreamble(getWriter().getDeclID(D));
973 Action->setHasEmittedPreamblePCH();
978 } // anonymous namespace
980 std::unique_ptr<ASTConsumer>
981 PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
984 std::string OutputFile;
985 raw_ostream *OS = GeneratePCHAction::ComputeASTConsumerArguments(
986 CI, InFile, Sysroot, OutputFile);
990 if (!CI.getFrontendOpts().RelocatablePCH)
993 CI.getPreprocessor().addPPCallbacks(
994 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
995 Unit.getCurrentTopLevelHashValue()));
996 return llvm::make_unique<PrecompilePreambleConsumer>(
997 Unit, this, CI.getPreprocessor(), Sysroot, OS);
1000 static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
1001 return StoredDiag.getLocation().isValid();
1005 checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
1006 // Get rid of stored diagnostics except the ones from the driver which do not
1007 // have a source location.
1009 std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
1013 static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
1015 SourceManager &SM) {
1016 // The stored diagnostic has the old source manager in it; update
1017 // the locations to refer into the new source manager. Since we've
1018 // been careful to make sure that the source manager's state
1019 // before and after are identical, so that we can reuse the source
1021 for (StoredDiagnostic &SD : StoredDiagnostics) {
1022 if (SD.getLocation().isValid()) {
1023 FullSourceLoc Loc(SD.getLocation(), SM);
1024 SD.setLocation(Loc);
1029 /// Parse the source file into a translation unit using the given compiler
1030 /// invocation, replacing the current translation unit.
1032 /// \returns True if a failure occurred that causes the ASTUnit not to
1033 /// contain any translation-unit information, false otherwise.
1034 bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1035 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer) {
1036 SavedMainFileBuffer.reset();
1041 // Create the compiler instance to use for building the AST.
1042 std::unique_ptr<CompilerInstance> Clang(
1043 new CompilerInstance(PCHContainerOps));
1045 // Recover resources if we crash before exiting this method.
1046 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1047 CICleanup(Clang.get());
1049 IntrusiveRefCntPtr<CompilerInvocation>
1050 CCInvocation(new CompilerInvocation(*Invocation));
1052 Clang->setInvocation(CCInvocation.get());
1053 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1055 // Set up diagnostics, capturing any diagnostics that would
1056 // otherwise be dropped.
1057 Clang->setDiagnostics(&getDiagnostics());
1059 // Create the target instance.
1060 Clang->setTarget(TargetInfo::CreateTargetInfo(
1061 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1062 if (!Clang->hasTarget())
1065 // Inform the target of the language options.
1067 // FIXME: We shouldn't need to do this, the target should be immutable once
1068 // created. This complexity should be lifted elsewhere.
1069 Clang->getTarget().adjust(Clang->getLangOpts());
1071 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1072 "Invocation must have exactly one source file!");
1073 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1074 "FIXME: AST inputs not yet supported here!");
1075 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1076 "IR inputs not support here!");
1078 // Configure the various subsystems.
1079 LangOpts = Clang->getInvocation().LangOpts;
1080 FileSystemOpts = Clang->getFileSystemOpts();
1082 Clang->createFileManager();
1083 FileMgr = &Clang->getFileManager();
1085 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1086 UserFilesAreVolatile);
1092 // Clear out old caches and data.
1093 TopLevelDecls.clear();
1094 clearFileLevelDecls();
1095 CleanTemporaryFiles();
1097 if (!OverrideMainBuffer) {
1098 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1099 TopLevelDeclsInPreamble.clear();
1102 // Create a file manager object to provide access to and cache the filesystem.
1103 Clang->setFileManager(&getFileManager());
1105 // Create the source manager.
1106 Clang->setSourceManager(&getSourceManager());
1108 // If the main file has been overridden due to the use of a preamble,
1109 // make that override happen and introduce the preamble.
1110 PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
1111 if (OverrideMainBuffer) {
1112 PreprocessorOpts.addRemappedFile(OriginalSourceFile,
1113 OverrideMainBuffer.get());
1114 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
1115 PreprocessorOpts.PrecompiledPreambleBytes.second
1116 = PreambleEndsAtStartOfLine;
1117 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
1118 PreprocessorOpts.DisablePCHValidation = true;
1120 // The stored diagnostic has the old source manager in it; update
1121 // the locations to refer into the new source manager. Since we've
1122 // been careful to make sure that the source manager's state
1123 // before and after are identical, so that we can reuse the source
1125 checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1127 // Keep track of the override buffer;
1128 SavedMainFileBuffer = std::move(OverrideMainBuffer);
1131 std::unique_ptr<TopLevelDeclTrackerAction> Act(
1132 new TopLevelDeclTrackerAction(*this));
1134 // Recover resources if we crash before exiting this method.
1135 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1136 ActCleanup(Act.get());
1138 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1141 if (SavedMainFileBuffer) {
1142 std::string ModName = getPreambleFile(this);
1143 TranslateStoredDiagnostics(getFileManager(), getSourceManager(),
1144 PreambleDiagnostics, StoredDiagnostics);
1147 if (!Act->Execute())
1150 transferASTDataFromCompilerInstance(*Clang);
1152 Act->EndSourceFile();
1154 FailedParseDiagnostics.clear();
1159 // Remove the overridden buffer we used for the preamble.
1160 SavedMainFileBuffer = nullptr;
1162 // Keep the ownership of the data in the ASTUnit because the client may
1163 // want to see the diagnostics.
1164 transferASTDataFromCompilerInstance(*Clang);
1165 FailedParseDiagnostics.swap(StoredDiagnostics);
1166 StoredDiagnostics.clear();
1167 NumStoredDiagnosticsFromDriver = 0;
1171 /// \brief Simple function to retrieve a path for a preamble precompiled header.
1172 static std::string GetPreamblePCHPath() {
1173 // FIXME: This is a hack so that we can override the preamble file during
1174 // crash-recovery testing, which is the only case where the preamble files
1175 // are not necessarily cleaned up.
1176 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1180 SmallString<128> Path;
1181 llvm::sys::fs::createTemporaryFile("preamble", "pch", Path);
1186 /// \brief Compute the preamble for the main file, providing the source buffer
1187 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1188 /// that describes the preamble.
1189 ASTUnit::ComputedPreamble
1190 ASTUnit::ComputePreamble(CompilerInvocation &Invocation, unsigned MaxLines) {
1191 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1192 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1194 // Try to determine if the main file has been remapped, either from the
1195 // command line (to another file) or directly through the compiler invocation
1196 // (to a memory buffer).
1197 llvm::MemoryBuffer *Buffer = nullptr;
1198 std::unique_ptr<llvm::MemoryBuffer> BufferOwner;
1199 std::string MainFilePath(FrontendOpts.Inputs[0].getFile());
1200 llvm::sys::fs::UniqueID MainFileID;
1201 if (!llvm::sys::fs::getUniqueID(MainFilePath, MainFileID)) {
1202 // Check whether there is a file-file remapping of the main file
1203 for (const auto &RF : PreprocessorOpts.RemappedFiles) {
1204 std::string MPath(RF.first);
1205 llvm::sys::fs::UniqueID MID;
1206 if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1207 if (MainFileID == MID) {
1208 // We found a remapping. Try to load the resulting, remapped source.
1209 BufferOwner = getBufferForFile(RF.second);
1211 return ComputedPreamble(nullptr, nullptr, 0, true);
1216 // Check whether there is a file-buffer remapping. It supercedes the
1217 // file-file remapping.
1218 for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
1219 std::string MPath(RB.first);
1220 llvm::sys::fs::UniqueID MID;
1221 if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1222 if (MainFileID == MID) {
1223 // We found a remapping.
1224 BufferOwner.reset();
1225 Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
1231 // If the main source file was not remapped, load it now.
1232 if (!Buffer && !BufferOwner) {
1233 BufferOwner = getBufferForFile(FrontendOpts.Inputs[0].getFile());
1235 return ComputedPreamble(nullptr, nullptr, 0, true);
1239 Buffer = BufferOwner.get();
1240 auto Pre = Lexer::ComputePreamble(Buffer->getBuffer(),
1241 *Invocation.getLangOpts(), MaxLines);
1242 return ComputedPreamble(Buffer, std::move(BufferOwner), Pre.first,
1246 ASTUnit::PreambleFileHash
1247 ASTUnit::PreambleFileHash::createForFile(off_t Size, time_t ModTime) {
1248 PreambleFileHash Result;
1250 Result.ModTime = ModTime;
1251 memset(Result.MD5, 0, sizeof(Result.MD5));
1255 ASTUnit::PreambleFileHash ASTUnit::PreambleFileHash::createForMemoryBuffer(
1256 const llvm::MemoryBuffer *Buffer) {
1257 PreambleFileHash Result;
1258 Result.Size = Buffer->getBufferSize();
1262 MD5Ctx.update(Buffer->getBuffer().data());
1263 MD5Ctx.final(Result.MD5);
1269 bool operator==(const ASTUnit::PreambleFileHash &LHS,
1270 const ASTUnit::PreambleFileHash &RHS) {
1271 return LHS.Size == RHS.Size && LHS.ModTime == RHS.ModTime &&
1272 memcmp(LHS.MD5, RHS.MD5, sizeof(LHS.MD5)) == 0;
1274 } // namespace clang
1276 static std::pair<unsigned, unsigned>
1277 makeStandaloneRange(CharSourceRange Range, const SourceManager &SM,
1278 const LangOptions &LangOpts) {
1279 CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts);
1280 unsigned Offset = SM.getFileOffset(FileRange.getBegin());
1281 unsigned EndOffset = SM.getFileOffset(FileRange.getEnd());
1282 return std::make_pair(Offset, EndOffset);
1285 static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM,
1286 const LangOptions &LangOpts,
1287 const FixItHint &InFix) {
1288 ASTUnit::StandaloneFixIt OutFix;
1289 OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts);
1290 OutFix.InsertFromRange = makeStandaloneRange(InFix.InsertFromRange, SM,
1292 OutFix.CodeToInsert = InFix.CodeToInsert;
1293 OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions;
1297 static ASTUnit::StandaloneDiagnostic
1298 makeStandaloneDiagnostic(const LangOptions &LangOpts,
1299 const StoredDiagnostic &InDiag) {
1300 ASTUnit::StandaloneDiagnostic OutDiag;
1301 OutDiag.ID = InDiag.getID();
1302 OutDiag.Level = InDiag.getLevel();
1303 OutDiag.Message = InDiag.getMessage();
1304 OutDiag.LocOffset = 0;
1305 if (InDiag.getLocation().isInvalid())
1307 const SourceManager &SM = InDiag.getLocation().getManager();
1308 SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation());
1309 OutDiag.Filename = SM.getFilename(FileLoc);
1310 if (OutDiag.Filename.empty())
1312 OutDiag.LocOffset = SM.getFileOffset(FileLoc);
1313 for (const CharSourceRange &Range : InDiag.getRanges())
1314 OutDiag.Ranges.push_back(makeStandaloneRange(Range, SM, LangOpts));
1315 for (const FixItHint &FixIt : InDiag.getFixIts())
1316 OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, FixIt));
1321 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1322 /// the source file.
1324 /// This routine will compute the preamble of the main source file. If a
1325 /// non-trivial preamble is found, it will precompile that preamble into a
1326 /// precompiled header so that the precompiled preamble can be used to reduce
1327 /// reparsing time. If a precompiled preamble has already been constructed,
1328 /// this routine will determine if it is still valid and, if so, avoid
1329 /// rebuilding the precompiled preamble.
1331 /// \param AllowRebuild When true (the default), this routine is
1332 /// allowed to rebuild the precompiled preamble if it is found to be
1335 /// \param MaxLines When non-zero, the maximum number of lines that
1336 /// can occur within the preamble.
1338 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1339 /// buffer that should be used in place of the main file when doing so.
1340 /// Otherwise, returns a NULL pointer.
1341 std::unique_ptr<llvm::MemoryBuffer>
1342 ASTUnit::getMainBufferWithPrecompiledPreamble(
1343 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1344 const CompilerInvocation &PreambleInvocationIn, bool AllowRebuild,
1345 unsigned MaxLines) {
1347 IntrusiveRefCntPtr<CompilerInvocation>
1348 PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
1349 FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
1350 PreprocessorOptions &PreprocessorOpts
1351 = PreambleInvocation->getPreprocessorOpts();
1353 ComputedPreamble NewPreamble = ComputePreamble(*PreambleInvocation, MaxLines);
1355 if (!NewPreamble.Size) {
1356 // We couldn't find a preamble in the main source. Clear out the current
1357 // preamble, if we have one. It's obviously no good any more.
1359 erasePreambleFile(this);
1361 // The next time we actually see a preamble, precompile it.
1362 PreambleRebuildCounter = 1;
1366 if (!Preamble.empty()) {
1367 // We've previously computed a preamble. Check whether we have the same
1368 // preamble now that we did before, and that there's enough space in
1369 // the main-file buffer within the precompiled preamble to fit the
1371 if (Preamble.size() == NewPreamble.Size &&
1372 PreambleEndsAtStartOfLine == NewPreamble.PreambleEndsAtStartOfLine &&
1373 memcmp(Preamble.getBufferStart(), NewPreamble.Buffer->getBufferStart(),
1374 NewPreamble.Size) == 0) {
1375 // The preamble has not changed. We may be able to re-use the precompiled
1378 // Check that none of the files used by the preamble have changed.
1379 bool AnyFileChanged = false;
1381 // First, make a record of those files that have been overridden via
1382 // remapping or unsaved_files.
1383 llvm::StringMap<PreambleFileHash> OverriddenFiles;
1384 for (const auto &R : PreprocessorOpts.RemappedFiles) {
1389 if (FileMgr->getNoncachedStatValue(R.second, Status)) {
1390 // If we can't stat the file we're remapping to, assume that something
1391 // horrible happened.
1392 AnyFileChanged = true;
1396 OverriddenFiles[R.first] = PreambleFileHash::createForFile(
1397 Status.getSize(), Status.getLastModificationTime().toEpochTime());
1400 for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
1403 OverriddenFiles[RB.first] =
1404 PreambleFileHash::createForMemoryBuffer(RB.second);
1407 // Check whether anything has changed.
1408 for (llvm::StringMap<PreambleFileHash>::iterator
1409 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1410 !AnyFileChanged && F != FEnd;
1412 llvm::StringMap<PreambleFileHash>::iterator Overridden
1413 = OverriddenFiles.find(F->first());
1414 if (Overridden != OverriddenFiles.end()) {
1415 // This file was remapped; check whether the newly-mapped file
1416 // matches up with the previous mapping.
1417 if (Overridden->second != F->second)
1418 AnyFileChanged = true;
1422 // The file was not remapped; check whether it has changed on disk.
1424 if (FileMgr->getNoncachedStatValue(F->first(), Status)) {
1425 // If we can't stat the file, assume that something horrible happened.
1426 AnyFileChanged = true;
1427 } else if (Status.getSize() != uint64_t(F->second.Size) ||
1428 Status.getLastModificationTime().toEpochTime() !=
1429 uint64_t(F->second.ModTime))
1430 AnyFileChanged = true;
1433 if (!AnyFileChanged) {
1434 // Okay! We can re-use the precompiled preamble.
1436 // Set the state of the diagnostic object to mimic its state
1437 // after parsing the preamble.
1438 getDiagnostics().Reset();
1439 ProcessWarningOptions(getDiagnostics(),
1440 PreambleInvocation->getDiagnosticOpts());
1441 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1443 return llvm::MemoryBuffer::getMemBufferCopy(
1444 NewPreamble.Buffer->getBuffer(), FrontendOpts.Inputs[0].getFile());
1448 // If we aren't allowed to rebuild the precompiled preamble, just
1453 // We can't reuse the previously-computed preamble. Build a new one.
1455 PreambleDiagnostics.clear();
1456 erasePreambleFile(this);
1457 PreambleRebuildCounter = 1;
1458 } else if (!AllowRebuild) {
1459 // We aren't allowed to rebuild the precompiled preamble; just
1464 // If the preamble rebuild counter > 1, it's because we previously
1465 // failed to build a preamble and we're not yet ready to try
1466 // again. Decrement the counter and return a failure.
1467 if (PreambleRebuildCounter > 1) {
1468 --PreambleRebuildCounter;
1472 // Create a temporary file for the precompiled preamble. In rare
1473 // circumstances, this can fail.
1474 std::string PreamblePCHPath = GetPreamblePCHPath();
1475 if (PreamblePCHPath.empty()) {
1476 // Try again next time.
1477 PreambleRebuildCounter = 1;
1481 // We did not previously compute a preamble, or it can't be reused anyway.
1482 SimpleTimer PreambleTimer(WantTiming);
1483 PreambleTimer.setOutput("Precompiling preamble");
1485 // Save the preamble text for later; we'll need to compare against it for
1486 // subsequent reparses.
1487 StringRef MainFilename = FrontendOpts.Inputs[0].getFile();
1488 Preamble.assign(FileMgr->getFile(MainFilename),
1489 NewPreamble.Buffer->getBufferStart(),
1490 NewPreamble.Buffer->getBufferStart() + NewPreamble.Size);
1491 PreambleEndsAtStartOfLine = NewPreamble.PreambleEndsAtStartOfLine;
1493 PreambleBuffer = llvm::MemoryBuffer::getMemBufferCopy(
1494 NewPreamble.Buffer->getBuffer().slice(0, Preamble.size()), MainFilename);
1496 // Remap the main source file to the preamble buffer.
1497 StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
1498 PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer.get());
1500 // Tell the compiler invocation to generate a temporary precompiled header.
1501 FrontendOpts.ProgramAction = frontend::GeneratePCH;
1502 // FIXME: Generate the precompiled header into memory?
1503 FrontendOpts.OutputFile = PreamblePCHPath;
1504 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1505 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1507 // Create the compiler instance to use for building the precompiled preamble.
1508 std::unique_ptr<CompilerInstance> Clang(
1509 new CompilerInstance(PCHContainerOps));
1511 // Recover resources if we crash before exiting this method.
1512 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1513 CICleanup(Clang.get());
1515 Clang->setInvocation(&*PreambleInvocation);
1516 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1518 // Set up diagnostics, capturing all of the diagnostics produced.
1519 Clang->setDiagnostics(&getDiagnostics());
1521 // Create the target instance.
1522 Clang->setTarget(TargetInfo::CreateTargetInfo(
1523 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1524 if (!Clang->hasTarget()) {
1525 llvm::sys::fs::remove(FrontendOpts.OutputFile);
1527 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1528 PreprocessorOpts.RemappedFileBuffers.pop_back();
1532 // Inform the target of the language options.
1534 // FIXME: We shouldn't need to do this, the target should be immutable once
1535 // created. This complexity should be lifted elsewhere.
1536 Clang->getTarget().adjust(Clang->getLangOpts());
1538 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1539 "Invocation must have exactly one source file!");
1540 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1541 "FIXME: AST inputs not yet supported here!");
1542 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1543 "IR inputs not support here!");
1545 // Clear out old caches and data.
1546 getDiagnostics().Reset();
1547 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1548 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1549 TopLevelDecls.clear();
1550 TopLevelDeclsInPreamble.clear();
1551 PreambleDiagnostics.clear();
1553 IntrusiveRefCntPtr<vfs::FileSystem> VFS =
1554 createVFSFromCompilerInvocation(Clang->getInvocation(), getDiagnostics());
1558 // Create a file manager object to provide access to and cache the filesystem.
1559 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
1561 // Create the source manager.
1562 Clang->setSourceManager(new SourceManager(getDiagnostics(),
1563 Clang->getFileManager()));
1565 auto PreambleDepCollector = std::make_shared<DependencyCollector>();
1566 Clang->addDependencyCollector(PreambleDepCollector);
1568 std::unique_ptr<PrecompilePreambleAction> Act;
1569 Act.reset(new PrecompilePreambleAction(*this));
1570 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1571 llvm::sys::fs::remove(FrontendOpts.OutputFile);
1573 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1574 PreprocessorOpts.RemappedFileBuffers.pop_back();
1580 // Transfer any diagnostics generated when parsing the preamble into the set
1581 // of preamble diagnostics.
1582 for (stored_diag_iterator I = stored_diag_afterDriver_begin(),
1583 E = stored_diag_end();
1585 PreambleDiagnostics.push_back(
1586 makeStandaloneDiagnostic(Clang->getLangOpts(), *I));
1588 Act->EndSourceFile();
1590 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1592 if (!Act->hasEmittedPreamblePCH()) {
1593 // The preamble PCH failed (e.g. there was a module loading fatal error),
1594 // so no precompiled header was generated. Forget that we even tried.
1595 // FIXME: Should we leave a note for ourselves to try again?
1596 llvm::sys::fs::remove(FrontendOpts.OutputFile);
1598 TopLevelDeclsInPreamble.clear();
1599 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1600 PreprocessorOpts.RemappedFileBuffers.pop_back();
1604 // Keep track of the preamble we precompiled.
1605 setPreambleFile(this, FrontendOpts.OutputFile);
1606 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1608 // Keep track of all of the files that the source manager knows about,
1609 // so we can verify whether they have changed or not.
1610 FilesInPreamble.clear();
1611 SourceManager &SourceMgr = Clang->getSourceManager();
1612 for (auto &Filename : PreambleDepCollector->getDependencies()) {
1613 const FileEntry *File = Clang->getFileManager().getFile(Filename);
1614 if (!File || File == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))
1616 if (time_t ModTime = File->getModificationTime()) {
1617 FilesInPreamble[File->getName()] = PreambleFileHash::createForFile(
1618 File->getSize(), ModTime);
1620 llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File);
1621 FilesInPreamble[File->getName()] =
1622 PreambleFileHash::createForMemoryBuffer(Buffer);
1626 PreambleRebuildCounter = 1;
1627 PreprocessorOpts.RemappedFileBuffers.pop_back();
1629 // If the hash of top-level entities differs from the hash of the top-level
1630 // entities the last time we rebuilt the preamble, clear out the completion
1632 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1633 CompletionCacheTopLevelHashValue = 0;
1634 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1637 return llvm::MemoryBuffer::getMemBufferCopy(NewPreamble.Buffer->getBuffer(),
1641 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1642 std::vector<Decl *> Resolved;
1643 Resolved.reserve(TopLevelDeclsInPreamble.size());
1644 ExternalASTSource &Source = *getASTContext().getExternalSource();
1645 for (serialization::DeclID TopLevelDecl : TopLevelDeclsInPreamble) {
1646 // Resolve the declaration ID to an actual declaration, possibly
1647 // deserializing the declaration in the process.
1648 if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1649 Resolved.push_back(D);
1651 TopLevelDeclsInPreamble.clear();
1652 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1655 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1656 // Steal the created target, context, and preprocessor if they have been
1658 assert(CI.hasInvocation() && "missing invocation");
1659 LangOpts = CI.getInvocation().LangOpts;
1660 TheSema = CI.takeSema();
1661 Consumer = CI.takeASTConsumer();
1662 if (CI.hasASTContext())
1663 Ctx = &CI.getASTContext();
1664 if (CI.hasPreprocessor())
1665 PP = &CI.getPreprocessor();
1666 CI.setSourceManager(nullptr);
1667 CI.setFileManager(nullptr);
1669 Target = &CI.getTarget();
1670 Reader = CI.getModuleManager();
1671 HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1674 StringRef ASTUnit::getMainFileName() const {
1675 if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1676 const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1678 return Input.getFile();
1680 return Input.getBuffer()->getBufferIdentifier();
1684 if (const FileEntry *
1685 FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1686 return FE->getName();
1692 StringRef ASTUnit::getASTFileName() const {
1693 if (!isMainFileAST())
1696 serialization::ModuleFile &
1697 Mod = Reader->getModuleManager().getPrimaryModule();
1698 return Mod.FileName;
1701 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1702 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1703 bool CaptureDiagnostics,
1704 bool UserFilesAreVolatile) {
1705 std::unique_ptr<ASTUnit> AST;
1706 AST.reset(new ASTUnit(false));
1707 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1708 AST->Diagnostics = Diags;
1709 AST->Invocation = CI;
1710 AST->FileSystemOpts = CI->getFileSystemOpts();
1711 IntrusiveRefCntPtr<vfs::FileSystem> VFS =
1712 createVFSFromCompilerInvocation(*CI, *Diags);
1715 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1716 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1717 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1718 UserFilesAreVolatile);
1720 return AST.release();
1723 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
1724 CompilerInvocation *CI,
1725 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1726 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, ASTFrontendAction *Action,
1727 ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
1728 bool OnlyLocalDecls, bool CaptureDiagnostics,
1729 unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults,
1730 bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
1731 std::unique_ptr<ASTUnit> *ErrAST) {
1732 assert(CI && "A CompilerInvocation is required");
1734 std::unique_ptr<ASTUnit> OwnAST;
1735 ASTUnit *AST = Unit;
1737 // Create the AST unit.
1738 OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile));
1744 if (!ResourceFilesPath.empty()) {
1745 // Override the resources path.
1746 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1748 AST->OnlyLocalDecls = OnlyLocalDecls;
1749 AST->CaptureDiagnostics = CaptureDiagnostics;
1750 if (PrecompilePreambleAfterNParses > 0)
1751 AST->PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1752 AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1753 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1754 AST->IncludeBriefCommentsInCodeCompletion
1755 = IncludeBriefCommentsInCodeCompletion;
1757 // Recover resources if we crash before exiting this method.
1758 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1759 ASTUnitCleanup(OwnAST.get());
1760 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1761 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1762 DiagCleanup(Diags.get());
1764 // We'll manage file buffers ourselves.
1765 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1766 CI->getFrontendOpts().DisableFree = false;
1767 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1769 // Create the compiler instance to use for building the AST.
1770 std::unique_ptr<CompilerInstance> Clang(
1771 new CompilerInstance(PCHContainerOps));
1773 // Recover resources if we crash before exiting this method.
1774 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1775 CICleanup(Clang.get());
1777 Clang->setInvocation(CI);
1778 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1780 // Set up diagnostics, capturing any diagnostics that would
1781 // otherwise be dropped.
1782 Clang->setDiagnostics(&AST->getDiagnostics());
1784 // Create the target instance.
1785 Clang->setTarget(TargetInfo::CreateTargetInfo(
1786 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1787 if (!Clang->hasTarget())
1790 // Inform the target of the language options.
1792 // FIXME: We shouldn't need to do this, the target should be immutable once
1793 // created. This complexity should be lifted elsewhere.
1794 Clang->getTarget().adjust(Clang->getLangOpts());
1796 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1797 "Invocation must have exactly one source file!");
1798 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1799 "FIXME: AST inputs not yet supported here!");
1800 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1801 "IR inputs not supported here!");
1803 // Configure the various subsystems.
1804 AST->TheSema.reset();
1807 AST->Reader = nullptr;
1809 // Create a file manager object to provide access to and cache the filesystem.
1810 Clang->setFileManager(&AST->getFileManager());
1812 // Create the source manager.
1813 Clang->setSourceManager(&AST->getSourceManager());
1815 ASTFrontendAction *Act = Action;
1817 std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
1819 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1820 Act = TrackerAct.get();
1823 // Recover resources if we crash before exiting this method.
1824 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1825 ActCleanup(TrackerAct.get());
1827 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1828 AST->transferASTDataFromCompilerInstance(*Clang);
1829 if (OwnAST && ErrAST)
1830 ErrAST->swap(OwnAST);
1835 if (Persistent && !TrackerAct) {
1836 Clang->getPreprocessor().addPPCallbacks(
1837 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
1838 AST->getCurrentTopLevelHashValue()));
1839 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
1840 if (Clang->hasASTConsumer())
1841 Consumers.push_back(Clang->takeASTConsumer());
1842 Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>(
1843 *AST, AST->getCurrentTopLevelHashValue()));
1844 Clang->setASTConsumer(
1845 llvm::make_unique<MultiplexConsumer>(std::move(Consumers)));
1847 if (!Act->Execute()) {
1848 AST->transferASTDataFromCompilerInstance(*Clang);
1849 if (OwnAST && ErrAST)
1850 ErrAST->swap(OwnAST);
1855 // Steal the created target, context, and preprocessor.
1856 AST->transferASTDataFromCompilerInstance(*Clang);
1858 Act->EndSourceFile();
1861 return OwnAST.release();
1866 bool ASTUnit::LoadFromCompilerInvocation(
1867 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1868 unsigned PrecompilePreambleAfterNParses) {
1872 // We'll manage file buffers ourselves.
1873 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1874 Invocation->getFrontendOpts().DisableFree = false;
1875 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1877 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1878 if (PrecompilePreambleAfterNParses > 0) {
1879 PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1880 OverrideMainBuffer =
1881 getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation);
1884 SimpleTimer ParsingTimer(WantTiming);
1885 ParsingTimer.setOutput("Parsing " + getMainFileName());
1887 // Recover resources if we crash before exiting this method.
1888 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1889 MemBufferCleanup(OverrideMainBuffer.get());
1891 return Parse(PCHContainerOps, std::move(OverrideMainBuffer));
1894 std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
1895 CompilerInvocation *CI,
1896 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1897 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
1898 bool OnlyLocalDecls, bool CaptureDiagnostics,
1899 unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1900 bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1901 bool UserFilesAreVolatile) {
1902 // Create the AST unit.
1903 std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1904 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1905 AST->Diagnostics = Diags;
1906 AST->OnlyLocalDecls = OnlyLocalDecls;
1907 AST->CaptureDiagnostics = CaptureDiagnostics;
1908 AST->TUKind = TUKind;
1909 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1910 AST->IncludeBriefCommentsInCodeCompletion
1911 = IncludeBriefCommentsInCodeCompletion;
1912 AST->Invocation = CI;
1913 AST->FileSystemOpts = FileMgr->getFileSystemOpts();
1914 AST->FileMgr = FileMgr;
1915 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1917 // Recover resources if we crash before exiting this method.
1918 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1919 ASTUnitCleanup(AST.get());
1920 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1921 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1922 DiagCleanup(Diags.get());
1924 if (AST->LoadFromCompilerInvocation(PCHContainerOps,
1925 PrecompilePreambleAfterNParses))
1930 ASTUnit *ASTUnit::LoadFromCommandLine(
1931 const char **ArgBegin, const char **ArgEnd,
1932 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1933 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
1934 bool OnlyLocalDecls, bool CaptureDiagnostics,
1935 ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
1936 unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1937 bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1938 bool AllowPCHWithCompilerErrors, bool SkipFunctionBodies,
1939 bool UserFilesAreVolatile, bool ForSerialization,
1940 llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST) {
1941 assert(Diags.get() && "no DiagnosticsEngine was provided");
1943 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1945 IntrusiveRefCntPtr<CompilerInvocation> CI;
1949 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1952 CI = clang::createInvocationFromCommandLine(
1953 llvm::makeArrayRef(ArgBegin, ArgEnd),
1959 // Override any files that need remapping
1960 for (const auto &RemappedFile : RemappedFiles) {
1961 CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1962 RemappedFile.second);
1964 PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1965 PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1966 PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1968 // Override the resources path.
1969 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1971 CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
1974 CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue();
1976 // Create the AST unit.
1977 std::unique_ptr<ASTUnit> AST;
1978 AST.reset(new ASTUnit(false));
1979 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1980 AST->Diagnostics = Diags;
1981 AST->FileSystemOpts = CI->getFileSystemOpts();
1982 IntrusiveRefCntPtr<vfs::FileSystem> VFS =
1983 createVFSFromCompilerInvocation(*CI, *Diags);
1986 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1987 AST->OnlyLocalDecls = OnlyLocalDecls;
1988 AST->CaptureDiagnostics = CaptureDiagnostics;
1989 AST->TUKind = TUKind;
1990 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1991 AST->IncludeBriefCommentsInCodeCompletion
1992 = IncludeBriefCommentsInCodeCompletion;
1993 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1994 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1995 AST->StoredDiagnostics.swap(StoredDiagnostics);
1996 AST->Invocation = CI;
1997 if (ForSerialization)
1998 AST->WriterData.reset(new ASTWriterData());
1999 // Zero out now to ease cleanup during crash recovery.
2003 // Recover resources if we crash before exiting this method.
2004 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
2005 ASTUnitCleanup(AST.get());
2007 if (AST->LoadFromCompilerInvocation(PCHContainerOps,
2008 PrecompilePreambleAfterNParses)) {
2009 // Some error occurred, if caller wants to examine diagnostics, pass it the
2012 AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
2018 return AST.release();
2021 bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2022 ArrayRef<RemappedFile> RemappedFiles) {
2026 clearFileLevelDecls();
2028 SimpleTimer ParsingTimer(WantTiming);
2029 ParsingTimer.setOutput("Reparsing " + getMainFileName());
2032 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
2033 for (const auto &RB : PPOpts.RemappedFileBuffers)
2036 Invocation->getPreprocessorOpts().clearRemappedFiles();
2037 for (const auto &RemappedFile : RemappedFiles) {
2038 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
2039 RemappedFile.second);
2042 // If we have a preamble file lying around, or if we might try to
2043 // build a precompiled preamble, do so now.
2044 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2045 if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
2046 OverrideMainBuffer =
2047 getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation);
2049 // Clear out the diagnostics state.
2051 getDiagnostics().Reset();
2052 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
2053 if (OverrideMainBuffer)
2054 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
2056 // Parse the sources
2057 bool Result = Parse(PCHContainerOps, std::move(OverrideMainBuffer));
2059 // If we're caching global code-completion results, and the top-level
2060 // declarations have changed, clear out the code-completion cache.
2061 if (!Result && ShouldCacheCodeCompletionResults &&
2062 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
2063 CacheCodeCompletionResults();
2065 // We now need to clear out the completion info related to this translation
2066 // unit; it'll be recreated if necessary.
2072 //----------------------------------------------------------------------------//
2074 //----------------------------------------------------------------------------//
2077 /// \brief Code completion consumer that combines the cached code-completion
2078 /// results from an ASTUnit with the code-completion results provided to it,
2079 /// then passes the result on to
2080 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
2081 uint64_t NormalContexts;
2083 CodeCompleteConsumer &Next;
2086 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
2087 const CodeCompleteOptions &CodeCompleteOpts)
2088 : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
2089 AST(AST), Next(Next)
2091 // Compute the set of contexts in which we will look when we don't have
2092 // any information about the specific context.
2094 = (1LL << CodeCompletionContext::CCC_TopLevel)
2095 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
2096 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
2097 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
2098 | (1LL << CodeCompletionContext::CCC_Statement)
2099 | (1LL << CodeCompletionContext::CCC_Expression)
2100 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
2101 | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
2102 | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
2103 | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
2104 | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
2105 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
2106 | (1LL << CodeCompletionContext::CCC_Recovery);
2108 if (AST.getASTContext().getLangOpts().CPlusPlus)
2109 NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
2110 | (1LL << CodeCompletionContext::CCC_UnionTag)
2111 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
2114 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
2115 CodeCompletionResult *Results,
2116 unsigned NumResults) override;
2118 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
2119 OverloadCandidate *Candidates,
2120 unsigned NumCandidates) override {
2121 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
2124 CodeCompletionAllocator &getAllocator() override {
2125 return Next.getAllocator();
2128 CodeCompletionTUInfo &getCodeCompletionTUInfo() override {
2129 return Next.getCodeCompletionTUInfo();
2132 } // anonymous namespace
2134 /// \brief Helper function that computes which global names are hidden by the
2135 /// local code-completion results.
2136 static void CalculateHiddenNames(const CodeCompletionContext &Context,
2137 CodeCompletionResult *Results,
2138 unsigned NumResults,
2140 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
2141 bool OnlyTagNames = false;
2142 switch (Context.getKind()) {
2143 case CodeCompletionContext::CCC_Recovery:
2144 case CodeCompletionContext::CCC_TopLevel:
2145 case CodeCompletionContext::CCC_ObjCInterface:
2146 case CodeCompletionContext::CCC_ObjCImplementation:
2147 case CodeCompletionContext::CCC_ObjCIvarList:
2148 case CodeCompletionContext::CCC_ClassStructUnion:
2149 case CodeCompletionContext::CCC_Statement:
2150 case CodeCompletionContext::CCC_Expression:
2151 case CodeCompletionContext::CCC_ObjCMessageReceiver:
2152 case CodeCompletionContext::CCC_DotMemberAccess:
2153 case CodeCompletionContext::CCC_ArrowMemberAccess:
2154 case CodeCompletionContext::CCC_ObjCPropertyAccess:
2155 case CodeCompletionContext::CCC_Namespace:
2156 case CodeCompletionContext::CCC_Type:
2157 case CodeCompletionContext::CCC_Name:
2158 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
2159 case CodeCompletionContext::CCC_ParenthesizedExpression:
2160 case CodeCompletionContext::CCC_ObjCInterfaceName:
2163 case CodeCompletionContext::CCC_EnumTag:
2164 case CodeCompletionContext::CCC_UnionTag:
2165 case CodeCompletionContext::CCC_ClassOrStructTag:
2166 OnlyTagNames = true;
2169 case CodeCompletionContext::CCC_ObjCProtocolName:
2170 case CodeCompletionContext::CCC_MacroName:
2171 case CodeCompletionContext::CCC_MacroNameUse:
2172 case CodeCompletionContext::CCC_PreprocessorExpression:
2173 case CodeCompletionContext::CCC_PreprocessorDirective:
2174 case CodeCompletionContext::CCC_NaturalLanguage:
2175 case CodeCompletionContext::CCC_SelectorName:
2176 case CodeCompletionContext::CCC_TypeQualifiers:
2177 case CodeCompletionContext::CCC_Other:
2178 case CodeCompletionContext::CCC_OtherWithMacros:
2179 case CodeCompletionContext::CCC_ObjCInstanceMessage:
2180 case CodeCompletionContext::CCC_ObjCClassMessage:
2181 case CodeCompletionContext::CCC_ObjCCategoryName:
2182 // We're looking for nothing, or we're looking for names that cannot
2187 typedef CodeCompletionResult Result;
2188 for (unsigned I = 0; I != NumResults; ++I) {
2189 if (Results[I].Kind != Result::RK_Declaration)
2193 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2195 bool Hiding = false;
2197 Hiding = (IDNS & Decl::IDNS_Tag);
2199 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2200 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2201 Decl::IDNS_NonMemberOperator);
2202 if (Ctx.getLangOpts().CPlusPlus)
2203 HiddenIDNS |= Decl::IDNS_Tag;
2204 Hiding = (IDNS & HiddenIDNS);
2210 DeclarationName Name = Results[I].Declaration->getDeclName();
2211 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2212 HiddenNames.insert(Identifier->getName());
2214 HiddenNames.insert(Name.getAsString());
2218 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2219 CodeCompletionContext Context,
2220 CodeCompletionResult *Results,
2221 unsigned NumResults) {
2222 // Merge the results we were given with the results we cached.
2223 bool AddedResult = false;
2224 uint64_t InContexts =
2225 Context.getKind() == CodeCompletionContext::CCC_Recovery
2226 ? NormalContexts : (1LL << Context.getKind());
2227 // Contains the set of names that are hidden by "local" completion results.
2228 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2229 typedef CodeCompletionResult Result;
2230 SmallVector<Result, 8> AllResults;
2231 for (ASTUnit::cached_completion_iterator
2232 C = AST.cached_completion_begin(),
2233 CEnd = AST.cached_completion_end();
2235 // If the context we are in matches any of the contexts we are
2236 // interested in, we'll add this result.
2237 if ((C->ShowInContexts & InContexts) == 0)
2240 // If we haven't added any results previously, do so now.
2242 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2244 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2248 // Determine whether this global completion result is hidden by a local
2249 // completion result. If so, skip it.
2250 if (C->Kind != CXCursor_MacroDefinition &&
2251 HiddenNames.count(C->Completion->getTypedText()))
2254 // Adjust priority based on similar type classes.
2255 unsigned Priority = C->Priority;
2256 CodeCompletionString *Completion = C->Completion;
2257 if (!Context.getPreferredType().isNull()) {
2258 if (C->Kind == CXCursor_MacroDefinition) {
2259 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2261 Context.getPreferredType()->isAnyPointerType());
2262 } else if (C->Type) {
2263 CanQualType Expected
2264 = S.Context.getCanonicalType(
2265 Context.getPreferredType().getUnqualifiedType());
2266 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2267 if (ExpectedSTC == C->TypeClass) {
2268 // We know this type is similar; check for an exact match.
2269 llvm::StringMap<unsigned> &CachedCompletionTypes
2270 = AST.getCachedCompletionTypes();
2271 llvm::StringMap<unsigned>::iterator Pos
2272 = CachedCompletionTypes.find(QualType(Expected).getAsString());
2273 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2274 Priority /= CCF_ExactTypeMatch;
2276 Priority /= CCF_SimilarTypeMatch;
2281 // Adjust the completion string, if required.
2282 if (C->Kind == CXCursor_MacroDefinition &&
2283 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2284 // Create a new code-completion string that just contains the
2285 // macro name, without its arguments.
2286 CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2287 CCP_CodePattern, C->Availability);
2288 Builder.AddTypedTextChunk(C->Completion->getTypedText());
2289 Priority = CCP_CodePattern;
2290 Completion = Builder.TakeString();
2293 AllResults.push_back(Result(Completion, Priority, C->Kind,
2297 // If we did not add any cached completion results, just forward the
2298 // results we were given to the next consumer.
2300 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2304 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2308 void ASTUnit::CodeComplete(
2309 StringRef File, unsigned Line, unsigned Column,
2310 ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
2311 bool IncludeCodePatterns, bool IncludeBriefComments,
2312 CodeCompleteConsumer &Consumer,
2313 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2314 DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr,
2315 FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2316 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2320 SimpleTimer CompletionTimer(WantTiming);
2321 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2322 Twine(Line) + ":" + Twine(Column));
2324 IntrusiveRefCntPtr<CompilerInvocation>
2325 CCInvocation(new CompilerInvocation(*Invocation));
2327 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2328 CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2329 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2331 CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2332 CachedCompletionResults.empty();
2333 CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2334 CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2335 CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2337 assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2339 FrontendOpts.CodeCompletionAt.FileName = File;
2340 FrontendOpts.CodeCompletionAt.Line = Line;
2341 FrontendOpts.CodeCompletionAt.Column = Column;
2343 // Set the language options appropriately.
2344 LangOpts = *CCInvocation->getLangOpts();
2346 // Spell-checking and warnings are wasteful during code-completion.
2347 LangOpts.SpellChecking = false;
2348 CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
2350 std::unique_ptr<CompilerInstance> Clang(
2351 new CompilerInstance(PCHContainerOps));
2353 // Recover resources if we crash before exiting this method.
2354 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2355 CICleanup(Clang.get());
2357 Clang->setInvocation(&*CCInvocation);
2358 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2360 // Set up diagnostics, capturing any diagnostics produced.
2361 Clang->setDiagnostics(&Diag);
2362 CaptureDroppedDiagnostics Capture(true,
2363 Clang->getDiagnostics(),
2365 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2367 // Create the target instance.
2368 Clang->setTarget(TargetInfo::CreateTargetInfo(
2369 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
2370 if (!Clang->hasTarget()) {
2371 Clang->setInvocation(nullptr);
2375 // Inform the target of the language options.
2377 // FIXME: We shouldn't need to do this, the target should be immutable once
2378 // created. This complexity should be lifted elsewhere.
2379 Clang->getTarget().adjust(Clang->getLangOpts());
2381 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2382 "Invocation must have exactly one source file!");
2383 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
2384 "FIXME: AST inputs not yet supported here!");
2385 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
2386 "IR inputs not support here!");
2389 // Use the source and file managers that we were given.
2390 Clang->setFileManager(&FileMgr);
2391 Clang->setSourceManager(&SourceMgr);
2394 PreprocessorOpts.clearRemappedFiles();
2395 PreprocessorOpts.RetainRemappedFileBuffers = true;
2396 for (const auto &RemappedFile : RemappedFiles) {
2397 PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second);
2398 OwnedBuffers.push_back(RemappedFile.second);
2401 // Use the code completion consumer we were given, but adding any cached
2402 // code-completion results.
2403 AugmentedCodeCompleteConsumer *AugmentedConsumer
2404 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2405 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2407 // If we have a precompiled preamble, try to use it. We only allow
2408 // the use of the precompiled preamble if we're if the completion
2409 // point is within the main file, after the end of the precompiled
2411 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2412 if (!getPreambleFile(this).empty()) {
2413 std::string CompleteFilePath(File);
2414 llvm::sys::fs::UniqueID CompleteFileID;
2416 if (!llvm::sys::fs::getUniqueID(CompleteFilePath, CompleteFileID)) {
2417 std::string MainPath(OriginalSourceFile);
2418 llvm::sys::fs::UniqueID MainID;
2419 if (!llvm::sys::fs::getUniqueID(MainPath, MainID)) {
2420 if (CompleteFileID == MainID && Line > 1)
2421 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2422 PCHContainerOps, *CCInvocation, false, Line - 1);
2427 // If the main file has been overridden due to the use of a preamble,
2428 // make that override happen and introduce the preamble.
2429 if (OverrideMainBuffer) {
2430 PreprocessorOpts.addRemappedFile(OriginalSourceFile,
2431 OverrideMainBuffer.get());
2432 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2433 PreprocessorOpts.PrecompiledPreambleBytes.second
2434 = PreambleEndsAtStartOfLine;
2435 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
2436 PreprocessorOpts.DisablePCHValidation = true;
2438 OwnedBuffers.push_back(OverrideMainBuffer.release());
2440 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2441 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2444 // Disable the preprocessing record if modules are not enabled.
2445 if (!Clang->getLangOpts().Modules)
2446 PreprocessorOpts.DetailedRecord = false;
2448 std::unique_ptr<SyntaxOnlyAction> Act;
2449 Act.reset(new SyntaxOnlyAction);
2450 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2452 Act->EndSourceFile();
2456 bool ASTUnit::Save(StringRef File) {
2457 if (HadModuleLoaderFatalFailure)
2460 // Write to a temporary file and later rename it to the actual file, to avoid
2461 // possible race conditions.
2462 SmallString<128> TempPath;
2464 TempPath += "-%%%%%%%%";
2466 if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath))
2469 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2470 // unconditionally create a stat cache when we parse the file?
2471 llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2475 if (Out.has_error()) {
2480 if (llvm::sys::fs::rename(TempPath, File)) {
2481 llvm::sys::fs::remove(TempPath);
2488 static bool serializeUnit(ASTWriter &Writer,
2489 SmallVectorImpl<char> &Buffer,
2493 Writer.WriteAST(S, std::string(), nullptr, "", hasErrors);
2495 // Write the generated bitstream to "Out".
2496 if (!Buffer.empty())
2497 OS.write(Buffer.data(), Buffer.size());
2502 bool ASTUnit::serialize(raw_ostream &OS) {
2503 bool hasErrors = getDiagnostics().hasErrorOccurred();
2506 return serializeUnit(WriterData->Writer, WriterData->Buffer,
2507 getSema(), hasErrors, OS);
2509 SmallString<128> Buffer;
2510 llvm::BitstreamWriter Stream(Buffer);
2511 ASTWriter Writer(Stream, { });
2512 return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2515 typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
2517 void ASTUnit::TranslateStoredDiagnostics(
2518 FileManager &FileMgr,
2519 SourceManager &SrcMgr,
2520 const SmallVectorImpl<StandaloneDiagnostic> &Diags,
2521 SmallVectorImpl<StoredDiagnostic> &Out) {
2522 // Map the standalone diagnostic into the new source manager. We also need to
2523 // remap all the locations to the new view. This includes the diag location,
2524 // any associated source ranges, and the source ranges of associated fix-its.
2525 // FIXME: There should be a cleaner way to do this.
2527 SmallVector<StoredDiagnostic, 4> Result;
2528 Result.reserve(Diags.size());
2529 for (const StandaloneDiagnostic &SD : Diags) {
2530 // Rebuild the StoredDiagnostic.
2531 if (SD.Filename.empty())
2533 const FileEntry *FE = FileMgr.getFile(SD.Filename);
2536 FileID FID = SrcMgr.translateFile(FE);
2537 SourceLocation FileLoc = SrcMgr.getLocForStartOfFile(FID);
2538 if (FileLoc.isInvalid())
2540 SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset);
2541 FullSourceLoc Loc(L, SrcMgr);
2543 SmallVector<CharSourceRange, 4> Ranges;
2544 Ranges.reserve(SD.Ranges.size());
2545 for (const auto &Range : SD.Ranges) {
2546 SourceLocation BL = FileLoc.getLocWithOffset(Range.first);
2547 SourceLocation EL = FileLoc.getLocWithOffset(Range.second);
2548 Ranges.push_back(CharSourceRange::getCharRange(BL, EL));
2551 SmallVector<FixItHint, 2> FixIts;
2552 FixIts.reserve(SD.FixIts.size());
2553 for (const StandaloneFixIt &FixIt : SD.FixIts) {
2554 FixIts.push_back(FixItHint());
2555 FixItHint &FH = FixIts.back();
2556 FH.CodeToInsert = FixIt.CodeToInsert;
2557 SourceLocation BL = FileLoc.getLocWithOffset(FixIt.RemoveRange.first);
2558 SourceLocation EL = FileLoc.getLocWithOffset(FixIt.RemoveRange.second);
2559 FH.RemoveRange = CharSourceRange::getCharRange(BL, EL);
2562 Result.push_back(StoredDiagnostic(SD.Level, SD.ID,
2563 SD.Message, Loc, Ranges, FixIts));
2568 void ASTUnit::addFileLevelDecl(Decl *D) {
2571 // We only care about local declarations.
2572 if (D->isFromASTFile())
2575 SourceManager &SM = *SourceMgr;
2576 SourceLocation Loc = D->getLocation();
2577 if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2580 // We only keep track of the file-level declarations of each file.
2581 if (!D->getLexicalDeclContext()->isFileContext())
2584 SourceLocation FileLoc = SM.getFileLoc(Loc);
2585 assert(SM.isLocalSourceLocation(FileLoc));
2588 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2589 if (FID.isInvalid())
2592 LocDeclsTy *&Decls = FileDecls[FID];
2594 Decls = new LocDeclsTy();
2596 std::pair<unsigned, Decl *> LocDecl(Offset, D);
2598 if (Decls->empty() || Decls->back().first <= Offset) {
2599 Decls->push_back(LocDecl);
2603 LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
2604 LocDecl, llvm::less_first());
2606 Decls->insert(I, LocDecl);
2609 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2610 SmallVectorImpl<Decl *> &Decls) {
2611 if (File.isInvalid())
2614 if (SourceMgr->isLoadedFileID(File)) {
2615 assert(Ctx->getExternalSource() && "No external source!");
2616 return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2620 FileDeclsTy::iterator I = FileDecls.find(File);
2621 if (I == FileDecls.end())
2624 LocDeclsTy &LocDecls = *I->second;
2625 if (LocDecls.empty())
2628 LocDeclsTy::iterator BeginIt =
2629 std::lower_bound(LocDecls.begin(), LocDecls.end(),
2630 std::make_pair(Offset, (Decl *)nullptr),
2631 llvm::less_first());
2632 if (BeginIt != LocDecls.begin())
2635 // If we are pointing at a top-level decl inside an objc container, we need
2636 // to backtrack until we find it otherwise we will fail to report that the
2637 // region overlaps with an objc container.
2638 while (BeginIt != LocDecls.begin() &&
2639 BeginIt->second->isTopLevelDeclInObjCContainer())
2642 LocDeclsTy::iterator EndIt = std::upper_bound(
2643 LocDecls.begin(), LocDecls.end(),
2644 std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first());
2645 if (EndIt != LocDecls.end())
2648 for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2649 Decls.push_back(DIt->second);
2652 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2653 unsigned Line, unsigned Col) const {
2654 const SourceManager &SM = getSourceManager();
2655 SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2656 return SM.getMacroArgExpandedLocation(Loc);
2659 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2660 unsigned Offset) const {
2661 const SourceManager &SM = getSourceManager();
2662 SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2663 return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2666 /// \brief If \arg Loc is a loaded location from the preamble, returns
2667 /// the corresponding local location of the main file, otherwise it returns
2669 SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
2672 PreambleID = SourceMgr->getPreambleFileID();
2674 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2678 if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
2679 SourceLocation FileLoc
2680 = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2681 return FileLoc.getLocWithOffset(Offs);
2687 /// \brief If \arg Loc is a local location of the main file but inside the
2688 /// preamble chunk, returns the corresponding loaded location from the
2689 /// preamble, otherwise it returns \arg Loc.
2690 SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
2693 PreambleID = SourceMgr->getPreambleFileID();
2695 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2699 if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2700 Offs < Preamble.size()) {
2701 SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2702 return FileLoc.getLocWithOffset(Offs);
2708 bool ASTUnit::isInPreambleFileID(SourceLocation Loc) {
2711 FID = SourceMgr->getPreambleFileID();
2713 if (Loc.isInvalid() || FID.isInvalid())
2716 return SourceMgr->isInFileID(Loc, FID);
2719 bool ASTUnit::isInMainFileID(SourceLocation Loc) {
2722 FID = SourceMgr->getMainFileID();
2724 if (Loc.isInvalid() || FID.isInvalid())
2727 return SourceMgr->isInFileID(Loc, FID);
2730 SourceLocation ASTUnit::getEndOfPreambleFileID() {
2733 FID = SourceMgr->getPreambleFileID();
2735 if (FID.isInvalid())
2736 return SourceLocation();
2738 return SourceMgr->getLocForEndOfFile(FID);
2741 SourceLocation ASTUnit::getStartOfMainFileID() {
2744 FID = SourceMgr->getMainFileID();
2746 if (FID.isInvalid())
2747 return SourceLocation();
2749 return SourceMgr->getLocForStartOfFile(FID);
2752 llvm::iterator_range<PreprocessingRecord::iterator>
2753 ASTUnit::getLocalPreprocessingEntities() const {
2754 if (isMainFileAST()) {
2755 serialization::ModuleFile &
2756 Mod = Reader->getModuleManager().getPrimaryModule();
2757 return Reader->getModulePreprocessedEntities(Mod);
2760 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2761 return llvm::make_range(PPRec->local_begin(), PPRec->local_end());
2763 return llvm::make_range(PreprocessingRecord::iterator(),
2764 PreprocessingRecord::iterator());
2767 bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2768 if (isMainFileAST()) {
2769 serialization::ModuleFile &
2770 Mod = Reader->getModuleManager().getPrimaryModule();
2771 for (const Decl *D : Reader->getModuleFileLevelDecls(Mod)) {
2772 if (!Fn(context, D))
2779 for (ASTUnit::top_level_iterator TL = top_level_begin(),
2780 TLEnd = top_level_end();
2781 TL != TLEnd; ++TL) {
2782 if (!Fn(context, *TL))
2789 const FileEntry *ASTUnit::getPCHFile() {
2793 serialization::ModuleFile *Mod = nullptr;
2794 Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) {
2796 case serialization::MK_ImplicitModule:
2797 case serialization::MK_ExplicitModule:
2798 return true; // skip dependencies.
2799 case serialization::MK_PCH:
2801 return true; // found it.
2802 case serialization::MK_Preamble:
2803 return false; // look in dependencies.
2804 case serialization::MK_MainFile:
2805 return false; // look in dependencies.
2816 bool ASTUnit::isModuleFile() {
2817 return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
2820 void ASTUnit::PreambleData::countLines() const {
2825 NumLines = std::count(Buffer.begin(), Buffer.end(), '\n');
2827 if (Buffer.back() != '\n')
2832 ASTUnit::ConcurrencyState::ConcurrencyState() {
2833 Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2836 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2837 delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2840 void ASTUnit::ConcurrencyState::start() {
2841 bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2842 assert(acquired && "Concurrent access to ASTUnit!");
2845 void ASTUnit::ConcurrencyState::finish() {
2846 static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2851 ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = nullptr; }
2852 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2853 void ASTUnit::ConcurrencyState::start() {}
2854 void ASTUnit::ConcurrencyState::finish() {}