dexonsmith created this revision.
dexonsmith added a reviewer: rsmith.
dexonsmith added a child revision: D62458: Frontend: Add CompilerInvocation and 
DiagnosticsEngine to the builder.

This sketches out the beginning of a `CompilerInstanceBuilder`.  For now
it just handles PCHContainerOps and InMemoryModuleCache, which are
already handled by the `CompilerInstance` constructor.

Subsequent commits will incrementally replace `CompilerInstance::set*`
and `CompilerInstance::create*` mutators with builder methods,
simplifying construction and possibly clarifying some lifetimes.


https://reviews.llvm.org/D62457

Files:
  clang/include/clang/Frontend/CompilerInstance.h
  clang/include/clang/Frontend/CompilerInstanceBuilder.h
  clang/lib/Frontend/CompilerInstance.cpp

Index: clang/lib/Frontend/CompilerInstance.cpp
===================================================================
--- clang/lib/Frontend/CompilerInstance.cpp
+++ clang/lib/Frontend/CompilerInstance.cpp
@@ -56,14 +56,31 @@
 
 using namespace clang;
 
+IntrusiveRefCntPtr<InMemoryModuleCache>
+CompilerInstanceBuilder::getModuleCache() {
+  return SharedModuleCache ? SharedModuleCache : new InMemoryModuleCache();
+}
+
+std::shared_ptr<PCHContainerOperations>
+CompilerInstanceBuilder::getPCHContainerOps() {
+  return PCHContainerOps ? PCHContainerOps
+                         : std::make_shared<PCHContainerOperations>();
+}
+
+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<PCHContainerOperations> 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?");
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<PCHContainerOperations> PCHContainerOps;
+
+public:
+  CompilerInstanceBuilder() = default;
+  CompilerInstanceBuilder(CompilerInstanceBuilder &&) = default;
+  CompilerInstanceBuilder(const CompilerInstanceBuilder &) = delete;
+
+  CompilerInstanceBuilder &&
+  pchContainerOps(std::shared_ptr<PCHContainerOperations> 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<InMemoryModuleCache> getModuleCache();
+  std::shared_ptr<PCHContainerOperations> getPCHContainerOps();
+  bool isBuildingModule();
+};
+
+} // end namespace clang
+
+#endif
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<PCHContainerOperations> PCHContainerOps =
           std::make_shared<PCHContainerOperations>(),
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to