Skip to content

Commit 8ff77a8

Browse files
authored
[NFC][LLD] Refactor some copy-paste into the Common library (llvm#67598)
1 parent 9d48856 commit 8ff77a8

File tree

5 files changed

+33
-51
lines changed

5 files changed

+33
-51
lines changed

lld/COFF/LTO.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Symbols.h"
1414
#include "lld/Common/Args.h"
1515
#include "lld/Common/CommonLinkerContext.h"
16+
#include "lld/Common/Filesystem.h"
1617
#include "lld/Common/Strings.h"
1718
#include "lld/Common/TargetOptionsCommandFlags.h"
1819
#include "llvm/ADT/STLExtras.h"
@@ -42,18 +43,6 @@ using namespace llvm::object;
4243
using namespace lld;
4344
using namespace lld::coff;
4445

45-
// Creates an empty file to and returns a raw_fd_ostream to write to it.
46-
static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
47-
std::error_code ec;
48-
auto ret =
49-
std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
50-
if (ec) {
51-
error("cannot open " + file + ": " + ec.message());
52-
return nullptr;
53-
}
54-
return ret;
55-
}
56-
5746
std::string BitcodeCompiler::getThinLTOOutputFile(StringRef path) {
5847
return lto::getThinLTOOutputFile(path, ctx.config.thinLTOPrefixReplaceOld,
5948
ctx.config.thinLTOPrefixReplaceNew);

lld/Common/Filesystem.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "lld/Common/Filesystem.h"
14+
#include "lld/Common/ErrorHandler.h"
1415
#include "llvm/Config/llvm-config.h"
1516
#include "llvm/Support/FileOutputBuffer.h"
1617
#include "llvm/Support/FileSystem.h"
@@ -127,3 +128,28 @@ std::error_code lld::tryCreateFile(StringRef path) {
127128
return std::error_code();
128129
return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError());
129130
}
131+
132+
// Creates an empty file to and returns a raw_fd_ostream to write to it.
133+
std::unique_ptr<raw_fd_ostream> lld::openFile(StringRef file) {
134+
std::error_code ec;
135+
auto ret =
136+
std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
137+
if (ec) {
138+
error("cannot open " + file + ": " + ec.message());
139+
return nullptr;
140+
}
141+
return ret;
142+
}
143+
144+
// The merged bitcode after LTO is large. Try opening a file stream that
145+
// supports reading, seeking and writing. Such a file allows BitcodeWriter to
146+
// flush buffered data to reduce memory consumption. If this fails, open a file
147+
// stream that supports only write.
148+
std::unique_ptr<raw_fd_ostream> lld::openLTOOutputFile(StringRef file) {
149+
std::error_code ec;
150+
std::unique_ptr<raw_fd_ostream> fs =
151+
std::make_unique<raw_fd_stream>(file, ec);
152+
if (!ec)
153+
return fs;
154+
return openFile(file);
155+
}

lld/ELF/LTO.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Symbols.h"
1414
#include "lld/Common/Args.h"
1515
#include "lld/Common/ErrorHandler.h"
16+
#include "lld/Common/Filesystem.h"
1617
#include "lld/Common/Strings.h"
1718
#include "lld/Common/TargetOptionsCommandFlags.h"
1819
#include "llvm/ADT/SmallString.h"
@@ -40,32 +41,6 @@ using namespace llvm::ELF;
4041
using namespace lld;
4142
using namespace lld::elf;
4243

43-
// Creates an empty file to store a list of object files for final
44-
// linking of distributed ThinLTO.
45-
static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
46-
std::error_code ec;
47-
auto ret =
48-
std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
49-
if (ec) {
50-
error("cannot open " + file + ": " + ec.message());
51-
return nullptr;
52-
}
53-
return ret;
54-
}
55-
56-
// The merged bitcode after LTO is large. Try opening a file stream that
57-
// supports reading, seeking and writing. Such a file allows BitcodeWriter to
58-
// flush buffered data to reduce memory consumption. If this fails, open a file
59-
// stream that supports only write.
60-
static std::unique_ptr<raw_fd_ostream> openLTOOutputFile(StringRef file) {
61-
std::error_code ec;
62-
std::unique_ptr<raw_fd_ostream> fs =
63-
std::make_unique<raw_fd_stream>(file, ec);
64-
if (!ec)
65-
return fs;
66-
return openFile(file);
67-
}
68-
6944
static std::string getThinLTOOutputFile(StringRef modulePath) {
7045
return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
7146
config->thinLTOPrefixReplaceNew);

lld/MachO/LTO.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "lld/Common/Args.h"
1717
#include "lld/Common/CommonLinkerContext.h"
18+
#include "lld/Common/Filesystem.h"
1819
#include "lld/Common/Strings.h"
1920
#include "lld/Common/TargetOptionsCommandFlags.h"
2021
#include "llvm/Bitcode/BitcodeWriter.h"
@@ -32,19 +33,6 @@ using namespace llvm;
3233
using namespace llvm::MachO;
3334
using namespace llvm::sys;
3435

35-
// Creates an empty file to store a list of object files for final
36-
// linking of distributed ThinLTO.
37-
static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
38-
std::error_code ec;
39-
auto ret =
40-
std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
41-
if (ec) {
42-
error("cannot open " + file + ": " + ec.message());
43-
return nullptr;
44-
}
45-
return ret;
46-
}
47-
4836
static std::string getThinLTOOutputFile(StringRef modulePath) {
4937
return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
5038
config->thinLTOPrefixReplaceNew);

lld/include/lld/Common/Filesystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
#define LLD_FILESYSTEM_H
1111

1212
#include "lld/Common/LLVM.h"
13+
#include "llvm/Support/raw_ostream.h"
14+
#include <memory>
1315
#include <system_error>
1416

1517
namespace lld {
1618
void unlinkAsync(StringRef path);
1719
std::error_code tryCreateFile(StringRef path);
20+
std::unique_ptr<llvm::raw_fd_ostream> openFile(StringRef file);
21+
std::unique_ptr<llvm::raw_fd_ostream> openLTOOutputFile(StringRef file);
1822
} // namespace lld
1923

2024
#endif

0 commit comments

Comments
 (0)