Skip to content

Commit f232267

Browse files
committed
Replace llvm::OwningPtr with std::unique_ptr
It looks like llvm::OwningPtr is going to be removed soon. Swift SVN r14729
1 parent 07cb1c9 commit f232267

File tree

16 files changed

+52
-81
lines changed

16 files changed

+52
-81
lines changed

Diff for: include/swift/Basic/SourceManager.h

+5
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,13 @@ class SourceManager {
9898
return unsigned(BufferID);
9999
}
100100

101+
// FIXME: remove this overload.
101102
size_t addNewSourceBuffer(llvm::MemoryBuffer *Buffer);
102103

104+
size_t addNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
105+
return addNewSourceBuffer(Buffer.release());
106+
}
107+
103108
/// Returns a buffer ID for a previously added buffer with the given
104109
/// buffer identifier, or Nothing if there is no such buffer.
105110
Optional<unsigned> getIDForBufferIdentifier(StringRef BufIdentifier);

Diff for: include/swift/Driver/OutputFileMap.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class OutputFileMap {
4848
/// Loads an OutputFileMap from the given \p Buffer, taking ownership
4949
/// of the buffer in the process.
5050
static std::unique_ptr<OutputFileMap>
51-
loadFromBuffer(llvm::MemoryBuffer *Buffer);
51+
loadFromBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer);
5252

5353
/// Get the map of outputs for the given \p Input, if present in the
5454
/// OutputFileMap. (If not present, returns nullptr.)
@@ -62,7 +62,7 @@ class OutputFileMap {
6262
/// of \p Buffer in the process.
6363
///
6464
/// \returns true on error, false on success
65-
bool parse(llvm::MemoryBuffer *Buffer);
65+
bool parse(std::unique_ptr<llvm::MemoryBuffer> Buffer);
6666
};
6767

6868
} // end namespace driver

Diff for: include/swift/Frontend/Frontend.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ class CompilerInvocation {
213213
FrontendOpts.InputFilenames.push_back(Filename);
214214
}
215215

216-
void addInputBuffer(llvm::MemoryBuffer *Buf) {
217-
FrontendOpts.InputBuffers.push_back(Buf);
216+
void addInputBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
217+
FrontendOpts.InputBuffers.push_back(Buffer.release());
218218
}
219219

220220
void clearInputs() {

Diff for: include/swift/Frontend/SerializedDiagnosticConsumer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#ifndef SWIFT_SERIALIZEDDIAGNOSTICCONSUMER_H
1919
#define SWIFT_SERIALIZEDDIAGNOSTICCONSUMER_H
2020

21+
#include <memory>
22+
2123
namespace llvm {
2224
class raw_ostream;
2325
}
@@ -34,7 +36,7 @@ namespace swift {
3436
/// ownership of the stream.
3537
///
3638
/// \returns A new diagnostic consumer that serializes diagnostics.
37-
DiagnosticConsumer *createConsumer(llvm::raw_ostream *OS);
39+
DiagnosticConsumer *createConsumer(std::unique_ptr<llvm::raw_ostream> OS);
3840
}
3941
}
4042

Diff for: include/swift/Serialization/ModuleFile.h

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "swift/Basic/LLVM.h"
2727
#include "llvm/ADT/ArrayRef.h"
2828
#include "llvm/ADT/DenseMap.h"
29-
#include "llvm/ADT/OwningPtr.h"
3029
#include "llvm/ADT/TinyPtrVector.h"
3130
#include "llvm/Bitcode/BitstreamReader.h"
3231

Diff for: lib/Driver/OutputFileMap.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Driver/OutputFileMap.h"
14-
15-
#include "llvm/ADT/OwningPtr.h"
1614
#include "llvm/ADT/StringRef.h"
1715
#include "llvm/Support/raw_ostream.h"
1816
#include "llvm/Support/system_error.h"
@@ -21,25 +19,25 @@ using namespace swift;
2119
using namespace swift::driver;
2220

2321
std::unique_ptr<OutputFileMap> OutputFileMap::loadFromPath(StringRef Path) {
24-
llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
22+
std::unique_ptr<llvm::MemoryBuffer> Buffer;
2523
llvm::error_code Result = llvm::MemoryBuffer::getFile(Path, Buffer);
2624
if (Result != 0)
2725
return nullptr;
28-
return loadFromBuffer(Buffer.take());
26+
return loadFromBuffer(std::move(Buffer));
2927
}
3028

3129
std::unique_ptr<OutputFileMap> OutputFileMap::loadFromBuffer(StringRef Data) {
32-
llvm::OwningPtr<llvm::MemoryBuffer> Buffer{
30+
std::unique_ptr<llvm::MemoryBuffer> Buffer{
3331
llvm::MemoryBuffer::getMemBuffer(Data)
3432
};
35-
return loadFromBuffer(Buffer.take());
33+
return loadFromBuffer(std::move(Buffer));
3634
}
3735

3836
std::unique_ptr<OutputFileMap>
39-
OutputFileMap::loadFromBuffer(llvm::MemoryBuffer *Buffer) {
37+
OutputFileMap::loadFromBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
4038
std::unique_ptr<OutputFileMap> OFM(new OutputFileMap());
4139

42-
if (OFM->parse(Buffer))
40+
if (OFM->parse(std::move(Buffer)))
4341
return nullptr;
4442

4543
return OFM;
@@ -91,9 +89,9 @@ void OutputFileMap::dump(llvm::raw_ostream &os, bool Sort) const {
9189
}
9290
}
9391

94-
bool OutputFileMap::parse(llvm::MemoryBuffer *Buffer) {
92+
bool OutputFileMap::parse(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
9593
llvm::SourceMgr SM;
96-
llvm::yaml::Stream YAMLStream(Buffer, SM);
94+
llvm::yaml::Stream YAMLStream(Buffer.release(), SM);
9795
auto I = YAMLStream.begin();
9896
if (I == YAMLStream.end())
9997
return true;

Diff for: lib/Frontend/Frontend.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ bool swift::CompilerInstance::setup(const CompilerInvocation &Invok) {
184184
}
185185

186186
// Open the input file.
187-
llvm::OwningPtr<llvm::MemoryBuffer> InputFile;
187+
std::unique_ptr<llvm::MemoryBuffer> InputFile;
188188
if (llvm::error_code Err =
189189
llvm::MemoryBuffer::getFileOrSTDIN(File, InputFile)) {
190190
Diagnostics.diagnose(SourceLoc(), diag::error_open_input_file,
191191
File, Err.message());
192192
return true;
193193
}
194194

195-
unsigned BufferID = SourceMgr.addNewSourceBuffer(InputFile.take());
195+
unsigned BufferID = SourceMgr.addNewSourceBuffer(std::move(InputFile));
196196

197197
// Transfer ownership of the MemoryBuffer to the SourceMgr.
198198
BufferIDs.push_back(BufferID);

Diff for: lib/Frontend/SerializedDiagnosticConsumer.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "swift/Parse/Lexer.h"
2222
#include "llvm/ADT/StringRef.h"
2323
#include "llvm/ADT/Twine.h"
24-
#include "llvm/ADT/OwningPtr.h"
2524
#include "llvm/ADT/DenseMap.h"
2625
#include "llvm/Support/raw_ostream.h"
2726
#include "llvm/ADT/IntrusiveRefCntPtr.h"
@@ -85,8 +84,8 @@ typedef SmallVector<uint64_t, 64> RecordData;
8584
typedef SmallVectorImpl<uint64_t> RecordDataImpl;
8685

8786
struct SharedState : llvm::RefCountedBase<SharedState> {
88-
SharedState(raw_ostream *os)
89-
: Stream(Buffer), OS(os), EmittedAnyDiagBlocks(false) { }
87+
SharedState(std::unique_ptr<raw_ostream> OS)
88+
: Stream(Buffer), OS(std::move(OS)), EmittedAnyDiagBlocks(false) { }
9089

9190
/// \brief The byte buffer for the serialized content.
9291
llvm::SmallString<1024> Buffer;
@@ -95,7 +94,7 @@ struct SharedState : llvm::RefCountedBase<SharedState> {
9594
llvm::BitstreamWriter Stream;
9695

9796
/// \brief The name of the diagnostics file.
98-
llvm::OwningPtr<raw_ostream> OS;
97+
std::unique_ptr<raw_ostream> OS;
9998

10099
/// \brief The set of constructed record abbreviations.
101100
AbbreviationMap Abbrevs;
@@ -126,7 +125,8 @@ class SerializedDiagnosticConsumer : public DiagnosticConsumer {
126125
/// \brief State shared among the various clones of this diagnostic consumer.
127126
llvm::IntrusiveRefCntPtr<SharedState> State;
128127
public:
129-
SerializedDiagnosticConsumer(raw_ostream *OS) : State(new SharedState(OS)) {
128+
SerializedDiagnosticConsumer(std::unique_ptr<raw_ostream> OS)
129+
: State(new SharedState(std::move(OS))) {
130130
emitPreamble();
131131
}
132132

@@ -195,8 +195,8 @@ class SerializedDiagnosticConsumer : public DiagnosticConsumer {
195195
}
196196

197197
namespace swift { namespace serialized_diagnostics {
198-
DiagnosticConsumer *createConsumer(llvm::raw_ostream *OS) {
199-
return new SerializedDiagnosticConsumer(OS);
198+
DiagnosticConsumer *createConsumer(std::unique_ptr<llvm::raw_ostream> OS) {
199+
return new SerializedDiagnosticConsumer(std::move(OS));
200200
}
201201
}}
202202

Diff for: lib/IRGen/IRGen.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static std::unique_ptr<llvm::Module> performIRGeneration(IRGenOptions &Opts,
199199
// Bail out if there are any errors.
200200
if (M->Ctx.hadError()) return nullptr;
201201

202-
llvm::OwningPtr<raw_fd_ostream> RawOS;
202+
std::unique_ptr<raw_fd_ostream> RawOS;
203203
formatted_raw_ostream FormattedOS;
204204
if (!Opts.OutputFilename.empty()) {
205205
// Try to open the output file. Clobbering an existing file is fine.

Diff for: lib/Immediate/Immediate.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,29 @@
3737
#include "swift/SIL/SILModule.h"
3838
#include "swift/SILPasses/Passes.h"
3939
#include "llvm/ADT/SmallPtrSet.h"
40-
#include "llvm/ADT/StringSwitch.h"
4140
#include "llvm/ADT/SmallString.h"
41+
#include "llvm/ADT/StringSwitch.h"
4242
#include "llvm/ExecutionEngine/JIT.h"
43+
#include "llvm/IR/DataLayout.h"
44+
#include "llvm/IR/LLVMContext.h"
45+
#include "llvm/IR/Module.h"
46+
#include "llvm/Linker/Linker.h"
47+
#include "llvm/PassManager.h"
4348
#include "llvm/Support/ConvertUTF.h"
4449
#include "llvm/Support/Debug.h"
4550
#include "llvm/Support/FileSystem.h"
4651
#include "llvm/Support/Host.h"
4752
#include "llvm/Support/MemoryBuffer.h"
48-
#include "llvm/Support/raw_ostream.h"
4953
#include "llvm/Support/Path.h"
5054
#include "llvm/Support/PrettyStackTrace.h"
5155
#include "llvm/Support/Process.h"
5256
#include "llvm/Support/SaveAndRestore.h"
5357
#include "llvm/Support/Signals.h"
54-
#include "llvm/Support/system_error.h"
5558
#include "llvm/Support/TargetSelect.h"
56-
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
59+
#include "llvm/Support/raw_ostream.h"
60+
#include "llvm/Support/system_error.h"
5761
#include "llvm/Transforms/IPO.h"
58-
#include "llvm/IR/DataLayout.h"
59-
#include "llvm/IR/LLVMContext.h"
60-
#include "llvm/IR/Module.h"
61-
#include "llvm/Linker.h"
62-
#include "llvm/PassManager.h"
62+
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
6363

6464
// FIXME: We need a more library-neutral way for frameworks to take ownership of
6565
// the main loop.

Diff for: lib/Sema/SourceLoader.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "swift/Parse/DelayedParsingCallbacks.h"
2323
#include "swift/Parse/PersistentParserState.h"
2424
#include "swift/Basic/SourceManager.h"
25-
#include "llvm/ADT/OwningPtr.h"
2625
#include "llvm/ADT/SmallString.h"
2726
#include "llvm/Support/MemoryBuffer.h"
2827
#include "llvm/Support/Path.h"
@@ -34,7 +33,7 @@ using namespace swift;
3433
// FIXME: Basically the same as SerializedModuleLoader.
3534
static llvm::error_code findModule(ASTContext &ctx, StringRef moduleID,
3635
SourceLoc importLoc,
37-
llvm::OwningPtr<llvm::MemoryBuffer> &buffer){
36+
std::unique_ptr<llvm::MemoryBuffer> &buffer){
3837
llvm::SmallString<128> inputFilename;
3938

4039
for (auto Path : ctx.SearchPathOpts.ImportSearchPaths) {
@@ -71,7 +70,7 @@ Module *SourceLoader::loadModule(SourceLoc importLoc,
7170

7271
auto moduleID = path[0];
7372

74-
llvm::OwningPtr<llvm::MemoryBuffer> inputFile;
73+
std::unique_ptr<llvm::MemoryBuffer> inputFile;
7574
if (llvm::error_code err = findModule(Ctx, moduleID.first.str(),
7675
moduleID.second, inputFile)) {
7776
if (err.value() != llvm::errc::no_such_file_or_directory) {
@@ -91,7 +90,7 @@ Module *SourceLoader::loadModule(SourceLoc importLoc,
9190
Ctx.SourceMgr.getIDForBufferIdentifier(inputFile->getBufferIdentifier()))
9291
bufferID = BufID.getValue();
9392
else
94-
bufferID = Ctx.SourceMgr.addNewSourceBuffer(inputFile.take());
93+
bufferID = Ctx.SourceMgr.addNewSourceBuffer(std::move(inputFile));
9594

9695
auto *importMod = Module::create(moduleID.first, Ctx);
9796
Ctx.LoadedModules[moduleID.first.str()] = importMod;

Diff for: lib/Serialization/SerializedModuleLoader.cpp

+4-35
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,7 @@ using namespace swift;
2727

2828
namespace {
2929
typedef std::pair<Identifier, SourceLoc> AccessPathElem;
30-
31-
/// An adapter class that allows a std::unique_ptr to be used as an
32-
/// llvm::OwningPtr.
33-
template <typename T>
34-
class OwningPtrAdapter {
35-
llvm::OwningPtr<T> Owner;
36-
std::unique_ptr<T> &Result;
37-
public:
38-
OwningPtrAdapter(std::unique_ptr<T> &result) : Result(result) {
39-
Owner.reset(Result.release());
40-
}
41-
42-
OwningPtrAdapter(const OwningPtrAdapter &other) = delete;
43-
OwningPtrAdapter &operator=(const OwningPtrAdapter &other) = delete;
44-
OwningPtrAdapter(OwningPtrAdapter &&other) = default;
45-
OwningPtrAdapter &operator=(OwningPtrAdapter &&other) = default;
46-
47-
~OwningPtrAdapter() {
48-
Result.reset(Owner.take());
49-
}
50-
operator llvm::OwningPtr<T> &() { return Owner; }
51-
};
52-
53-
template <typename T>
54-
OwningPtrAdapter<T> makeOwningPtrAdapter(std::unique_ptr<T> &result) {
55-
return result;
56-
}
57-
58-
}
30+
} // end unnamed namespace
5931

6032
// Defined out-of-line so that we can see ~ModuleFile.
6133
SerializedModuleLoader::SerializedModuleLoader(ASTContext &ctx) : Ctx(ctx) {}
@@ -74,8 +46,7 @@ static llvm::error_code findModule(ASTContext &ctx, AccessPathElem moduleID,
7446
for (auto path : ctx.SearchPathOpts.ImportSearchPaths) {
7547
inputFilename = path;
7648
llvm::sys::path::append(inputFilename, moduleFilename.str());
77-
auto err = llvm::MemoryBuffer::getFile(inputFilename.str(),
78-
makeOwningPtrAdapter(buffer));
49+
auto err = llvm::MemoryBuffer::getFile(inputFilename.str(), buffer);
7950
if (!err || err.value() != llvm::errc::no_such_file_or_directory)
8051
return err;
8152
}
@@ -96,8 +67,7 @@ static llvm::error_code findModule(ASTContext &ctx, AccessPathElem moduleID,
9667
inputFilename = path;
9768
llvm::sys::path::append(inputFilename, moduleFramework.str(),
9869
moduleFilename.str(), archFilename.str());
99-
auto err = llvm::MemoryBuffer::getFile(inputFilename.str(),
100-
makeOwningPtrAdapter(buffer));
70+
auto err = llvm::MemoryBuffer::getFile(inputFilename.str(), buffer);
10171
if (!err || err.value() != llvm::errc::no_such_file_or_directory)
10272
return err;
10373
}
@@ -106,8 +76,7 @@ static llvm::error_code findModule(ASTContext &ctx, AccessPathElem moduleID,
10676
isFramework = false;
10777
inputFilename = ctx.SearchPathOpts.RuntimeLibraryImportPath;
10878
llvm::sys::path::append(inputFilename, moduleFilename.str());
109-
return llvm::MemoryBuffer::getFile(inputFilename.str(),
110-
makeOwningPtrAdapter(buffer));
79+
return llvm::MemoryBuffer::getFile(inputFilename.str(), buffer);
11180
}
11281

11382
FileUnit *

Diff for: tools/driver/frontend_main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ int frontend_main(ArrayRef<const char *>Args,
298298
}
299299

300300
SerializedConsumer.reset(
301-
serialized_diagnostics::createConsumer(OS.release()));
301+
serialized_diagnostics::createConsumer(std::move(OS)));
302302
Instance.addDiagnosticConsumer(SerializedConsumer.get());
303303
}
304304
}

Diff for: tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "swift/Basic/Dwarf.h"
2020
#include "swift/Frontend/Frontend.h"
2121
#include "swift/ASTSectionImporter/ASTSectionImporter.h"
22-
#include "llvm/ADT/OwningPtr.h"
2322
#include "llvm/Support/CommandLine.h"
2423
#include "llvm/Support/PrettyStackTrace.h"
2524
#include "llvm/Support/Signals.h"

Diff for: tools/sil-extract/SILExtract.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,16 @@ int main(int argc, char **argv) {
8181
Invocation.setImportSearchPaths(ImportPaths);
8282

8383
// Load the input file.
84-
llvm::OwningPtr<llvm::MemoryBuffer> InputFile;
84+
std::unique_ptr<llvm::MemoryBuffer> InputFile;
8585
if (llvm::MemoryBuffer::getFileOrSTDIN(InputFilename, InputFile)) {
8686
fprintf(stderr, "Error! Failed to open file: %s\n", InputFilename.c_str());
8787
exit(-1);
8888
}
8989

9090
// If it looks like we have an AST, set the source file kind to SIL and the
9191
// name of the module to the file's name.
92-
Invocation.addInputBuffer(InputFile.get());
9392
bool IsModule = false;
94-
if (SerializedModuleLoader::isSerializedAST(InputFile.get()->getBuffer())) {
93+
if (SerializedModuleLoader::isSerializedAST(InputFile->getBuffer())) {
9594
IsModule = true;
9695
const StringRef Stem = ModuleName.size() ?
9796
StringRef(ModuleName) :
@@ -102,6 +101,7 @@ int main(int argc, char **argv) {
102101
Invocation.setModuleName("main");
103102
Invocation.setInputKind(SourceFileKind::SIL);
104103
}
104+
Invocation.addInputBuffer(std::move(InputFile));
105105

106106
CompilerInstance CI;
107107
PrintingDiagnosticConsumer PrintDiags;

0 commit comments

Comments
 (0)