Index: clang/include/clang/Frontend/CompilerInstance.h =================================================================== --- clang/include/clang/Frontend/CompilerInstance.h +++ clang/include/clang/Frontend/CompilerInstance.h @@ -12,6 +12,7 @@ #include "clang/AST/ASTConsumer.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/SourceManager.h" +#include "clang/Frontend/CompilerInstanceBuilder.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/PCHContainerOperations.h" #include "clang/Frontend/Utils.h" @@ -186,6 +187,7 @@ CompilerInstance(const CompilerInstance &) = delete; void operator=(const CompilerInstance &) = delete; public: + explicit CompilerInstance(CompilerInstanceBuilder Builder); explicit CompilerInstance( std::shared_ptr PCHContainerOps = std::make_shared(), Index: clang/include/clang/Frontend/CompilerInstanceBuilder.h =================================================================== --- /dev/null +++ clang/include/clang/Frontend/CompilerInstanceBuilder.h @@ -0,0 +1,55 @@ +//===- CompilerInstanceBuilder.h - CompilerInstance Builder -----*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCEBUILDER_H_ +#define LLVM_CLANG_FRONTEND_COMPILERINSTANCEBUILDER_H_ + +namespace clang { + +class InMemoryModuleCache; +class PCHContainerOperations; + +class CompilerInstanceBuilder { + InMemoryModuleCache *SharedModuleCache = nullptr; + std::shared_ptr PCHContainerOps; + +public: + CompilerInstanceBuilder() = default; + CompilerInstanceBuilder(CompilerInstanceBuilder &&) = default; + CompilerInstanceBuilder(const CompilerInstanceBuilder &) = delete; + + CompilerInstanceBuilder && + pchContainerOps(std::shared_ptr PCHContainerOps) && { + this->PCHContainerOps = std::move(PCHContainerOps); + return std::move(*this); + } + + CompilerInstanceBuilder && + moduleCache(InMemoryModuleCache &SharedModuleCache) && { + this->SharedModuleCache = &SharedModuleCache; + return std::move(*this); + } + +private: + friend class CompilerInstance; + + // Deprecated, for CompilerInstance's current constructor. + CompilerInstanceBuilder && + sharedModuleCache(InMemoryModuleCache *SharedModuleCache) && { + this->SharedModuleCache = SharedModuleCache; + return std::move(*this); + } + + IntrusiveRefCntPtr getModuleCache(); + std::shared_ptr getPCHContainerOps(); + bool isBuildingModule(); +}; + +} // end namespace clang + +#endif Index: clang/lib/Frontend/CompilerInstance.cpp =================================================================== --- clang/lib/Frontend/CompilerInstance.cpp +++ clang/lib/Frontend/CompilerInstance.cpp @@ -56,14 +56,31 @@ using namespace clang; +IntrusiveRefCntPtr +CompilerInstanceBuilder::getModuleCache() { + return SharedModuleCache ? SharedModuleCache : new InMemoryModuleCache(); +} + +std::shared_ptr +CompilerInstanceBuilder::getPCHContainerOps() { + return PCHContainerOps ? PCHContainerOps + : std::make_shared(); +} + +bool CompilerInstanceBuilder::isBuildingModule() { return SharedModuleCache; } + +CompilerInstance::CompilerInstance(CompilerInstanceBuilder Builder) + : ModuleLoader(Builder.isBuildingModule()), + Invocation(new CompilerInvocation()), + ModuleCache(Builder.getModuleCache()), + ThePCHContainerOperations(Builder.getPCHContainerOps()) {} + CompilerInstance::CompilerInstance( std::shared_ptr PCHContainerOps, InMemoryModuleCache *SharedModuleCache) - : ModuleLoader(/* BuildingModule = */ SharedModuleCache), - Invocation(new CompilerInvocation()), - ModuleCache(SharedModuleCache ? SharedModuleCache - : new InMemoryModuleCache), - ThePCHContainerOperations(std::move(PCHContainerOps)) {} + : CompilerInstance(CompilerInstanceBuilder() + .sharedModuleCache(SharedModuleCache) + .pchContainerOps(PCHContainerOps)) {} CompilerInstance::~CompilerInstance() { assert(OutputFiles.empty() && "Still output files in flight?");