Index: include/clang/Frontend/PrecompiledPreamble.h =================================================================== --- include/clang/Frontend/PrecompiledPreamble.h +++ include/clang/Frontend/PrecompiledPreamble.h @@ -77,7 +77,7 @@ const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds, DiagnosticsEngine &Diagnostics, IntrusiveRefCntPtr VFS, std::shared_ptr PCHContainerOps, - PreambleCallbacks &Callbacks); + PreambleCallbacks &Callbacks, std::unique_ptr PPCallbacks); PrecompiledPreamble(PrecompiledPreamble &&) = default; PrecompiledPreamble &operator=(PrecompiledPreamble &&) = default; Index: lib/Frontend/ASTUnit.cpp =================================================================== --- lib/Frontend/ASTUnit.cpp +++ lib/Frontend/ASTUnit.cpp @@ -53,54 +53,52 @@ using llvm::TimeRecord; namespace { - class SimpleTimer { - bool WantTiming; - TimeRecord Start; - std::string Output; - - public: - explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) { - if (WantTiming) - Start = TimeRecord::getCurrentTime(); - } - - void setOutput(const Twine &Output) { - if (WantTiming) - this->Output = Output.str(); - } - - ~SimpleTimer() { - if (WantTiming) { - TimeRecord Elapsed = TimeRecord::getCurrentTime(); - Elapsed -= Start; - llvm::errs() << Output << ':'; - Elapsed.print(Elapsed, llvm::errs()); - llvm::errs() << '\n'; - } - } - }; +class SimpleTimer { + bool WantTiming; + TimeRecord Start; + std::string Output; - template - std::unique_ptr valueOrNull(llvm::ErrorOr> Val) { - if (!Val) - return nullptr; - return std::move(*Val); +public: + explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) { + if (WantTiming) + Start = TimeRecord::getCurrentTime(); + } + + void setOutput(const Twine &Output) { + if (WantTiming) + this->Output = Output.str(); + } + + ~SimpleTimer() { + if (WantTiming) { + TimeRecord Elapsed = TimeRecord::getCurrentTime(); + Elapsed -= Start; + llvm::errs() << Output << ':'; + Elapsed.print(Elapsed, llvm::errs()); + llvm::errs() << '\n'; + } } +}; - template - bool moveOnNoError(llvm::ErrorOr Val, T &Output) { - if (!Val) - return false; - Output = std::move(*Val); - return true; - } +template +std::unique_ptr valueOrNull(llvm::ErrorOr> Val) { + if (!Val) + return nullptr; + return std::move(*Val); +} + +template bool moveOnNoError(llvm::ErrorOr Val, T &Output) { + if (!Val) + return false; + Output = std::move(*Val); + return true; +} /// \brief Get a source buffer for \p MainFilePath, handling all file-to-file /// and file-to-buffer remappings inside \p Invocation. static std::unique_ptr getBufferForFileHandlingRemapping(const CompilerInvocation &Invocation, - vfs::FileSystem *VFS, - StringRef FilePath) { + vfs::FileSystem *VFS, StringRef FilePath) { const auto &PreprocessorOpts = Invocation.getPreprocessorOpts(); // Try to determine if the main file has been remapped, either from the @@ -156,7 +154,7 @@ return nullptr; return llvm::MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(), FilePath); } -} +} // namespace struct ASTUnit::ASTWriterData { SmallString<128> Buffer; @@ -167,9 +165,7 @@ : Stream(Buffer), Writer(Stream, Buffer, PCMCache, {}) {} }; -void ASTUnit::clearFileLevelDecls() { - llvm::DeleteContainerSeconds(FileDecls); -} +void ASTUnit::clearFileLevelDecls() { llvm::DeleteContainerSeconds(FileDecls); } /// \brief After failing to build a precompiled preamble (due to /// errors in the source that occurs in the preamble), the number of @@ -183,20 +179,15 @@ static std::atomic ActiveASTUnitObjects; ASTUnit::ASTUnit(bool _MainFileIsAST) - : Reader(nullptr), HadModuleLoaderFatalFailure(false), - OnlyLocalDecls(false), CaptureDiagnostics(false), - MainFileIsAST(_MainFileIsAST), - TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")), - OwnsRemappedFileBuffers(true), - NumStoredDiagnosticsFromDriver(0), - PreambleRebuildCounter(0), - NumWarningsInPreamble(0), - ShouldCacheCodeCompletionResults(false), - IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false), - CompletionCacheTopLevelHashValue(0), - PreambleTopLevelHashValue(0), - CurrentTopLevelHashValue(0), - UnsafeToFree(false) { + : Reader(nullptr), HadModuleLoaderFatalFailure(false), + OnlyLocalDecls(false), CaptureDiagnostics(false), + MainFileIsAST(_MainFileIsAST), TUKind(TU_Complete), + WantTiming(getenv("LIBCLANG_TIMING")), OwnsRemappedFileBuffers(true), + NumStoredDiagnosticsFromDriver(0), PreambleRebuildCounter(0), + NumWarningsInPreamble(0), ShouldCacheCodeCompletionResults(false), + IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false), + CompletionCacheTopLevelHashValue(0), PreambleTopLevelHashValue(0), + CurrentTopLevelHashValue(0), UnsafeToFree(false) { if (getenv("LIBCLANG_OBJTRACKING")) fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects); } @@ -219,8 +210,8 @@ delete RB.second; } - ClearCachedCompletionResults(); - + ClearCachedCompletionResults(); + if (getenv("LIBCLANG_OBJTRACKING")) fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects); } @@ -229,40 +220,40 @@ this->PP = std::move(PP); } -/// \brief Determine the set of code-completion contexts in which this +/// \brief Determine the set of code-completion contexts in which this /// declaration should be shown. static unsigned getDeclShowContexts(const NamedDecl *ND, const LangOptions &LangOpts, bool &IsNestedNameSpecifier) { IsNestedNameSpecifier = false; - + if (isa(ND)) ND = dyn_cast(ND->getUnderlyingDecl()); if (!ND) return 0; - + uint64_t Contexts = 0; - if (isa(ND) || isa(ND) || + if (isa(ND) || isa(ND) || isa(ND) || isa(ND) || isa(ND)) { // Types can appear in these contexts. if (LangOpts.CPlusPlus || !isa(ND)) - Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel) - | (1LL << CodeCompletionContext::CCC_ObjCIvarList) - | (1LL << CodeCompletionContext::CCC_ClassStructUnion) - | (1LL << CodeCompletionContext::CCC_Statement) - | (1LL << CodeCompletionContext::CCC_Type) - | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); + Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel) | + (1LL << CodeCompletionContext::CCC_ObjCIvarList) | + (1LL << CodeCompletionContext::CCC_ClassStructUnion) | + (1LL << CodeCompletionContext::CCC_Statement) | + (1LL << CodeCompletionContext::CCC_Type) | + (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); // In C++, types can appear in expressions contexts (for functional casts). if (LangOpts.CPlusPlus) Contexts |= (1LL << CodeCompletionContext::CCC_Expression); - + // In Objective-C, message sends can send interfaces. In Objective-C++, // all types are available due to functional casts. if (LangOpts.CPlusPlus || isa(ND)) Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); - + // In Objective-C, you can only be a subclass of another Objective-C class if (isa(ND)) Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName); @@ -270,7 +261,7 @@ // Deal with tag names. if (isa(ND)) { Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag); - + // Part of the nested-name-specifier in C++0x. if (LangOpts.CPlusPlus11) IsNestedNameSpecifier = true; @@ -279,49 +270,49 @@ Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag); else Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag); - + if (LangOpts.CPlusPlus) IsNestedNameSpecifier = true; } else if (isa(ND)) IsNestedNameSpecifier = true; } else if (isa(ND) || isa(ND)) { // Values can appear in these contexts. - Contexts = (1LL << CodeCompletionContext::CCC_Statement) - | (1LL << CodeCompletionContext::CCC_Expression) - | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) - | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); + Contexts = (1LL << CodeCompletionContext::CCC_Statement) | + (1LL << CodeCompletionContext::CCC_Expression) | + (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) | + (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); } else if (isa(ND)) { Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName); } else if (isa(ND)) { Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName); } else if (isa(ND) || isa(ND)) { Contexts = (1LL << CodeCompletionContext::CCC_Namespace); - + // Part of the nested-name-specifier. IsNestedNameSpecifier = true; } - + return Contexts; } void ASTUnit::CacheCodeCompletionResults() { if (!TheSema) return; - + SimpleTimer Timer(WantTiming); Timer.setOutput("Cache global code completions for " + getMainFileName()); // Clear out the previous results. ClearCachedCompletionResults(); - + // Gather the set of global code completions. typedef CodeCompletionResult Result; SmallVector Results; CachedCompletionAllocator = std::make_shared(); CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator); - TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, - CCTUInfo, Results); - + TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, CCTUInfo, + Results); + // Translate global code completions into cached completions. llvm::DenseMap CompletionTypes; CodeCompletionContext CCContext(CodeCompletionContext::CCC_TopLevel); @@ -340,58 +331,58 @@ CachedResult.Kind = R.CursorKind; CachedResult.Availability = R.Availability; - // Keep track of the type of this completion in an ASTContext-agnostic + // Keep track of the type of this completion in an ASTContext-agnostic // way. QualType UsageType = getDeclUsageType(*Ctx, R.Declaration); if (UsageType.isNull()) { CachedResult.TypeClass = STC_Void; CachedResult.Type = 0; } else { - CanQualType CanUsageType - = Ctx->getCanonicalType(UsageType.getUnqualifiedType()); + CanQualType CanUsageType = + Ctx->getCanonicalType(UsageType.getUnqualifiedType()); CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType); // Determine whether we have already seen this type. If so, we save - // ourselves the work of formatting the type string by using the + // ourselves the work of formatting the type string by using the // temporary, CanQualType-based hash table to find the associated value. unsigned &TypeValue = CompletionTypes[CanUsageType]; if (TypeValue == 0) { TypeValue = CompletionTypes.size(); - CachedCompletionTypes[QualType(CanUsageType).getAsString()] - = TypeValue; + CachedCompletionTypes[QualType(CanUsageType).getAsString()] = + TypeValue; } - + CachedResult.Type = TypeValue; } - + CachedCompletionResults.push_back(CachedResult); - + /// Handle nested-name-specifiers in C++. if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier && !R.StartsNestedNameSpecifier) { // The contexts in which a nested-name-specifier can appear in C++. - uint64_t NNSContexts - = (1LL << CodeCompletionContext::CCC_TopLevel) - | (1LL << CodeCompletionContext::CCC_ObjCIvarList) - | (1LL << CodeCompletionContext::CCC_ClassStructUnion) - | (1LL << CodeCompletionContext::CCC_Statement) - | (1LL << CodeCompletionContext::CCC_Expression) - | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) - | (1LL << CodeCompletionContext::CCC_EnumTag) - | (1LL << CodeCompletionContext::CCC_UnionTag) - | (1LL << CodeCompletionContext::CCC_ClassOrStructTag) - | (1LL << CodeCompletionContext::CCC_Type) - | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName) - | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); + uint64_t NNSContexts = + (1LL << CodeCompletionContext::CCC_TopLevel) | + (1LL << CodeCompletionContext::CCC_ObjCIvarList) | + (1LL << CodeCompletionContext::CCC_ClassStructUnion) | + (1LL << CodeCompletionContext::CCC_Statement) | + (1LL << CodeCompletionContext::CCC_Expression) | + (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) | + (1LL << CodeCompletionContext::CCC_EnumTag) | + (1LL << CodeCompletionContext::CCC_UnionTag) | + (1LL << CodeCompletionContext::CCC_ClassOrStructTag) | + (1LL << CodeCompletionContext::CCC_Type) | + (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName) | + (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); if (isa(R.Declaration) || isa(R.Declaration)) NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace); - if (unsigned RemainingContexts - = NNSContexts & ~CachedResult.ShowInContexts) { - // If there any contexts where this completion can be a - // nested-name-specifier but isn't already an option, create a + if (unsigned RemainingContexts = + NNSContexts & ~CachedResult.ShowInContexts) { + // If there any contexts where this completion can be a + // nested-name-specifier but isn't already an option, create a // nested-name-specifier completion. R.StartsNestedNameSpecifier = true; CachedResult.Completion = R.CreateCodeCompletionString( @@ -406,31 +397,31 @@ } break; } - + case Result::RK_Keyword: case Result::RK_Pattern: // Ignore keywords and patterns; we don't care, since they are so // easily regenerated. break; - + case Result::RK_Macro: { CachedCodeCompletionResult CachedResult; CachedResult.Completion = R.CreateCodeCompletionString( *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, IncludeBriefCommentsInCodeCompletion); - CachedResult.ShowInContexts - = (1LL << CodeCompletionContext::CCC_TopLevel) - | (1LL << CodeCompletionContext::CCC_ObjCInterface) - | (1LL << CodeCompletionContext::CCC_ObjCImplementation) - | (1LL << CodeCompletionContext::CCC_ObjCIvarList) - | (1LL << CodeCompletionContext::CCC_ClassStructUnion) - | (1LL << CodeCompletionContext::CCC_Statement) - | (1LL << CodeCompletionContext::CCC_Expression) - | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) - | (1LL << CodeCompletionContext::CCC_MacroNameUse) - | (1LL << CodeCompletionContext::CCC_PreprocessorExpression) - | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) - | (1LL << CodeCompletionContext::CCC_OtherWithMacros); + CachedResult.ShowInContexts = + (1LL << CodeCompletionContext::CCC_TopLevel) | + (1LL << CodeCompletionContext::CCC_ObjCInterface) | + (1LL << CodeCompletionContext::CCC_ObjCImplementation) | + (1LL << CodeCompletionContext::CCC_ObjCIvarList) | + (1LL << CodeCompletionContext::CCC_ClassStructUnion) | + (1LL << CodeCompletionContext::CCC_Statement) | + (1LL << CodeCompletionContext::CCC_Expression) | + (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) | + (1LL << CodeCompletionContext::CCC_MacroNameUse) | + (1LL << CodeCompletionContext::CCC_PreprocessorExpression) | + (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) | + (1LL << CodeCompletionContext::CCC_OtherWithMacros); CachedResult.Priority = R.Priority; CachedResult.Kind = R.CursorKind; @@ -442,7 +433,7 @@ } } } - + // Save the current top-level hash value. CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue; } @@ -468,6 +459,7 @@ unsigned &Counter; bool InitializedLanguage; + public: ASTInfoCollector(Preprocessor &PP, ASTContext *Context, HeaderSearchOptions &HSOpts, PreprocessorOptions &PPOpts, @@ -482,10 +474,10 @@ bool AllowCompatibleDifferences) override { if (InitializedLanguage) return false; - + LangOpt = LangOpts; InitializedLanguage = true; - + updated(); return false; } @@ -553,7 +545,7 @@ } }; - /// \brief Diagnostic consumer that saves each diagnostic it is given. +/// \brief Diagnostic consumer that saves each diagnostic it is given. class StoredDiagnosticConsumer : public DiagnosticConsumer { SmallVectorImpl *StoredDiags; SmallVectorImpl *StandaloneDiags; @@ -590,11 +582,12 @@ std::unique_ptr OwningPreviousClient; public: - CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags, - SmallVectorImpl *StoredDiags, - SmallVectorImpl *StandaloneDiags) - : Diags(Diags), Client(StoredDiags, StandaloneDiags), PreviousClient(nullptr) - { + CaptureDroppedDiagnostics( + bool RequestCapture, DiagnosticsEngine &Diags, + SmallVectorImpl *StoredDiags, + SmallVectorImpl *StandaloneDiags) + : Diags(Diags), Client(StoredDiags, StandaloneDiags), + PreviousClient(nullptr) { if (RequestCapture || Diags.getClient() == nullptr) { OwningPreviousClient = Diags.takeClient(); PreviousClient = Diags.getClient(); @@ -641,9 +634,7 @@ } } -IntrusiveRefCntPtr ASTUnit::getASTReader() const { - return Reader; -} +IntrusiveRefCntPtr ASTUnit::getASTReader() const { return Reader; } ASTMutationListener *ASTUnit::getASTMutationListener() { if (WriterData) @@ -673,7 +664,8 @@ ASTUnit &AST, bool CaptureDiagnostics) { assert(Diags.get() && "no DiagnosticsEngine was provided"); if (CaptureDiagnostics) - Diags->setClient(new StoredDiagnosticConsumer(&AST.StoredDiagnostics, nullptr)); + Diags->setClient( + new StoredDiagnosticConsumer(&AST.StoredDiagnostics, nullptr)); } std::unique_ptr ASTUnit::LoadFromASTFile( @@ -686,11 +678,11 @@ std::unique_ptr AST(new ASTUnit(true)); // Recover resources if we crash before exiting this method. - llvm::CrashRecoveryContextCleanupRegistrar - ASTUnitCleanup(AST.get()); - llvm::CrashRecoveryContextCleanupRegistrar > - DiagCleanup(Diags.get()); + llvm::CrashRecoveryContextCleanupRegistrar ASTUnitCleanup(AST.get()); + llvm::CrashRecoveryContextCleanupRegistrar< + DiagnosticsEngine, + llvm::CrashRecoveryContextReleaseRefCleanup> + DiagCleanup(Diags.get()); ConfigureDiags(Diags, *AST, CaptureDiagnostics); @@ -701,14 +693,12 @@ IntrusiveRefCntPtr VFS = vfs::getRealFileSystem(); AST->FileMgr = new FileManager(FileSystemOpts, VFS); AST->UserFilesAreVolatile = UserFilesAreVolatile; - AST->SourceMgr = new SourceManager(AST->getDiagnostics(), - AST->getFileManager(), - UserFilesAreVolatile); + AST->SourceMgr = new SourceManager( + AST->getDiagnostics(), AST->getFileManager(), UserFilesAreVolatile); AST->PCMCache = new MemoryBufferCache; AST->HSOpts = std::make_shared(); AST->HSOpts->ModuleFormat = PCHContainerRdr.getFormat(); - AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts, - AST->getSourceManager(), + AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts, AST->getSourceManager(), AST->getDiagnostics(), AST->getLangOpts(), /*Target=*/nullptr)); @@ -737,7 +727,7 @@ bool disableValid = false; if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION")) disableValid = true; - AST->Reader = new ASTReader(PP, AST->Ctx.get(), PCHContainerRdr, { }, + AST->Reader = new ASTReader(PP, AST->Ctx.get(), PCHContainerRdr, {}, /*isysroot=*/"", /*DisableValidation=*/disableValid, AllowPCHWithCompilerErrors); @@ -755,7 +745,7 @@ AST->Ctx->setExternalSource(AST->Reader); switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile, - SourceLocation(), ASTReader::ARR_None)) { + SourceLocation(), ASTReader::ARR_None)) { case ASTReader::Success: break; @@ -797,13 +787,13 @@ Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash); } -/// \brief Preprocessor callback class that updates a hash value with the names +/// \brief Preprocessor callback class that updates a hash value with the names /// of all macros that have been defined by the translation unit. class MacroDefinitionTrackerPPCallbacks : public PPCallbacks { unsigned &Hash; - + public: - explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { } + explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) {} void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) override { @@ -815,11 +805,11 @@ void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) { if (!D) return; - + DeclContext *DC = D->getDeclContext(); if (!DC) return; - + if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit())) return; @@ -856,10 +846,10 @@ class TopLevelDeclTrackerConsumer : public ASTConsumer { ASTUnit &Unit; unsigned &Hash; - + public: TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash) - : Unit(_Unit), Hash(Hash) { + : Unit(_Unit), Hash(Hash) { Hash = 0; } @@ -919,7 +909,7 @@ StringRef InFile) override { CI.getPreprocessor().addPPCallbacks( llvm::make_unique( - Unit.getCurrentTopLevelHashValue())); + Unit.getCurrentTopLevelHashValue())); return llvm::make_unique( Unit, Unit.getCurrentTopLevelHashValue()); } @@ -929,7 +919,7 @@ bool hasCodeCompletionSupport() const override { return false; } TranslationUnitKind getTranslationUnitKind() override { - return Unit.getTranslationUnitKind(); + return Unit.getTranslationUnitKind(); } }; @@ -993,9 +983,9 @@ StoredDiags.end()); } -static void checkAndSanitizeDiags(SmallVectorImpl & - StoredDiagnostics, - SourceManager &SM) { +static void +checkAndSanitizeDiags(SmallVectorImpl &StoredDiagnostics, + SourceManager &SM) { // The stored diagnostic has the old source manager in it; update // the locations to refer into the new source manager. Since we've // been careful to make sure that the source manager's state @@ -1009,20 +999,20 @@ } } -static IntrusiveRefCntPtr createVFSOverlayForPreamblePCH( - StringRef PCHFilename, - IntrusiveRefCntPtr RealFS, - IntrusiveRefCntPtr VFS) { +static IntrusiveRefCntPtr +createVFSOverlayForPreamblePCH(StringRef PCHFilename, + IntrusiveRefCntPtr RealFS, + IntrusiveRefCntPtr VFS) { // We want only the PCH file from the real filesystem to be available, // so we create an in-memory VFS with just that and overlay it on top. auto Buf = RealFS->getBufferForFile(PCHFilename); if (!Buf) return VFS; - IntrusiveRefCntPtr - PCHFS(new vfs::InMemoryFileSystem()); + IntrusiveRefCntPtr PCHFS( + new vfs::InMemoryFileSystem()); PCHFS->addFile(PCHFilename, 0, std::move(*Buf)); - IntrusiveRefCntPtr - Overlay(new vfs::OverlayFileSystem(VFS)); + IntrusiveRefCntPtr Overlay( + new vfs::OverlayFileSystem(VFS)); Overlay->pushOverlay(PCHFS); return Overlay; } @@ -1060,23 +1050,22 @@ if (FileMgr) { FileMgr = new FileManager(FileMgr->getFileSystemOpts(), VFS); Clang->setFileManager(FileMgr.get()); - } - else { + } else { Clang->setVirtualFileSystem(VFS); } } // Recover resources if we crash before exiting this method. - llvm::CrashRecoveryContextCleanupRegistrar - CICleanup(Clang.get()); + llvm::CrashRecoveryContextCleanupRegistrar CICleanup( + Clang.get()); Clang->setInvocation(std::make_shared(*Invocation)); OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); - + // Set up diagnostics, capturing any diagnostics that would // otherwise be dropped. Clang->setDiagnostics(&getDiagnostics()); - + // Create the target instance. Clang->setTarget(TargetInfo::CreateTargetInfo( Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); @@ -1088,7 +1077,7 @@ // FIXME: We shouldn't need to do this, the target should be immutable once // created. This complexity should be lifted elsewhere. Clang->getTarget().adjust(Clang->getLangOpts()); - + assert(Clang->getFrontendOpts().Inputs.size() == 1 && "Invocation must have exactly one source file!"); assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == @@ -1108,8 +1097,8 @@ ResetForParse(); - SourceMgr = new SourceManager(getDiagnostics(), *FileMgr, - UserFilesAreVolatile); + SourceMgr = + new SourceManager(getDiagnostics(), *FileMgr, UserFilesAreVolatile); if (!OverrideMainBuffer) { checkAndRemoveNonDriverDiags(StoredDiagnostics); TopLevelDeclsInPreamble.clear(); @@ -1117,16 +1106,18 @@ // Create a file manager object to provide access to and cache the filesystem. Clang->setFileManager(&getFileManager()); - + // Create the source manager. Clang->setSourceManager(&getSourceManager()); - + // If the main file has been overridden due to the use of a preamble, // make that override happen and introduce the preamble. if (OverrideMainBuffer) { - assert(Preamble && "No preamble was built, but OverrideMainBuffer is not null"); - Preamble->AddImplicitPreamble(Clang->getInvocation(), OverrideMainBuffer.get()); - + assert(Preamble && + "No preamble was built, but OverrideMainBuffer is not null"); + Preamble->AddImplicitPreamble(Clang->getInvocation(), + OverrideMainBuffer.get()); + // The stored diagnostic has the old source manager in it; update // the locations to refer into the new source manager. Since we've // been careful to make sure that the source manager's state @@ -1143,7 +1134,7 @@ // Recover resources if we crash before exiting this method. llvm::CrashRecoveryContextCleanupRegistrar - ActCleanup(Act.get()); + ActCleanup(Act.get()); if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) goto error; @@ -1158,7 +1149,7 @@ goto error; transferASTDataFromCompilerInstance(*Clang); - + Act->EndSourceFile(); FailedParseDiagnostics.clear(); @@ -1192,8 +1183,8 @@ const FixItHint &InFix) { ASTUnit::StandaloneFixIt OutFix; OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts); - OutFix.InsertFromRange = makeStandaloneRange(InFix.InsertFromRange, SM, - LangOpts); + OutFix.InsertFromRange = + makeStandaloneRange(InFix.InsertFromRange, SM, LangOpts); OutFix.CodeToInsert = InFix.CodeToInsert; OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions; return OutFix; @@ -1227,10 +1218,10 @@ /// the source file. /// /// This routine will compute the preamble of the main source file. If a -/// non-trivial preamble is found, it will precompile that preamble into a +/// non-trivial preamble is found, it will precompile that preamble into a /// precompiled header so that the precompiled preamble can be used to reduce /// reparsing time. If a precompiled preamble has already been constructed, -/// this routine will determine if it is still valid and, if so, avoid +/// this routine will determine if it is still valid and, if so, avoid /// rebuilding the precompiled preamble. /// /// \param AllowRebuild When true (the default), this routine is @@ -1258,9 +1249,8 @@ if (!MainFileBuffer) return nullptr; - PreambleBounds Bounds = - ComputePreambleBounds(*PreambleInvocationIn.getLangOpts(), - MainFileBuffer.get(), MaxLines); + PreambleBounds Bounds = ComputePreambleBounds( + *PreambleInvocationIn.getLangOpts(), MainFileBuffer.get(), MaxLines); if (!Bounds.Size) return nullptr; @@ -1315,7 +1305,7 @@ llvm::ErrorOr NewPreamble = PrecompiledPreamble::Build( PreambleInvocationIn, MainFileBuffer.get(), Bounds, *Diagnostics, VFS, - PCHContainerOps, Callbacks); + PCHContainerOps, Callbacks, nullptr); if (NewPreamble) { Preamble = std::move(*NewPreamble); PreambleRebuildCounter = 1; @@ -1406,8 +1396,8 @@ } if (SourceMgr) { - if (const FileEntry * - FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID())) + if (const FileEntry *FE = + SourceMgr->getFileEntryForID(SourceMgr->getMainFileID())) return FE->getName(); } @@ -1418,8 +1408,8 @@ if (!isMainFileAST()) return StringRef(); - serialization::ModuleFile & - Mod = Reader->getModuleManager().getPrimaryModule(); + serialization::ModuleFile &Mod = + Reader->getModuleManager().getPrimaryModule(); return Mod.FileName; } @@ -1465,7 +1455,7 @@ if (!AST) return nullptr; } - + if (!ResourceFilesPath.empty()) { // Override the resources path. CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; @@ -1476,15 +1466,16 @@ AST->PreambleRebuildCounter = PrecompilePreambleAfterNParses; AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete; AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; - AST->IncludeBriefCommentsInCodeCompletion - = IncludeBriefCommentsInCodeCompletion; + AST->IncludeBriefCommentsInCodeCompletion = + IncludeBriefCommentsInCodeCompletion; // Recover resources if we crash before exiting this method. - llvm::CrashRecoveryContextCleanupRegistrar - ASTUnitCleanup(OwnAST.get()); - llvm::CrashRecoveryContextCleanupRegistrar > - DiagCleanup(Diags.get()); + llvm::CrashRecoveryContextCleanupRegistrar ASTUnitCleanup( + OwnAST.get()); + llvm::CrashRecoveryContextCleanupRegistrar< + DiagnosticsEngine, + llvm::CrashRecoveryContextReleaseRefCleanup> + DiagCleanup(Diags.get()); // We'll manage file buffers ourselves. CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; @@ -1496,16 +1487,16 @@ new CompilerInstance(std::move(PCHContainerOps))); // Recover resources if we crash before exiting this method. - llvm::CrashRecoveryContextCleanupRegistrar - CICleanup(Clang.get()); + llvm::CrashRecoveryContextCleanupRegistrar CICleanup( + Clang.get()); Clang->setInvocation(std::move(CI)); AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); - + // Set up diagnostics, capturing any diagnostics that would // otherwise be dropped. Clang->setDiagnostics(&AST->getDiagnostics()); - + // Create the target instance. Clang->setTarget(TargetInfo::CreateTargetInfo( Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); @@ -1517,7 +1508,7 @@ // FIXME: We shouldn't need to do this, the target should be immutable once // created. This complexity should be lifted elsewhere. Clang->getTarget().adjust(Clang->getLangOpts()); - + assert(Clang->getFrontendOpts().Inputs.size() == 1 && "Invocation must have exactly one source file!"); assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == @@ -1535,7 +1526,7 @@ // Create a file manager object to provide access to and cache the filesystem. Clang->setFileManager(&AST->getFileManager()); - + // Create the source manager. Clang->setSourceManager(&AST->getSourceManager()); @@ -1549,7 +1540,7 @@ // Recover resources if we crash before exiting this method. llvm::CrashRecoveryContextCleanupRegistrar - ActCleanup(TrackerAct.get()); + ActCleanup(TrackerAct.get()); if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { AST->transferASTDataFromCompilerInstance(*Clang); @@ -1562,7 +1553,7 @@ if (Persistent && !TrackerAct) { Clang->getPreprocessor().addPPCallbacks( llvm::make_unique( - AST->getCurrentTopLevelHashValue())); + AST->getCurrentTopLevelHashValue())); std::vector> Consumers; if (Clang->hasASTConsumer()) Consumers.push_back(Clang->takeASTConsumer()); @@ -1581,7 +1572,7 @@ // Steal the created target, context, and preprocessor. AST->transferASTDataFromCompilerInstance(*Clang); - + Act->EndSourceFile(); if (OwnAST) @@ -1619,7 +1610,7 @@ // Recover resources if we crash before exiting this method. llvm::CrashRecoveryContextCleanupRegistrar - MemBufferCleanup(OverrideMainBuffer.get()); + MemBufferCleanup(OverrideMainBuffer.get()); return Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS); } @@ -1640,19 +1631,19 @@ AST->CaptureDiagnostics = CaptureDiagnostics; AST->TUKind = TUKind; AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; - AST->IncludeBriefCommentsInCodeCompletion - = IncludeBriefCommentsInCodeCompletion; + AST->IncludeBriefCommentsInCodeCompletion = + IncludeBriefCommentsInCodeCompletion; AST->Invocation = std::move(CI); AST->FileSystemOpts = FileMgr->getFileSystemOpts(); AST->FileMgr = FileMgr; AST->UserFilesAreVolatile = UserFilesAreVolatile; - + // Recover resources if we crash before exiting this method. - llvm::CrashRecoveryContextCleanupRegistrar - ASTUnitCleanup(AST.get()); - llvm::CrashRecoveryContextCleanupRegistrar > - DiagCleanup(Diags.get()); + llvm::CrashRecoveryContextCleanupRegistrar ASTUnitCleanup(AST.get()); + llvm::CrashRecoveryContextCleanupRegistrar< + DiagnosticsEngine, + llvm::CrashRecoveryContextReleaseRefCleanup> + DiagCleanup(Diags.get()); if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps), PrecompilePreambleAfterNParses, @@ -1699,7 +1690,7 @@ PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName; PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors; PPOpts.SingleFileParseMode = SingleFileParse; - + // Override the resources path. CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; @@ -1725,8 +1716,8 @@ AST->CaptureDiagnostics = CaptureDiagnostics; AST->TUKind = TUKind; AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; - AST->IncludeBriefCommentsInCodeCompletion - = IncludeBriefCommentsInCodeCompletion; + AST->IncludeBriefCommentsInCodeCompletion = + IncludeBriefCommentsInCodeCompletion; AST->UserFilesAreVolatile = UserFilesAreVolatile; AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size(); AST->StoredDiagnostics.swap(StoredDiagnostics); @@ -1738,12 +1729,10 @@ Diags = nullptr; // Recover resources if we crash before exiting this method. - llvm::CrashRecoveryContextCleanupRegistrar - ASTUnitCleanup(AST.get()); + llvm::CrashRecoveryContextCleanupRegistrar ASTUnitCleanup(AST.get()); if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps), - PrecompilePreambleAfterNParses, - VFS)) { + PrecompilePreambleAfterNParses, VFS)) { // Some error occurred, if caller wants to examine diagnostics, pass it the // ASTUnit. if (ErrAST) { @@ -1768,7 +1757,7 @@ } clearFileLevelDecls(); - + SimpleTimer ParsingTimer(WantTiming); ParsingTimer.setOutput("Reparsing " + getMainFileName()); @@ -1790,7 +1779,6 @@ OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS); - // Clear out the diagnostics state. FileMgr.reset(); getDiagnostics().Reset(); @@ -1802,7 +1790,7 @@ bool Result = Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS); - // If we're caching global code-completion results, and the top-level + // If we're caching global code-completion results, and the top-level // declarations have changed, clear out the code-completion cache. if (!Result && ShouldCacheCodeCompletionResults && CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue) @@ -1811,7 +1799,7 @@ // We now need to clear out the completion info related to this translation // unit; it'll be recreated if necessary. CCTUInfo.reset(); - + return Result; } @@ -1833,70 +1821,69 @@ //----------------------------------------------------------------------------// namespace { - /// \brief Code completion consumer that combines the cached code-completion - /// results from an ASTUnit with the code-completion results provided to it, - /// then passes the result on to - class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { - uint64_t NormalContexts; - ASTUnit &AST; - CodeCompleteConsumer &Next; - - public: - AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, - const CodeCompleteOptions &CodeCompleteOpts) - : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()), - AST(AST), Next(Next) - { - // Compute the set of contexts in which we will look when we don't have - // any information about the specific context. - NormalContexts - = (1LL << CodeCompletionContext::CCC_TopLevel) - | (1LL << CodeCompletionContext::CCC_ObjCInterface) - | (1LL << CodeCompletionContext::CCC_ObjCImplementation) - | (1LL << CodeCompletionContext::CCC_ObjCIvarList) - | (1LL << CodeCompletionContext::CCC_Statement) - | (1LL << CodeCompletionContext::CCC_Expression) - | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) - | (1LL << CodeCompletionContext::CCC_DotMemberAccess) - | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess) - | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess) - | (1LL << CodeCompletionContext::CCC_ObjCProtocolName) - | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) - | (1LL << CodeCompletionContext::CCC_Recovery); - - if (AST.getASTContext().getLangOpts().CPlusPlus) - NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag) - | (1LL << CodeCompletionContext::CCC_UnionTag) - | (1LL << CodeCompletionContext::CCC_ClassOrStructTag); - } - - void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, - CodeCompletionResult *Results, - unsigned NumResults) override; - - void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, - OverloadCandidate *Candidates, - unsigned NumCandidates) override { - Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates); - } +/// \brief Code completion consumer that combines the cached code-completion +/// results from an ASTUnit with the code-completion results provided to it, +/// then passes the result on to +class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { + uint64_t NormalContexts; + ASTUnit &AST; + CodeCompleteConsumer &Next; - CodeCompletionAllocator &getAllocator() override { - return Next.getAllocator(); - } +public: + AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, + const CodeCompleteOptions &CodeCompleteOpts) + : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()), AST(AST), + Next(Next) { + // Compute the set of contexts in which we will look when we don't have + // any information about the specific context. + NormalContexts = + (1LL << CodeCompletionContext::CCC_TopLevel) | + (1LL << CodeCompletionContext::CCC_ObjCInterface) | + (1LL << CodeCompletionContext::CCC_ObjCImplementation) | + (1LL << CodeCompletionContext::CCC_ObjCIvarList) | + (1LL << CodeCompletionContext::CCC_Statement) | + (1LL << CodeCompletionContext::CCC_Expression) | + (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) | + (1LL << CodeCompletionContext::CCC_DotMemberAccess) | + (1LL << CodeCompletionContext::CCC_ArrowMemberAccess) | + (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess) | + (1LL << CodeCompletionContext::CCC_ObjCProtocolName) | + (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) | + (1LL << CodeCompletionContext::CCC_Recovery); + + if (AST.getASTContext().getLangOpts().CPlusPlus) + NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag) | + (1LL << CodeCompletionContext::CCC_UnionTag) | + (1LL << CodeCompletionContext::CCC_ClassOrStructTag); + } + + void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, + CodeCompletionResult *Results, + unsigned NumResults) override; + + void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, + OverloadCandidate *Candidates, + unsigned NumCandidates) override { + Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates); + } - CodeCompletionTUInfo &getCodeCompletionTUInfo() override { - return Next.getCodeCompletionTUInfo(); - } - }; + CodeCompletionAllocator &getAllocator() override { + return Next.getAllocator(); + } + + CodeCompletionTUInfo &getCodeCompletionTUInfo() override { + return Next.getCodeCompletionTUInfo(); + } +}; } // anonymous namespace /// \brief Helper function that computes which global names are hidden by the /// local code-completion results. -static void CalculateHiddenNames(const CodeCompletionContext &Context, - CodeCompletionResult *Results, - unsigned NumResults, - ASTContext &Ctx, - llvm::StringSet &HiddenNames){ +static void +CalculateHiddenNames(const CodeCompletionContext &Context, + CodeCompletionResult *Results, unsigned NumResults, + ASTContext &Ctx, + llvm::StringSet &HiddenNames) { bool OnlyTagNames = false; switch (Context.getKind()) { case CodeCompletionContext::CCC_Recovery: @@ -1918,13 +1905,13 @@ case CodeCompletionContext::CCC_ParenthesizedExpression: case CodeCompletionContext::CCC_ObjCInterfaceName: break; - + case CodeCompletionContext::CCC_EnumTag: case CodeCompletionContext::CCC_UnionTag: case CodeCompletionContext::CCC_ClassOrStructTag: OnlyTagNames = true; break; - + case CodeCompletionContext::CCC_ObjCProtocolName: case CodeCompletionContext::CCC_MacroName: case CodeCompletionContext::CCC_MacroNameUse: @@ -1942,30 +1929,30 @@ // be hidden. return; } - + typedef CodeCompletionResult Result; for (unsigned I = 0; I != NumResults; ++I) { if (Results[I].Kind != Result::RK_Declaration) continue; - - unsigned IDNS - = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace(); + + unsigned IDNS = + Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace(); bool Hiding = false; if (OnlyTagNames) Hiding = (IDNS & Decl::IDNS_Tag); else { - unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member | - Decl::IDNS_Namespace | Decl::IDNS_Ordinary | - Decl::IDNS_NonMemberOperator); + unsigned HiddenIDNS = + (Decl::IDNS_Type | Decl::IDNS_Member | Decl::IDNS_Namespace | + Decl::IDNS_Ordinary | Decl::IDNS_NonMemberOperator); if (Ctx.getLangOpts().CPlusPlus) HiddenIDNS |= Decl::IDNS_Tag; Hiding = (IDNS & HiddenIDNS); } - + if (!Hiding) continue; - + DeclarationName Name = Results[I].Declaration->getDeclName(); if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo()) HiddenNames.insert(Identifier->getName()); @@ -1974,61 +1961,58 @@ } } -void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S, - CodeCompletionContext Context, - CodeCompletionResult *Results, - unsigned NumResults) { +void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults( + Sema &S, CodeCompletionContext Context, CodeCompletionResult *Results, + unsigned NumResults) { // Merge the results we were given with the results we cached. bool AddedResult = false; - uint64_t InContexts = - Context.getKind() == CodeCompletionContext::CCC_Recovery - ? NormalContexts : (1LL << Context.getKind()); + uint64_t InContexts = Context.getKind() == CodeCompletionContext::CCC_Recovery + ? NormalContexts + : (1LL << Context.getKind()); // Contains the set of names that are hidden by "local" completion results. llvm::StringSet HiddenNames; typedef CodeCompletionResult Result; SmallVector AllResults; - for (ASTUnit::cached_completion_iterator - C = AST.cached_completion_begin(), - CEnd = AST.cached_completion_end(); + for (ASTUnit::cached_completion_iterator C = AST.cached_completion_begin(), + CEnd = AST.cached_completion_end(); C != CEnd; ++C) { - // If the context we are in matches any of the contexts we are + // If the context we are in matches any of the contexts we are // interested in, we'll add this result. if ((C->ShowInContexts & InContexts) == 0) continue; - + // If we haven't added any results previously, do so now. if (!AddedResult) { - CalculateHiddenNames(Context, Results, NumResults, S.Context, + CalculateHiddenNames(Context, Results, NumResults, S.Context, HiddenNames); AllResults.insert(AllResults.end(), Results, Results + NumResults); AddedResult = true; } - + // Determine whether this global completion result is hidden by a local // completion result. If so, skip it. if (C->Kind != CXCursor_MacroDefinition && HiddenNames.count(C->Completion->getTypedText())) continue; - + // Adjust priority based on similar type classes. unsigned Priority = C->Priority; CodeCompletionString *Completion = C->Completion; if (!Context.getPreferredType().isNull()) { if (C->Kind == CXCursor_MacroDefinition) { - Priority = getMacroUsagePriority(C->Completion->getTypedText(), - S.getLangOpts(), - Context.getPreferredType()->isAnyPointerType()); + Priority = getMacroUsagePriority( + C->Completion->getTypedText(), S.getLangOpts(), + Context.getPreferredType()->isAnyPointerType()); } else if (C->Type) { - CanQualType Expected - = S.Context.getCanonicalType( - Context.getPreferredType().getUnqualifiedType()); + CanQualType Expected = S.Context.getCanonicalType( + Context.getPreferredType().getUnqualifiedType()); SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected); if (ExpectedSTC == C->TypeClass) { // We know this type is similar; check for an exact match. - llvm::StringMap &CachedCompletionTypes - = AST.getCachedCompletionTypes(); - llvm::StringMap::iterator Pos - = CachedCompletionTypes.find(QualType(Expected).getAsString()); + llvm::StringMap &CachedCompletionTypes = + AST.getCachedCompletionTypes(); + llvm::StringMap::iterator Pos = + CachedCompletionTypes.find(QualType(Expected).getAsString()); if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type) Priority /= CCF_ExactTypeMatch; else @@ -2036,7 +2020,7 @@ } } } - + // Adjust the completion string, if required. if (C->Kind == CXCursor_MacroDefinition && Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) { @@ -2048,18 +2032,18 @@ Priority = CCP_CodePattern; Completion = Builder.TakeString(); } - - AllResults.push_back(Result(Completion, Priority, C->Kind, - C->Availability)); + + AllResults.push_back( + Result(Completion, Priority, C->Kind, C->Availability)); } - + // If we did not add any cached completion results, just forward the // results we were given to the next consumer. if (!AddedResult) { Next.ProcessCodeCompleteResults(S, Context, Results, NumResults); return; } - + Next.ProcessCodeCompleteResults(S, Context, AllResults.data(), AllResults.size()); } @@ -2077,8 +2061,8 @@ return; SimpleTimer CompletionTimer(WantTiming); - CompletionTimer.setOutput("Code completion @ " + File + ":" + - Twine(Line) + ":" + Twine(Column)); + CompletionTimer.setOutput("Code completion @ " + File + ":" + Twine(Line) + + ":" + Twine(Column)); auto CCInvocation = std::make_shared(*Invocation); @@ -2086,8 +2070,8 @@ CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts; PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts(); - CodeCompleteOpts.IncludeMacros = IncludeMacros && - CachedCompletionResults.empty(); + CodeCompleteOpts.IncludeMacros = + IncludeMacros && CachedCompletionResults.empty(); CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns; CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty(); CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments; @@ -2109,17 +2093,16 @@ new CompilerInstance(PCHContainerOps)); // Recover resources if we crash before exiting this method. - llvm::CrashRecoveryContextCleanupRegistrar - CICleanup(Clang.get()); + llvm::CrashRecoveryContextCleanupRegistrar CICleanup( + Clang.get()); auto &Inv = *CCInvocation; Clang->setInvocation(std::move(CCInvocation)); OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); - + // Set up diagnostics, capturing any diagnostics produced. Clang->setDiagnostics(&Diag); - CaptureDroppedDiagnostics Capture(true, - Clang->getDiagnostics(), + CaptureDroppedDiagnostics Capture(true, Clang->getDiagnostics(), &StoredDiagnostics, nullptr); ProcessWarningOptions(Diag, Inv.getDiagnosticOpts()); @@ -2130,13 +2113,13 @@ Clang->setInvocation(nullptr); return; } - + // Inform the target of the language options. // // FIXME: We shouldn't need to do this, the target should be immutable once // created. This complexity should be lifted elsewhere. Clang->getTarget().adjust(Clang->getLangOpts()); - + assert(Clang->getFrontendOpts().Inputs.size() == 1 && "Invocation must have exactly one source file!"); assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == @@ -2145,7 +2128,7 @@ assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind::LLVM_IR && "IR inputs not support here!"); - + // Use the source and file managers that we were given. Clang->setFileManager(&FileMgr); Clang->setSourceManager(&SourceMgr); @@ -2160,8 +2143,8 @@ // Use the code completion consumer we were given, but adding any cached // code-completion results. - AugmentedCodeCompleteConsumer *AugmentedConsumer - = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts); + AugmentedCodeCompleteConsumer *AugmentedConsumer = + new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts); Clang->setCodeCompletionConsumer(AugmentedConsumer); // If we have a precompiled preamble, try to use it. We only allow @@ -2175,7 +2158,8 @@ auto VFS = FileMgr.getVirtualFileSystem(); auto CompleteFileStatus = VFS->status(CompleteFilePath); if (CompleteFileStatus) { - llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID(); + llvm::sys::fs::UniqueID CompleteFileID = + CompleteFileStatus->getUniqueID(); std::string MainPath(OriginalSourceFile); auto MainStatus = VFS->status(MainPath); @@ -2191,8 +2175,10 @@ // If the main file has been overridden due to the use of a preamble, // make that override happen and introduce the preamble. if (OverrideMainBuffer) { - assert(Preamble && "No preamble was built, but OverrideMainBuffer is not null"); - Preamble->AddImplicitPreamble(Clang->getInvocation(), OverrideMainBuffer.get()); + assert(Preamble && + "No preamble was built, but OverrideMainBuffer is not null"); + Preamble->AddImplicitPreamble(Clang->getInvocation(), + OverrideMainBuffer.get()); OwnedBuffers.push_back(OverrideMainBuffer.release()); } else { PreprocessorOpts.PrecompiledPreambleBytes.first = 0; @@ -2224,7 +2210,7 @@ if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath)) return true; - // FIXME: Can we somehow regenerate the stat cache here, or do we need to + // FIXME: Can we somehow regenerate the stat cache here, or do we need to // unconditionally create a stat cache when we parse the file? llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true); @@ -2243,11 +2229,8 @@ return false; } -static bool serializeUnit(ASTWriter &Writer, - SmallVectorImpl &Buffer, - Sema &S, - bool hasErrors, - raw_ostream &OS) { +static bool serializeUnit(ASTWriter &Writer, SmallVectorImpl &Buffer, + Sema &S, bool hasErrors, raw_ostream &OS) { Writer.WriteAST(S, std::string(), nullptr, "", hasErrors); // Write the generated bitstream to "Out". @@ -2258,12 +2241,13 @@ } bool ASTUnit::serialize(raw_ostream &OS) { - // For serialization we are lenient if the errors were only warn-as-error kind. + // For serialization we are lenient if the errors were only warn-as-error + // kind. bool hasErrors = getDiagnostics().hasUncompilableErrorOccurred(); if (WriterData) - return serializeUnit(WriterData->Writer, WriterData->Buffer, - getSema(), hasErrors, OS); + return serializeUnit(WriterData->Writer, WriterData->Buffer, getSema(), + hasErrors, OS); SmallString<128> Buffer; llvm::BitstreamWriter Stream(Buffer); @@ -2275,10 +2259,9 @@ typedef ContinuousRangeMap SLocRemap; void ASTUnit::TranslateStoredDiagnostics( - FileManager &FileMgr, - SourceManager &SrcMgr, - const SmallVectorImpl &Diags, - SmallVectorImpl &Out) { + FileManager &FileMgr, SourceManager &SrcMgr, + const SmallVectorImpl &Diags, + SmallVectorImpl &Out) { // Map the standalone diagnostic into the new source manager. We also need to // remap all the locations to the new view. This includes the diag location, // any associated source ranges, and the source ranges of associated fix-its. @@ -2327,15 +2310,15 @@ FH.RemoveRange = CharSourceRange::getCharRange(BL, EL); } - Result.push_back(StoredDiagnostic(SD.Level, SD.ID, - SD.Message, Loc, Ranges, FixIts)); + Result.push_back( + StoredDiagnostic(SD.Level, SD.ID, SD.Message, Loc, Ranges, FixIts)); } Result.swap(Out); } void ASTUnit::addFileLevelDecl(Decl *D) { assert(D); - + // We only care about local declarations. if (D->isFromASTFile()) return; @@ -2393,10 +2376,9 @@ if (LocDecls.empty()) return; - LocDeclsTy::iterator BeginIt = - std::lower_bound(LocDecls.begin(), LocDecls.end(), - std::make_pair(Offset, (Decl *)nullptr), - llvm::less_first()); + LocDeclsTy::iterator BeginIt = std::lower_bound( + LocDecls.begin(), LocDecls.end(), std::make_pair(Offset, (Decl *)nullptr), + llvm::less_first()); if (BeginIt != LocDecls.begin()) --BeginIt; @@ -2412,13 +2394,13 @@ std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first()); if (EndIt != LocDecls.end()) ++EndIt; - + for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt) Decls.push_back(DIt->second); } -SourceLocation ASTUnit::getLocation(const FileEntry *File, - unsigned Line, unsigned Col) const { +SourceLocation ASTUnit::getLocation(const FileEntry *File, unsigned Line, + unsigned Col) const { const SourceManager &SM = getSourceManager(); SourceLocation Loc = SM.translateFileLineCol(File, Line, Col); return SM.getMacroArgExpandedLocation(Loc); @@ -2443,9 +2425,10 @@ return Loc; unsigned Offs; - if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble->getBounds().Size) { - SourceLocation FileLoc - = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID()); + if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && + Offs < Preamble->getBounds().Size) { + SourceLocation FileLoc = + SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID()); return FileLoc.getLocWithOffset(Offs); } @@ -2477,10 +2460,10 @@ FileID FID; if (SourceMgr) FID = SourceMgr->getPreambleFileID(); - + if (Loc.isInvalid() || FID.isInvalid()) return false; - + return SourceMgr->isInFileID(Loc, FID); } @@ -2488,10 +2471,10 @@ FileID FID; if (SourceMgr) FID = SourceMgr->getMainFileID(); - + if (Loc.isInvalid() || FID.isInvalid()) return false; - + return SourceMgr->isInFileID(Loc, FID); } @@ -2499,7 +2482,7 @@ FileID FID; if (SourceMgr) FID = SourceMgr->getPreambleFileID(); - + if (FID.isInvalid()) return SourceLocation(); @@ -2510,18 +2493,18 @@ FileID FID; if (SourceMgr) FID = SourceMgr->getMainFileID(); - + if (FID.isInvalid()) return SourceLocation(); - + return SourceMgr->getLocForStartOfFile(FID); } llvm::iterator_range ASTUnit::getLocalPreprocessingEntities() const { if (isMainFileAST()) { - serialization::ModuleFile & - Mod = Reader->getModuleManager().getPrimaryModule(); + serialization::ModuleFile &Mod = + Reader->getModuleManager().getPrimaryModule(); return Reader->getModulePreprocessedEntities(Mod); } @@ -2534,8 +2517,8 @@ bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) { if (isMainFileAST()) { - serialization::ModuleFile & - Mod = Reader->getModuleManager().getPrimaryModule(); + serialization::ModuleFile &Mod = + Reader->getModuleManager().getPrimaryModule(); for (const Decl *D : Reader->getModuleFileLevelDecls(Mod)) { if (!Fn(context, D)) return false; @@ -2545,8 +2528,8 @@ } for (ASTUnit::top_level_iterator TL = top_level_begin(), - TLEnd = top_level_end(); - TL != TLEnd; ++TL) { + TLEnd = top_level_end(); + TL != TLEnd; ++TL) { if (!Fn(context, *TL)) return false; } Index: lib/Frontend/PrecompiledPreamble.cpp =================================================================== --- lib/Frontend/PrecompiledPreamble.cpp +++ lib/Frontend/PrecompiledPreamble.cpp @@ -203,7 +203,7 @@ const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds, DiagnosticsEngine &Diagnostics, IntrusiveRefCntPtr VFS, std::shared_ptr PCHContainerOps, - PreambleCallbacks &Callbacks) { + PreambleCallbacks &Callbacks, std::unique_ptr PPCallbacks) { assert(VFS && "VFS is null"); if (!Bounds.Size) @@ -307,6 +307,7 @@ if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) return BuildPreambleError::BeginSourceFileFailed; + Clang->getPreprocessor().addPPCallbacks(std::move(PPCallbacks)); Act->Execute(); // Run the callbacks.