diff --git a/include/swift/Basic/SourceManager.h b/include/swift/Basic/SourceManager.h index 473d1ee8b7d..c9ff66fdbe2 100644 --- a/include/swift/Basic/SourceManager.h +++ b/include/swift/Basic/SourceManager.h @@ -98,8 +98,13 @@ public: return unsigned(BufferID); } + // FIXME: remove this overload. size_t addNewSourceBuffer(llvm::MemoryBuffer *Buffer); + size_t addNewSourceBuffer(std::unique_ptr Buffer) { + return addNewSourceBuffer(Buffer.release()); + } + /// Returns a buffer ID for a previously added buffer with the given /// buffer identifier, or Nothing if there is no such buffer. Optional getIDForBufferIdentifier(StringRef BufIdentifier); diff --git a/include/swift/Driver/OutputFileMap.h b/include/swift/Driver/OutputFileMap.h index e0374adc9ca..d2f54efe667 100644 --- a/include/swift/Driver/OutputFileMap.h +++ b/include/swift/Driver/OutputFileMap.h @@ -48,7 +48,7 @@ public: /// Loads an OutputFileMap from the given \p Buffer, taking ownership /// of the buffer in the process. static std::unique_ptr - loadFromBuffer(llvm::MemoryBuffer *Buffer); + loadFromBuffer(std::unique_ptr Buffer); /// Get the map of outputs for the given \p Input, if present in the /// OutputFileMap. (If not present, returns nullptr.) @@ -62,7 +62,7 @@ private: /// of \p Buffer in the process. /// /// \returns true on error, false on success - bool parse(llvm::MemoryBuffer *Buffer); + bool parse(std::unique_ptr Buffer); }; } // end namespace driver diff --git a/include/swift/Frontend/Frontend.h b/include/swift/Frontend/Frontend.h index 8aaf8c60905..f004bdb6ced 100644 --- a/include/swift/Frontend/Frontend.h +++ b/include/swift/Frontend/Frontend.h @@ -213,8 +213,8 @@ public: FrontendOpts.InputFilenames.push_back(Filename); } - void addInputBuffer(llvm::MemoryBuffer *Buf) { - FrontendOpts.InputBuffers.push_back(Buf); + void addInputBuffer(std::unique_ptr Buffer) { + FrontendOpts.InputBuffers.push_back(Buffer.release()); } void clearInputs() { diff --git a/include/swift/Frontend/SerializedDiagnosticConsumer.h b/include/swift/Frontend/SerializedDiagnosticConsumer.h index 1809eccd929..ad866121f0e 100644 --- a/include/swift/Frontend/SerializedDiagnosticConsumer.h +++ b/include/swift/Frontend/SerializedDiagnosticConsumer.h @@ -18,6 +18,8 @@ #ifndef SWIFT_SERIALIZEDDIAGNOSTICCONSUMER_H #define SWIFT_SERIALIZEDDIAGNOSTICCONSUMER_H +#include + namespace llvm { class raw_ostream; } @@ -34,7 +36,7 @@ namespace swift { /// ownership of the stream. /// /// \returns A new diagnostic consumer that serializes diagnostics. - DiagnosticConsumer *createConsumer(llvm::raw_ostream *OS); + DiagnosticConsumer *createConsumer(std::unique_ptr OS); } } diff --git a/include/swift/Serialization/ModuleFile.h b/include/swift/Serialization/ModuleFile.h index 36bd334e96a..161514a3a32 100644 --- a/include/swift/Serialization/ModuleFile.h +++ b/include/swift/Serialization/ModuleFile.h @@ -26,7 +26,6 @@ #include "swift/Basic/LLVM.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" -#include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/TinyPtrVector.h" #include "llvm/Bitcode/BitstreamReader.h" diff --git a/lib/Driver/OutputFileMap.cpp b/lib/Driver/OutputFileMap.cpp index e736a7ff0e6..b82b864f15b 100644 --- a/lib/Driver/OutputFileMap.cpp +++ b/lib/Driver/OutputFileMap.cpp @@ -11,8 +11,6 @@ //===----------------------------------------------------------------------===// #include "swift/Driver/OutputFileMap.h" - -#include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/system_error.h" @@ -21,25 +19,25 @@ using namespace swift; using namespace swift::driver; std::unique_ptr OutputFileMap::loadFromPath(StringRef Path) { - llvm::OwningPtr Buffer; + std::unique_ptr Buffer; llvm::error_code Result = llvm::MemoryBuffer::getFile(Path, Buffer); if (Result != 0) return nullptr; - return loadFromBuffer(Buffer.take()); + return loadFromBuffer(std::move(Buffer)); } std::unique_ptr OutputFileMap::loadFromBuffer(StringRef Data) { - llvm::OwningPtr Buffer{ + std::unique_ptr Buffer{ llvm::MemoryBuffer::getMemBuffer(Data) }; - return loadFromBuffer(Buffer.take()); + return loadFromBuffer(std::move(Buffer)); } std::unique_ptr -OutputFileMap::loadFromBuffer(llvm::MemoryBuffer *Buffer) { +OutputFileMap::loadFromBuffer(std::unique_ptr Buffer) { std::unique_ptr OFM(new OutputFileMap()); - if (OFM->parse(Buffer)) + if (OFM->parse(std::move(Buffer))) return nullptr; return OFM; @@ -91,9 +89,9 @@ void OutputFileMap::dump(llvm::raw_ostream &os, bool Sort) const { } } -bool OutputFileMap::parse(llvm::MemoryBuffer *Buffer) { +bool OutputFileMap::parse(std::unique_ptr Buffer) { llvm::SourceMgr SM; - llvm::yaml::Stream YAMLStream(Buffer, SM); + llvm::yaml::Stream YAMLStream(Buffer.release(), SM); auto I = YAMLStream.begin(); if (I == YAMLStream.end()) return true; diff --git a/lib/Frontend/Frontend.cpp b/lib/Frontend/Frontend.cpp index 1a78b6381c8..1bda3f2b0b2 100644 --- a/lib/Frontend/Frontend.cpp +++ b/lib/Frontend/Frontend.cpp @@ -184,7 +184,7 @@ bool swift::CompilerInstance::setup(const CompilerInvocation &Invok) { } // Open the input file. - llvm::OwningPtr InputFile; + std::unique_ptr InputFile; if (llvm::error_code Err = llvm::MemoryBuffer::getFileOrSTDIN(File, InputFile)) { Diagnostics.diagnose(SourceLoc(), diag::error_open_input_file, @@ -192,7 +192,7 @@ bool swift::CompilerInstance::setup(const CompilerInvocation &Invok) { return true; } - unsigned BufferID = SourceMgr.addNewSourceBuffer(InputFile.take()); + unsigned BufferID = SourceMgr.addNewSourceBuffer(std::move(InputFile)); // Transfer ownership of the MemoryBuffer to the SourceMgr. BufferIDs.push_back(BufferID); diff --git a/lib/Frontend/SerializedDiagnosticConsumer.cpp b/lib/Frontend/SerializedDiagnosticConsumer.cpp index acc848e5200..0c01d53a224 100644 --- a/lib/Frontend/SerializedDiagnosticConsumer.cpp +++ b/lib/Frontend/SerializedDiagnosticConsumer.cpp @@ -21,7 +21,6 @@ #include "swift/Parse/Lexer.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" -#include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/DenseMap.h" #include "llvm/Support/raw_ostream.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" @@ -85,8 +84,8 @@ typedef SmallVector RecordData; typedef SmallVectorImpl RecordDataImpl; struct SharedState : llvm::RefCountedBase { - SharedState(raw_ostream *os) - : Stream(Buffer), OS(os), EmittedAnyDiagBlocks(false) { } + SharedState(std::unique_ptr OS) + : Stream(Buffer), OS(std::move(OS)), EmittedAnyDiagBlocks(false) { } /// \brief The byte buffer for the serialized content. llvm::SmallString<1024> Buffer; @@ -95,7 +94,7 @@ struct SharedState : llvm::RefCountedBase { llvm::BitstreamWriter Stream; /// \brief The name of the diagnostics file. - llvm::OwningPtr OS; + std::unique_ptr OS; /// \brief The set of constructed record abbreviations. AbbreviationMap Abbrevs; @@ -126,7 +125,8 @@ class SerializedDiagnosticConsumer : public DiagnosticConsumer { /// \brief State shared among the various clones of this diagnostic consumer. llvm::IntrusiveRefCntPtr State; public: - SerializedDiagnosticConsumer(raw_ostream *OS) : State(new SharedState(OS)) { + SerializedDiagnosticConsumer(std::unique_ptr OS) + : State(new SharedState(std::move(OS))) { emitPreamble(); } @@ -195,8 +195,8 @@ private: } namespace swift { namespace serialized_diagnostics { - DiagnosticConsumer *createConsumer(llvm::raw_ostream *OS) { - return new SerializedDiagnosticConsumer(OS); + DiagnosticConsumer *createConsumer(std::unique_ptr OS) { + return new SerializedDiagnosticConsumer(std::move(OS)); } }} diff --git a/lib/IRGen/IRGen.cpp b/lib/IRGen/IRGen.cpp index 61cf7377388..076123004e5 100644 --- a/lib/IRGen/IRGen.cpp +++ b/lib/IRGen/IRGen.cpp @@ -199,7 +199,7 @@ static std::unique_ptr performIRGeneration(IRGenOptions &Opts, // Bail out if there are any errors. if (M->Ctx.hadError()) return nullptr; - llvm::OwningPtr RawOS; + std::unique_ptr RawOS; formatted_raw_ostream FormattedOS; if (!Opts.OutputFilename.empty()) { // Try to open the output file. Clobbering an existing file is fine. diff --git a/lib/Immediate/Immediate.cpp b/lib/Immediate/Immediate.cpp index 73ace1ab1d2..0704e8fd6fe 100644 --- a/lib/Immediate/Immediate.cpp +++ b/lib/Immediate/Immediate.cpp @@ -37,29 +37,29 @@ #include "swift/SIL/SILModule.h" #include "swift/SILPasses/Passes.h" #include "llvm/ADT/SmallPtrSet.h" -#include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringSwitch.h" #include "llvm/ExecutionEngine/JIT.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/Linker/Linker.h" +#include "llvm/PassManager.h" #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/Debug.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Host.h" #include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Support/Path.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Process.h" #include "llvm/Support/SaveAndRestore.h" #include "llvm/Support/Signals.h" -#include "llvm/Support/system_error.h" #include "llvm/Support/TargetSelect.h" -#include "llvm/Transforms/IPO/PassManagerBuilder.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/system_error.h" #include "llvm/Transforms/IPO.h" -#include "llvm/IR/DataLayout.h" -#include "llvm/IR/LLVMContext.h" -#include "llvm/IR/Module.h" -#include "llvm/Linker.h" -#include "llvm/PassManager.h" +#include "llvm/Transforms/IPO/PassManagerBuilder.h" // FIXME: We need a more library-neutral way for frameworks to take ownership of // the main loop. diff --git a/lib/Sema/SourceLoader.cpp b/lib/Sema/SourceLoader.cpp index 848214a9043..14068c03da2 100644 --- a/lib/Sema/SourceLoader.cpp +++ b/lib/Sema/SourceLoader.cpp @@ -22,7 +22,6 @@ #include "swift/Parse/DelayedParsingCallbacks.h" #include "swift/Parse/PersistentParserState.h" #include "swift/Basic/SourceManager.h" -#include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" @@ -34,7 +33,7 @@ using namespace swift; // FIXME: Basically the same as SerializedModuleLoader. static llvm::error_code findModule(ASTContext &ctx, StringRef moduleID, SourceLoc importLoc, - llvm::OwningPtr &buffer){ + std::unique_ptr &buffer){ llvm::SmallString<128> inputFilename; for (auto Path : ctx.SearchPathOpts.ImportSearchPaths) { @@ -71,7 +70,7 @@ Module *SourceLoader::loadModule(SourceLoc importLoc, auto moduleID = path[0]; - llvm::OwningPtr inputFile; + std::unique_ptr inputFile; if (llvm::error_code err = findModule(Ctx, moduleID.first.str(), moduleID.second, inputFile)) { if (err.value() != llvm::errc::no_such_file_or_directory) { @@ -91,7 +90,7 @@ Module *SourceLoader::loadModule(SourceLoc importLoc, Ctx.SourceMgr.getIDForBufferIdentifier(inputFile->getBufferIdentifier())) bufferID = BufID.getValue(); else - bufferID = Ctx.SourceMgr.addNewSourceBuffer(inputFile.take()); + bufferID = Ctx.SourceMgr.addNewSourceBuffer(std::move(inputFile)); auto *importMod = Module::create(moduleID.first, Ctx); Ctx.LoadedModules[moduleID.first.str()] = importMod; diff --git a/lib/Serialization/SerializedModuleLoader.cpp b/lib/Serialization/SerializedModuleLoader.cpp index e8eeeffb550..a57d94fd51d 100644 --- a/lib/Serialization/SerializedModuleLoader.cpp +++ b/lib/Serialization/SerializedModuleLoader.cpp @@ -27,35 +27,7 @@ using namespace swift; namespace { typedef std::pair AccessPathElem; - -/// An adapter class that allows a std::unique_ptr to be used as an -/// llvm::OwningPtr. -template -class OwningPtrAdapter { - llvm::OwningPtr Owner; - std::unique_ptr &Result; -public: - OwningPtrAdapter(std::unique_ptr &result) : Result(result) { - Owner.reset(Result.release()); - } - - OwningPtrAdapter(const OwningPtrAdapter &other) = delete; - OwningPtrAdapter &operator=(const OwningPtrAdapter &other) = delete; - OwningPtrAdapter(OwningPtrAdapter &&other) = default; - OwningPtrAdapter &operator=(OwningPtrAdapter &&other) = default; - - ~OwningPtrAdapter() { - Result.reset(Owner.take()); - } - operator llvm::OwningPtr &() { return Owner; } -}; - -template -OwningPtrAdapter makeOwningPtrAdapter(std::unique_ptr &result) { - return result; -} - -} +} // end unnamed namespace // Defined out-of-line so that we can see ~ModuleFile. SerializedModuleLoader::SerializedModuleLoader(ASTContext &ctx) : Ctx(ctx) {} @@ -74,8 +46,7 @@ static llvm::error_code findModule(ASTContext &ctx, AccessPathElem moduleID, for (auto path : ctx.SearchPathOpts.ImportSearchPaths) { inputFilename = path; llvm::sys::path::append(inputFilename, moduleFilename.str()); - auto err = llvm::MemoryBuffer::getFile(inputFilename.str(), - makeOwningPtrAdapter(buffer)); + auto err = llvm::MemoryBuffer::getFile(inputFilename.str(), buffer); if (!err || err.value() != llvm::errc::no_such_file_or_directory) return err; } @@ -96,8 +67,7 @@ static llvm::error_code findModule(ASTContext &ctx, AccessPathElem moduleID, inputFilename = path; llvm::sys::path::append(inputFilename, moduleFramework.str(), moduleFilename.str(), archFilename.str()); - auto err = llvm::MemoryBuffer::getFile(inputFilename.str(), - makeOwningPtrAdapter(buffer)); + auto err = llvm::MemoryBuffer::getFile(inputFilename.str(), buffer); if (!err || err.value() != llvm::errc::no_such_file_or_directory) return err; } @@ -106,8 +76,7 @@ static llvm::error_code findModule(ASTContext &ctx, AccessPathElem moduleID, isFramework = false; inputFilename = ctx.SearchPathOpts.RuntimeLibraryImportPath; llvm::sys::path::append(inputFilename, moduleFilename.str()); - return llvm::MemoryBuffer::getFile(inputFilename.str(), - makeOwningPtrAdapter(buffer)); + return llvm::MemoryBuffer::getFile(inputFilename.str(), buffer); } FileUnit * diff --git a/tools/driver/frontend_main.cpp b/tools/driver/frontend_main.cpp index 57f58be16b6..81416e56d19 100644 --- a/tools/driver/frontend_main.cpp +++ b/tools/driver/frontend_main.cpp @@ -298,7 +298,7 @@ int frontend_main(ArrayRefArgs, } SerializedConsumer.reset( - serialized_diagnostics::createConsumer(OS.release())); + serialized_diagnostics::createConsumer(std::move(OS))); Instance.addDiagnosticConsumer(SerializedConsumer.get()); } } diff --git a/tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp b/tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp index 1ca68b001d2..c116611a36e 100644 --- a/tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp +++ b/tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp @@ -19,7 +19,6 @@ #include "swift/Basic/Dwarf.h" #include "swift/Frontend/Frontend.h" #include "swift/ASTSectionImporter/ASTSectionImporter.h" -#include "llvm/ADT/OwningPtr.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Signals.h" diff --git a/tools/sil-extract/SILExtract.cpp b/tools/sil-extract/SILExtract.cpp index 2e376ca1dd7..3e76ebb96b7 100644 --- a/tools/sil-extract/SILExtract.cpp +++ b/tools/sil-extract/SILExtract.cpp @@ -81,7 +81,7 @@ int main(int argc, char **argv) { Invocation.setImportSearchPaths(ImportPaths); // Load the input file. - llvm::OwningPtr InputFile; + std::unique_ptr InputFile; if (llvm::MemoryBuffer::getFileOrSTDIN(InputFilename, InputFile)) { fprintf(stderr, "Error! Failed to open file: %s\n", InputFilename.c_str()); exit(-1); @@ -89,9 +89,8 @@ int main(int argc, char **argv) { // If it looks like we have an AST, set the source file kind to SIL and the // name of the module to the file's name. - Invocation.addInputBuffer(InputFile.get()); bool IsModule = false; - if (SerializedModuleLoader::isSerializedAST(InputFile.get()->getBuffer())) { + if (SerializedModuleLoader::isSerializedAST(InputFile->getBuffer())) { IsModule = true; const StringRef Stem = ModuleName.size() ? StringRef(ModuleName) : @@ -102,6 +101,7 @@ int main(int argc, char **argv) { Invocation.setModuleName("main"); Invocation.setInputKind(SourceFileKind::SIL); } + Invocation.addInputBuffer(std::move(InputFile)); CompilerInstance CI; PrintingDiagnosticConsumer PrintDiags; diff --git a/tools/sil-opt/SILOpt.cpp b/tools/sil-opt/SILOpt.cpp index 04d6f2a5829..81cfd942b99 100644 --- a/tools/sil-opt/SILOpt.cpp +++ b/tools/sil-opt/SILOpt.cpp @@ -237,7 +237,7 @@ int main(int argc, char **argv) { Invocation.setImportSearchPaths(ImportPaths); // Load the input file. - llvm::OwningPtr InputFile; + std::unique_ptr InputFile; if (llvm::MemoryBuffer::getFileOrSTDIN(InputFilename, InputFile)) { fprintf(stderr, "Error! Failed to open file: %s\n", InputFilename.c_str()); exit(-1); @@ -245,7 +245,6 @@ int main(int argc, char **argv) { // If it looks like we have an AST, set the source file kind to SIL and the // name of the module to the file's name. - Invocation.addInputBuffer(InputFile.get()); bool IsModule = false; if (SerializedModuleLoader::isSerializedAST(InputFile.get()->getBuffer())) { IsModule = true; @@ -258,6 +257,7 @@ int main(int argc, char **argv) { Invocation.setModuleName("main"); Invocation.setInputKind(SourceFileKind::SIL); } + Invocation.addInputBuffer(std::move(InputFile)); CompilerInstance CI; PrintingDiagnosticConsumer PrintDiags;