- Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang] Split ObjectFilePCHContainerReader from ObjectFilePCHContainerWriter #99599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| @llvm/pr-subscribers-lldb @llvm/pr-subscribers-clang-modules Author: Chuanqi Xu (ChuanqiXu9) ChangesClose #99479 See #99479 for details Full diff: https://github.com/llvm/llvm-project/pull/99599.diff 16 Files Affected:
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" |
You can test this locally with the following command:git-clang-format --diff 962d018234cb8c94e387fe3950cd030658850541 2249d5021fb3f9de213772603893e6fa2a0ff7f8 --extensions h,cpp -- clang/include/clang/Serialization/ObjectFilePCHContainerReader.h clang/lib/Serialization/ObjectFilePCHContainerReader.cpp clang/lib/Interpreter/Interpreter.cpp clang/lib/Interpreter/InterpreterUtils.h clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp clang/tools/c-index-test/core_main.cpp clang/tools/clang-check/ClangCheck.cpp clang/tools/driver/cc1_main.cpp lldb/source/Commands/CommandObjectTarget.cpp lldb/tools/lldb-instr/Instrument.cpp clang/include/clang/CodeGen/ObjectFilePCHContainerWriter.h clang/lib/CodeGen/ObjectFilePCHContainerWriter.cppView the diff from clang-format here.diff --git a/clang/include/clang/CodeGen/ObjectFilePCHContainerWriter.h b/clang/include/clang/CodeGen/ObjectFilePCHContainerWriter.h index 26ee9f2225..ccb4faa93d 100644 --- a/clang/include/clang/CodeGen/ObjectFilePCHContainerWriter.h +++ b/clang/include/clang/CodeGen/ObjectFilePCHContainerWriter.h @@ -29,6 +29,6 @@ class ObjectFilePCHContainerWriter : public PCHContainerWriter { std::shared_ptr<PCHBuffer> Buffer) const override; }; -} +} // namespace clang #endif |
jansvoboda11 left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM sans the missing newline at the end of file and file headers not aligned to 80 columns.
benlangmuir left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM other than what Jan already mentioned, thanks!
…rWriter Close llvm#99479 See llvm#99479 for details
36e24bd to 2249d50 Compare …rWriter (#99599) Summary: Close #99479 See #99479 for details Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251769
…rWriter (llvm#99599) Close llvm#99479 See llvm#99479 for details (cherry picked from commit d64eccf) Conflicts: clang/lib/Tooling/DependencyScanning/CMakeLists.txt clang/tools/c-index-test/CMakeLists.txt clang/tools/c-index-test/core_main.cpp clang/tools/driver/cc1_main.cpp
…rWriter (llvm#99599) Close llvm#99479 See llvm#99479 for details (cherry picked from commit d64eccf) rdar://144790713
…rWriter (llvm#99599) Close llvm#99479 See llvm#99479 for details (cherry picked from commit d64eccf) Conflicts: clang/lib/Tooling/DependencyScanning/CMakeLists.txt clang/tools/c-index-test/CMakeLists.txt clang/tools/c-index-test/core_main.cpp clang/tools/driver/cc1_main.cpp (cherry picked from commit 756acd5)
Close #99479
See #99479 for details