1 //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
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"
50 using namespace clang;
52 using llvm::TimeRecord;
61 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
63 Start = TimeRecord::getCurrentTime();
66 void setOutput(const Twine &Output) {
68 this->Output = Output.str();
73 TimeRecord Elapsed = TimeRecord::getCurrentTime();
75 llvm::errs() << Output << ':';
76 Elapsed.print(Elapsed, llvm::errs());
83 /// \brief The file in which the precompiled preamble is stored.
84 std::string PreambleFile;
86 /// \brief Temporary files that should be removed when the ASTUnit is
88 SmallVector<std::string, 4> TemporaryFiles;
90 /// \brief Erase temporary files.
91 void CleanTemporaryFiles();
93 /// \brief Erase the preamble file.
94 void CleanPreambleFile();
96 /// \brief Erase temporary files and the preamble file.
101 static llvm::sys::SmartMutex<false> &getOnDiskMutex() {
102 static llvm::sys::SmartMutex<false> M(/* recursive = */ true);
106 static void cleanupOnDiskMapAtExit();
108 typedef llvm::DenseMap<const ASTUnit *,
109 std::unique_ptr<OnDiskData>> OnDiskDataMap;
110 static OnDiskDataMap &getOnDiskDataMap() {
111 static OnDiskDataMap M;
112 static bool hasRegisteredAtExit = false;
113 if (!hasRegisteredAtExit) {
114 hasRegisteredAtExit = true;
115 atexit(cleanupOnDiskMapAtExit);
120 static void cleanupOnDiskMapAtExit() {
121 // Use the mutex because there can be an alive thread destroying an ASTUnit.
122 llvm::MutexGuard Guard(getOnDiskMutex());
123 OnDiskDataMap &M = getOnDiskDataMap();
124 for (OnDiskDataMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
125 // We don't worry about freeing the memory associated with OnDiskDataMap.
126 // All we care about is erasing stale files.
127 I->second->Cleanup();
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 (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
168 llvm::sys::fs::remove(TemporaryFiles[I]);
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;
358 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
359 switch (Results[I].Kind) {
360 case Result::RK_Declaration: {
361 bool IsNestedNameSpecifier = false;
362 CachedCodeCompletionResult CachedResult;
363 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
364 *CachedCompletionAllocator,
366 IncludeBriefCommentsInCodeCompletion);
367 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
369 IsNestedNameSpecifier);
370 CachedResult.Priority = Results[I].Priority;
371 CachedResult.Kind = Results[I].CursorKind;
372 CachedResult.Availability = Results[I].Availability;
374 // Keep track of the type of this completion in an ASTContext-agnostic
376 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
377 if (UsageType.isNull()) {
378 CachedResult.TypeClass = STC_Void;
379 CachedResult.Type = 0;
381 CanQualType CanUsageType
382 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
383 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
385 // Determine whether we have already seen this type. If so, we save
386 // ourselves the work of formatting the type string by using the
387 // temporary, CanQualType-based hash table to find the associated value.
388 unsigned &TypeValue = CompletionTypes[CanUsageType];
389 if (TypeValue == 0) {
390 TypeValue = CompletionTypes.size();
391 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
395 CachedResult.Type = TypeValue;
398 CachedCompletionResults.push_back(CachedResult);
400 /// Handle nested-name-specifiers in C++.
401 if (TheSema->Context.getLangOpts().CPlusPlus &&
402 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
403 // The contexts in which a nested-name-specifier can appear in C++.
405 = (1LL << CodeCompletionContext::CCC_TopLevel)
406 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
407 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
408 | (1LL << CodeCompletionContext::CCC_Statement)
409 | (1LL << CodeCompletionContext::CCC_Expression)
410 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
411 | (1LL << CodeCompletionContext::CCC_EnumTag)
412 | (1LL << CodeCompletionContext::CCC_UnionTag)
413 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
414 | (1LL << CodeCompletionContext::CCC_Type)
415 | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName)
416 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
418 if (isa<NamespaceDecl>(Results[I].Declaration) ||
419 isa<NamespaceAliasDecl>(Results[I].Declaration))
420 NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
422 if (unsigned RemainingContexts
423 = NNSContexts & ~CachedResult.ShowInContexts) {
424 // If there any contexts where this completion can be a
425 // nested-name-specifier but isn't already an option, create a
426 // nested-name-specifier completion.
427 Results[I].StartsNestedNameSpecifier = true;
428 CachedResult.Completion
429 = Results[I].CreateCodeCompletionString(*TheSema,
430 *CachedCompletionAllocator,
432 IncludeBriefCommentsInCodeCompletion);
433 CachedResult.ShowInContexts = RemainingContexts;
434 CachedResult.Priority = CCP_NestedNameSpecifier;
435 CachedResult.TypeClass = STC_Void;
436 CachedResult.Type = 0;
437 CachedCompletionResults.push_back(CachedResult);
443 case Result::RK_Keyword:
444 case Result::RK_Pattern:
445 // Ignore keywords and patterns; we don't care, since they are so
446 // easily regenerated.
449 case Result::RK_Macro: {
450 CachedCodeCompletionResult CachedResult;
451 CachedResult.Completion
452 = Results[I].CreateCodeCompletionString(*TheSema,
453 *CachedCompletionAllocator,
455 IncludeBriefCommentsInCodeCompletion);
456 CachedResult.ShowInContexts
457 = (1LL << CodeCompletionContext::CCC_TopLevel)
458 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
459 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
460 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
461 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
462 | (1LL << CodeCompletionContext::CCC_Statement)
463 | (1LL << CodeCompletionContext::CCC_Expression)
464 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
465 | (1LL << CodeCompletionContext::CCC_MacroNameUse)
466 | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
467 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
468 | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
470 CachedResult.Priority = Results[I].Priority;
471 CachedResult.Kind = Results[I].CursorKind;
472 CachedResult.Availability = Results[I].Availability;
473 CachedResult.TypeClass = STC_Void;
474 CachedResult.Type = 0;
475 CachedCompletionResults.push_back(CachedResult);
481 // Save the current top-level hash value.
482 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
485 void ASTUnit::ClearCachedCompletionResults() {
486 CachedCompletionResults.clear();
487 CachedCompletionTypes.clear();
488 CachedCompletionAllocator = nullptr;
493 /// \brief Gathers information from ASTReader that will be used to initialize
495 class ASTInfoCollector : public ASTReaderListener {
498 LangOptions &LangOpt;
499 std::shared_ptr<TargetOptions> &TargetOpts;
500 IntrusiveRefCntPtr<TargetInfo> &Target;
503 bool InitializedLanguage;
505 ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt,
506 std::shared_ptr<TargetOptions> &TargetOpts,
507 IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter)
508 : PP(PP), Context(Context), LangOpt(LangOpt), TargetOpts(TargetOpts),
509 Target(Target), Counter(Counter), InitializedLanguage(false) {}
511 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
512 bool AllowCompatibleDifferences) override {
513 if (InitializedLanguage)
517 InitializedLanguage = true;
523 bool ReadTargetOptions(const TargetOptions &TargetOpts,
524 bool Complain) override {
525 // If we've already initialized the target, don't do it again.
529 this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts);
531 TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts);
537 void ReadCounter(const serialization::ModuleFile &M,
538 unsigned Value) override {
544 if (!Target || !InitializedLanguage)
547 // Inform the target of the language options.
549 // FIXME: We shouldn't need to do this, the target should be immutable once
550 // created. This complexity should be lifted elsewhere.
551 Target->adjust(LangOpt);
553 // Initialize the preprocessor.
554 PP.Initialize(*Target);
556 // Initialize the ASTContext
557 Context.InitBuiltinTypes(*Target);
559 // We didn't have access to the comment options when the ASTContext was
560 // constructed, so register them now.
561 Context.getCommentCommandTraits().registerCommentOptions(
562 LangOpt.CommentOpts);
566 /// \brief Diagnostic consumer that saves each diagnostic it is given.
567 class StoredDiagnosticConsumer : public DiagnosticConsumer {
568 SmallVectorImpl<StoredDiagnostic> &StoredDiags;
569 SourceManager *SourceMgr;
572 explicit StoredDiagnosticConsumer(
573 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
574 : StoredDiags(StoredDiags), SourceMgr(nullptr) {}
576 void BeginSourceFile(const LangOptions &LangOpts,
577 const Preprocessor *PP = nullptr) override {
579 SourceMgr = &PP->getSourceManager();
582 void HandleDiagnostic(DiagnosticsEngine::Level Level,
583 const Diagnostic &Info) override;
586 /// \brief RAII object that optionally captures diagnostics, if
587 /// there is no diagnostic client to capture them already.
588 class CaptureDroppedDiagnostics {
589 DiagnosticsEngine &Diags;
590 StoredDiagnosticConsumer Client;
591 DiagnosticConsumer *PreviousClient;
592 std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
595 CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
596 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
597 : Diags(Diags), Client(StoredDiags), PreviousClient(nullptr)
599 if (RequestCapture || Diags.getClient() == nullptr) {
600 OwningPreviousClient = Diags.takeClient();
601 PreviousClient = Diags.getClient();
602 Diags.setClient(&Client, false);
606 ~CaptureDroppedDiagnostics() {
607 if (Diags.getClient() == &Client)
608 Diags.setClient(PreviousClient, !!OwningPreviousClient.release());
612 } // anonymous namespace
614 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
615 const Diagnostic &Info) {
616 // Default implementation (Warnings/errors count).
617 DiagnosticConsumer::HandleDiagnostic(Level, Info);
619 // Only record the diagnostic if it's part of the source manager we know
620 // about. This effectively drops diagnostics from modules we're building.
621 // FIXME: In the long run, ee don't want to drop source managers from modules.
622 if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr)
623 StoredDiags.push_back(StoredDiagnostic(Level, Info));
626 ASTMutationListener *ASTUnit::getASTMutationListener() {
628 return &WriterData->Writer;
632 ASTDeserializationListener *ASTUnit::getDeserializationListener() {
634 return &WriterData->Writer;
638 std::unique_ptr<llvm::MemoryBuffer>
639 ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) {
641 auto Buffer = FileMgr->getBufferForFile(Filename);
643 return std::move(*Buffer);
645 *ErrorStr = Buffer.getError().message();
649 /// \brief Configure the diagnostics object for use with ASTUnit.
650 void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
651 ASTUnit &AST, bool CaptureDiagnostics) {
652 assert(Diags.get() && "no DiagnosticsEngine was provided");
653 if (CaptureDiagnostics)
654 Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics));
657 std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
658 const std::string &Filename, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
659 const FileSystemOptions &FileSystemOpts, bool OnlyLocalDecls,
660 ArrayRef<RemappedFile> RemappedFiles, bool CaptureDiagnostics,
661 bool AllowPCHWithCompilerErrors, bool UserFilesAreVolatile) {
662 std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
664 // Recover resources if we crash before exiting this method.
665 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
666 ASTUnitCleanup(AST.get());
667 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
668 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
669 DiagCleanup(Diags.get());
671 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
673 AST->OnlyLocalDecls = OnlyLocalDecls;
674 AST->CaptureDiagnostics = CaptureDiagnostics;
675 AST->Diagnostics = Diags;
676 IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem();
677 AST->FileMgr = new FileManager(FileSystemOpts, VFS);
678 AST->UserFilesAreVolatile = UserFilesAreVolatile;
679 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
680 AST->getFileManager(),
681 UserFilesAreVolatile);
682 AST->HSOpts = new HeaderSearchOptions();
684 AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
685 AST->getSourceManager(),
686 AST->getDiagnostics(),
687 AST->ASTFileLangOpts,
688 /*Target=*/nullptr));
690 PreprocessorOptions *PPOpts = new PreprocessorOptions();
692 for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I)
693 PPOpts->addRemappedFile(RemappedFiles[I].first, RemappedFiles[I].second);
695 // Gather Info for preprocessor construction later on.
697 HeaderSearch &HeaderInfo = *AST->HeaderInfo;
701 new Preprocessor(PPOpts, AST->getDiagnostics(), AST->ASTFileLangOpts,
702 AST->getSourceManager(), HeaderInfo, *AST,
703 /*IILookup=*/nullptr,
704 /*OwnsHeaderSearch=*/false);
705 Preprocessor &PP = *AST->PP;
707 AST->Ctx = new ASTContext(AST->ASTFileLangOpts, AST->getSourceManager(),
708 PP.getIdentifierTable(), PP.getSelectorTable(),
709 PP.getBuiltinInfo());
710 ASTContext &Context = *AST->Ctx;
712 bool disableValid = false;
713 if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
715 AST->Reader = new ASTReader(PP, Context,
717 /*DisableValidation=*/disableValid,
718 AllowPCHWithCompilerErrors);
720 AST->Reader->setListener(llvm::make_unique<ASTInfoCollector>(
721 *AST->PP, Context, AST->ASTFileLangOpts, AST->TargetOpts, AST->Target,
724 switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile,
725 SourceLocation(), ASTReader::ARR_None)) {
726 case ASTReader::Success:
729 case ASTReader::Failure:
730 case ASTReader::Missing:
731 case ASTReader::OutOfDate:
732 case ASTReader::VersionMismatch:
733 case ASTReader::ConfigurationMismatch:
734 case ASTReader::HadErrors:
735 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
739 AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile();
741 PP.setCounterValue(Counter);
743 // Attach the AST reader to the AST context as an external AST
744 // source, so that declarations will be deserialized from the
745 // AST file as needed.
746 Context.setExternalSource(AST->Reader);
748 // Create an AST consumer, even though it isn't used.
749 AST->Consumer.reset(new ASTConsumer);
751 // Create a semantic analysis object and tell the AST reader about it.
752 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
753 AST->TheSema->Initialize();
754 AST->Reader->InitializeSema(*AST->TheSema);
756 // Tell the diagnostic client that we have started a source file.
757 AST->getDiagnostics().getClient()->BeginSourceFile(Context.getLangOpts(),&PP);
764 /// \brief Preprocessor callback class that updates a hash value with the names
765 /// of all macros that have been defined by the translation unit.
766 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
770 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
772 void MacroDefined(const Token &MacroNameTok,
773 const MacroDirective *MD) override {
774 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
778 /// \brief Add the given declaration to the hash of all top-level entities.
779 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
783 DeclContext *DC = D->getDeclContext();
787 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
790 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
791 if (EnumDecl *EnumD = dyn_cast<EnumDecl>(D)) {
792 // For an unscoped enum include the enumerators in the hash since they
793 // enter the top-level namespace.
794 if (!EnumD->isScoped()) {
795 for (const auto *EI : EnumD->enumerators()) {
796 if (EI->getIdentifier())
797 Hash = llvm::HashString(EI->getIdentifier()->getName(), Hash);
802 if (ND->getIdentifier())
803 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
804 else if (DeclarationName Name = ND->getDeclName()) {
805 std::string NameStr = Name.getAsString();
806 Hash = llvm::HashString(NameStr, Hash);
811 if (ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) {
812 if (Module *Mod = ImportD->getImportedModule()) {
813 std::string ModName = Mod->getFullModuleName();
814 Hash = llvm::HashString(ModName, Hash);
820 class TopLevelDeclTrackerConsumer : public ASTConsumer {
825 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
826 : Unit(_Unit), Hash(Hash) {
830 void handleTopLevelDecl(Decl *D) {
834 // FIXME: Currently ObjC method declarations are incorrectly being
835 // reported as top-level declarations, even though their DeclContext
836 // is the containing ObjC @interface/@implementation. This is a
837 // fundamental problem in the parser right now.
838 if (isa<ObjCMethodDecl>(D))
841 AddTopLevelDeclarationToHash(D, Hash);
842 Unit.addTopLevelDecl(D);
844 handleFileLevelDecl(D);
847 void handleFileLevelDecl(Decl *D) {
848 Unit.addFileLevelDecl(D);
849 if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) {
850 for (auto *I : NSD->decls())
851 handleFileLevelDecl(I);
855 bool HandleTopLevelDecl(DeclGroupRef D) override {
856 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
857 handleTopLevelDecl(*it);
861 // We're not interested in "interesting" decls.
862 void HandleInterestingDecl(DeclGroupRef) override {}
864 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
865 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
866 handleTopLevelDecl(*it);
869 ASTMutationListener *GetASTMutationListener() override {
870 return Unit.getASTMutationListener();
873 ASTDeserializationListener *GetASTDeserializationListener() override {
874 return Unit.getDeserializationListener();
878 class TopLevelDeclTrackerAction : public ASTFrontendAction {
882 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
883 StringRef InFile) override {
884 CI.getPreprocessor().addPPCallbacks(
885 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
886 Unit.getCurrentTopLevelHashValue()));
887 return llvm::make_unique<TopLevelDeclTrackerConsumer>(
888 Unit, Unit.getCurrentTopLevelHashValue());
892 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
894 bool hasCodeCompletionSupport() const override { return false; }
895 TranslationUnitKind getTranslationUnitKind() override {
896 return Unit.getTranslationUnitKind();
900 class PrecompilePreambleAction : public ASTFrontendAction {
902 bool HasEmittedPreamblePCH;
905 explicit PrecompilePreambleAction(ASTUnit &Unit)
906 : Unit(Unit), HasEmittedPreamblePCH(false) {}
908 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
909 StringRef InFile) override;
910 bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
911 void setHasEmittedPreamblePCH() { HasEmittedPreamblePCH = true; }
912 bool shouldEraseOutputFiles() override { return !hasEmittedPreamblePCH(); }
914 bool hasCodeCompletionSupport() const override { return false; }
915 bool hasASTFileSupport() const override { return false; }
916 TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; }
919 class PrecompilePreambleConsumer : public PCHGenerator {
922 std::vector<Decl *> TopLevelDecls;
923 PrecompilePreambleAction *Action;
926 PrecompilePreambleConsumer(ASTUnit &Unit, PrecompilePreambleAction *Action,
927 const Preprocessor &PP, StringRef isysroot,
929 : PCHGenerator(PP, "", nullptr, isysroot, Out, /*AllowASTWithErrors=*/true),
930 Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()), Action(Action) {
934 bool HandleTopLevelDecl(DeclGroupRef D) override {
935 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
937 // FIXME: Currently ObjC method declarations are incorrectly being
938 // reported as top-level declarations, even though their DeclContext
939 // is the containing ObjC @interface/@implementation. This is a
940 // fundamental problem in the parser right now.
941 if (isa<ObjCMethodDecl>(D))
943 AddTopLevelDeclarationToHash(D, Hash);
944 TopLevelDecls.push_back(D);
949 void HandleTranslationUnit(ASTContext &Ctx) override {
950 PCHGenerator::HandleTranslationUnit(Ctx);
951 if (hasEmittedPCH()) {
952 // Translate the top-level declarations we captured during
953 // parsing into declaration IDs in the precompiled
954 // preamble. This will allow us to deserialize those top-level
955 // declarations when requested.
956 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) {
957 Decl *D = TopLevelDecls[I];
958 // Invalid top-level decls may not have been serialized.
959 if (D->isInvalidDecl())
961 Unit.addTopLevelDeclFromPreamble(getWriter().getDeclID(D));
964 Action->setHasEmittedPreamblePCH();
971 std::unique_ptr<ASTConsumer>
972 PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
975 std::string OutputFile;
976 raw_ostream *OS = nullptr;
977 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
981 if (!CI.getFrontendOpts().RelocatablePCH)
984 CI.getPreprocessor().addPPCallbacks(
985 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
986 Unit.getCurrentTopLevelHashValue()));
987 return llvm::make_unique<PrecompilePreambleConsumer>(
988 Unit, this, CI.getPreprocessor(), Sysroot, OS);
991 static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
992 return StoredDiag.getLocation().isValid();
996 checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
997 // Get rid of stored diagnostics except the ones from the driver which do not
998 // have a source location.
1000 std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
1004 static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
1006 SourceManager &SM) {
1007 // The stored diagnostic has the old source manager in it; update
1008 // the locations to refer into the new source manager. Since we've
1009 // been careful to make sure that the source manager's state
1010 // before and after are identical, so that we can reuse the source
1012 for (unsigned I = 0, N = StoredDiagnostics.size(); I < N; ++I) {
1013 if (StoredDiagnostics[I].getLocation().isValid()) {
1014 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SM);
1015 StoredDiagnostics[I].setLocation(Loc);
1020 /// Parse the source file into a translation unit using the given compiler
1021 /// invocation, replacing the current translation unit.
1023 /// \returns True if a failure occurred that causes the ASTUnit not to
1024 /// contain any translation-unit information, false otherwise.
1025 bool ASTUnit::Parse(std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer) {
1026 SavedMainFileBuffer.reset();
1031 // Create the compiler instance to use for building the AST.
1032 std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
1034 // Recover resources if we crash before exiting this method.
1035 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1036 CICleanup(Clang.get());
1038 IntrusiveRefCntPtr<CompilerInvocation>
1039 CCInvocation(new CompilerInvocation(*Invocation));
1041 Clang->setInvocation(CCInvocation.get());
1042 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1044 // Set up diagnostics, capturing any diagnostics that would
1045 // otherwise be dropped.
1046 Clang->setDiagnostics(&getDiagnostics());
1048 // Create the target instance.
1049 Clang->setTarget(TargetInfo::CreateTargetInfo(
1050 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1051 if (!Clang->hasTarget())
1054 // Inform the target of the language options.
1056 // FIXME: We shouldn't need to do this, the target should be immutable once
1057 // created. This complexity should be lifted elsewhere.
1058 Clang->getTarget().adjust(Clang->getLangOpts());
1060 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1061 "Invocation must have exactly one source file!");
1062 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1063 "FIXME: AST inputs not yet supported here!");
1064 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1065 "IR inputs not support here!");
1067 // Configure the various subsystems.
1068 LangOpts = Clang->getInvocation().LangOpts;
1069 FileSystemOpts = Clang->getFileSystemOpts();
1070 IntrusiveRefCntPtr<vfs::FileSystem> VFS =
1071 createVFSFromCompilerInvocation(Clang->getInvocation(), getDiagnostics());
1074 FileMgr = new FileManager(FileSystemOpts, VFS);
1075 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1076 UserFilesAreVolatile);
1082 // Clear out old caches and data.
1083 TopLevelDecls.clear();
1084 clearFileLevelDecls();
1085 CleanTemporaryFiles();
1087 if (!OverrideMainBuffer) {
1088 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1089 TopLevelDeclsInPreamble.clear();
1092 // Create a file manager object to provide access to and cache the filesystem.
1093 Clang->setFileManager(&getFileManager());
1095 // Create the source manager.
1096 Clang->setSourceManager(&getSourceManager());
1098 // If the main file has been overridden due to the use of a preamble,
1099 // make that override happen and introduce the preamble.
1100 PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
1101 if (OverrideMainBuffer) {
1102 PreprocessorOpts.addRemappedFile(OriginalSourceFile,
1103 OverrideMainBuffer.get());
1104 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
1105 PreprocessorOpts.PrecompiledPreambleBytes.second
1106 = PreambleEndsAtStartOfLine;
1107 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
1108 PreprocessorOpts.DisablePCHValidation = true;
1110 // The stored diagnostic has the old source manager in it; update
1111 // the locations to refer into the new source manager. Since we've
1112 // been careful to make sure that the source manager's state
1113 // before and after are identical, so that we can reuse the source
1115 checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1117 // Keep track of the override buffer;
1118 SavedMainFileBuffer = std::move(OverrideMainBuffer);
1121 std::unique_ptr<TopLevelDeclTrackerAction> Act(
1122 new TopLevelDeclTrackerAction(*this));
1124 // Recover resources if we crash before exiting this method.
1125 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1126 ActCleanup(Act.get());
1128 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1131 if (SavedMainFileBuffer) {
1132 std::string ModName = getPreambleFile(this);
1133 TranslateStoredDiagnostics(getFileManager(), getSourceManager(),
1134 PreambleDiagnostics, StoredDiagnostics);
1137 if (!Act->Execute())
1140 transferASTDataFromCompilerInstance(*Clang);
1142 Act->EndSourceFile();
1144 FailedParseDiagnostics.clear();
1149 // Remove the overridden buffer we used for the preamble.
1150 SavedMainFileBuffer = nullptr;
1152 // Keep the ownership of the data in the ASTUnit because the client may
1153 // want to see the diagnostics.
1154 transferASTDataFromCompilerInstance(*Clang);
1155 FailedParseDiagnostics.swap(StoredDiagnostics);
1156 StoredDiagnostics.clear();
1157 NumStoredDiagnosticsFromDriver = 0;
1161 /// \brief Simple function to retrieve a path for a preamble precompiled header.
1162 static std::string GetPreamblePCHPath() {
1163 // FIXME: This is a hack so that we can override the preamble file during
1164 // crash-recovery testing, which is the only case where the preamble files
1165 // are not necessarily cleaned up.
1166 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1170 SmallString<128> Path;
1171 llvm::sys::fs::createTemporaryFile("preamble", "pch", Path);
1176 /// \brief Compute the preamble for the main file, providing the source buffer
1177 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1178 /// that describes the preamble.
1179 ASTUnit::ComputedPreamble
1180 ASTUnit::ComputePreamble(CompilerInvocation &Invocation, unsigned MaxLines) {
1181 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1182 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1184 // Try to determine if the main file has been remapped, either from the
1185 // command line (to another file) or directly through the compiler invocation
1186 // (to a memory buffer).
1187 llvm::MemoryBuffer *Buffer = nullptr;
1188 std::unique_ptr<llvm::MemoryBuffer> BufferOwner;
1189 std::string MainFilePath(FrontendOpts.Inputs[0].getFile());
1190 llvm::sys::fs::UniqueID MainFileID;
1191 if (!llvm::sys::fs::getUniqueID(MainFilePath, MainFileID)) {
1192 // Check whether there is a file-file remapping of the main file
1193 for (const auto &RF : PreprocessorOpts.RemappedFiles) {
1194 std::string MPath(RF.first);
1195 llvm::sys::fs::UniqueID MID;
1196 if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1197 if (MainFileID == MID) {
1198 // We found a remapping. Try to load the resulting, remapped source.
1199 BufferOwner = getBufferForFile(RF.second);
1201 return ComputedPreamble(nullptr, nullptr, 0, true);
1206 // Check whether there is a file-buffer remapping. It supercedes the
1207 // file-file remapping.
1208 for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
1209 std::string MPath(RB.first);
1210 llvm::sys::fs::UniqueID MID;
1211 if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1212 if (MainFileID == MID) {
1213 // We found a remapping.
1214 BufferOwner.reset();
1215 Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
1221 // If the main source file was not remapped, load it now.
1222 if (!Buffer && !BufferOwner) {
1223 BufferOwner = getBufferForFile(FrontendOpts.Inputs[0].getFile());
1225 return ComputedPreamble(nullptr, nullptr, 0, true);
1229 Buffer = BufferOwner.get();
1230 auto Pre = Lexer::ComputePreamble(Buffer->getBuffer(),
1231 *Invocation.getLangOpts(), MaxLines);
1232 return ComputedPreamble(Buffer, std::move(BufferOwner), Pre.first,
1236 ASTUnit::PreambleFileHash
1237 ASTUnit::PreambleFileHash::createForFile(off_t Size, time_t ModTime) {
1238 PreambleFileHash Result;
1240 Result.ModTime = ModTime;
1241 memset(Result.MD5, 0, sizeof(Result.MD5));
1245 ASTUnit::PreambleFileHash ASTUnit::PreambleFileHash::createForMemoryBuffer(
1246 const llvm::MemoryBuffer *Buffer) {
1247 PreambleFileHash Result;
1248 Result.Size = Buffer->getBufferSize();
1252 MD5Ctx.update(Buffer->getBuffer().data());
1253 MD5Ctx.final(Result.MD5);
1259 bool operator==(const ASTUnit::PreambleFileHash &LHS,
1260 const ASTUnit::PreambleFileHash &RHS) {
1261 return LHS.Size == RHS.Size && LHS.ModTime == RHS.ModTime &&
1262 memcmp(LHS.MD5, RHS.MD5, sizeof(LHS.MD5)) == 0;
1264 } // namespace clang
1266 static std::pair<unsigned, unsigned>
1267 makeStandaloneRange(CharSourceRange Range, const SourceManager &SM,
1268 const LangOptions &LangOpts) {
1269 CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts);
1270 unsigned Offset = SM.getFileOffset(FileRange.getBegin());
1271 unsigned EndOffset = SM.getFileOffset(FileRange.getEnd());
1272 return std::make_pair(Offset, EndOffset);
1275 static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM,
1276 const LangOptions &LangOpts,
1277 const FixItHint &InFix) {
1278 ASTUnit::StandaloneFixIt OutFix;
1279 OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts);
1280 OutFix.InsertFromRange = makeStandaloneRange(InFix.InsertFromRange, SM,
1282 OutFix.CodeToInsert = InFix.CodeToInsert;
1283 OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions;
1287 static ASTUnit::StandaloneDiagnostic
1288 makeStandaloneDiagnostic(const LangOptions &LangOpts,
1289 const StoredDiagnostic &InDiag) {
1290 ASTUnit::StandaloneDiagnostic OutDiag;
1291 OutDiag.ID = InDiag.getID();
1292 OutDiag.Level = InDiag.getLevel();
1293 OutDiag.Message = InDiag.getMessage();
1294 OutDiag.LocOffset = 0;
1295 if (InDiag.getLocation().isInvalid())
1297 const SourceManager &SM = InDiag.getLocation().getManager();
1298 SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation());
1299 OutDiag.Filename = SM.getFilename(FileLoc);
1300 if (OutDiag.Filename.empty())
1302 OutDiag.LocOffset = SM.getFileOffset(FileLoc);
1303 for (StoredDiagnostic::range_iterator
1304 I = InDiag.range_begin(), E = InDiag.range_end(); I != E; ++I) {
1305 OutDiag.Ranges.push_back(makeStandaloneRange(*I, SM, LangOpts));
1307 for (StoredDiagnostic::fixit_iterator I = InDiag.fixit_begin(),
1308 E = InDiag.fixit_end();
1310 OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, *I));
1315 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1316 /// the source file.
1318 /// This routine will compute the preamble of the main source file. If a
1319 /// non-trivial preamble is found, it will precompile that preamble into a
1320 /// precompiled header so that the precompiled preamble can be used to reduce
1321 /// reparsing time. If a precompiled preamble has already been constructed,
1322 /// this routine will determine if it is still valid and, if so, avoid
1323 /// rebuilding the precompiled preamble.
1325 /// \param AllowRebuild When true (the default), this routine is
1326 /// allowed to rebuild the precompiled preamble if it is found to be
1329 /// \param MaxLines When non-zero, the maximum number of lines that
1330 /// can occur within the preamble.
1332 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1333 /// buffer that should be used in place of the main file when doing so.
1334 /// Otherwise, returns a NULL pointer.
1335 std::unique_ptr<llvm::MemoryBuffer>
1336 ASTUnit::getMainBufferWithPrecompiledPreamble(
1337 const CompilerInvocation &PreambleInvocationIn, bool AllowRebuild,
1338 unsigned MaxLines) {
1340 IntrusiveRefCntPtr<CompilerInvocation>
1341 PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
1342 FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
1343 PreprocessorOptions &PreprocessorOpts
1344 = PreambleInvocation->getPreprocessorOpts();
1346 ComputedPreamble NewPreamble = ComputePreamble(*PreambleInvocation, MaxLines);
1348 if (!NewPreamble.Size) {
1349 // We couldn't find a preamble in the main source. Clear out the current
1350 // preamble, if we have one. It's obviously no good any more.
1352 erasePreambleFile(this);
1354 // The next time we actually see a preamble, precompile it.
1355 PreambleRebuildCounter = 1;
1359 if (!Preamble.empty()) {
1360 // We've previously computed a preamble. Check whether we have the same
1361 // preamble now that we did before, and that there's enough space in
1362 // the main-file buffer within the precompiled preamble to fit the
1364 if (Preamble.size() == NewPreamble.Size &&
1365 PreambleEndsAtStartOfLine == NewPreamble.PreambleEndsAtStartOfLine &&
1366 memcmp(Preamble.getBufferStart(), NewPreamble.Buffer->getBufferStart(),
1367 NewPreamble.Size) == 0) {
1368 // The preamble has not changed. We may be able to re-use the precompiled
1371 // Check that none of the files used by the preamble have changed.
1372 bool AnyFileChanged = false;
1374 // First, make a record of those files that have been overridden via
1375 // remapping or unsaved_files.
1376 llvm::StringMap<PreambleFileHash> OverriddenFiles;
1377 for (const auto &R : PreprocessorOpts.RemappedFiles) {
1382 if (FileMgr->getNoncachedStatValue(R.second, Status)) {
1383 // If we can't stat the file we're remapping to, assume that something
1384 // horrible happened.
1385 AnyFileChanged = true;
1389 OverriddenFiles[R.first] = PreambleFileHash::createForFile(
1390 Status.getSize(), Status.getLastModificationTime().toEpochTime());
1393 for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
1396 OverriddenFiles[RB.first] =
1397 PreambleFileHash::createForMemoryBuffer(RB.second);
1400 // Check whether anything has changed.
1401 for (llvm::StringMap<PreambleFileHash>::iterator
1402 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1403 !AnyFileChanged && F != FEnd;
1405 llvm::StringMap<PreambleFileHash>::iterator Overridden
1406 = OverriddenFiles.find(F->first());
1407 if (Overridden != OverriddenFiles.end()) {
1408 // This file was remapped; check whether the newly-mapped file
1409 // matches up with the previous mapping.
1410 if (Overridden->second != F->second)
1411 AnyFileChanged = true;
1415 // The file was not remapped; check whether it has changed on disk.
1417 if (FileMgr->getNoncachedStatValue(F->first(), Status)) {
1418 // If we can't stat the file, assume that something horrible happened.
1419 AnyFileChanged = true;
1420 } else if (Status.getSize() != uint64_t(F->second.Size) ||
1421 Status.getLastModificationTime().toEpochTime() !=
1422 uint64_t(F->second.ModTime))
1423 AnyFileChanged = true;
1426 if (!AnyFileChanged) {
1427 // Okay! We can re-use the precompiled preamble.
1429 // Set the state of the diagnostic object to mimic its state
1430 // after parsing the preamble.
1431 getDiagnostics().Reset();
1432 ProcessWarningOptions(getDiagnostics(),
1433 PreambleInvocation->getDiagnosticOpts());
1434 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1436 return llvm::MemoryBuffer::getMemBufferCopy(
1437 NewPreamble.Buffer->getBuffer(), FrontendOpts.Inputs[0].getFile());
1441 // If we aren't allowed to rebuild the precompiled preamble, just
1446 // We can't reuse the previously-computed preamble. Build a new one.
1448 PreambleDiagnostics.clear();
1449 erasePreambleFile(this);
1450 PreambleRebuildCounter = 1;
1451 } else if (!AllowRebuild) {
1452 // We aren't allowed to rebuild the precompiled preamble; just
1457 // If the preamble rebuild counter > 1, it's because we previously
1458 // failed to build a preamble and we're not yet ready to try
1459 // again. Decrement the counter and return a failure.
1460 if (PreambleRebuildCounter > 1) {
1461 --PreambleRebuildCounter;
1465 // Create a temporary file for the precompiled preamble. In rare
1466 // circumstances, this can fail.
1467 std::string PreamblePCHPath = GetPreamblePCHPath();
1468 if (PreamblePCHPath.empty()) {
1469 // Try again next time.
1470 PreambleRebuildCounter = 1;
1474 // We did not previously compute a preamble, or it can't be reused anyway.
1475 SimpleTimer PreambleTimer(WantTiming);
1476 PreambleTimer.setOutput("Precompiling preamble");
1478 // Save the preamble text for later; we'll need to compare against it for
1479 // subsequent reparses.
1480 StringRef MainFilename = FrontendOpts.Inputs[0].getFile();
1481 Preamble.assign(FileMgr->getFile(MainFilename),
1482 NewPreamble.Buffer->getBufferStart(),
1483 NewPreamble.Buffer->getBufferStart() + NewPreamble.Size);
1484 PreambleEndsAtStartOfLine = NewPreamble.PreambleEndsAtStartOfLine;
1486 PreambleBuffer = llvm::MemoryBuffer::getMemBufferCopy(
1487 NewPreamble.Buffer->getBuffer().slice(0, Preamble.size()), MainFilename);
1489 // Remap the main source file to the preamble buffer.
1490 StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
1491 PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer.get());
1493 // Tell the compiler invocation to generate a temporary precompiled header.
1494 FrontendOpts.ProgramAction = frontend::GeneratePCH;
1495 // FIXME: Generate the precompiled header into memory?
1496 FrontendOpts.OutputFile = PreamblePCHPath;
1497 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1498 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1500 // Create the compiler instance to use for building the precompiled preamble.
1501 std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
1503 // Recover resources if we crash before exiting this method.
1504 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1505 CICleanup(Clang.get());
1507 Clang->setInvocation(&*PreambleInvocation);
1508 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1510 // Set up diagnostics, capturing all of the diagnostics produced.
1511 Clang->setDiagnostics(&getDiagnostics());
1513 // Create the target instance.
1514 Clang->setTarget(TargetInfo::CreateTargetInfo(
1515 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1516 if (!Clang->hasTarget()) {
1517 llvm::sys::fs::remove(FrontendOpts.OutputFile);
1519 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1520 PreprocessorOpts.RemappedFileBuffers.pop_back();
1524 // Inform the target of the language options.
1526 // FIXME: We shouldn't need to do this, the target should be immutable once
1527 // created. This complexity should be lifted elsewhere.
1528 Clang->getTarget().adjust(Clang->getLangOpts());
1530 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1531 "Invocation must have exactly one source file!");
1532 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1533 "FIXME: AST inputs not yet supported here!");
1534 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1535 "IR inputs not support here!");
1537 // Clear out old caches and data.
1538 getDiagnostics().Reset();
1539 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1540 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1541 TopLevelDecls.clear();
1542 TopLevelDeclsInPreamble.clear();
1543 PreambleDiagnostics.clear();
1545 IntrusiveRefCntPtr<vfs::FileSystem> VFS =
1546 createVFSFromCompilerInvocation(Clang->getInvocation(), getDiagnostics());
1550 // Create a file manager object to provide access to and cache the filesystem.
1551 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
1553 // Create the source manager.
1554 Clang->setSourceManager(new SourceManager(getDiagnostics(),
1555 Clang->getFileManager()));
1557 auto PreambleDepCollector = std::make_shared<DependencyCollector>();
1558 Clang->addDependencyCollector(PreambleDepCollector);
1560 std::unique_ptr<PrecompilePreambleAction> Act;
1561 Act.reset(new PrecompilePreambleAction(*this));
1562 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1563 llvm::sys::fs::remove(FrontendOpts.OutputFile);
1565 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1566 PreprocessorOpts.RemappedFileBuffers.pop_back();
1572 // Transfer any diagnostics generated when parsing the preamble into the set
1573 // of preamble diagnostics.
1574 for (stored_diag_iterator I = stored_diag_afterDriver_begin(),
1575 E = stored_diag_end();
1577 PreambleDiagnostics.push_back(
1578 makeStandaloneDiagnostic(Clang->getLangOpts(), *I));
1580 Act->EndSourceFile();
1582 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1584 if (!Act->hasEmittedPreamblePCH()) {
1585 // The preamble PCH failed (e.g. there was a module loading fatal error),
1586 // so no precompiled header was generated. Forget that we even tried.
1587 // FIXME: Should we leave a note for ourselves to try again?
1588 llvm::sys::fs::remove(FrontendOpts.OutputFile);
1590 TopLevelDeclsInPreamble.clear();
1591 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1592 PreprocessorOpts.RemappedFileBuffers.pop_back();
1596 // Keep track of the preamble we precompiled.
1597 setPreambleFile(this, FrontendOpts.OutputFile);
1598 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1600 // Keep track of all of the files that the source manager knows about,
1601 // so we can verify whether they have changed or not.
1602 FilesInPreamble.clear();
1603 SourceManager &SourceMgr = Clang->getSourceManager();
1604 for (auto &Filename : PreambleDepCollector->getDependencies()) {
1605 const FileEntry *File = Clang->getFileManager().getFile(Filename);
1606 if (!File || File == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))
1608 if (time_t ModTime = File->getModificationTime()) {
1609 FilesInPreamble[File->getName()] = PreambleFileHash::createForFile(
1610 File->getSize(), ModTime);
1612 llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File);
1613 FilesInPreamble[File->getName()] =
1614 PreambleFileHash::createForMemoryBuffer(Buffer);
1618 PreambleRebuildCounter = 1;
1619 PreprocessorOpts.RemappedFileBuffers.pop_back();
1621 // If the hash of top-level entities differs from the hash of the top-level
1622 // entities the last time we rebuilt the preamble, clear out the completion
1624 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1625 CompletionCacheTopLevelHashValue = 0;
1626 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1629 return llvm::MemoryBuffer::getMemBufferCopy(NewPreamble.Buffer->getBuffer(),
1633 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1634 std::vector<Decl *> Resolved;
1635 Resolved.reserve(TopLevelDeclsInPreamble.size());
1636 ExternalASTSource &Source = *getASTContext().getExternalSource();
1637 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1638 // Resolve the declaration ID to an actual declaration, possibly
1639 // deserializing the declaration in the process.
1640 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1642 Resolved.push_back(D);
1644 TopLevelDeclsInPreamble.clear();
1645 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1648 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1649 // Steal the created target, context, and preprocessor if they have been
1651 assert(CI.hasInvocation() && "missing invocation");
1652 LangOpts = CI.getInvocation().LangOpts;
1653 TheSema = CI.takeSema();
1654 Consumer = CI.takeASTConsumer();
1655 if (CI.hasASTContext())
1656 Ctx = &CI.getASTContext();
1657 if (CI.hasPreprocessor())
1658 PP = &CI.getPreprocessor();
1659 CI.setSourceManager(nullptr);
1660 CI.setFileManager(nullptr);
1662 Target = &CI.getTarget();
1663 Reader = CI.getModuleManager();
1664 HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1667 StringRef ASTUnit::getMainFileName() const {
1668 if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1669 const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1671 return Input.getFile();
1673 return Input.getBuffer()->getBufferIdentifier();
1677 if (const FileEntry *
1678 FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1679 return FE->getName();
1685 StringRef ASTUnit::getASTFileName() const {
1686 if (!isMainFileAST())
1689 serialization::ModuleFile &
1690 Mod = Reader->getModuleManager().getPrimaryModule();
1691 return Mod.FileName;
1694 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1695 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1696 bool CaptureDiagnostics,
1697 bool UserFilesAreVolatile) {
1698 std::unique_ptr<ASTUnit> AST;
1699 AST.reset(new ASTUnit(false));
1700 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1701 AST->Diagnostics = Diags;
1702 AST->Invocation = CI;
1703 AST->FileSystemOpts = CI->getFileSystemOpts();
1704 IntrusiveRefCntPtr<vfs::FileSystem> VFS =
1705 createVFSFromCompilerInvocation(*CI, *Diags);
1708 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1709 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1710 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1711 UserFilesAreVolatile);
1713 return AST.release();
1716 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
1717 CompilerInvocation *CI, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1718 ASTFrontendAction *Action, ASTUnit *Unit, bool Persistent,
1719 StringRef ResourceFilesPath, bool OnlyLocalDecls, bool CaptureDiagnostics,
1720 bool PrecompilePreamble, bool CacheCodeCompletionResults,
1721 bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
1722 std::unique_ptr<ASTUnit> *ErrAST) {
1723 assert(CI && "A CompilerInvocation is required");
1725 std::unique_ptr<ASTUnit> OwnAST;
1726 ASTUnit *AST = Unit;
1728 // Create the AST unit.
1729 OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile));
1735 if (!ResourceFilesPath.empty()) {
1736 // Override the resources path.
1737 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1739 AST->OnlyLocalDecls = OnlyLocalDecls;
1740 AST->CaptureDiagnostics = CaptureDiagnostics;
1741 if (PrecompilePreamble)
1742 AST->PreambleRebuildCounter = 2;
1743 AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1744 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1745 AST->IncludeBriefCommentsInCodeCompletion
1746 = IncludeBriefCommentsInCodeCompletion;
1748 // Recover resources if we crash before exiting this method.
1749 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1750 ASTUnitCleanup(OwnAST.get());
1751 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1752 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1753 DiagCleanup(Diags.get());
1755 // We'll manage file buffers ourselves.
1756 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1757 CI->getFrontendOpts().DisableFree = false;
1758 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1760 // Create the compiler instance to use for building the AST.
1761 std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
1763 // Recover resources if we crash before exiting this method.
1764 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1765 CICleanup(Clang.get());
1767 Clang->setInvocation(CI);
1768 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1770 // Set up diagnostics, capturing any diagnostics that would
1771 // otherwise be dropped.
1772 Clang->setDiagnostics(&AST->getDiagnostics());
1774 // Create the target instance.
1775 Clang->setTarget(TargetInfo::CreateTargetInfo(
1776 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1777 if (!Clang->hasTarget())
1780 // Inform the target of the language options.
1782 // FIXME: We shouldn't need to do this, the target should be immutable once
1783 // created. This complexity should be lifted elsewhere.
1784 Clang->getTarget().adjust(Clang->getLangOpts());
1786 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1787 "Invocation must have exactly one source file!");
1788 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1789 "FIXME: AST inputs not yet supported here!");
1790 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1791 "IR inputs not supported here!");
1793 // Configure the various subsystems.
1794 AST->TheSema.reset();
1797 AST->Reader = nullptr;
1799 // Create a file manager object to provide access to and cache the filesystem.
1800 Clang->setFileManager(&AST->getFileManager());
1802 // Create the source manager.
1803 Clang->setSourceManager(&AST->getSourceManager());
1805 ASTFrontendAction *Act = Action;
1807 std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
1809 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1810 Act = TrackerAct.get();
1813 // Recover resources if we crash before exiting this method.
1814 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1815 ActCleanup(TrackerAct.get());
1817 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1818 AST->transferASTDataFromCompilerInstance(*Clang);
1819 if (OwnAST && ErrAST)
1820 ErrAST->swap(OwnAST);
1825 if (Persistent && !TrackerAct) {
1826 Clang->getPreprocessor().addPPCallbacks(
1827 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
1828 AST->getCurrentTopLevelHashValue()));
1829 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
1830 if (Clang->hasASTConsumer())
1831 Consumers.push_back(Clang->takeASTConsumer());
1832 Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>(
1833 *AST, AST->getCurrentTopLevelHashValue()));
1834 Clang->setASTConsumer(
1835 llvm::make_unique<MultiplexConsumer>(std::move(Consumers)));
1837 if (!Act->Execute()) {
1838 AST->transferASTDataFromCompilerInstance(*Clang);
1839 if (OwnAST && ErrAST)
1840 ErrAST->swap(OwnAST);
1845 // Steal the created target, context, and preprocessor.
1846 AST->transferASTDataFromCompilerInstance(*Clang);
1848 Act->EndSourceFile();
1851 return OwnAST.release();
1856 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1860 // We'll manage file buffers ourselves.
1861 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1862 Invocation->getFrontendOpts().DisableFree = false;
1863 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1865 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1866 if (PrecompilePreamble) {
1867 PreambleRebuildCounter = 2;
1868 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
1871 SimpleTimer ParsingTimer(WantTiming);
1872 ParsingTimer.setOutput("Parsing " + getMainFileName());
1874 // Recover resources if we crash before exiting this method.
1875 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1876 MemBufferCleanup(OverrideMainBuffer.get());
1878 return Parse(std::move(OverrideMainBuffer));
1881 std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
1882 CompilerInvocation *CI, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1883 bool OnlyLocalDecls, bool CaptureDiagnostics, bool PrecompilePreamble,
1884 TranslationUnitKind TUKind, bool CacheCodeCompletionResults,
1885 bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile) {
1886 // Create the AST unit.
1887 std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1888 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1889 AST->Diagnostics = Diags;
1890 AST->OnlyLocalDecls = OnlyLocalDecls;
1891 AST->CaptureDiagnostics = CaptureDiagnostics;
1892 AST->TUKind = TUKind;
1893 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1894 AST->IncludeBriefCommentsInCodeCompletion
1895 = IncludeBriefCommentsInCodeCompletion;
1896 AST->Invocation = CI;
1897 AST->FileSystemOpts = CI->getFileSystemOpts();
1898 IntrusiveRefCntPtr<vfs::FileSystem> VFS =
1899 createVFSFromCompilerInvocation(*CI, *Diags);
1902 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1903 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1905 // Recover resources if we crash before exiting this method.
1906 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1907 ASTUnitCleanup(AST.get());
1908 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1909 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1910 DiagCleanup(Diags.get());
1912 if (AST->LoadFromCompilerInvocation(PrecompilePreamble))
1917 ASTUnit *ASTUnit::LoadFromCommandLine(
1918 const char **ArgBegin, const char **ArgEnd,
1919 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
1920 bool OnlyLocalDecls, bool CaptureDiagnostics,
1921 ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
1922 bool PrecompilePreamble, TranslationUnitKind TUKind,
1923 bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1924 bool AllowPCHWithCompilerErrors, bool SkipFunctionBodies,
1925 bool UserFilesAreVolatile, bool ForSerialization,
1926 std::unique_ptr<ASTUnit> *ErrAST) {
1927 assert(Diags.get() && "no DiagnosticsEngine was provided");
1929 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1931 IntrusiveRefCntPtr<CompilerInvocation> CI;
1935 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1938 CI = clang::createInvocationFromCommandLine(
1939 llvm::makeArrayRef(ArgBegin, ArgEnd),
1945 // Override any files that need remapping
1946 for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
1947 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1948 RemappedFiles[I].second);
1950 PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1951 PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1952 PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1954 // Override the resources path.
1955 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1957 CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
1959 // Create the AST unit.
1960 std::unique_ptr<ASTUnit> AST;
1961 AST.reset(new ASTUnit(false));
1962 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1963 AST->Diagnostics = Diags;
1964 AST->FileSystemOpts = CI->getFileSystemOpts();
1965 IntrusiveRefCntPtr<vfs::FileSystem> VFS =
1966 createVFSFromCompilerInvocation(*CI, *Diags);
1969 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1970 AST->OnlyLocalDecls = OnlyLocalDecls;
1971 AST->CaptureDiagnostics = CaptureDiagnostics;
1972 AST->TUKind = TUKind;
1973 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1974 AST->IncludeBriefCommentsInCodeCompletion
1975 = IncludeBriefCommentsInCodeCompletion;
1976 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1977 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1978 AST->StoredDiagnostics.swap(StoredDiagnostics);
1979 AST->Invocation = CI;
1980 if (ForSerialization)
1981 AST->WriterData.reset(new ASTWriterData());
1982 // Zero out now to ease cleanup during crash recovery.
1986 // Recover resources if we crash before exiting this method.
1987 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1988 ASTUnitCleanup(AST.get());
1990 if (AST->LoadFromCompilerInvocation(PrecompilePreamble)) {
1991 // Some error occurred, if caller wants to examine diagnostics, pass it the
1994 AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
2000 return AST.release();
2003 bool ASTUnit::Reparse(ArrayRef<RemappedFile> RemappedFiles) {
2007 clearFileLevelDecls();
2009 SimpleTimer ParsingTimer(WantTiming);
2010 ParsingTimer.setOutput("Reparsing " + getMainFileName());
2013 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
2014 for (const auto &RB : PPOpts.RemappedFileBuffers)
2017 Invocation->getPreprocessorOpts().clearRemappedFiles();
2018 for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
2019 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
2020 RemappedFiles[I].second);
2023 // If we have a preamble file lying around, or if we might try to
2024 // build a precompiled preamble, do so now.
2025 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2026 if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
2027 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
2029 // Clear out the diagnostics state.
2030 getDiagnostics().Reset();
2031 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
2032 if (OverrideMainBuffer)
2033 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
2035 // Parse the sources
2036 bool Result = Parse(std::move(OverrideMainBuffer));
2038 // If we're caching global code-completion results, and the top-level
2039 // declarations have changed, clear out the code-completion cache.
2040 if (!Result && ShouldCacheCodeCompletionResults &&
2041 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
2042 CacheCodeCompletionResults();
2044 // We now need to clear out the completion info related to this translation
2045 // unit; it'll be recreated if necessary.
2051 //----------------------------------------------------------------------------//
2053 //----------------------------------------------------------------------------//
2056 /// \brief Code completion consumer that combines the cached code-completion
2057 /// results from an ASTUnit with the code-completion results provided to it,
2058 /// then passes the result on to
2059 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
2060 uint64_t NormalContexts;
2062 CodeCompleteConsumer &Next;
2065 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
2066 const CodeCompleteOptions &CodeCompleteOpts)
2067 : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
2068 AST(AST), Next(Next)
2070 // Compute the set of contexts in which we will look when we don't have
2071 // any information about the specific context.
2073 = (1LL << CodeCompletionContext::CCC_TopLevel)
2074 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
2075 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
2076 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
2077 | (1LL << CodeCompletionContext::CCC_Statement)
2078 | (1LL << CodeCompletionContext::CCC_Expression)
2079 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
2080 | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
2081 | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
2082 | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
2083 | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
2084 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
2085 | (1LL << CodeCompletionContext::CCC_Recovery);
2087 if (AST.getASTContext().getLangOpts().CPlusPlus)
2088 NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
2089 | (1LL << CodeCompletionContext::CCC_UnionTag)
2090 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
2093 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
2094 CodeCompletionResult *Results,
2095 unsigned NumResults) override;
2097 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
2098 OverloadCandidate *Candidates,
2099 unsigned NumCandidates) override {
2100 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
2103 CodeCompletionAllocator &getAllocator() override {
2104 return Next.getAllocator();
2107 CodeCompletionTUInfo &getCodeCompletionTUInfo() override {
2108 return Next.getCodeCompletionTUInfo();
2113 /// \brief Helper function that computes which global names are hidden by the
2114 /// local code-completion results.
2115 static void CalculateHiddenNames(const CodeCompletionContext &Context,
2116 CodeCompletionResult *Results,
2117 unsigned NumResults,
2119 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
2120 bool OnlyTagNames = false;
2121 switch (Context.getKind()) {
2122 case CodeCompletionContext::CCC_Recovery:
2123 case CodeCompletionContext::CCC_TopLevel:
2124 case CodeCompletionContext::CCC_ObjCInterface:
2125 case CodeCompletionContext::CCC_ObjCImplementation:
2126 case CodeCompletionContext::CCC_ObjCIvarList:
2127 case CodeCompletionContext::CCC_ClassStructUnion:
2128 case CodeCompletionContext::CCC_Statement:
2129 case CodeCompletionContext::CCC_Expression:
2130 case CodeCompletionContext::CCC_ObjCMessageReceiver:
2131 case CodeCompletionContext::CCC_DotMemberAccess:
2132 case CodeCompletionContext::CCC_ArrowMemberAccess:
2133 case CodeCompletionContext::CCC_ObjCPropertyAccess:
2134 case CodeCompletionContext::CCC_Namespace:
2135 case CodeCompletionContext::CCC_Type:
2136 case CodeCompletionContext::CCC_Name:
2137 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
2138 case CodeCompletionContext::CCC_ParenthesizedExpression:
2139 case CodeCompletionContext::CCC_ObjCInterfaceName:
2142 case CodeCompletionContext::CCC_EnumTag:
2143 case CodeCompletionContext::CCC_UnionTag:
2144 case CodeCompletionContext::CCC_ClassOrStructTag:
2145 OnlyTagNames = true;
2148 case CodeCompletionContext::CCC_ObjCProtocolName:
2149 case CodeCompletionContext::CCC_MacroName:
2150 case CodeCompletionContext::CCC_MacroNameUse:
2151 case CodeCompletionContext::CCC_PreprocessorExpression:
2152 case CodeCompletionContext::CCC_PreprocessorDirective:
2153 case CodeCompletionContext::CCC_NaturalLanguage:
2154 case CodeCompletionContext::CCC_SelectorName:
2155 case CodeCompletionContext::CCC_TypeQualifiers:
2156 case CodeCompletionContext::CCC_Other:
2157 case CodeCompletionContext::CCC_OtherWithMacros:
2158 case CodeCompletionContext::CCC_ObjCInstanceMessage:
2159 case CodeCompletionContext::CCC_ObjCClassMessage:
2160 case CodeCompletionContext::CCC_ObjCCategoryName:
2161 // We're looking for nothing, or we're looking for names that cannot
2166 typedef CodeCompletionResult Result;
2167 for (unsigned I = 0; I != NumResults; ++I) {
2168 if (Results[I].Kind != Result::RK_Declaration)
2172 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2174 bool Hiding = false;
2176 Hiding = (IDNS & Decl::IDNS_Tag);
2178 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2179 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2180 Decl::IDNS_NonMemberOperator);
2181 if (Ctx.getLangOpts().CPlusPlus)
2182 HiddenIDNS |= Decl::IDNS_Tag;
2183 Hiding = (IDNS & HiddenIDNS);
2189 DeclarationName Name = Results[I].Declaration->getDeclName();
2190 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2191 HiddenNames.insert(Identifier->getName());
2193 HiddenNames.insert(Name.getAsString());
2198 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2199 CodeCompletionContext Context,
2200 CodeCompletionResult *Results,
2201 unsigned NumResults) {
2202 // Merge the results we were given with the results we cached.
2203 bool AddedResult = false;
2204 uint64_t InContexts =
2205 Context.getKind() == CodeCompletionContext::CCC_Recovery
2206 ? NormalContexts : (1LL << Context.getKind());
2207 // Contains the set of names that are hidden by "local" completion results.
2208 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2209 typedef CodeCompletionResult Result;
2210 SmallVector<Result, 8> AllResults;
2211 for (ASTUnit::cached_completion_iterator
2212 C = AST.cached_completion_begin(),
2213 CEnd = AST.cached_completion_end();
2215 // If the context we are in matches any of the contexts we are
2216 // interested in, we'll add this result.
2217 if ((C->ShowInContexts & InContexts) == 0)
2220 // If we haven't added any results previously, do so now.
2222 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2224 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2228 // Determine whether this global completion result is hidden by a local
2229 // completion result. If so, skip it.
2230 if (C->Kind != CXCursor_MacroDefinition &&
2231 HiddenNames.count(C->Completion->getTypedText()))
2234 // Adjust priority based on similar type classes.
2235 unsigned Priority = C->Priority;
2236 CodeCompletionString *Completion = C->Completion;
2237 if (!Context.getPreferredType().isNull()) {
2238 if (C->Kind == CXCursor_MacroDefinition) {
2239 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2241 Context.getPreferredType()->isAnyPointerType());
2242 } else if (C->Type) {
2243 CanQualType Expected
2244 = S.Context.getCanonicalType(
2245 Context.getPreferredType().getUnqualifiedType());
2246 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2247 if (ExpectedSTC == C->TypeClass) {
2248 // We know this type is similar; check for an exact match.
2249 llvm::StringMap<unsigned> &CachedCompletionTypes
2250 = AST.getCachedCompletionTypes();
2251 llvm::StringMap<unsigned>::iterator Pos
2252 = CachedCompletionTypes.find(QualType(Expected).getAsString());
2253 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2254 Priority /= CCF_ExactTypeMatch;
2256 Priority /= CCF_SimilarTypeMatch;
2261 // Adjust the completion string, if required.
2262 if (C->Kind == CXCursor_MacroDefinition &&
2263 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2264 // Create a new code-completion string that just contains the
2265 // macro name, without its arguments.
2266 CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2267 CCP_CodePattern, C->Availability);
2268 Builder.AddTypedTextChunk(C->Completion->getTypedText());
2269 Priority = CCP_CodePattern;
2270 Completion = Builder.TakeString();
2273 AllResults.push_back(Result(Completion, Priority, C->Kind,
2277 // If we did not add any cached completion results, just forward the
2278 // results we were given to the next consumer.
2280 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2284 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2290 void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
2291 ArrayRef<RemappedFile> RemappedFiles,
2293 bool IncludeCodePatterns,
2294 bool IncludeBriefComments,
2295 CodeCompleteConsumer &Consumer,
2296 DiagnosticsEngine &Diag, LangOptions &LangOpts,
2297 SourceManager &SourceMgr, FileManager &FileMgr,
2298 SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2299 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2303 SimpleTimer CompletionTimer(WantTiming);
2304 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2305 Twine(Line) + ":" + Twine(Column));
2307 IntrusiveRefCntPtr<CompilerInvocation>
2308 CCInvocation(new CompilerInvocation(*Invocation));
2310 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2311 CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2312 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2314 CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2315 CachedCompletionResults.empty();
2316 CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2317 CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2318 CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2320 assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2322 FrontendOpts.CodeCompletionAt.FileName = File;
2323 FrontendOpts.CodeCompletionAt.Line = Line;
2324 FrontendOpts.CodeCompletionAt.Column = Column;
2326 // Set the language options appropriately.
2327 LangOpts = *CCInvocation->getLangOpts();
2329 // Spell-checking and warnings are wasteful during code-completion.
2330 LangOpts.SpellChecking = false;
2331 CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
2333 std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
2335 // Recover resources if we crash before exiting this method.
2336 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2337 CICleanup(Clang.get());
2339 Clang->setInvocation(&*CCInvocation);
2340 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2342 // Set up diagnostics, capturing any diagnostics produced.
2343 Clang->setDiagnostics(&Diag);
2344 CaptureDroppedDiagnostics Capture(true,
2345 Clang->getDiagnostics(),
2347 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2349 // Create the target instance.
2350 Clang->setTarget(TargetInfo::CreateTargetInfo(
2351 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
2352 if (!Clang->hasTarget()) {
2353 Clang->setInvocation(nullptr);
2357 // Inform the target of the language options.
2359 // FIXME: We shouldn't need to do this, the target should be immutable once
2360 // created. This complexity should be lifted elsewhere.
2361 Clang->getTarget().adjust(Clang->getLangOpts());
2363 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2364 "Invocation must have exactly one source file!");
2365 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
2366 "FIXME: AST inputs not yet supported here!");
2367 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
2368 "IR inputs not support here!");
2371 // Use the source and file managers that we were given.
2372 Clang->setFileManager(&FileMgr);
2373 Clang->setSourceManager(&SourceMgr);
2376 PreprocessorOpts.clearRemappedFiles();
2377 PreprocessorOpts.RetainRemappedFileBuffers = true;
2378 for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
2379 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first,
2380 RemappedFiles[I].second);
2381 OwnedBuffers.push_back(RemappedFiles[I].second);
2384 // Use the code completion consumer we were given, but adding any cached
2385 // code-completion results.
2386 AugmentedCodeCompleteConsumer *AugmentedConsumer
2387 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2388 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2390 // If we have a precompiled preamble, try to use it. We only allow
2391 // the use of the precompiled preamble if we're if the completion
2392 // point is within the main file, after the end of the precompiled
2394 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2395 if (!getPreambleFile(this).empty()) {
2396 std::string CompleteFilePath(File);
2397 llvm::sys::fs::UniqueID CompleteFileID;
2399 if (!llvm::sys::fs::getUniqueID(CompleteFilePath, CompleteFileID)) {
2400 std::string MainPath(OriginalSourceFile);
2401 llvm::sys::fs::UniqueID MainID;
2402 if (!llvm::sys::fs::getUniqueID(MainPath, MainID)) {
2403 if (CompleteFileID == MainID && Line > 1)
2404 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2405 *CCInvocation, false, Line - 1);
2410 // If the main file has been overridden due to the use of a preamble,
2411 // make that override happen and introduce the preamble.
2412 if (OverrideMainBuffer) {
2413 PreprocessorOpts.addRemappedFile(OriginalSourceFile,
2414 OverrideMainBuffer.get());
2415 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2416 PreprocessorOpts.PrecompiledPreambleBytes.second
2417 = PreambleEndsAtStartOfLine;
2418 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
2419 PreprocessorOpts.DisablePCHValidation = true;
2421 OwnedBuffers.push_back(OverrideMainBuffer.release());
2423 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2424 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2427 // Disable the preprocessing record if modules are not enabled.
2428 if (!Clang->getLangOpts().Modules)
2429 PreprocessorOpts.DetailedRecord = false;
2431 std::unique_ptr<SyntaxOnlyAction> Act;
2432 Act.reset(new SyntaxOnlyAction);
2433 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2435 Act->EndSourceFile();
2439 bool ASTUnit::Save(StringRef File) {
2440 if (HadModuleLoaderFatalFailure)
2443 // Write to a temporary file and later rename it to the actual file, to avoid
2444 // possible race conditions.
2445 SmallString<128> TempPath;
2447 TempPath += "-%%%%%%%%";
2449 if (llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath))
2452 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2453 // unconditionally create a stat cache when we parse the file?
2454 llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2458 if (Out.has_error()) {
2463 if (llvm::sys::fs::rename(TempPath.str(), File)) {
2464 llvm::sys::fs::remove(TempPath.str());
2471 static bool serializeUnit(ASTWriter &Writer,
2472 SmallVectorImpl<char> &Buffer,
2476 Writer.WriteAST(S, std::string(), nullptr, "", hasErrors);
2478 // Write the generated bitstream to "Out".
2479 if (!Buffer.empty())
2480 OS.write(Buffer.data(), Buffer.size());
2485 bool ASTUnit::serialize(raw_ostream &OS) {
2486 bool hasErrors = getDiagnostics().hasErrorOccurred();
2489 return serializeUnit(WriterData->Writer, WriterData->Buffer,
2490 getSema(), hasErrors, OS);
2492 SmallString<128> Buffer;
2493 llvm::BitstreamWriter Stream(Buffer);
2494 ASTWriter Writer(Stream);
2495 return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2498 typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
2500 void ASTUnit::TranslateStoredDiagnostics(
2501 FileManager &FileMgr,
2502 SourceManager &SrcMgr,
2503 const SmallVectorImpl<StandaloneDiagnostic> &Diags,
2504 SmallVectorImpl<StoredDiagnostic> &Out) {
2505 // Map the standalone diagnostic into the new source manager. We also need to
2506 // remap all the locations to the new view. This includes the diag location,
2507 // any associated source ranges, and the source ranges of associated fix-its.
2508 // FIXME: There should be a cleaner way to do this.
2510 SmallVector<StoredDiagnostic, 4> Result;
2511 Result.reserve(Diags.size());
2512 for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
2513 // Rebuild the StoredDiagnostic.
2514 const StandaloneDiagnostic &SD = Diags[I];
2515 if (SD.Filename.empty())
2517 const FileEntry *FE = FileMgr.getFile(SD.Filename);
2520 FileID FID = SrcMgr.translateFile(FE);
2521 SourceLocation FileLoc = SrcMgr.getLocForStartOfFile(FID);
2522 if (FileLoc.isInvalid())
2524 SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset);
2525 FullSourceLoc Loc(L, SrcMgr);
2527 SmallVector<CharSourceRange, 4> Ranges;
2528 Ranges.reserve(SD.Ranges.size());
2529 for (std::vector<std::pair<unsigned, unsigned> >::const_iterator
2530 I = SD.Ranges.begin(), E = SD.Ranges.end(); I != E; ++I) {
2531 SourceLocation BL = FileLoc.getLocWithOffset((*I).first);
2532 SourceLocation EL = FileLoc.getLocWithOffset((*I).second);
2533 Ranges.push_back(CharSourceRange::getCharRange(BL, EL));
2536 SmallVector<FixItHint, 2> FixIts;
2537 FixIts.reserve(SD.FixIts.size());
2538 for (std::vector<StandaloneFixIt>::const_iterator
2539 I = SD.FixIts.begin(), E = SD.FixIts.end();
2541 FixIts.push_back(FixItHint());
2542 FixItHint &FH = FixIts.back();
2543 FH.CodeToInsert = I->CodeToInsert;
2544 SourceLocation BL = FileLoc.getLocWithOffset(I->RemoveRange.first);
2545 SourceLocation EL = FileLoc.getLocWithOffset(I->RemoveRange.second);
2546 FH.RemoveRange = CharSourceRange::getCharRange(BL, EL);
2549 Result.push_back(StoredDiagnostic(SD.Level, SD.ID,
2550 SD.Message, Loc, Ranges, FixIts));
2555 void ASTUnit::addFileLevelDecl(Decl *D) {
2558 // We only care about local declarations.
2559 if (D->isFromASTFile())
2562 SourceManager &SM = *SourceMgr;
2563 SourceLocation Loc = D->getLocation();
2564 if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2567 // We only keep track of the file-level declarations of each file.
2568 if (!D->getLexicalDeclContext()->isFileContext())
2571 SourceLocation FileLoc = SM.getFileLoc(Loc);
2572 assert(SM.isLocalSourceLocation(FileLoc));
2575 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2576 if (FID.isInvalid())
2579 LocDeclsTy *&Decls = FileDecls[FID];
2581 Decls = new LocDeclsTy();
2583 std::pair<unsigned, Decl *> LocDecl(Offset, D);
2585 if (Decls->empty() || Decls->back().first <= Offset) {
2586 Decls->push_back(LocDecl);
2590 LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
2591 LocDecl, llvm::less_first());
2593 Decls->insert(I, LocDecl);
2596 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2597 SmallVectorImpl<Decl *> &Decls) {
2598 if (File.isInvalid())
2601 if (SourceMgr->isLoadedFileID(File)) {
2602 assert(Ctx->getExternalSource() && "No external source!");
2603 return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2607 FileDeclsTy::iterator I = FileDecls.find(File);
2608 if (I == FileDecls.end())
2611 LocDeclsTy &LocDecls = *I->second;
2612 if (LocDecls.empty())
2615 LocDeclsTy::iterator BeginIt =
2616 std::lower_bound(LocDecls.begin(), LocDecls.end(),
2617 std::make_pair(Offset, (Decl *)nullptr),
2618 llvm::less_first());
2619 if (BeginIt != LocDecls.begin())
2622 // If we are pointing at a top-level decl inside an objc container, we need
2623 // to backtrack until we find it otherwise we will fail to report that the
2624 // region overlaps with an objc container.
2625 while (BeginIt != LocDecls.begin() &&
2626 BeginIt->second->isTopLevelDeclInObjCContainer())
2629 LocDeclsTy::iterator EndIt = std::upper_bound(
2630 LocDecls.begin(), LocDecls.end(),
2631 std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first());
2632 if (EndIt != LocDecls.end())
2635 for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2636 Decls.push_back(DIt->second);
2639 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2640 unsigned Line, unsigned Col) const {
2641 const SourceManager &SM = getSourceManager();
2642 SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2643 return SM.getMacroArgExpandedLocation(Loc);
2646 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2647 unsigned Offset) const {
2648 const SourceManager &SM = getSourceManager();
2649 SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2650 return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2653 /// \brief If \arg Loc is a loaded location from the preamble, returns
2654 /// the corresponding local location of the main file, otherwise it returns
2656 SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
2659 PreambleID = SourceMgr->getPreambleFileID();
2661 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2665 if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
2666 SourceLocation FileLoc
2667 = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2668 return FileLoc.getLocWithOffset(Offs);
2674 /// \brief If \arg Loc is a local location of the main file but inside the
2675 /// preamble chunk, returns the corresponding loaded location from the
2676 /// preamble, otherwise it returns \arg Loc.
2677 SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
2680 PreambleID = SourceMgr->getPreambleFileID();
2682 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2686 if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2687 Offs < Preamble.size()) {
2688 SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2689 return FileLoc.getLocWithOffset(Offs);
2695 bool ASTUnit::isInPreambleFileID(SourceLocation Loc) {
2698 FID = SourceMgr->getPreambleFileID();
2700 if (Loc.isInvalid() || FID.isInvalid())
2703 return SourceMgr->isInFileID(Loc, FID);
2706 bool ASTUnit::isInMainFileID(SourceLocation Loc) {
2709 FID = SourceMgr->getMainFileID();
2711 if (Loc.isInvalid() || FID.isInvalid())
2714 return SourceMgr->isInFileID(Loc, FID);
2717 SourceLocation ASTUnit::getEndOfPreambleFileID() {
2720 FID = SourceMgr->getPreambleFileID();
2722 if (FID.isInvalid())
2723 return SourceLocation();
2725 return SourceMgr->getLocForEndOfFile(FID);
2728 SourceLocation ASTUnit::getStartOfMainFileID() {
2731 FID = SourceMgr->getMainFileID();
2733 if (FID.isInvalid())
2734 return SourceLocation();
2736 return SourceMgr->getLocForStartOfFile(FID);
2739 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
2740 ASTUnit::getLocalPreprocessingEntities() const {
2741 if (isMainFileAST()) {
2742 serialization::ModuleFile &
2743 Mod = Reader->getModuleManager().getPrimaryModule();
2744 return Reader->getModulePreprocessedEntities(Mod);
2747 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2748 return std::make_pair(PPRec->local_begin(), PPRec->local_end());
2750 return std::make_pair(PreprocessingRecord::iterator(),
2751 PreprocessingRecord::iterator());
2754 bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2755 if (isMainFileAST()) {
2756 serialization::ModuleFile &
2757 Mod = Reader->getModuleManager().getPrimaryModule();
2758 ASTReader::ModuleDeclIterator MDI, MDE;
2759 std::tie(MDI, MDE) = Reader->getModuleFileLevelDecls(Mod);
2760 for (; MDI != MDE; ++MDI) {
2761 if (!Fn(context, *MDI))
2768 for (ASTUnit::top_level_iterator TL = top_level_begin(),
2769 TLEnd = top_level_end();
2770 TL != TLEnd; ++TL) {
2771 if (!Fn(context, *TL))
2779 struct PCHLocatorInfo {
2780 serialization::ModuleFile *Mod;
2781 PCHLocatorInfo() : Mod(nullptr) {}
2785 static bool PCHLocator(serialization::ModuleFile &M, void *UserData) {
2786 PCHLocatorInfo &Info = *static_cast<PCHLocatorInfo*>(UserData);
2788 case serialization::MK_ImplicitModule:
2789 case serialization::MK_ExplicitModule:
2790 return true; // skip dependencies.
2791 case serialization::MK_PCH:
2793 return true; // found it.
2794 case serialization::MK_Preamble:
2795 return false; // look in dependencies.
2796 case serialization::MK_MainFile:
2797 return false; // look in dependencies.
2803 const FileEntry *ASTUnit::getPCHFile() {
2807 PCHLocatorInfo Info;
2808 Reader->getModuleManager().visit(PCHLocator, &Info);
2810 return Info.Mod->File;
2815 bool ASTUnit::isModuleFile() {
2816 return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
2819 void ASTUnit::PreambleData::countLines() const {
2824 for (std::vector<char>::const_iterator
2825 I = Buffer.begin(), E = Buffer.end(); I != E; ++I) {
2829 if (Buffer.back() != '\n')
2834 ASTUnit::ConcurrencyState::ConcurrencyState() {
2835 Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2838 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2839 delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2842 void ASTUnit::ConcurrencyState::start() {
2843 bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2844 assert(acquired && "Concurrent access to ASTUnit!");
2847 void ASTUnit::ConcurrencyState::finish() {
2848 static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2853 ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = 0; }
2854 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2855 void ASTUnit::ConcurrencyState::start() {}
2856 void ASTUnit::ConcurrencyState::finish() {}