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/CommentCommandTraits.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclGroup.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "clang/AST/DeclarationName.h"
25 #include "clang/AST/ExternalASTSource.h"
26 #include "clang/AST/PrettyPrinter.h"
27 #include "clang/AST/Type.h"
28 #include "clang/AST/TypeOrdering.h"
29 #include "clang/Basic/Diagnostic.h"
30 #include "clang/Basic/FileManager.h"
31 #include "clang/Basic/IdentifierTable.h"
32 #include "clang/Basic/LLVM.h"
33 #include "clang/Basic/LangOptions.h"
34 #include "clang/Basic/MemoryBufferCache.h"
35 #include "clang/Basic/Module.h"
36 #include "clang/Basic/SourceLocation.h"
37 #include "clang/Basic/SourceManager.h"
38 #include "clang/Basic/TargetInfo.h"
39 #include "clang/Basic/TargetOptions.h"
40 #include "clang/Frontend/CompilerInstance.h"
41 #include "clang/Frontend/CompilerInvocation.h"
42 #include "clang/Frontend/FrontendAction.h"
43 #include "clang/Frontend/FrontendActions.h"
44 #include "clang/Frontend/FrontendDiagnostic.h"
45 #include "clang/Frontend/FrontendOptions.h"
46 #include "clang/Frontend/MultiplexConsumer.h"
47 #include "clang/Frontend/PrecompiledPreamble.h"
48 #include "clang/Frontend/Utils.h"
49 #include "clang/Lex/HeaderSearch.h"
50 #include "clang/Lex/HeaderSearchOptions.h"
51 #include "clang/Lex/Lexer.h"
52 #include "clang/Lex/PPCallbacks.h"
53 #include "clang/Lex/PreprocessingRecord.h"
54 #include "clang/Lex/Preprocessor.h"
55 #include "clang/Lex/PreprocessorOptions.h"
56 #include "clang/Lex/Token.h"
57 #include "clang/Sema/CodeCompleteConsumer.h"
58 #include "clang/Sema/CodeCompleteOptions.h"
59 #include "clang/Sema/Sema.h"
60 #include "clang/Serialization/ASTBitCodes.h"
61 #include "clang/Serialization/ASTReader.h"
62 #include "clang/Serialization/ASTWriter.h"
63 #include "clang/Serialization/ContinuousRangeMap.h"
64 #include "clang/Serialization/Module.h"
65 #include "clang/Serialization/PCHContainerOperations.h"
66 #include "llvm/ADT/ArrayRef.h"
67 #include "llvm/ADT/DenseMap.h"
68 #include "llvm/ADT/IntrusiveRefCntPtr.h"
69 #include "llvm/ADT/None.h"
70 #include "llvm/ADT/Optional.h"
71 #include "llvm/ADT/STLExtras.h"
72 #include "llvm/ADT/SmallString.h"
73 #include "llvm/ADT/SmallVector.h"
74 #include "llvm/ADT/StringMap.h"
75 #include "llvm/ADT/StringRef.h"
76 #include "llvm/ADT/StringSet.h"
77 #include "llvm/ADT/Twine.h"
78 #include "llvm/ADT/iterator_range.h"
79 #include "llvm/Bitcode/BitstreamWriter.h"
80 #include "llvm/Support/Allocator.h"
81 #include "llvm/Support/Casting.h"
82 #include "llvm/Support/CrashRecoveryContext.h"
83 #include "llvm/Support/DJB.h"
84 #include "llvm/Support/ErrorHandling.h"
85 #include "llvm/Support/ErrorOr.h"
86 #include "llvm/Support/FileSystem.h"
87 #include "llvm/Support/MemoryBuffer.h"
88 #include "llvm/Support/Mutex.h"
89 #include "llvm/Support/Timer.h"
90 #include "llvm/Support/VirtualFileSystem.h"
91 #include "llvm/Support/raw_ostream.h"
104 using namespace clang;
106 using llvm::TimeRecord;
116 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
118 Start = TimeRecord::getCurrentTime();
123 TimeRecord Elapsed = TimeRecord::getCurrentTime();
125 llvm::errs() << Output << ':';
126 Elapsed.print(Elapsed, llvm::errs());
127 llvm::errs() << '\n';
131 void setOutput(const Twine &Output) {
133 this->Output = Output.str();
140 static std::unique_ptr<T> valueOrNull(llvm::ErrorOr<std::unique_ptr<T>> Val) {
143 return std::move(*Val);
147 static bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
150 Output = std::move(*Val);
154 /// Get a source buffer for \p MainFilePath, handling all file-to-file
155 /// and file-to-buffer remappings inside \p Invocation.
156 static std::unique_ptr<llvm::MemoryBuffer>
157 getBufferForFileHandlingRemapping(const CompilerInvocation &Invocation,
158 llvm::vfs::FileSystem *VFS,
159 StringRef FilePath, bool isVolatile) {
160 const auto &PreprocessorOpts = Invocation.getPreprocessorOpts();
162 // Try to determine if the main file has been remapped, either from the
163 // command line (to another file) or directly through the compiler
164 // invocation (to a memory buffer).
165 llvm::MemoryBuffer *Buffer = nullptr;
166 std::unique_ptr<llvm::MemoryBuffer> BufferOwner;
167 auto FileStatus = VFS->status(FilePath);
169 llvm::sys::fs::UniqueID MainFileID = FileStatus->getUniqueID();
171 // Check whether there is a file-file remapping of the main file
172 for (const auto &RF : PreprocessorOpts.RemappedFiles) {
173 std::string MPath(RF.first);
174 auto MPathStatus = VFS->status(MPath);
176 llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
177 if (MainFileID == MID) {
178 // We found a remapping. Try to load the resulting, remapped source.
179 BufferOwner = valueOrNull(VFS->getBufferForFile(RF.second, -1, true, isVolatile));
186 // Check whether there is a file-buffer remapping. It supercedes the
187 // file-file remapping.
188 for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
189 std::string MPath(RB.first);
190 auto MPathStatus = VFS->status(MPath);
192 llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
193 if (MainFileID == MID) {
194 // We found a remapping.
196 Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
202 // If the main source file was not remapped, load it now.
203 if (!Buffer && !BufferOwner) {
204 BufferOwner = valueOrNull(VFS->getBufferForFile(FilePath, -1, true, isVolatile));
213 return llvm::MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(), FilePath);
216 struct ASTUnit::ASTWriterData {
217 SmallString<128> Buffer;
218 llvm::BitstreamWriter Stream;
221 ASTWriterData(MemoryBufferCache &PCMCache)
222 : Stream(Buffer), Writer(Stream, Buffer, PCMCache, {}) {}
225 void ASTUnit::clearFileLevelDecls() {
226 llvm::DeleteContainerSeconds(FileDecls);
229 /// After failing to build a precompiled preamble (due to
230 /// errors in the source that occurs in the preamble), the number of
231 /// reparses during which we'll skip even trying to precompile the
233 const unsigned DefaultPreambleRebuildInterval = 5;
235 /// Tracks the number of ASTUnit objects that are currently active.
237 /// Used for debugging purposes only.
238 static std::atomic<unsigned> ActiveASTUnitObjects;
240 ASTUnit::ASTUnit(bool _MainFileIsAST)
241 : MainFileIsAST(_MainFileIsAST), WantTiming(getenv("LIBCLANG_TIMING")),
242 ShouldCacheCodeCompletionResults(false),
243 IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
244 UnsafeToFree(false) {
245 if (getenv("LIBCLANG_OBJTRACKING"))
246 fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects);
249 ASTUnit::~ASTUnit() {
250 // If we loaded from an AST file, balance out the BeginSourceFile call.
251 if (MainFileIsAST && getDiagnostics().getClient()) {
252 getDiagnostics().getClient()->EndSourceFile();
255 clearFileLevelDecls();
257 // Free the buffers associated with remapped files. We are required to
258 // perform this operation here because we explicitly request that the
259 // compiler instance *not* free these buffers for each invocation of the
261 if (Invocation && OwnsRemappedFileBuffers) {
262 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
263 for (const auto &RB : PPOpts.RemappedFileBuffers)
267 ClearCachedCompletionResults();
269 if (getenv("LIBCLANG_OBJTRACKING"))
270 fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects);
273 void ASTUnit::setPreprocessor(std::shared_ptr<Preprocessor> PP) {
274 this->PP = std::move(PP);
277 void ASTUnit::enableSourceFileDiagnostics() {
278 assert(getDiagnostics().getClient() && Ctx &&
279 "Bad context for source file");
280 getDiagnostics().getClient()->BeginSourceFile(Ctx->getLangOpts(), PP.get());
283 /// Determine the set of code-completion contexts in which this
284 /// declaration should be shown.
285 static uint64_t getDeclShowContexts(const NamedDecl *ND,
286 const LangOptions &LangOpts,
287 bool &IsNestedNameSpecifier) {
288 IsNestedNameSpecifier = false;
290 if (isa<UsingShadowDecl>(ND))
291 ND = ND->getUnderlyingDecl();
295 uint64_t Contexts = 0;
296 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
297 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND) ||
298 isa<TypeAliasTemplateDecl>(ND)) {
299 // Types can appear in these contexts.
300 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
301 Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
302 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
303 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
304 | (1LL << CodeCompletionContext::CCC_Statement)
305 | (1LL << CodeCompletionContext::CCC_Type)
306 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
308 // In C++, types can appear in expressions contexts (for functional casts).
309 if (LangOpts.CPlusPlus)
310 Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
312 // In Objective-C, message sends can send interfaces. In Objective-C++,
313 // all types are available due to functional casts.
314 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
315 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
317 // In Objective-C, you can only be a subclass of another Objective-C class
318 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) {
319 // Objective-C interfaces can be used in a class property expression.
320 if (ID->getDefinition())
321 Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
322 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName);
325 // Deal with tag names.
326 if (isa<EnumDecl>(ND)) {
327 Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
329 // Part of the nested-name-specifier in C++0x.
330 if (LangOpts.CPlusPlus11)
331 IsNestedNameSpecifier = true;
332 } else if (const auto *Record = dyn_cast<RecordDecl>(ND)) {
333 if (Record->isUnion())
334 Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
336 Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
338 if (LangOpts.CPlusPlus)
339 IsNestedNameSpecifier = true;
340 } else if (isa<ClassTemplateDecl>(ND))
341 IsNestedNameSpecifier = true;
342 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
343 // Values can appear in these contexts.
344 Contexts = (1LL << CodeCompletionContext::CCC_Statement)
345 | (1LL << CodeCompletionContext::CCC_Expression)
346 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
347 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
348 } else if (isa<ObjCProtocolDecl>(ND)) {
349 Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName);
350 } else if (isa<ObjCCategoryDecl>(ND)) {
351 Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName);
352 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
353 Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
355 // Part of the nested-name-specifier.
356 IsNestedNameSpecifier = true;
362 void ASTUnit::CacheCodeCompletionResults() {
366 SimpleTimer Timer(WantTiming);
367 Timer.setOutput("Cache global code completions for " + getMainFileName());
369 // Clear out the previous results.
370 ClearCachedCompletionResults();
372 // Gather the set of global code completions.
373 using Result = CodeCompletionResult;
374 SmallVector<Result, 8> Results;
375 CachedCompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
376 CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
377 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
380 // Translate global code completions into cached completions.
381 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
382 CodeCompletionContext CCContext(CodeCompletionContext::CCC_TopLevel);
384 for (auto &R : Results) {
386 case Result::RK_Declaration: {
387 bool IsNestedNameSpecifier = false;
388 CachedCodeCompletionResult CachedResult;
389 CachedResult.Completion = R.CreateCodeCompletionString(
390 *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
391 IncludeBriefCommentsInCodeCompletion);
392 CachedResult.ShowInContexts = getDeclShowContexts(
393 R.Declaration, Ctx->getLangOpts(), IsNestedNameSpecifier);
394 CachedResult.Priority = R.Priority;
395 CachedResult.Kind = R.CursorKind;
396 CachedResult.Availability = R.Availability;
398 // Keep track of the type of this completion in an ASTContext-agnostic
400 QualType UsageType = getDeclUsageType(*Ctx, R.Declaration);
401 if (UsageType.isNull()) {
402 CachedResult.TypeClass = STC_Void;
403 CachedResult.Type = 0;
405 CanQualType CanUsageType
406 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
407 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
409 // Determine whether we have already seen this type. If so, we save
410 // ourselves the work of formatting the type string by using the
411 // temporary, CanQualType-based hash table to find the associated value.
412 unsigned &TypeValue = CompletionTypes[CanUsageType];
413 if (TypeValue == 0) {
414 TypeValue = CompletionTypes.size();
415 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
419 CachedResult.Type = TypeValue;
422 CachedCompletionResults.push_back(CachedResult);
424 /// Handle nested-name-specifiers in C++.
425 if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier &&
426 !R.StartsNestedNameSpecifier) {
427 // The contexts in which a nested-name-specifier can appear in C++.
429 = (1LL << CodeCompletionContext::CCC_TopLevel)
430 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
431 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
432 | (1LL << CodeCompletionContext::CCC_Statement)
433 | (1LL << CodeCompletionContext::CCC_Expression)
434 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
435 | (1LL << CodeCompletionContext::CCC_EnumTag)
436 | (1LL << CodeCompletionContext::CCC_UnionTag)
437 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
438 | (1LL << CodeCompletionContext::CCC_Type)
439 | (1LL << CodeCompletionContext::CCC_Symbol)
440 | (1LL << CodeCompletionContext::CCC_SymbolOrNewName)
441 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
443 if (isa<NamespaceDecl>(R.Declaration) ||
444 isa<NamespaceAliasDecl>(R.Declaration))
445 NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
447 if (uint64_t RemainingContexts
448 = NNSContexts & ~CachedResult.ShowInContexts) {
449 // If there any contexts where this completion can be a
450 // nested-name-specifier but isn't already an option, create a
451 // nested-name-specifier completion.
452 R.StartsNestedNameSpecifier = true;
453 CachedResult.Completion = R.CreateCodeCompletionString(
454 *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
455 IncludeBriefCommentsInCodeCompletion);
456 CachedResult.ShowInContexts = RemainingContexts;
457 CachedResult.Priority = CCP_NestedNameSpecifier;
458 CachedResult.TypeClass = STC_Void;
459 CachedResult.Type = 0;
460 CachedCompletionResults.push_back(CachedResult);
466 case Result::RK_Keyword:
467 case Result::RK_Pattern:
468 // Ignore keywords and patterns; we don't care, since they are so
469 // easily regenerated.
472 case Result::RK_Macro: {
473 CachedCodeCompletionResult CachedResult;
474 CachedResult.Completion = R.CreateCodeCompletionString(
475 *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
476 IncludeBriefCommentsInCodeCompletion);
477 CachedResult.ShowInContexts
478 = (1LL << CodeCompletionContext::CCC_TopLevel)
479 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
480 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
481 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
482 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
483 | (1LL << CodeCompletionContext::CCC_Statement)
484 | (1LL << CodeCompletionContext::CCC_Expression)
485 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
486 | (1LL << CodeCompletionContext::CCC_MacroNameUse)
487 | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
488 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
489 | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
491 CachedResult.Priority = R.Priority;
492 CachedResult.Kind = R.CursorKind;
493 CachedResult.Availability = R.Availability;
494 CachedResult.TypeClass = STC_Void;
495 CachedResult.Type = 0;
496 CachedCompletionResults.push_back(CachedResult);
502 // Save the current top-level hash value.
503 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
506 void ASTUnit::ClearCachedCompletionResults() {
507 CachedCompletionResults.clear();
508 CachedCompletionTypes.clear();
509 CachedCompletionAllocator = nullptr;
514 /// Gathers information from ASTReader that will be used to initialize
516 class ASTInfoCollector : public ASTReaderListener {
519 HeaderSearchOptions &HSOpts;
520 PreprocessorOptions &PPOpts;
521 LangOptions &LangOpt;
522 std::shared_ptr<TargetOptions> &TargetOpts;
523 IntrusiveRefCntPtr<TargetInfo> &Target;
525 bool InitializedLanguage = false;
528 ASTInfoCollector(Preprocessor &PP, ASTContext *Context,
529 HeaderSearchOptions &HSOpts, PreprocessorOptions &PPOpts,
530 LangOptions &LangOpt,
531 std::shared_ptr<TargetOptions> &TargetOpts,
532 IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter)
533 : PP(PP), Context(Context), HSOpts(HSOpts), PPOpts(PPOpts),
534 LangOpt(LangOpt), TargetOpts(TargetOpts), Target(Target),
537 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
538 bool AllowCompatibleDifferences) override {
539 if (InitializedLanguage)
543 InitializedLanguage = true;
549 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
550 StringRef SpecificModuleCachePath,
551 bool Complain) override {
552 this->HSOpts = HSOpts;
556 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain,
557 std::string &SuggestedPredefines) override {
558 this->PPOpts = PPOpts;
562 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
563 bool AllowCompatibleDifferences) override {
564 // If we've already initialized the target, don't do it again.
568 this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts);
570 TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts);
576 void ReadCounter(const serialization::ModuleFile &M,
577 unsigned Value) override {
583 if (!Target || !InitializedLanguage)
586 // Inform the target of the language options.
588 // FIXME: We shouldn't need to do this, the target should be immutable once
589 // created. This complexity should be lifted elsewhere.
590 Target->adjust(LangOpt);
592 // Initialize the preprocessor.
593 PP.Initialize(*Target);
598 // Initialize the ASTContext
599 Context->InitBuiltinTypes(*Target);
601 // Adjust printing policy based on language options.
602 Context->setPrintingPolicy(PrintingPolicy(LangOpt));
604 // We didn't have access to the comment options when the ASTContext was
605 // constructed, so register them now.
606 Context->getCommentCommandTraits().registerCommentOptions(
607 LangOpt.CommentOpts);
611 /// Diagnostic consumer that saves each diagnostic it is given.
612 class StoredDiagnosticConsumer : public DiagnosticConsumer {
613 SmallVectorImpl<StoredDiagnostic> *StoredDiags;
614 SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags;
615 const LangOptions *LangOpts = nullptr;
616 SourceManager *SourceMgr = nullptr;
619 StoredDiagnosticConsumer(
620 SmallVectorImpl<StoredDiagnostic> *StoredDiags,
621 SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags)
622 : StoredDiags(StoredDiags), StandaloneDiags(StandaloneDiags) {
623 assert((StoredDiags || StandaloneDiags) &&
624 "No output collections were passed to StoredDiagnosticConsumer.");
627 void BeginSourceFile(const LangOptions &LangOpts,
628 const Preprocessor *PP = nullptr) override {
629 this->LangOpts = &LangOpts;
631 SourceMgr = &PP->getSourceManager();
634 void HandleDiagnostic(DiagnosticsEngine::Level Level,
635 const Diagnostic &Info) override;
638 /// RAII object that optionally captures diagnostics, if
639 /// there is no diagnostic client to capture them already.
640 class CaptureDroppedDiagnostics {
641 DiagnosticsEngine &Diags;
642 StoredDiagnosticConsumer Client;
643 DiagnosticConsumer *PreviousClient = nullptr;
644 std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
647 CaptureDroppedDiagnostics(
648 bool RequestCapture, DiagnosticsEngine &Diags,
649 SmallVectorImpl<StoredDiagnostic> *StoredDiags,
650 SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags)
651 : Diags(Diags), Client(StoredDiags, StandaloneDiags) {
652 if (RequestCapture || Diags.getClient() == nullptr) {
653 OwningPreviousClient = Diags.takeClient();
654 PreviousClient = Diags.getClient();
655 Diags.setClient(&Client, false);
659 ~CaptureDroppedDiagnostics() {
660 if (Diags.getClient() == &Client)
661 Diags.setClient(PreviousClient, !!OwningPreviousClient.release());
667 static ASTUnit::StandaloneDiagnostic
668 makeStandaloneDiagnostic(const LangOptions &LangOpts,
669 const StoredDiagnostic &InDiag);
671 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
672 const Diagnostic &Info) {
673 // Default implementation (Warnings/errors count).
674 DiagnosticConsumer::HandleDiagnostic(Level, Info);
676 // Only record the diagnostic if it's part of the source manager we know
677 // about. This effectively drops diagnostics from modules we're building.
678 // FIXME: In the long run, ee don't want to drop source managers from modules.
679 if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr) {
680 StoredDiagnostic *ResultDiag = nullptr;
682 StoredDiags->emplace_back(Level, Info);
683 ResultDiag = &StoredDiags->back();
686 if (StandaloneDiags) {
687 llvm::Optional<StoredDiagnostic> StoredDiag = None;
689 StoredDiag.emplace(Level, Info);
690 ResultDiag = StoredDiag.getPointer();
692 StandaloneDiags->push_back(
693 makeStandaloneDiagnostic(*LangOpts, *ResultDiag));
698 IntrusiveRefCntPtr<ASTReader> ASTUnit::getASTReader() const {
702 ASTMutationListener *ASTUnit::getASTMutationListener() {
704 return &WriterData->Writer;
708 ASTDeserializationListener *ASTUnit::getDeserializationListener() {
710 return &WriterData->Writer;
714 std::unique_ptr<llvm::MemoryBuffer>
715 ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) {
717 auto Buffer = FileMgr->getBufferForFile(Filename, UserFilesAreVolatile);
719 return std::move(*Buffer);
721 *ErrorStr = Buffer.getError().message();
725 /// Configure the diagnostics object for use with ASTUnit.
726 void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
727 ASTUnit &AST, bool CaptureDiagnostics) {
728 assert(Diags.get() && "no DiagnosticsEngine was provided");
729 if (CaptureDiagnostics)
730 Diags->setClient(new StoredDiagnosticConsumer(&AST.StoredDiagnostics, nullptr));
733 std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
734 const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
735 WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
736 const FileSystemOptions &FileSystemOpts, bool UseDebugInfo,
737 bool OnlyLocalDecls, ArrayRef<RemappedFile> RemappedFiles,
738 bool CaptureDiagnostics, bool AllowPCHWithCompilerErrors,
739 bool UserFilesAreVolatile) {
740 std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
742 // Recover resources if we crash before exiting this method.
743 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
744 ASTUnitCleanup(AST.get());
745 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
746 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
747 DiagCleanup(Diags.get());
749 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
751 AST->LangOpts = std::make_shared<LangOptions>();
752 AST->OnlyLocalDecls = OnlyLocalDecls;
753 AST->CaptureDiagnostics = CaptureDiagnostics;
754 AST->Diagnostics = Diags;
755 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
756 llvm::vfs::getRealFileSystem();
757 AST->FileMgr = new FileManager(FileSystemOpts, VFS);
758 AST->UserFilesAreVolatile = UserFilesAreVolatile;
759 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
760 AST->getFileManager(),
761 UserFilesAreVolatile);
762 AST->PCMCache = new MemoryBufferCache;
763 AST->HSOpts = std::make_shared<HeaderSearchOptions>();
764 AST->HSOpts->ModuleFormat = PCHContainerRdr.getFormat();
765 AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
766 AST->getSourceManager(),
767 AST->getDiagnostics(),
769 /*Target=*/nullptr));
770 AST->PPOpts = std::make_shared<PreprocessorOptions>();
772 for (const auto &RemappedFile : RemappedFiles)
773 AST->PPOpts->addRemappedFile(RemappedFile.first, RemappedFile.second);
775 // Gather Info for preprocessor construction later on.
777 HeaderSearch &HeaderInfo = *AST->HeaderInfo;
780 AST->PP = std::make_shared<Preprocessor>(
781 AST->PPOpts, AST->getDiagnostics(), *AST->LangOpts,
782 AST->getSourceManager(), *AST->PCMCache, HeaderInfo, AST->ModuleLoader,
783 /*IILookup=*/nullptr,
784 /*OwnsHeaderSearch=*/false);
785 Preprocessor &PP = *AST->PP;
787 if (ToLoad >= LoadASTOnly)
788 AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(),
789 PP.getIdentifierTable(), PP.getSelectorTable(),
790 PP.getBuiltinInfo());
792 bool disableValid = false;
793 if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
795 AST->Reader = new ASTReader(PP, AST->Ctx.get(), PCHContainerRdr, {},
797 /*DisableValidation=*/disableValid,
798 AllowPCHWithCompilerErrors);
800 AST->Reader->setListener(llvm::make_unique<ASTInfoCollector>(
801 *AST->PP, AST->Ctx.get(), *AST->HSOpts, *AST->PPOpts, *AST->LangOpts,
802 AST->TargetOpts, AST->Target, Counter));
804 // Attach the AST reader to the AST context as an external AST
805 // source, so that declarations will be deserialized from the
806 // AST file as needed.
807 // We need the external source to be set up before we read the AST, because
808 // eagerly-deserialized declarations may use it.
810 AST->Ctx->setExternalSource(AST->Reader);
812 switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile,
813 SourceLocation(), ASTReader::ARR_None)) {
814 case ASTReader::Success:
817 case ASTReader::Failure:
818 case ASTReader::Missing:
819 case ASTReader::OutOfDate:
820 case ASTReader::VersionMismatch:
821 case ASTReader::ConfigurationMismatch:
822 case ASTReader::HadErrors:
823 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
827 AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile();
829 PP.setCounterValue(Counter);
831 // Create an AST consumer, even though it isn't used.
832 if (ToLoad >= LoadASTOnly)
833 AST->Consumer.reset(new ASTConsumer);
835 // Create a semantic analysis object and tell the AST reader about it.
836 if (ToLoad >= LoadEverything) {
837 AST->TheSema.reset(new Sema(PP, *AST->Ctx, *AST->Consumer));
838 AST->TheSema->Initialize();
839 AST->Reader->InitializeSema(*AST->TheSema);
842 // Tell the diagnostic client that we have started a source file.
843 AST->getDiagnostics().getClient()->BeginSourceFile(PP.getLangOpts(), &PP);
848 /// Add the given macro to the hash of all top-level entities.
849 static void AddDefinedMacroToHash(const Token &MacroNameTok, unsigned &Hash) {
850 Hash = llvm::djbHash(MacroNameTok.getIdentifierInfo()->getName(), Hash);
855 /// Preprocessor callback class that updates a hash value with the names
856 /// of all macros that have been defined by the translation unit.
857 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
861 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) {}
863 void MacroDefined(const Token &MacroNameTok,
864 const MacroDirective *MD) override {
865 AddDefinedMacroToHash(MacroNameTok, Hash);
871 /// Add the given declaration to the hash of all top-level entities.
872 static void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
876 DeclContext *DC = D->getDeclContext();
880 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
883 if (const auto *ND = dyn_cast<NamedDecl>(D)) {
884 if (const auto *EnumD = dyn_cast<EnumDecl>(D)) {
885 // For an unscoped enum include the enumerators in the hash since they
886 // enter the top-level namespace.
887 if (!EnumD->isScoped()) {
888 for (const auto *EI : EnumD->enumerators()) {
889 if (EI->getIdentifier())
890 Hash = llvm::djbHash(EI->getIdentifier()->getName(), Hash);
895 if (ND->getIdentifier())
896 Hash = llvm::djbHash(ND->getIdentifier()->getName(), Hash);
897 else if (DeclarationName Name = ND->getDeclName()) {
898 std::string NameStr = Name.getAsString();
899 Hash = llvm::djbHash(NameStr, Hash);
904 if (const auto *ImportD = dyn_cast<ImportDecl>(D)) {
905 if (const Module *Mod = ImportD->getImportedModule()) {
906 std::string ModName = Mod->getFullModuleName();
907 Hash = llvm::djbHash(ModName, Hash);
915 class TopLevelDeclTrackerConsumer : public ASTConsumer {
920 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
921 : Unit(_Unit), Hash(Hash) {
925 void handleTopLevelDecl(Decl *D) {
929 // FIXME: Currently ObjC method declarations are incorrectly being
930 // reported as top-level declarations, even though their DeclContext
931 // is the containing ObjC @interface/@implementation. This is a
932 // fundamental problem in the parser right now.
933 if (isa<ObjCMethodDecl>(D))
936 AddTopLevelDeclarationToHash(D, Hash);
937 Unit.addTopLevelDecl(D);
939 handleFileLevelDecl(D);
942 void handleFileLevelDecl(Decl *D) {
943 Unit.addFileLevelDecl(D);
944 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
945 for (auto *I : NSD->decls())
946 handleFileLevelDecl(I);
950 bool HandleTopLevelDecl(DeclGroupRef D) override {
951 for (auto *TopLevelDecl : D)
952 handleTopLevelDecl(TopLevelDecl);
956 // We're not interested in "interesting" decls.
957 void HandleInterestingDecl(DeclGroupRef) override {}
959 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
960 for (auto *TopLevelDecl : D)
961 handleTopLevelDecl(TopLevelDecl);
964 ASTMutationListener *GetASTMutationListener() override {
965 return Unit.getASTMutationListener();
968 ASTDeserializationListener *GetASTDeserializationListener() override {
969 return Unit.getDeserializationListener();
973 class TopLevelDeclTrackerAction : public ASTFrontendAction {
977 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
978 StringRef InFile) override {
979 CI.getPreprocessor().addPPCallbacks(
980 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
981 Unit.getCurrentTopLevelHashValue()));
982 return llvm::make_unique<TopLevelDeclTrackerConsumer>(
983 Unit, Unit.getCurrentTopLevelHashValue());
987 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
989 bool hasCodeCompletionSupport() const override { return false; }
991 TranslationUnitKind getTranslationUnitKind() override {
992 return Unit.getTranslationUnitKind();
996 class ASTUnitPreambleCallbacks : public PreambleCallbacks {
998 unsigned getHash() const { return Hash; }
1000 std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
1002 std::vector<serialization::DeclID> takeTopLevelDeclIDs() {
1003 return std::move(TopLevelDeclIDs);
1006 void AfterPCHEmitted(ASTWriter &Writer) override {
1007 TopLevelDeclIDs.reserve(TopLevelDecls.size());
1008 for (const auto *D : TopLevelDecls) {
1009 // Invalid top-level decls may not have been serialized.
1010 if (D->isInvalidDecl())
1012 TopLevelDeclIDs.push_back(Writer.getDeclID(D));
1016 void HandleTopLevelDecl(DeclGroupRef DG) override {
1017 for (auto *D : DG) {
1018 // FIXME: Currently ObjC method declarations are incorrectly being
1019 // reported as top-level declarations, even though their DeclContext
1020 // is the containing ObjC @interface/@implementation. This is a
1021 // fundamental problem in the parser right now.
1022 if (isa<ObjCMethodDecl>(D))
1024 AddTopLevelDeclarationToHash(D, Hash);
1025 TopLevelDecls.push_back(D);
1029 std::unique_ptr<PPCallbacks> createPPCallbacks() override {
1030 return llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(Hash);
1035 std::vector<Decl *> TopLevelDecls;
1036 std::vector<serialization::DeclID> TopLevelDeclIDs;
1037 llvm::SmallVector<ASTUnit::StandaloneDiagnostic, 4> PreambleDiags;
1042 static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
1043 return StoredDiag.getLocation().isValid();
1047 checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
1048 // Get rid of stored diagnostics except the ones from the driver which do not
1049 // have a source location.
1051 std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
1055 static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
1057 SourceManager &SM) {
1058 // The stored diagnostic has the old source manager in it; update
1059 // the locations to refer into the new source manager. Since we've
1060 // been careful to make sure that the source manager's state
1061 // before and after are identical, so that we can reuse the source
1063 for (auto &SD : StoredDiagnostics) {
1064 if (SD.getLocation().isValid()) {
1065 FullSourceLoc Loc(SD.getLocation(), SM);
1066 SD.setLocation(Loc);
1071 /// Parse the source file into a translation unit using the given compiler
1072 /// invocation, replacing the current translation unit.
1074 /// \returns True if a failure occurred that causes the ASTUnit not to
1075 /// contain any translation-unit information, false otherwise.
1076 bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1077 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
1078 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1082 auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
1083 if (OverrideMainBuffer) {
1085 "No preamble was built, but OverrideMainBuffer is not null");
1086 IntrusiveRefCntPtr<llvm::vfs::FileSystem> OldVFS = VFS;
1087 Preamble->AddImplicitPreamble(*CCInvocation, VFS, OverrideMainBuffer.get());
1088 if (OldVFS != VFS && FileMgr) {
1089 assert(OldVFS == FileMgr->getVirtualFileSystem() &&
1090 "VFS passed to Parse and VFS in FileMgr are different");
1091 FileMgr = new FileManager(FileMgr->getFileSystemOpts(), VFS);
1095 // Create the compiler instance to use for building the AST.
1096 std::unique_ptr<CompilerInstance> Clang(
1097 new CompilerInstance(std::move(PCHContainerOps)));
1098 if (FileMgr && VFS) {
1099 assert(VFS == FileMgr->getVirtualFileSystem() &&
1100 "VFS passed to Parse and VFS in FileMgr are different");
1102 Clang->setVirtualFileSystem(VFS);
1105 // Recover resources if we crash before exiting this method.
1106 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1107 CICleanup(Clang.get());
1109 Clang->setInvocation(CCInvocation);
1110 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1112 // Set up diagnostics, capturing any diagnostics that would
1113 // otherwise be dropped.
1114 Clang->setDiagnostics(&getDiagnostics());
1116 // Create the target instance.
1117 Clang->setTarget(TargetInfo::CreateTargetInfo(
1118 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1119 if (!Clang->hasTarget())
1122 // Inform the target of the language options.
1124 // FIXME: We shouldn't need to do this, the target should be immutable once
1125 // created. This complexity should be lifted elsewhere.
1126 Clang->getTarget().adjust(Clang->getLangOpts());
1128 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1129 "Invocation must have exactly one source file!");
1130 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1131 InputKind::Source &&
1132 "FIXME: AST inputs not yet supported here!");
1133 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1134 InputKind::LLVM_IR &&
1135 "IR inputs not support here!");
1137 // Configure the various subsystems.
1138 LangOpts = Clang->getInvocation().LangOpts;
1139 FileSystemOpts = Clang->getFileSystemOpts();
1141 Clang->createFileManager();
1142 FileMgr = &Clang->getFileManager();
1147 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1148 UserFilesAreVolatile);
1149 if (!OverrideMainBuffer) {
1150 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1151 TopLevelDeclsInPreamble.clear();
1154 // Create a file manager object to provide access to and cache the filesystem.
1155 Clang->setFileManager(&getFileManager());
1157 // Create the source manager.
1158 Clang->setSourceManager(&getSourceManager());
1160 // If the main file has been overridden due to the use of a preamble,
1161 // make that override happen and introduce the preamble.
1162 if (OverrideMainBuffer) {
1163 // The stored diagnostic has the old source manager in it; update
1164 // the locations to refer into the new source manager. Since we've
1165 // been careful to make sure that the source manager's state
1166 // before and after are identical, so that we can reuse the source
1168 checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1170 // Keep track of the override buffer;
1171 SavedMainFileBuffer = std::move(OverrideMainBuffer);
1174 std::unique_ptr<TopLevelDeclTrackerAction> Act(
1175 new TopLevelDeclTrackerAction(*this));
1177 // Recover resources if we crash before exiting this method.
1178 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1179 ActCleanup(Act.get());
1181 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1184 if (SavedMainFileBuffer)
1185 TranslateStoredDiagnostics(getFileManager(), getSourceManager(),
1186 PreambleDiagnostics, StoredDiagnostics);
1188 PreambleSrcLocCache.clear();
1190 if (!Act->Execute())
1193 transferASTDataFromCompilerInstance(*Clang);
1195 Act->EndSourceFile();
1197 FailedParseDiagnostics.clear();
1202 // Remove the overridden buffer we used for the preamble.
1203 SavedMainFileBuffer = nullptr;
1205 // Keep the ownership of the data in the ASTUnit because the client may
1206 // want to see the diagnostics.
1207 transferASTDataFromCompilerInstance(*Clang);
1208 FailedParseDiagnostics.swap(StoredDiagnostics);
1209 StoredDiagnostics.clear();
1210 NumStoredDiagnosticsFromDriver = 0;
1214 static std::pair<unsigned, unsigned>
1215 makeStandaloneRange(CharSourceRange Range, const SourceManager &SM,
1216 const LangOptions &LangOpts) {
1217 CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts);
1218 unsigned Offset = SM.getFileOffset(FileRange.getBegin());
1219 unsigned EndOffset = SM.getFileOffset(FileRange.getEnd());
1220 return std::make_pair(Offset, EndOffset);
1223 static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM,
1224 const LangOptions &LangOpts,
1225 const FixItHint &InFix) {
1226 ASTUnit::StandaloneFixIt OutFix;
1227 OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts);
1228 OutFix.InsertFromRange = makeStandaloneRange(InFix.InsertFromRange, SM,
1230 OutFix.CodeToInsert = InFix.CodeToInsert;
1231 OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions;
1235 static ASTUnit::StandaloneDiagnostic
1236 makeStandaloneDiagnostic(const LangOptions &LangOpts,
1237 const StoredDiagnostic &InDiag) {
1238 ASTUnit::StandaloneDiagnostic OutDiag;
1239 OutDiag.ID = InDiag.getID();
1240 OutDiag.Level = InDiag.getLevel();
1241 OutDiag.Message = InDiag.getMessage();
1242 OutDiag.LocOffset = 0;
1243 if (InDiag.getLocation().isInvalid())
1245 const SourceManager &SM = InDiag.getLocation().getManager();
1246 SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation());
1247 OutDiag.Filename = SM.getFilename(FileLoc);
1248 if (OutDiag.Filename.empty())
1250 OutDiag.LocOffset = SM.getFileOffset(FileLoc);
1251 for (const auto &Range : InDiag.getRanges())
1252 OutDiag.Ranges.push_back(makeStandaloneRange(Range, SM, LangOpts));
1253 for (const auto &FixIt : InDiag.getFixIts())
1254 OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, FixIt));
1259 /// Attempt to build or re-use a precompiled preamble when (re-)parsing
1260 /// the source file.
1262 /// This routine will compute the preamble of the main source file. If a
1263 /// non-trivial preamble is found, it will precompile that preamble into a
1264 /// precompiled header so that the precompiled preamble can be used to reduce
1265 /// reparsing time. If a precompiled preamble has already been constructed,
1266 /// this routine will determine if it is still valid and, if so, avoid
1267 /// rebuilding the precompiled preamble.
1269 /// \param AllowRebuild When true (the default), this routine is
1270 /// allowed to rebuild the precompiled preamble if it is found to be
1273 /// \param MaxLines When non-zero, the maximum number of lines that
1274 /// can occur within the preamble.
1276 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1277 /// buffer that should be used in place of the main file when doing so.
1278 /// Otherwise, returns a NULL pointer.
1279 std::unique_ptr<llvm::MemoryBuffer>
1280 ASTUnit::getMainBufferWithPrecompiledPreamble(
1281 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1282 CompilerInvocation &PreambleInvocationIn,
1283 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool AllowRebuild,
1284 unsigned MaxLines) {
1286 PreambleInvocationIn.getFrontendOpts().Inputs[0].getFile();
1287 std::unique_ptr<llvm::MemoryBuffer> MainFileBuffer =
1288 getBufferForFileHandlingRemapping(PreambleInvocationIn, VFS.get(),
1289 MainFilePath, UserFilesAreVolatile);
1290 if (!MainFileBuffer)
1293 PreambleBounds Bounds =
1294 ComputePreambleBounds(*PreambleInvocationIn.getLangOpts(),
1295 MainFileBuffer.get(), MaxLines);
1300 if (Preamble->CanReuse(PreambleInvocationIn, MainFileBuffer.get(), Bounds,
1302 // Okay! We can re-use the precompiled preamble.
1304 // Set the state of the diagnostic object to mimic its state
1305 // after parsing the preamble.
1306 getDiagnostics().Reset();
1307 ProcessWarningOptions(getDiagnostics(),
1308 PreambleInvocationIn.getDiagnosticOpts());
1309 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1311 PreambleRebuildCounter = 1;
1312 return MainFileBuffer;
1315 PreambleDiagnostics.clear();
1316 TopLevelDeclsInPreamble.clear();
1317 PreambleSrcLocCache.clear();
1318 PreambleRebuildCounter = 1;
1322 // If the preamble rebuild counter > 1, it's because we previously
1323 // failed to build a preamble and we're not yet ready to try
1324 // again. Decrement the counter and return a failure.
1325 if (PreambleRebuildCounter > 1) {
1326 --PreambleRebuildCounter;
1330 assert(!Preamble && "No Preamble should be stored at that point");
1331 // If we aren't allowed to rebuild the precompiled preamble, just
1336 SmallVector<StandaloneDiagnostic, 4> NewPreambleDiagsStandalone;
1337 SmallVector<StoredDiagnostic, 4> NewPreambleDiags;
1338 ASTUnitPreambleCallbacks Callbacks;
1340 llvm::Optional<CaptureDroppedDiagnostics> Capture;
1341 if (CaptureDiagnostics)
1342 Capture.emplace(/*RequestCapture=*/true, *Diagnostics, &NewPreambleDiags,
1343 &NewPreambleDiagsStandalone);
1345 // We did not previously compute a preamble, or it can't be reused anyway.
1346 SimpleTimer PreambleTimer(WantTiming);
1347 PreambleTimer.setOutput("Precompiling preamble");
1349 const bool PreviousSkipFunctionBodies =
1350 PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies;
1351 if (SkipFunctionBodies == SkipFunctionBodiesScope::Preamble)
1352 PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies = true;
1354 llvm::ErrorOr<PrecompiledPreamble> NewPreamble = PrecompiledPreamble::Build(
1355 PreambleInvocationIn, MainFileBuffer.get(), Bounds, *Diagnostics, VFS,
1356 PCHContainerOps, /*StoreInMemory=*/false, Callbacks);
1358 PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies =
1359 PreviousSkipFunctionBodies;
1362 Preamble = std::move(*NewPreamble);
1363 PreambleRebuildCounter = 1;
1365 switch (static_cast<BuildPreambleError>(NewPreamble.getError().value())) {
1366 case BuildPreambleError::CouldntCreateTempFile:
1367 // Try again next time.
1368 PreambleRebuildCounter = 1;
1370 case BuildPreambleError::CouldntCreateTargetInfo:
1371 case BuildPreambleError::BeginSourceFileFailed:
1372 case BuildPreambleError::CouldntEmitPCH:
1373 // These erros are more likely to repeat, retry after some period.
1374 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1377 llvm_unreachable("unexpected BuildPreambleError");
1381 assert(Preamble && "Preamble wasn't built");
1383 TopLevelDecls.clear();
1384 TopLevelDeclsInPreamble = Callbacks.takeTopLevelDeclIDs();
1385 PreambleTopLevelHashValue = Callbacks.getHash();
1387 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1389 checkAndRemoveNonDriverDiags(NewPreambleDiags);
1390 StoredDiagnostics = std::move(NewPreambleDiags);
1391 PreambleDiagnostics = std::move(NewPreambleDiagsStandalone);
1393 // If the hash of top-level entities differs from the hash of the top-level
1394 // entities the last time we rebuilt the preamble, clear out the completion
1396 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1397 CompletionCacheTopLevelHashValue = 0;
1398 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1401 return MainFileBuffer;
1404 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1405 assert(Preamble && "Should only be called when preamble was built");
1407 std::vector<Decl *> Resolved;
1408 Resolved.reserve(TopLevelDeclsInPreamble.size());
1409 ExternalASTSource &Source = *getASTContext().getExternalSource();
1410 for (const auto TopLevelDecl : TopLevelDeclsInPreamble) {
1411 // Resolve the declaration ID to an actual declaration, possibly
1412 // deserializing the declaration in the process.
1413 if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1414 Resolved.push_back(D);
1416 TopLevelDeclsInPreamble.clear();
1417 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1420 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1421 // Steal the created target, context, and preprocessor if they have been
1423 assert(CI.hasInvocation() && "missing invocation");
1424 LangOpts = CI.getInvocation().LangOpts;
1425 TheSema = CI.takeSema();
1426 Consumer = CI.takeASTConsumer();
1427 if (CI.hasASTContext())
1428 Ctx = &CI.getASTContext();
1429 if (CI.hasPreprocessor())
1430 PP = CI.getPreprocessorPtr();
1431 CI.setSourceManager(nullptr);
1432 CI.setFileManager(nullptr);
1434 Target = &CI.getTarget();
1435 Reader = CI.getModuleManager();
1436 HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1439 StringRef ASTUnit::getMainFileName() const {
1440 if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1441 const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1443 return Input.getFile();
1445 return Input.getBuffer()->getBufferIdentifier();
1449 if (const FileEntry *
1450 FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1451 return FE->getName();
1457 StringRef ASTUnit::getASTFileName() const {
1458 if (!isMainFileAST())
1461 serialization::ModuleFile &
1462 Mod = Reader->getModuleManager().getPrimaryModule();
1463 return Mod.FileName;
1466 std::unique_ptr<ASTUnit>
1467 ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
1468 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1469 bool CaptureDiagnostics, bool UserFilesAreVolatile) {
1470 std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1471 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1472 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1473 createVFSFromCompilerInvocation(*CI, *Diags);
1474 AST->Diagnostics = Diags;
1475 AST->FileSystemOpts = CI->getFileSystemOpts();
1476 AST->Invocation = std::move(CI);
1477 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1478 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1479 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1480 UserFilesAreVolatile);
1481 AST->PCMCache = new MemoryBufferCache;
1486 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
1487 std::shared_ptr<CompilerInvocation> CI,
1488 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1489 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FrontendAction *Action,
1490 ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
1491 bool OnlyLocalDecls, bool CaptureDiagnostics,
1492 unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults,
1493 bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
1494 std::unique_ptr<ASTUnit> *ErrAST) {
1495 assert(CI && "A CompilerInvocation is required");
1497 std::unique_ptr<ASTUnit> OwnAST;
1498 ASTUnit *AST = Unit;
1500 // Create the AST unit.
1501 OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile);
1507 if (!ResourceFilesPath.empty()) {
1508 // Override the resources path.
1509 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1511 AST->OnlyLocalDecls = OnlyLocalDecls;
1512 AST->CaptureDiagnostics = CaptureDiagnostics;
1513 if (PrecompilePreambleAfterNParses > 0)
1514 AST->PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1515 AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1516 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1517 AST->IncludeBriefCommentsInCodeCompletion
1518 = IncludeBriefCommentsInCodeCompletion;
1520 // Recover resources if we crash before exiting this method.
1521 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1522 ASTUnitCleanup(OwnAST.get());
1523 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1524 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1525 DiagCleanup(Diags.get());
1527 // We'll manage file buffers ourselves.
1528 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1529 CI->getFrontendOpts().DisableFree = false;
1530 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1532 // Create the compiler instance to use for building the AST.
1533 std::unique_ptr<CompilerInstance> Clang(
1534 new CompilerInstance(std::move(PCHContainerOps)));
1536 // Recover resources if we crash before exiting this method.
1537 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1538 CICleanup(Clang.get());
1540 Clang->setInvocation(std::move(CI));
1541 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1543 // Set up diagnostics, capturing any diagnostics that would
1544 // otherwise be dropped.
1545 Clang->setDiagnostics(&AST->getDiagnostics());
1547 // Create the target instance.
1548 Clang->setTarget(TargetInfo::CreateTargetInfo(
1549 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1550 if (!Clang->hasTarget())
1553 // Inform the target of the language options.
1555 // FIXME: We shouldn't need to do this, the target should be immutable once
1556 // created. This complexity should be lifted elsewhere.
1557 Clang->getTarget().adjust(Clang->getLangOpts());
1559 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1560 "Invocation must have exactly one source file!");
1561 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1562 InputKind::Source &&
1563 "FIXME: AST inputs not yet supported here!");
1564 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1565 InputKind::LLVM_IR &&
1566 "IR inputs not support here!");
1568 // Configure the various subsystems.
1569 AST->TheSema.reset();
1572 AST->Reader = nullptr;
1574 // Create a file manager object to provide access to and cache the filesystem.
1575 Clang->setFileManager(&AST->getFileManager());
1577 // Create the source manager.
1578 Clang->setSourceManager(&AST->getSourceManager());
1580 FrontendAction *Act = Action;
1582 std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
1584 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1585 Act = TrackerAct.get();
1588 // Recover resources if we crash before exiting this method.
1589 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1590 ActCleanup(TrackerAct.get());
1592 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1593 AST->transferASTDataFromCompilerInstance(*Clang);
1594 if (OwnAST && ErrAST)
1595 ErrAST->swap(OwnAST);
1600 if (Persistent && !TrackerAct) {
1601 Clang->getPreprocessor().addPPCallbacks(
1602 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
1603 AST->getCurrentTopLevelHashValue()));
1604 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
1605 if (Clang->hasASTConsumer())
1606 Consumers.push_back(Clang->takeASTConsumer());
1607 Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>(
1608 *AST, AST->getCurrentTopLevelHashValue()));
1609 Clang->setASTConsumer(
1610 llvm::make_unique<MultiplexConsumer>(std::move(Consumers)));
1612 if (!Act->Execute()) {
1613 AST->transferASTDataFromCompilerInstance(*Clang);
1614 if (OwnAST && ErrAST)
1615 ErrAST->swap(OwnAST);
1620 // Steal the created target, context, and preprocessor.
1621 AST->transferASTDataFromCompilerInstance(*Clang);
1623 Act->EndSourceFile();
1626 return OwnAST.release();
1631 bool ASTUnit::LoadFromCompilerInvocation(
1632 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1633 unsigned PrecompilePreambleAfterNParses,
1634 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1638 assert(VFS && "VFS is null");
1640 // We'll manage file buffers ourselves.
1641 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1642 Invocation->getFrontendOpts().DisableFree = false;
1643 getDiagnostics().Reset();
1644 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1646 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1647 if (PrecompilePreambleAfterNParses > 0) {
1648 PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1649 OverrideMainBuffer =
1650 getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1651 getDiagnostics().Reset();
1652 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1655 SimpleTimer ParsingTimer(WantTiming);
1656 ParsingTimer.setOutput("Parsing " + getMainFileName());
1658 // Recover resources if we crash before exiting this method.
1659 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1660 MemBufferCleanup(OverrideMainBuffer.get());
1662 return Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1665 std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
1666 std::shared_ptr<CompilerInvocation> CI,
1667 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1668 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
1669 bool OnlyLocalDecls, bool CaptureDiagnostics,
1670 unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1671 bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1672 bool UserFilesAreVolatile) {
1673 // Create the AST unit.
1674 std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1675 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1676 AST->Diagnostics = Diags;
1677 AST->OnlyLocalDecls = OnlyLocalDecls;
1678 AST->CaptureDiagnostics = CaptureDiagnostics;
1679 AST->TUKind = TUKind;
1680 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1681 AST->IncludeBriefCommentsInCodeCompletion
1682 = IncludeBriefCommentsInCodeCompletion;
1683 AST->Invocation = std::move(CI);
1684 AST->FileSystemOpts = FileMgr->getFileSystemOpts();
1685 AST->FileMgr = FileMgr;
1686 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1688 // Recover resources if we crash before exiting this method.
1689 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1690 ASTUnitCleanup(AST.get());
1691 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1692 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1693 DiagCleanup(Diags.get());
1695 if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1696 PrecompilePreambleAfterNParses,
1697 AST->FileMgr->getVirtualFileSystem()))
1702 ASTUnit *ASTUnit::LoadFromCommandLine(
1703 const char **ArgBegin, const char **ArgEnd,
1704 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1705 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
1706 bool OnlyLocalDecls, bool CaptureDiagnostics,
1707 ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
1708 unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1709 bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1710 bool AllowPCHWithCompilerErrors, SkipFunctionBodiesScope SkipFunctionBodies,
1711 bool SingleFileParse, bool UserFilesAreVolatile, bool ForSerialization,
1712 llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST,
1713 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1714 assert(Diags.get() && "no DiagnosticsEngine was provided");
1716 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1718 std::shared_ptr<CompilerInvocation> CI;
1721 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1722 &StoredDiagnostics, nullptr);
1724 CI = createInvocationFromCommandLine(
1725 llvm::makeArrayRef(ArgBegin, ArgEnd), Diags, VFS);
1730 // Override any files that need remapping
1731 for (const auto &RemappedFile : RemappedFiles) {
1732 CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1733 RemappedFile.second);
1735 PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1736 PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1737 PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1738 PPOpts.SingleFileParseMode = SingleFileParse;
1740 // Override the resources path.
1741 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1743 CI->getFrontendOpts().SkipFunctionBodies =
1744 SkipFunctionBodies == SkipFunctionBodiesScope::PreambleAndMainFile;
1747 CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue();
1749 // Create the AST unit.
1750 std::unique_ptr<ASTUnit> AST;
1751 AST.reset(new ASTUnit(false));
1752 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1753 AST->StoredDiagnostics.swap(StoredDiagnostics);
1754 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1755 AST->Diagnostics = Diags;
1756 AST->FileSystemOpts = CI->getFileSystemOpts();
1758 VFS = llvm::vfs::getRealFileSystem();
1759 VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS);
1760 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1761 AST->PCMCache = new MemoryBufferCache;
1762 AST->OnlyLocalDecls = OnlyLocalDecls;
1763 AST->CaptureDiagnostics = CaptureDiagnostics;
1764 AST->TUKind = TUKind;
1765 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1766 AST->IncludeBriefCommentsInCodeCompletion
1767 = IncludeBriefCommentsInCodeCompletion;
1768 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1769 AST->Invocation = CI;
1770 AST->SkipFunctionBodies = SkipFunctionBodies;
1771 if (ForSerialization)
1772 AST->WriterData.reset(new ASTWriterData(*AST->PCMCache));
1773 // Zero out now to ease cleanup during crash recovery.
1777 // Recover resources if we crash before exiting this method.
1778 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1779 ASTUnitCleanup(AST.get());
1781 if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1782 PrecompilePreambleAfterNParses,
1784 // Some error occurred, if caller wants to examine diagnostics, pass it the
1787 AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
1793 return AST.release();
1796 bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1797 ArrayRef<RemappedFile> RemappedFiles,
1798 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1803 assert(FileMgr && "FileMgr is null on Reparse call");
1804 VFS = FileMgr->getVirtualFileSystem();
1807 clearFileLevelDecls();
1809 SimpleTimer ParsingTimer(WantTiming);
1810 ParsingTimer.setOutput("Reparsing " + getMainFileName());
1813 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1814 for (const auto &RB : PPOpts.RemappedFileBuffers)
1817 Invocation->getPreprocessorOpts().clearRemappedFiles();
1818 for (const auto &RemappedFile : RemappedFiles) {
1819 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1820 RemappedFile.second);
1823 // If we have a preamble file lying around, or if we might try to
1824 // build a precompiled preamble, do so now.
1825 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1826 if (Preamble || PreambleRebuildCounter > 0)
1827 OverrideMainBuffer =
1828 getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1830 // Clear out the diagnostics state.
1832 getDiagnostics().Reset();
1833 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1834 if (OverrideMainBuffer)
1835 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1837 // Parse the sources
1839 Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1841 // If we're caching global code-completion results, and the top-level
1842 // declarations have changed, clear out the code-completion cache.
1843 if (!Result && ShouldCacheCodeCompletionResults &&
1844 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1845 CacheCodeCompletionResults();
1847 // We now need to clear out the completion info related to this translation
1848 // unit; it'll be recreated if necessary.
1854 void ASTUnit::ResetForParse() {
1855 SavedMainFileBuffer.reset();
1863 TopLevelDecls.clear();
1864 clearFileLevelDecls();
1867 //----------------------------------------------------------------------------//
1869 //----------------------------------------------------------------------------//
1873 /// Code completion consumer that combines the cached code-completion
1874 /// results from an ASTUnit with the code-completion results provided to it,
1875 /// then passes the result on to
1876 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1877 uint64_t NormalContexts;
1879 CodeCompleteConsumer &Next;
1882 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1883 const CodeCompleteOptions &CodeCompleteOpts)
1884 : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
1885 AST(AST), Next(Next) {
1886 // Compute the set of contexts in which we will look when we don't have
1887 // any information about the specific context.
1889 = (1LL << CodeCompletionContext::CCC_TopLevel)
1890 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
1891 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
1892 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
1893 | (1LL << CodeCompletionContext::CCC_Statement)
1894 | (1LL << CodeCompletionContext::CCC_Expression)
1895 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
1896 | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
1897 | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
1898 | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
1899 | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
1900 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
1901 | (1LL << CodeCompletionContext::CCC_Recovery);
1903 if (AST.getASTContext().getLangOpts().CPlusPlus)
1904 NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
1905 | (1LL << CodeCompletionContext::CCC_UnionTag)
1906 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
1909 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
1910 CodeCompletionResult *Results,
1911 unsigned NumResults) override;
1913 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1914 OverloadCandidate *Candidates,
1915 unsigned NumCandidates,
1916 SourceLocation OpenParLoc) override {
1917 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates,
1921 CodeCompletionAllocator &getAllocator() override {
1922 return Next.getAllocator();
1925 CodeCompletionTUInfo &getCodeCompletionTUInfo() override {
1926 return Next.getCodeCompletionTUInfo();
1932 /// Helper function that computes which global names are hidden by the
1933 /// local code-completion results.
1934 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1935 CodeCompletionResult *Results,
1936 unsigned NumResults,
1938 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1939 bool OnlyTagNames = false;
1940 switch (Context.getKind()) {
1941 case CodeCompletionContext::CCC_Recovery:
1942 case CodeCompletionContext::CCC_TopLevel:
1943 case CodeCompletionContext::CCC_ObjCInterface:
1944 case CodeCompletionContext::CCC_ObjCImplementation:
1945 case CodeCompletionContext::CCC_ObjCIvarList:
1946 case CodeCompletionContext::CCC_ClassStructUnion:
1947 case CodeCompletionContext::CCC_Statement:
1948 case CodeCompletionContext::CCC_Expression:
1949 case CodeCompletionContext::CCC_ObjCMessageReceiver:
1950 case CodeCompletionContext::CCC_DotMemberAccess:
1951 case CodeCompletionContext::CCC_ArrowMemberAccess:
1952 case CodeCompletionContext::CCC_ObjCPropertyAccess:
1953 case CodeCompletionContext::CCC_Namespace:
1954 case CodeCompletionContext::CCC_Type:
1955 case CodeCompletionContext::CCC_Symbol:
1956 case CodeCompletionContext::CCC_SymbolOrNewName:
1957 case CodeCompletionContext::CCC_ParenthesizedExpression:
1958 case CodeCompletionContext::CCC_ObjCInterfaceName:
1961 case CodeCompletionContext::CCC_EnumTag:
1962 case CodeCompletionContext::CCC_UnionTag:
1963 case CodeCompletionContext::CCC_ClassOrStructTag:
1964 OnlyTagNames = true;
1967 case CodeCompletionContext::CCC_ObjCProtocolName:
1968 case CodeCompletionContext::CCC_MacroName:
1969 case CodeCompletionContext::CCC_MacroNameUse:
1970 case CodeCompletionContext::CCC_PreprocessorExpression:
1971 case CodeCompletionContext::CCC_PreprocessorDirective:
1972 case CodeCompletionContext::CCC_NaturalLanguage:
1973 case CodeCompletionContext::CCC_SelectorName:
1974 case CodeCompletionContext::CCC_TypeQualifiers:
1975 case CodeCompletionContext::CCC_Other:
1976 case CodeCompletionContext::CCC_OtherWithMacros:
1977 case CodeCompletionContext::CCC_ObjCInstanceMessage:
1978 case CodeCompletionContext::CCC_ObjCClassMessage:
1979 case CodeCompletionContext::CCC_ObjCCategoryName:
1980 case CodeCompletionContext::CCC_IncludedFile:
1981 case CodeCompletionContext::CCC_NewName:
1982 // We're looking for nothing, or we're looking for names that cannot
1987 using Result = CodeCompletionResult;
1988 for (unsigned I = 0; I != NumResults; ++I) {
1989 if (Results[I].Kind != Result::RK_Declaration)
1993 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
1995 bool Hiding = false;
1997 Hiding = (IDNS & Decl::IDNS_Tag);
1999 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2000 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2001 Decl::IDNS_NonMemberOperator);
2002 if (Ctx.getLangOpts().CPlusPlus)
2003 HiddenIDNS |= Decl::IDNS_Tag;
2004 Hiding = (IDNS & HiddenIDNS);
2010 DeclarationName Name = Results[I].Declaration->getDeclName();
2011 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2012 HiddenNames.insert(Identifier->getName());
2014 HiddenNames.insert(Name.getAsString());
2018 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2019 CodeCompletionContext Context,
2020 CodeCompletionResult *Results,
2021 unsigned NumResults) {
2022 // Merge the results we were given with the results we cached.
2023 bool AddedResult = false;
2024 uint64_t InContexts =
2025 Context.getKind() == CodeCompletionContext::CCC_Recovery
2026 ? NormalContexts : (1LL << Context.getKind());
2027 // Contains the set of names that are hidden by "local" completion results.
2028 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2029 using Result = CodeCompletionResult;
2030 SmallVector<Result, 8> AllResults;
2031 for (ASTUnit::cached_completion_iterator
2032 C = AST.cached_completion_begin(),
2033 CEnd = AST.cached_completion_end();
2035 // If the context we are in matches any of the contexts we are
2036 // interested in, we'll add this result.
2037 if ((C->ShowInContexts & InContexts) == 0)
2040 // If we haven't added any results previously, do so now.
2042 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2044 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2048 // Determine whether this global completion result is hidden by a local
2049 // completion result. If so, skip it.
2050 if (C->Kind != CXCursor_MacroDefinition &&
2051 HiddenNames.count(C->Completion->getTypedText()))
2054 // Adjust priority based on similar type classes.
2055 unsigned Priority = C->Priority;
2056 CodeCompletionString *Completion = C->Completion;
2057 if (!Context.getPreferredType().isNull()) {
2058 if (C->Kind == CXCursor_MacroDefinition) {
2059 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2061 Context.getPreferredType()->isAnyPointerType());
2062 } else if (C->Type) {
2063 CanQualType Expected
2064 = S.Context.getCanonicalType(
2065 Context.getPreferredType().getUnqualifiedType());
2066 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2067 if (ExpectedSTC == C->TypeClass) {
2068 // We know this type is similar; check for an exact match.
2069 llvm::StringMap<unsigned> &CachedCompletionTypes
2070 = AST.getCachedCompletionTypes();
2071 llvm::StringMap<unsigned>::iterator Pos
2072 = CachedCompletionTypes.find(QualType(Expected).getAsString());
2073 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2074 Priority /= CCF_ExactTypeMatch;
2076 Priority /= CCF_SimilarTypeMatch;
2081 // Adjust the completion string, if required.
2082 if (C->Kind == CXCursor_MacroDefinition &&
2083 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2084 // Create a new code-completion string that just contains the
2085 // macro name, without its arguments.
2086 CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2087 CCP_CodePattern, C->Availability);
2088 Builder.AddTypedTextChunk(C->Completion->getTypedText());
2089 Priority = CCP_CodePattern;
2090 Completion = Builder.TakeString();
2093 AllResults.push_back(Result(Completion, Priority, C->Kind,
2097 // If we did not add any cached completion results, just forward the
2098 // results we were given to the next consumer.
2100 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2104 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2108 void ASTUnit::CodeComplete(
2109 StringRef File, unsigned Line, unsigned Column,
2110 ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
2111 bool IncludeCodePatterns, bool IncludeBriefComments,
2112 CodeCompleteConsumer &Consumer,
2113 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2114 DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr,
2115 FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2116 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2120 SimpleTimer CompletionTimer(WantTiming);
2121 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2122 Twine(Line) + ":" + Twine(Column));
2124 auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
2126 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2127 CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2128 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2130 CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2131 CachedCompletionResults.empty();
2132 CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2133 CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2134 CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2135 CodeCompleteOpts.LoadExternal = Consumer.loadExternal();
2136 CodeCompleteOpts.IncludeFixIts = Consumer.includeFixIts();
2138 assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2140 FrontendOpts.CodeCompletionAt.FileName = File;
2141 FrontendOpts.CodeCompletionAt.Line = Line;
2142 FrontendOpts.CodeCompletionAt.Column = Column;
2144 // Set the language options appropriately.
2145 LangOpts = *CCInvocation->getLangOpts();
2147 // Spell-checking and warnings are wasteful during code-completion.
2148 LangOpts.SpellChecking = false;
2149 CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
2151 std::unique_ptr<CompilerInstance> Clang(
2152 new CompilerInstance(PCHContainerOps));
2154 // Recover resources if we crash before exiting this method.
2155 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2156 CICleanup(Clang.get());
2158 auto &Inv = *CCInvocation;
2159 Clang->setInvocation(std::move(CCInvocation));
2160 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2162 // Set up diagnostics, capturing any diagnostics produced.
2163 Clang->setDiagnostics(&Diag);
2164 CaptureDroppedDiagnostics Capture(true,
2165 Clang->getDiagnostics(),
2166 &StoredDiagnostics, nullptr);
2167 ProcessWarningOptions(Diag, Inv.getDiagnosticOpts());
2169 // Create the target instance.
2170 Clang->setTarget(TargetInfo::CreateTargetInfo(
2171 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
2172 if (!Clang->hasTarget()) {
2173 Clang->setInvocation(nullptr);
2177 // Inform the target of the language options.
2179 // FIXME: We shouldn't need to do this, the target should be immutable once
2180 // created. This complexity should be lifted elsewhere.
2181 Clang->getTarget().adjust(Clang->getLangOpts());
2183 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2184 "Invocation must have exactly one source file!");
2185 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
2186 InputKind::Source &&
2187 "FIXME: AST inputs not yet supported here!");
2188 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
2189 InputKind::LLVM_IR &&
2190 "IR inputs not support here!");
2192 // Use the source and file managers that we were given.
2193 Clang->setFileManager(&FileMgr);
2194 Clang->setSourceManager(&SourceMgr);
2197 PreprocessorOpts.clearRemappedFiles();
2198 PreprocessorOpts.RetainRemappedFileBuffers = true;
2199 for (const auto &RemappedFile : RemappedFiles) {
2200 PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second);
2201 OwnedBuffers.push_back(RemappedFile.second);
2204 // Use the code completion consumer we were given, but adding any cached
2205 // code-completion results.
2206 AugmentedCodeCompleteConsumer *AugmentedConsumer
2207 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2208 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2210 // If we have a precompiled preamble, try to use it. We only allow
2211 // the use of the precompiled preamble if we're if the completion
2212 // point is within the main file, after the end of the precompiled
2214 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2216 std::string CompleteFilePath(File);
2218 auto VFS = FileMgr.getVirtualFileSystem();
2219 auto CompleteFileStatus = VFS->status(CompleteFilePath);
2220 if (CompleteFileStatus) {
2221 llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID();
2223 std::string MainPath(OriginalSourceFile);
2224 auto MainStatus = VFS->status(MainPath);
2226 llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID();
2227 if (CompleteFileID == MainID && Line > 1)
2228 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2229 PCHContainerOps, Inv, VFS, false, Line - 1);
2234 // If the main file has been overridden due to the use of a preamble,
2235 // make that override happen and introduce the preamble.
2236 if (OverrideMainBuffer) {
2238 "No preamble was built, but OverrideMainBuffer is not null");
2240 auto VFS = FileMgr.getVirtualFileSystem();
2241 Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS,
2242 OverrideMainBuffer.get());
2243 // FIXME: there is no way to update VFS if it was changed by
2244 // AddImplicitPreamble as FileMgr is accepted as a parameter by this method.
2245 // We use on-disk preambles instead and rely on FileMgr's VFS to ensure the
2246 // PCH files are always readable.
2247 OwnedBuffers.push_back(OverrideMainBuffer.release());
2249 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2250 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2253 // Disable the preprocessing record if modules are not enabled.
2254 if (!Clang->getLangOpts().Modules)
2255 PreprocessorOpts.DetailedRecord = false;
2257 std::unique_ptr<SyntaxOnlyAction> Act;
2258 Act.reset(new SyntaxOnlyAction);
2259 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2261 Act->EndSourceFile();
2265 bool ASTUnit::Save(StringRef File) {
2266 if (HadModuleLoaderFatalFailure)
2269 // Write to a temporary file and later rename it to the actual file, to avoid
2270 // possible race conditions.
2271 SmallString<128> TempPath;
2273 TempPath += "-%%%%%%%%";
2275 if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath))
2278 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2279 // unconditionally create a stat cache when we parse the file?
2280 llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2284 if (Out.has_error()) {
2289 if (llvm::sys::fs::rename(TempPath, File)) {
2290 llvm::sys::fs::remove(TempPath);
2297 static bool serializeUnit(ASTWriter &Writer,
2298 SmallVectorImpl<char> &Buffer,
2302 Writer.WriteAST(S, std::string(), nullptr, "", hasErrors);
2304 // Write the generated bitstream to "Out".
2305 if (!Buffer.empty())
2306 OS.write(Buffer.data(), Buffer.size());
2311 bool ASTUnit::serialize(raw_ostream &OS) {
2312 // For serialization we are lenient if the errors were only warn-as-error kind.
2313 bool hasErrors = getDiagnostics().hasUncompilableErrorOccurred();
2316 return serializeUnit(WriterData->Writer, WriterData->Buffer,
2317 getSema(), hasErrors, OS);
2319 SmallString<128> Buffer;
2320 llvm::BitstreamWriter Stream(Buffer);
2321 MemoryBufferCache PCMCache;
2322 ASTWriter Writer(Stream, Buffer, PCMCache, {});
2323 return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2326 using SLocRemap = ContinuousRangeMap<unsigned, int, 2>;
2328 void ASTUnit::TranslateStoredDiagnostics(
2329 FileManager &FileMgr,
2330 SourceManager &SrcMgr,
2331 const SmallVectorImpl<StandaloneDiagnostic> &Diags,
2332 SmallVectorImpl<StoredDiagnostic> &Out) {
2333 // Map the standalone diagnostic into the new source manager. We also need to
2334 // remap all the locations to the new view. This includes the diag location,
2335 // any associated source ranges, and the source ranges of associated fix-its.
2336 // FIXME: There should be a cleaner way to do this.
2337 SmallVector<StoredDiagnostic, 4> Result;
2338 Result.reserve(Diags.size());
2340 for (const auto &SD : Diags) {
2341 // Rebuild the StoredDiagnostic.
2342 if (SD.Filename.empty())
2344 const FileEntry *FE = FileMgr.getFile(SD.Filename);
2347 SourceLocation FileLoc;
2348 auto ItFileID = PreambleSrcLocCache.find(SD.Filename);
2349 if (ItFileID == PreambleSrcLocCache.end()) {
2350 FileID FID = SrcMgr.translateFile(FE);
2351 FileLoc = SrcMgr.getLocForStartOfFile(FID);
2352 PreambleSrcLocCache[SD.Filename] = FileLoc;
2354 FileLoc = ItFileID->getValue();
2357 if (FileLoc.isInvalid())
2359 SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset);
2360 FullSourceLoc Loc(L, SrcMgr);
2362 SmallVector<CharSourceRange, 4> Ranges;
2363 Ranges.reserve(SD.Ranges.size());
2364 for (const auto &Range : SD.Ranges) {
2365 SourceLocation BL = FileLoc.getLocWithOffset(Range.first);
2366 SourceLocation EL = FileLoc.getLocWithOffset(Range.second);
2367 Ranges.push_back(CharSourceRange::getCharRange(BL, EL));
2370 SmallVector<FixItHint, 2> FixIts;
2371 FixIts.reserve(SD.FixIts.size());
2372 for (const auto &FixIt : SD.FixIts) {
2373 FixIts.push_back(FixItHint());
2374 FixItHint &FH = FixIts.back();
2375 FH.CodeToInsert = FixIt.CodeToInsert;
2376 SourceLocation BL = FileLoc.getLocWithOffset(FixIt.RemoveRange.first);
2377 SourceLocation EL = FileLoc.getLocWithOffset(FixIt.RemoveRange.second);
2378 FH.RemoveRange = CharSourceRange::getCharRange(BL, EL);
2381 Result.push_back(StoredDiagnostic(SD.Level, SD.ID,
2382 SD.Message, Loc, Ranges, FixIts));
2387 void ASTUnit::addFileLevelDecl(Decl *D) {
2390 // We only care about local declarations.
2391 if (D->isFromASTFile())
2394 SourceManager &SM = *SourceMgr;
2395 SourceLocation Loc = D->getLocation();
2396 if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2399 // We only keep track of the file-level declarations of each file.
2400 if (!D->getLexicalDeclContext()->isFileContext())
2403 SourceLocation FileLoc = SM.getFileLoc(Loc);
2404 assert(SM.isLocalSourceLocation(FileLoc));
2407 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2408 if (FID.isInvalid())
2411 LocDeclsTy *&Decls = FileDecls[FID];
2413 Decls = new LocDeclsTy();
2415 std::pair<unsigned, Decl *> LocDecl(Offset, D);
2417 if (Decls->empty() || Decls->back().first <= Offset) {
2418 Decls->push_back(LocDecl);
2422 LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
2423 LocDecl, llvm::less_first());
2425 Decls->insert(I, LocDecl);
2428 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2429 SmallVectorImpl<Decl *> &Decls) {
2430 if (File.isInvalid())
2433 if (SourceMgr->isLoadedFileID(File)) {
2434 assert(Ctx->getExternalSource() && "No external source!");
2435 return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2439 FileDeclsTy::iterator I = FileDecls.find(File);
2440 if (I == FileDecls.end())
2443 LocDeclsTy &LocDecls = *I->second;
2444 if (LocDecls.empty())
2447 LocDeclsTy::iterator BeginIt =
2448 std::lower_bound(LocDecls.begin(), LocDecls.end(),
2449 std::make_pair(Offset, (Decl *)nullptr),
2450 llvm::less_first());
2451 if (BeginIt != LocDecls.begin())
2454 // If we are pointing at a top-level decl inside an objc container, we need
2455 // to backtrack until we find it otherwise we will fail to report that the
2456 // region overlaps with an objc container.
2457 while (BeginIt != LocDecls.begin() &&
2458 BeginIt->second->isTopLevelDeclInObjCContainer())
2461 LocDeclsTy::iterator EndIt = std::upper_bound(
2462 LocDecls.begin(), LocDecls.end(),
2463 std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first());
2464 if (EndIt != LocDecls.end())
2467 for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2468 Decls.push_back(DIt->second);
2471 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2472 unsigned Line, unsigned Col) const {
2473 const SourceManager &SM = getSourceManager();
2474 SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2475 return SM.getMacroArgExpandedLocation(Loc);
2478 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2479 unsigned Offset) const {
2480 const SourceManager &SM = getSourceManager();
2481 SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2482 return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2485 /// If \arg Loc is a loaded location from the preamble, returns
2486 /// the corresponding local location of the main file, otherwise it returns
2488 SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) const {
2491 PreambleID = SourceMgr->getPreambleFileID();
2493 if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2497 if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble->getBounds().Size) {
2498 SourceLocation FileLoc
2499 = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2500 return FileLoc.getLocWithOffset(Offs);
2506 /// If \arg Loc is a local location of the main file but inside the
2507 /// preamble chunk, returns the corresponding loaded location from the
2508 /// preamble, otherwise it returns \arg Loc.
2509 SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) const {
2512 PreambleID = SourceMgr->getPreambleFileID();
2514 if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2518 if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2519 Offs < Preamble->getBounds().Size) {
2520 SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2521 return FileLoc.getLocWithOffset(Offs);
2527 bool ASTUnit::isInPreambleFileID(SourceLocation Loc) const {
2530 FID = SourceMgr->getPreambleFileID();
2532 if (Loc.isInvalid() || FID.isInvalid())
2535 return SourceMgr->isInFileID(Loc, FID);
2538 bool ASTUnit::isInMainFileID(SourceLocation Loc) const {
2541 FID = SourceMgr->getMainFileID();
2543 if (Loc.isInvalid() || FID.isInvalid())
2546 return SourceMgr->isInFileID(Loc, FID);
2549 SourceLocation ASTUnit::getEndOfPreambleFileID() const {
2552 FID = SourceMgr->getPreambleFileID();
2554 if (FID.isInvalid())
2557 return SourceMgr->getLocForEndOfFile(FID);
2560 SourceLocation ASTUnit::getStartOfMainFileID() const {
2563 FID = SourceMgr->getMainFileID();
2565 if (FID.isInvalid())
2568 return SourceMgr->getLocForStartOfFile(FID);
2571 llvm::iterator_range<PreprocessingRecord::iterator>
2572 ASTUnit::getLocalPreprocessingEntities() const {
2573 if (isMainFileAST()) {
2574 serialization::ModuleFile &
2575 Mod = Reader->getModuleManager().getPrimaryModule();
2576 return Reader->getModulePreprocessedEntities(Mod);
2579 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2580 return llvm::make_range(PPRec->local_begin(), PPRec->local_end());
2582 return llvm::make_range(PreprocessingRecord::iterator(),
2583 PreprocessingRecord::iterator());
2586 bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2587 if (isMainFileAST()) {
2588 serialization::ModuleFile &
2589 Mod = Reader->getModuleManager().getPrimaryModule();
2590 for (const auto *D : Reader->getModuleFileLevelDecls(Mod)) {
2591 if (!Fn(context, D))
2598 for (ASTUnit::top_level_iterator TL = top_level_begin(),
2599 TLEnd = top_level_end();
2600 TL != TLEnd; ++TL) {
2601 if (!Fn(context, *TL))
2608 const FileEntry *ASTUnit::getPCHFile() {
2612 serialization::ModuleFile *Mod = nullptr;
2613 Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) {
2615 case serialization::MK_ImplicitModule:
2616 case serialization::MK_ExplicitModule:
2617 case serialization::MK_PrebuiltModule:
2618 return true; // skip dependencies.
2619 case serialization::MK_PCH:
2621 return true; // found it.
2622 case serialization::MK_Preamble:
2623 return false; // look in dependencies.
2624 case serialization::MK_MainFile:
2625 return false; // look in dependencies.
2636 bool ASTUnit::isModuleFile() const {
2637 return isMainFileAST() && getLangOpts().isCompilingModule();
2640 InputKind ASTUnit::getInputKind() const {
2641 auto &LangOpts = getLangOpts();
2643 InputKind::Language Lang;
2644 if (LangOpts.OpenCL)
2645 Lang = InputKind::OpenCL;
2646 else if (LangOpts.CUDA)
2647 Lang = InputKind::CUDA;
2648 else if (LangOpts.RenderScript)
2649 Lang = InputKind::RenderScript;
2650 else if (LangOpts.CPlusPlus)
2651 Lang = LangOpts.ObjC ? InputKind::ObjCXX : InputKind::CXX;
2653 Lang = LangOpts.ObjC ? InputKind::ObjC : InputKind::C;
2655 InputKind::Format Fmt = InputKind::Source;
2656 if (LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap)
2657 Fmt = InputKind::ModuleMap;
2659 // We don't know if input was preprocessed. Assume not.
2662 return InputKind(Lang, Fmt, PP);
2666 ASTUnit::ConcurrencyState::ConcurrencyState() {
2667 Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2670 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2671 delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2674 void ASTUnit::ConcurrencyState::start() {
2675 bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2676 assert(acquired && "Concurrent access to ASTUnit!");
2679 void ASTUnit::ConcurrencyState::finish() {
2680 static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2685 ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = nullptr; }
2686 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2687 void ASTUnit::ConcurrencyState::start() {}
2688 void ASTUnit::ConcurrencyState::finish() {}