Skip to content

Commit 068a290

Browse files
ChuanqiXu9yuxuanchen1997
authored andcommitted
[clang] Split ObjectFilePCHContainerReader from ObjectFilePCHContainerWriter (#99599)
Summary: Close #99479 See #99479 for details Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251769
1 parent cdac073 commit 068a290

File tree

16 files changed

+96
-65
lines changed

16 files changed

+96
-65
lines changed

clang/include/clang/CodeGen/ObjectFilePCHContainerOperations.h renamed to clang/include/clang/CodeGen/ObjectFilePCHContainerWriter.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- CodeGen/ObjectFilePCHContainerOperations.h - ------------*- C++ -*-===//
1+
//===-- CodeGen/ObjectFilePCHContainerWriter.h ------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -29,14 +29,6 @@ class ObjectFilePCHContainerWriter : public PCHContainerWriter {
2929
std::shared_ptr<PCHBuffer> Buffer) const override;
3030
};
3131

32-
/// A PCHContainerReader implementation that uses LLVM to
33-
/// wraps Clang modules inside a COFF, ELF, or Mach-O container.
34-
class ObjectFilePCHContainerReader : public PCHContainerReader {
35-
ArrayRef<StringRef> getFormats() const override;
36-
37-
/// Returns the serialized AST inside the PCH container Buffer.
38-
StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override;
39-
};
4032
}
4133

4234
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Serialization/ObjectFilePCHContainerReader.h ------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_SERIALIZATION_OBJECTFILEPCHCONTAINERREADER_H
10+
#define LLVM_CLANG_SERIALIZATION_OBJECTFILEPCHCONTAINERREADER_H
11+
12+
#include "clang/Frontend/PCHContainerOperations.h"
13+
14+
namespace clang {
15+
/// A PCHContainerReader implementation that uses LLVM to
16+
/// wraps Clang modules inside a COFF, ELF, or Mach-O container.
17+
class ObjectFilePCHContainerReader : public PCHContainerReader {
18+
ArrayRef<StringRef> getFormats() const override;
19+
20+
/// Returns the serialized AST inside the PCH container Buffer.
21+
StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override;
22+
};
23+
} // namespace clang
24+
25+
#endif

clang/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ add_clang_library(clangCodeGen
110110
MacroPPCallbacks.cpp
111111
MicrosoftCXXABI.cpp
112112
ModuleBuilder.cpp
113-
ObjectFilePCHContainerOperations.cpp
113+
ObjectFilePCHContainerWriter.cpp
114114
PatternInit.cpp
115115
SanitizerMetadata.cpp
116116
SwiftCallingConv.cpp

clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp renamed to clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//===--- ObjectFilePCHContainerOperations.cpp -----------------------------===//
1+
//===--- ObjectFilePCHContainerWriter.cpp -----------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
9+
#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
1010
#include "CGDebugInfo.h"
1111
#include "CodeGenModule.h"
1212
#include "clang/AST/ASTContext.h"
@@ -351,46 +351,3 @@ ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
351351
return std::make_unique<PCHContainerGenerator>(
352352
CI, MainFileName, OutputFileName, std::move(OS), Buffer);
353353
}
354-
355-
ArrayRef<StringRef> ObjectFilePCHContainerReader::getFormats() const {
356-
static StringRef Formats[] = {"obj", "raw"};
357-
return Formats;
358-
}
359-
360-
StringRef
361-
ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
362-
StringRef PCH;
363-
auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
364-
if (OFOrErr) {
365-
auto &OF = OFOrErr.get();
366-
bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
367-
// Find the clang AST section in the container.
368-
for (auto &Section : OF->sections()) {
369-
StringRef Name;
370-
if (Expected<StringRef> NameOrErr = Section.getName())
371-
Name = *NameOrErr;
372-
else
373-
consumeError(NameOrErr.takeError());
374-
375-
if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
376-
if (Expected<StringRef> E = Section.getContents())
377-
return *E;
378-
else {
379-
handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
380-
EIB.log(llvm::errs());
381-
});
382-
return "";
383-
}
384-
}
385-
}
386-
}
387-
handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
388-
if (EIB.convertToErrorCode() ==
389-
llvm::object::object_error::invalid_file_type)
390-
// As a fallback, treat the buffer as a raw AST.
391-
PCH = Buffer.getBuffer();
392-
else
393-
EIB.log(llvm::errs());
394-
});
395-
return PCH;
396-
}

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "clang/Basic/TargetInfo.h"
2727
#include "clang/CodeGen/CodeGenAction.h"
2828
#include "clang/CodeGen/ModuleBuilder.h"
29-
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
29+
#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
3030
#include "clang/Driver/Compilation.h"
3131
#include "clang/Driver/Driver.h"
3232
#include "clang/Driver/Job.h"
@@ -38,6 +38,7 @@
3838
#include "clang/Interpreter/Value.h"
3939
#include "clang/Lex/PreprocessorOptions.h"
4040
#include "clang/Sema/Lookup.h"
41+
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
4142
#include "llvm/ExecutionEngine/JITSymbol.h"
4243
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
4344
#include "llvm/IR/Module.h"

clang/lib/Interpreter/InterpreterUtils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "clang/AST/TypeVisitor.h"
1919
#include "clang/Basic/TargetInfo.h"
2020
#include "clang/CodeGen/ModuleBuilder.h"
21-
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
2221
#include "clang/Driver/Compilation.h"
2322
#include "clang/Driver/Driver.h"
2423
#include "clang/Driver/Job.h"

clang/lib/Serialization/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(LLVM_LINK_COMPONENTS
22
BitReader
33
BitstreamReader
4+
Object
45
Support
56
TargetParser
67
)
@@ -21,6 +22,7 @@ add_clang_library(clangSerialization
2122
ModuleFileExtension.cpp
2223
ModuleManager.cpp
2324
PCHContainerOperations.cpp
25+
ObjectFilePCHContainerReader.cpp
2426

2527
ADDITIONAL_HEADERS
2628
ASTCommon.h
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===--- ObjectFilePCHContainerReader.cpp ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
10+
#include "llvm/Object/COFF.h"
11+
#include "llvm/Object/ObjectFile.h"
12+
13+
using namespace clang;
14+
15+
ArrayRef<StringRef> ObjectFilePCHContainerReader::getFormats() const {
16+
static StringRef Formats[] = {"obj", "raw"};
17+
return Formats;
18+
}
19+
20+
StringRef
21+
ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
22+
StringRef PCH;
23+
auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
24+
if (OFOrErr) {
25+
auto &OF = OFOrErr.get();
26+
bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
27+
// Find the clang AST section in the container.
28+
for (auto &Section : OF->sections()) {
29+
StringRef Name;
30+
if (Expected<StringRef> NameOrErr = Section.getName())
31+
Name = *NameOrErr;
32+
else
33+
consumeError(NameOrErr.takeError());
34+
35+
if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
36+
if (Expected<StringRef> E = Section.getContents())
37+
return *E;
38+
else {
39+
handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
40+
EIB.log(llvm::errs());
41+
});
42+
return "";
43+
}
44+
}
45+
}
46+
}
47+
handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
48+
if (EIB.convertToErrorCode() ==
49+
llvm::object::object_error::invalid_file_type)
50+
// As a fallback, treat the buffer as a raw AST.
51+
PCH = Buffer.getBuffer();
52+
else
53+
EIB.log(llvm::errs());
54+
});
55+
return PCH;
56+
}

clang/lib/Tooling/DependencyScanning/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ add_clang_library(clangDependencyScanning
1919
LINK_LIBS
2020
clangAST
2121
clangBasic
22-
clangCodeGen
2322
clangDriver
2423
clangFrontend
2524
clangLex

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "clang/Basic/DiagnosticDriver.h"
1111
#include "clang/Basic/DiagnosticFrontend.h"
1212
#include "clang/Basic/DiagnosticSerialization.h"
13-
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
1413
#include "clang/Driver/Compilation.h"
1514
#include "clang/Driver/Driver.h"
1615
#include "clang/Driver/Job.h"
@@ -21,6 +20,7 @@
2120
#include "clang/Frontend/TextDiagnosticPrinter.h"
2221
#include "clang/Frontend/Utils.h"
2322
#include "clang/Lex/PreprocessorOptions.h"
23+
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
2424
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
2525
#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
2626
#include "clang/Tooling/Tooling.h"

clang/tools/c-index-test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ else()
2727
libclang
2828
clangAST
2929
clangBasic
30-
clangCodeGen
3130
clangFrontend
3231
clangIndex
3332
clangSerialization

clang/tools/c-index-test/core_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "clang/AST/Mangle.h"
1010
#include "clang/Basic/LangOptions.h"
11-
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
1211
#include "clang/Frontend/ASTUnit.h"
1312
#include "clang/Frontend/CompilerInstance.h"
1413
#include "clang/Frontend/CompilerInvocation.h"
@@ -19,6 +18,7 @@
1918
#include "clang/Index/USRGeneration.h"
2019
#include "clang/Lex/Preprocessor.h"
2120
#include "clang/Serialization/ASTReader.h"
21+
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
2222
#include "llvm/Support/CommandLine.h"
2323
#include "llvm/Support/FileSystem.h"
2424
#include "llvm/Support/PrettyStackTrace.h"

clang/tools/clang-check/ClangCheck.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "clang/AST/ASTConsumer.h"
19-
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
2019
#include "clang/Driver/Options.h"
2120
#include "clang/Frontend/ASTConsumers.h"
2221
#include "clang/Frontend/CompilerInstance.h"

clang/tools/driver/cc1_main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include "clang/Basic/Stack.h"
1616
#include "clang/Basic/TargetOptions.h"
17-
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
17+
#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
1818
#include "clang/Config/config.h"
1919
#include "clang/Driver/DriverDiagnostic.h"
2020
#include "clang/Driver/Options.h"
@@ -25,6 +25,7 @@
2525
#include "clang/Frontend/TextDiagnosticPrinter.h"
2626
#include "clang/Frontend/Utils.h"
2727
#include "clang/FrontendTool/Utils.h"
28+
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
2829
#include "llvm/ADT/Statistic.h"
2930
#include "llvm/ADT/StringExtras.h"
3031
#include "llvm/Config/llvm-config.h"

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@
5959
#include "lldb/lldb-enumerations.h"
6060
#include "lldb/lldb-private-enumerations.h"
6161

62-
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
6362
#include "clang/Frontend/CompilerInstance.h"
6463
#include "clang/Frontend/CompilerInvocation.h"
6564
#include "clang/Frontend/FrontendActions.h"
65+
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
6666
#include "llvm/ADT/ScopeExit.h"
6767
#include "llvm/ADT/StringRef.h"
6868
#include "llvm/Support/FileSystem.h"

lldb/tools/lldb-instr/Instrument.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "clang/AST/AST.h"
22
#include "clang/AST/ASTConsumer.h"
33
#include "clang/AST/RecursiveASTVisitor.h"
4-
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
4+
#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
55
#include "clang/Frontend/ASTConsumers.h"
66
#include "clang/Frontend/CompilerInstance.h"
77
#include "clang/Frontend/FrontendActions.h"
88
#include "clang/Rewrite/Core/Rewriter.h"
9+
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
910
#include "clang/Tooling/CommonOptionsParser.h"
1011
#include "clang/Tooling/Tooling.h"
1112

0 commit comments

Comments
 (0)