[clang] [lldb] [clang] Split ObjectFilePCHContainerReader from ObjectFilePCHContainerWriter (PR #99599)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 18 20:08:41 PDT 2024


https://github.com/ChuanqiXu9 created https://github.com/llvm/llvm-project/pull/99599

Close https://github.com/llvm/llvm-project/issues/99479

See https://github.com/llvm/llvm-project/issues/99479 for details

>From 36e24bd88649e9d5771f1dbb668632d33ffe52d7 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Fri, 19 Jul 2024 11:02:36 +0800
Subject: [PATCH] [clang] Split ObjectFilePCHContainerReader from
 ObjectFilePCHContainerWriter

Close https://github.com/llvm/llvm-project/issues/99479

See https://github.com/llvm/llvm-project/issues/99479 for details
---
 ...tions.h => ObjectFilePCHContainerWriter.h} | 10 +---
 .../ObjectFilePCHContainerReader.h            | 26 +++++++++
 clang/lib/CodeGen/CMakeLists.txt              |  2 +-
 ...s.cpp => ObjectFilePCHContainerWriter.cpp} | 47 +---------------
 clang/lib/Interpreter/Interpreter.cpp         |  3 +-
 clang/lib/Interpreter/InterpreterUtils.h      |  1 -
 clang/lib/Serialization/CMakeLists.txt        |  2 +
 .../ObjectFilePCHContainerReader.cpp          | 56 +++++++++++++++++++
 .../Tooling/DependencyScanning/CMakeLists.txt |  1 -
 .../DependencyScanningWorker.cpp              |  2 +-
 clang/tools/c-index-test/CMakeLists.txt       |  1 -
 clang/tools/c-index-test/core_main.cpp        |  2 +-
 clang/tools/clang-check/ClangCheck.cpp        |  1 -
 clang/tools/driver/cc1_main.cpp               |  3 +-
 lldb/source/Commands/CommandObjectTarget.cpp  |  2 +-
 lldb/tools/lldb-instr/Instrument.cpp          |  3 +-
 16 files changed, 97 insertions(+), 65 deletions(-)
 rename clang/include/clang/CodeGen/{ObjectFilePCHContainerOperations.h => ObjectFilePCHContainerWriter.h} (73%)
 create mode 100644 clang/include/clang/Serialization/ObjectFilePCHContainerReader.h
 rename clang/lib/CodeGen/{ObjectFilePCHContainerOperations.cpp => ObjectFilePCHContainerWriter.cpp} (89%)
 create mode 100644 clang/lib/Serialization/ObjectFilePCHContainerReader.cpp

diff --git a/clang/include/clang/CodeGen/ObjectFilePCHContainerOperations.h b/clang/include/clang/CodeGen/ObjectFilePCHContainerWriter.h
similarity index 73%
rename from clang/include/clang/CodeGen/ObjectFilePCHContainerOperations.h
rename to clang/include/clang/CodeGen/ObjectFilePCHContainerWriter.h
index 7a02d8725885a..5aef4a2d23de0 100644
--- a/clang/include/clang/CodeGen/ObjectFilePCHContainerOperations.h
+++ b/clang/include/clang/CodeGen/ObjectFilePCHContainerWriter.h
@@ -1,4 +1,4 @@
-//===-- CodeGen/ObjectFilePCHContainerOperations.h - ------------*- C++ -*-===//
+//===-- CodeGen/ObjectFilePCHContainerWriter.h - ------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -29,14 +29,6 @@ class ObjectFilePCHContainerWriter : public PCHContainerWriter {
                               std::shared_ptr<PCHBuffer> Buffer) const override;
 };
 
-/// A PCHContainerReader implementation that uses LLVM to
-/// wraps Clang modules inside a COFF, ELF, or Mach-O container.
-class ObjectFilePCHContainerReader : public PCHContainerReader {
-  ArrayRef<StringRef> getFormats() const override;
-
-  /// Returns the serialized AST inside the PCH container Buffer.
-  StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override;
-};
 }
 
 #endif
diff --git a/clang/include/clang/Serialization/ObjectFilePCHContainerReader.h b/clang/include/clang/Serialization/ObjectFilePCHContainerReader.h
new file mode 100644
index 0000000000000..65b90299545f7
--- /dev/null
+++ b/clang/include/clang/Serialization/ObjectFilePCHContainerReader.h
@@ -0,0 +1,26 @@
+//===-- Serialization/ObjectFilePCHContainerReader.h ------------*- 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_SERIALIZATION_OBJECTFILEPCHCONTAINERREADER_H
+#define LLVM_CLANG_SERIALIZATION_OBJECTFILEPCHCONTAINERREADER_H
+
+#include "clang/Frontend/PCHContainerOperations.h"
+
+namespace clang {
+/// A PCHContainerReader implementation that uses LLVM to
+/// wraps Clang modules inside a COFF, ELF, or Mach-O container.
+class ObjectFilePCHContainerReader : public PCHContainerReader {
+  ArrayRef<StringRef> getFormats() const override;
+
+  /// Returns the serialized AST inside the PCH container Buffer.
+  StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override;
+};
+}
+
+#endif
\ No newline at end of file
diff --git a/clang/lib/CodeGen/CMakeLists.txt b/clang/lib/CodeGen/CMakeLists.txt
index 2a179deddcc31..deb7b27266d73 100644
--- a/clang/lib/CodeGen/CMakeLists.txt
+++ b/clang/lib/CodeGen/CMakeLists.txt
@@ -110,7 +110,7 @@ add_clang_library(clangCodeGen
   MacroPPCallbacks.cpp
   MicrosoftCXXABI.cpp
   ModuleBuilder.cpp
-  ObjectFilePCHContainerOperations.cpp
+  ObjectFilePCHContainerWriter.cpp
   PatternInit.cpp
   SanitizerMetadata.cpp
   SwiftCallingConv.cpp
diff --git a/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp b/clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp
similarity index 89%
rename from clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
rename to clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp
index ee543e40b4609..3a1f745d9ed77 100644
--- a/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
+++ b/clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp
@@ -1,4 +1,4 @@
-//===--- ObjectFilePCHContainerOperations.cpp -----------------------------===//
+//===--- ObjectFilePCHContainerWriter.cpp -----------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
+#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
 #include "CGDebugInfo.h"
 #include "CodeGenModule.h"
 #include "clang/AST/ASTContext.h"
@@ -351,46 +351,3 @@ ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
   return std::make_unique<PCHContainerGenerator>(
       CI, MainFileName, OutputFileName, std::move(OS), Buffer);
 }
-
-ArrayRef<StringRef> ObjectFilePCHContainerReader::getFormats() const {
-  static StringRef Formats[] = {"obj", "raw"};
-  return Formats;
-}
-
-StringRef
-ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
-  StringRef PCH;
-  auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
-  if (OFOrErr) {
-    auto &OF = OFOrErr.get();
-    bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
-    // Find the clang AST section in the container.
-    for (auto &Section : OF->sections()) {
-      StringRef Name;
-      if (Expected<StringRef> NameOrErr = Section.getName())
-        Name = *NameOrErr;
-      else
-        consumeError(NameOrErr.takeError());
-
-      if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
-        if (Expected<StringRef> E = Section.getContents())
-          return *E;
-        else {
-          handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
-            EIB.log(llvm::errs());
-          });
-          return "";
-        }
-      }
-    }
-  }
-  handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
-    if (EIB.convertToErrorCode() ==
-        llvm::object::object_error::invalid_file_type)
-      // As a fallback, treat the buffer as a raw AST.
-      PCH = Buffer.getBuffer();
-    else
-      EIB.log(llvm::errs());
-  });
-  return PCH;
-}
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index b4882ab5d2236..7209a33272ef2 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -26,7 +26,7 @@
 #include "clang/Basic/TargetInfo.h"
 #include "clang/CodeGen/CodeGenAction.h"
 #include "clang/CodeGen/ModuleBuilder.h"
-#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
+#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/Job.h"
@@ -38,6 +38,7 @@
 #include "clang/Interpreter/Value.h"
 #include "clang/Lex/PreprocessorOptions.h"
 #include "clang/Sema/Lookup.h"
+#include "clang/Serialization/ObjectFilePCHContainerReader.h"
 #include "llvm/ExecutionEngine/JITSymbol.h"
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
 #include "llvm/IR/Module.h"
diff --git a/clang/lib/Interpreter/InterpreterUtils.h b/clang/lib/Interpreter/InterpreterUtils.h
index 8df158c17d491..c7b405b486d93 100644
--- a/clang/lib/Interpreter/InterpreterUtils.h
+++ b/clang/lib/Interpreter/InterpreterUtils.h
@@ -18,7 +18,6 @@
 #include "clang/AST/TypeVisitor.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/CodeGen/ModuleBuilder.h"
-#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/Job.h"
diff --git a/clang/lib/Serialization/CMakeLists.txt b/clang/lib/Serialization/CMakeLists.txt
index 5a4b3a58e9c45..99c47c15a2f47 100644
--- a/clang/lib/Serialization/CMakeLists.txt
+++ b/clang/lib/Serialization/CMakeLists.txt
@@ -1,6 +1,7 @@
 set(LLVM_LINK_COMPONENTS
   BitReader
   BitstreamReader
+  Object
   Support
   TargetParser
   )
@@ -21,6 +22,7 @@ add_clang_library(clangSerialization
   ModuleFileExtension.cpp
   ModuleManager.cpp
   PCHContainerOperations.cpp
+  ObjectFilePCHContainerReader.cpp
 
   ADDITIONAL_HEADERS
   ASTCommon.h
diff --git a/clang/lib/Serialization/ObjectFilePCHContainerReader.cpp b/clang/lib/Serialization/ObjectFilePCHContainerReader.cpp
new file mode 100644
index 0000000000000..59fc46960dc60
--- /dev/null
+++ b/clang/lib/Serialization/ObjectFilePCHContainerReader.cpp
@@ -0,0 +1,56 @@
+//===--- ObjectFilePCHContainerReader.cpp ---------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Serialization/ObjectFilePCHContainerReader.h"
+#include "llvm/Object/COFF.h"
+#include "llvm/Object/ObjectFile.h"
+
+using namespace clang;
+
+ArrayRef<StringRef> ObjectFilePCHContainerReader::getFormats() const {
+  static StringRef Formats[] = {"obj", "raw"};
+  return Formats;
+}
+
+StringRef
+ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
+  StringRef PCH;
+  auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
+  if (OFOrErr) {
+    auto &OF = OFOrErr.get();
+    bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
+    // Find the clang AST section in the container.
+    for (auto &Section : OF->sections()) {
+      StringRef Name;
+      if (Expected<StringRef> NameOrErr = Section.getName())
+        Name = *NameOrErr;
+      else
+        consumeError(NameOrErr.takeError());
+
+      if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
+        if (Expected<StringRef> E = Section.getContents())
+          return *E;
+        else {
+          handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
+            EIB.log(llvm::errs());
+          });
+          return "";
+        }
+      }
+    }
+  }
+  handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
+    if (EIB.convertToErrorCode() ==
+        llvm::object::object_error::invalid_file_type)
+      // As a fallback, treat the buffer as a raw AST.
+      PCH = Buffer.getBuffer();
+    else
+      EIB.log(llvm::errs());
+  });
+  return PCH;
+}
diff --git a/clang/lib/Tooling/DependencyScanning/CMakeLists.txt b/clang/lib/Tooling/DependencyScanning/CMakeLists.txt
index bc807e162c919..66795b0be0baa 100644
--- a/clang/lib/Tooling/DependencyScanning/CMakeLists.txt
+++ b/clang/lib/Tooling/DependencyScanning/CMakeLists.txt
@@ -19,7 +19,6 @@ add_clang_library(clangDependencyScanning
   LINK_LIBS
   clangAST
   clangBasic
-  clangCodeGen
   clangDriver
   clangFrontend
   clangLex
diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index 0f82f22d8b9a8..91842627b001c 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -10,7 +10,6 @@
 #include "clang/Basic/DiagnosticDriver.h"
 #include "clang/Basic/DiagnosticFrontend.h"
 #include "clang/Basic/DiagnosticSerialization.h"
-#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/Job.h"
@@ -21,6 +20,7 @@
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Frontend/Utils.h"
 #include "clang/Lex/PreprocessorOptions.h"
+#include "clang/Serialization/ObjectFilePCHContainerReader.h"
 #include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
 #include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
 #include "clang/Tooling/Tooling.h"
diff --git a/clang/tools/c-index-test/CMakeLists.txt b/clang/tools/c-index-test/CMakeLists.txt
index 0ae1b4e55244e..24e7c9692ca56 100644
--- a/clang/tools/c-index-test/CMakeLists.txt
+++ b/clang/tools/c-index-test/CMakeLists.txt
@@ -27,7 +27,6 @@ else()
     libclang
     clangAST
     clangBasic
-    clangCodeGen
     clangFrontend
     clangIndex
     clangSerialization
diff --git a/clang/tools/c-index-test/core_main.cpp b/clang/tools/c-index-test/core_main.cpp
index c552466c9a188..003b1baef3a25 100644
--- a/clang/tools/c-index-test/core_main.cpp
+++ b/clang/tools/c-index-test/core_main.cpp
@@ -8,7 +8,6 @@
 
 #include "clang/AST/Mangle.h"
 #include "clang/Basic/LangOptions.h"
-#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/CompilerInvocation.h"
@@ -19,6 +18,7 @@
 #include "clang/Index/USRGeneration.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Serialization/ASTReader.h"
+#include "clang/Serialization/ObjectFilePCHContainerReader.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/PrettyStackTrace.h"
diff --git a/clang/tools/clang-check/ClangCheck.cpp b/clang/tools/clang-check/ClangCheck.cpp
index 0524becf4f484..fa6dd06a1ee58 100644
--- a/clang/tools/clang-check/ClangCheck.cpp
+++ b/clang/tools/clang-check/ClangCheck.cpp
@@ -16,7 +16,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/AST/ASTConsumer.h"
-#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
 #include "clang/Driver/Options.h"
 #include "clang/Frontend/ASTConsumers.h"
 #include "clang/Frontend/CompilerInstance.h"
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index c2ccb47a15bc8..1d9b04c319ff9 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -14,7 +14,7 @@
 
 #include "clang/Basic/Stack.h"
 #include "clang/Basic/TargetOptions.h"
-#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
+#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
 #include "clang/Config/config.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/Options.h"
@@ -25,6 +25,7 @@
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Frontend/Utils.h"
 #include "clang/FrontendTool/Utils.h"
+#include "clang/Serialization/ObjectFilePCHContainerReader.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Config/llvm-config.h"
diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp
index d594330934ad7..cc381a2ecee1d 100644
--- a/lldb/source/Commands/CommandObjectTarget.cpp
+++ b/lldb/source/Commands/CommandObjectTarget.cpp
@@ -59,10 +59,10 @@
 #include "lldb/lldb-enumerations.h"
 #include "lldb/lldb-private-enumerations.h"
 
-#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/CompilerInvocation.h"
 #include "clang/Frontend/FrontendActions.h"
+#include "clang/Serialization/ObjectFilePCHContainerReader.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/FileSystem.h"
diff --git a/lldb/tools/lldb-instr/Instrument.cpp b/lldb/tools/lldb-instr/Instrument.cpp
index 4b8725396a61f..d07ccf121bdf3 100644
--- a/lldb/tools/lldb-instr/Instrument.cpp
+++ b/lldb/tools/lldb-instr/Instrument.cpp
@@ -1,11 +1,12 @@
 #include "clang/AST/AST.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/RecursiveASTVisitor.h"
-#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
+#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
 #include "clang/Frontend/ASTConsumers.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Rewrite/Core/Rewriter.h"
+#include "clang/Serialization/ObjectFilePCHContainerReader.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 



More information about the cfe-commits mailing list