From 63f61da7dfd7a0511b9ccd214943ff846b459191 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Tue, 4 Apr 2023 18:24:11 -0700 Subject: [PATCH 01/12] Use std::filesystem::path instead of string for file paths --- src/support/file.cpp | 19 +++++++------------ src/support/file.h | 15 +++++++-------- src/tools/wasm-reduce.cpp | 2 ++ src/wasm-io.h | 30 +++++++++++++++--------------- src/wasm/wasm-io.cpp | 34 +++++++++++++++++----------------- 5 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/support/file.cpp b/src/support/file.cpp index cfd656391be..1ddf8a1b728 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -47,7 +48,7 @@ template<> std::string do_read_stdin::operator()() { } template -T wasm::read_file(const std::string& filename, Flags::BinaryOption binary) { +T wasm::read_file(const std::filesystem::path& filename, Flags::BinaryOption binary) { if (filename == "-") { return do_read_stdin{}(); } @@ -95,11 +96,11 @@ std::string wasm::read_possible_response_file(const std::string& input) { } // Explicit instantiations for the explicit specializations. -template std::string wasm::read_file<>(const std::string&, Flags::BinaryOption); -template std::vector wasm::read_file<>(const std::string&, +template std::string wasm::read_file<>(const std::filesystem::path&, Flags::BinaryOption); +template std::vector wasm::read_file<>(const std::filesystem::path&, Flags::BinaryOption); -wasm::Output::Output(const std::string& filename, Flags::BinaryOption binary) +wasm::Output::Output(const std::filesystem::path& filename, Flags::BinaryOption binary) : outfile(), out([this, filename, binary]() { // Ensure a single return at the very end, to avoid clang-tidy warnings // about the types of different returns here. @@ -121,13 +122,7 @@ wasm::Output::Output(const std::string& filename, Flags::BinaryOption binary) return buffer; }()) {} -void wasm::copy_file(std::string input, std::string output) { - std::ifstream src(input, std::ios::binary); - std::ofstream dst(output, std::ios::binary); - dst << src.rdbuf(); +void wasm::copy_file(std::filesystem::path input, std::filesystem::path output) { + std::filesystem::copy_file(input, output, std::filesystem::copy_options::overwrite_existing); } -size_t wasm::file_size(std::string filename) { - std::ifstream infile(filename, std::ifstream::ate | std::ifstream::binary); - return infile.tellg(); -} diff --git a/src/support/file.h b/src/support/file.h index ae91831c98f..279200c0b73 100644 --- a/src/support/file.h +++ b/src/support/file.h @@ -21,6 +21,7 @@ #ifndef wasm_support_file_h #define wasm_support_file_h +#include #include #include #include @@ -35,12 +36,12 @@ enum BinaryOption { Binary, Text }; std::vector read_stdin(); template -T read_file(const std::string& filename, Flags::BinaryOption binary); +T read_file(const std::filesystem::path& filename, Flags::BinaryOption binary); // Declare the valid explicit specializations. -extern template std::string read_file<>(const std::string&, +extern template std::string read_file<>(const std::filesystem::path&, Flags::BinaryOption); -extern template std::vector read_file<>(const std::string&, +extern template std::vector read_file<>(const std::filesystem::path&, Flags::BinaryOption); // Given a string which may be a response file (i.e., a filename starting @@ -51,7 +52,7 @@ std::string read_possible_response_file(const std::string&); class Output { public: // An empty filename or "-" will open stdout instead. - Output(const std::string& filename, Flags::BinaryOption binary); + Output(const std::filesystem::path& filename, Flags::BinaryOption binary); ~Output() = default; template std::ostream& operator<<(const T& v) { return out << v; } @@ -69,11 +70,9 @@ class Output { std::ostream out; }; -// Copies a file to another file -void copy_file(std::string input, std::string output); +// Copies a file to another file, overwriting if the fiel exists +void copy_file(std::filesystem::path input, std::filesystem::path output); -// Retusn the size of a file -size_t file_size(std::string filename); } // namespace wasm diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index f3f252477bd..0cc620fb802 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include "ir/branch-utils.h" @@ -71,6 +72,7 @@ std::string GetLastErrorStdStr() { return std::string(); } #endif +using std::filesystem::file_size; using namespace wasm; // A timeout on every execution of the command. diff --git a/src/wasm-io.h b/src/wasm-io.h index ae66c39320d..11a22043ce3 100644 --- a/src/wasm-io.h +++ b/src/wasm-io.h @@ -58,17 +58,17 @@ class ModuleReader : public ModuleIOBase { } // read text - void readText(std::string filename, Module& wasm); + void readText(std::filesystem::path filename, Module& wasm); // read binary - void readBinary(std::string filename, + void readBinary(std::filesystem::path filename, Module& wasm, - std::string sourceMapFilename = ""); + std::filesystem::path sourceMapFilename = ""); // read text or binary, checking the contents for what it is. If `filename` is // empty, read from stdin. void - read(std::string filename, Module& wasm, std::string sourceMapFilename = ""); + read(std::filesystem::path filename, Module& wasm, std::filesystem::path sourceMapFilename = ""); // check whether a file is a wasm binary - bool isBinaryFile(std::string filename); + bool isBinaryFile(std::filesystem::path filename); private: bool DWARF = false; @@ -77,11 +77,11 @@ class ModuleReader : public ModuleIOBase { bool skipFunctionBodies = false; - void readStdin(Module& wasm, std::string sourceMapFilename); + void readStdin(Module& wasm, std::filesystem::path sourceMapFilename); void readBinaryData(std::vector& input, Module& wasm, - std::string sourceMapFilename); + std::filesystem::path sourceMapFilename); }; class ModuleWriter : public ModuleIOBase { @@ -90,9 +90,9 @@ class ModuleWriter : public ModuleIOBase { // TODO: Remove `emitModuleName`. See the comment in wasm-binary.h bool emitModuleName = false; - std::string symbolMap; - std::string sourceMapFilename; - std::string sourceMapUrl; + std::filesystem::path symbolMap; + std::filesystem::path sourceMapFilename; + std::filesystem::path sourceMapUrl; public: // Writing defaults to not storing the names section. Storing it is a user- @@ -100,8 +100,8 @@ class ModuleWriter : public ModuleIOBase { ModuleWriter() { setDebugInfo(false); } void setBinary(bool binary_) { binary = binary_; } - void setSymbolMap(std::string symbolMap_) { symbolMap = symbolMap_; } - void setSourceMapFilename(std::string sourceMapFilename_) { + void setSymbolMap(std::filesystem::path symbolMap_) { symbolMap = symbolMap_; } + void setSourceMapFilename(std::filesystem::path sourceMapFilename_) { sourceMapFilename = sourceMapFilename_; } void setSourceMapUrl(std::string sourceMapUrl_) { @@ -111,15 +111,15 @@ class ModuleWriter : public ModuleIOBase { // write text void writeText(Module& wasm, Output& output); - void writeText(Module& wasm, std::string filename); + void writeText(Module& wasm, std::filesystem::path filename); // write binary void writeBinary(Module& wasm, Output& output); - void writeBinary(Module& wasm, std::string filename); + void writeBinary(Module& wasm, std::filesystem::path filename); // write text or binary, defaulting to binary unless setBinary(false), // and unless there is no output file (in which case we write text // to stdout). void write(Module& wasm, Output& output); - void write(Module& wasm, std::string filename); + void write(Module& wasm, std::filesystem::path filename); }; } // namespace wasm diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp index b544046f6ce..88ea992b60f 100644 --- a/src/wasm/wasm-io.cpp +++ b/src/wasm/wasm-io.cpp @@ -51,7 +51,7 @@ static void readTextData(std::string& input, Module& wasm, IRProfile profile) { } } -void ModuleReader::readText(std::string filename, Module& wasm) { +void ModuleReader::readText(std::filesystem::path filename, Module& wasm) { BYN_TRACE("reading text from " << filename << "\n"); auto input(read_file(filename, Flags::Text)); readTextData(input, wasm, profile); @@ -59,7 +59,7 @@ void ModuleReader::readText(std::string filename, Module& wasm) { void ModuleReader::readBinaryData(std::vector& input, Module& wasm, - std::string sourceMapFilename) { + std::filesystem::path sourceMapFilename) { std::unique_ptr sourceMapStream; // Assume that the wasm has had its initial features applied, and use those // while parsing. @@ -67,7 +67,7 @@ void ModuleReader::readBinaryData(std::vector& input, parser.setDebugInfo(debugInfo); parser.setDWARF(DWARF); parser.setSkipFunctionBodies(skipFunctionBodies); - if (sourceMapFilename.size()) { + if (!sourceMapFilename.empty()) { sourceMapStream = std::make_unique(); sourceMapStream->open(sourceMapFilename); parser.setDebugLocations(sourceMapStream.get()); @@ -78,15 +78,15 @@ void ModuleReader::readBinaryData(std::vector& input, } } -void ModuleReader::readBinary(std::string filename, +void ModuleReader::readBinary(std::filesystem::path filename, Module& wasm, - std::string sourceMapFilename) { + std::filesystem::path sourceMapFilename) { BYN_TRACE("reading binary from " << filename << "\n"); auto input(read_file>(filename, Flags::Binary)); readBinaryData(input, wasm, sourceMapFilename); } -bool ModuleReader::isBinaryFile(std::string filename) { +bool ModuleReader::isBinaryFile(std::filesystem::path filename) { std::ifstream infile; std::ios_base::openmode flags = std::ifstream::in | std::ifstream::binary; infile.open(filename, flags); @@ -97,11 +97,11 @@ bool ModuleReader::isBinaryFile(std::string filename) { buffer[3] == 'm'; } -void ModuleReader::read(std::string filename, +void ModuleReader::read(std::filesystem::path filename, Module& wasm, - std::string sourceMapFilename) { + std::filesystem::path sourceMapFilename) { // empty filename or "-" means read from stdin - if (!filename.size() || filename == "-") { + if (filename.empty() || filename == "-") { readStdin(wasm, sourceMapFilename); return; } @@ -109,7 +109,7 @@ void ModuleReader::read(std::string filename, readBinary(filename, wasm, sourceMapFilename); } else { // default to text - if (sourceMapFilename.size()) { + if (!sourceMapFilename.empty()) { std::cerr << "Binaryen ModuleReader::read() - source map filename " "provided, but file appears to not be binary\n"; } @@ -119,7 +119,7 @@ void ModuleReader::read(std::string filename, // TODO: reading into a vector then copying into a string is unnecessarily // inefficient. It would be better to read just once into a stringstream. -void ModuleReader::readStdin(Module& wasm, std::string sourceMapFilename) { +void ModuleReader::readStdin(Module& wasm, std::filesystem::path sourceMapFilename) { std::vector input = read_stdin(); if (input.size() >= 4 && input[0] == '\0' && input[1] == 'a' && input[2] == 's' && input[3] == 'm') { @@ -140,7 +140,7 @@ void ModuleWriter::writeText(Module& wasm, Output& output) { output.getStream() << wasm; } -void ModuleWriter::writeText(Module& wasm, std::string filename) { +void ModuleWriter::writeText(Module& wasm, std::filesystem::path filename) { BYN_TRACE("writing text to " << filename << "\n"); Output output(filename, Flags::Text); writeText(wasm, output); @@ -155,12 +155,12 @@ void ModuleWriter::writeBinary(Module& wasm, Output& output) { writer.setEmitModuleName(true); } std::unique_ptr sourceMapStream; - if (sourceMapFilename.size()) { + if (!sourceMapFilename.empty()) { sourceMapStream = std::make_unique(); sourceMapStream->open(sourceMapFilename); writer.setSourceMap(sourceMapStream.get(), sourceMapUrl); } - if (symbolMap.size() > 0) { + if (!symbolMap.empty()) { writer.setSymbolMap(symbolMap); } writer.write(); @@ -170,7 +170,7 @@ void ModuleWriter::writeBinary(Module& wasm, Output& output) { } } -void ModuleWriter::writeBinary(Module& wasm, std::string filename) { +void ModuleWriter::writeBinary(Module& wasm, std::filesystem::path filename) { BYN_TRACE("writing binary to " << filename << "\n"); Output output(filename, Flags::Binary); writeBinary(wasm, output); @@ -184,8 +184,8 @@ void ModuleWriter::write(Module& wasm, Output& output) { } } -void ModuleWriter::write(Module& wasm, std::string filename) { - if (binary && filename.size() > 0) { +void ModuleWriter::write(Module& wasm, std::filesystem::path filename) { + if (binary && !filename.empty()) { writeBinary(wasm, filename); } else { writeText(wasm, filename); From afc99149f82ea9e6015e96a9e1a607bd61749fd0 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Tue, 4 Apr 2023 18:26:27 -0700 Subject: [PATCH 02/12] clang-format --- src/support/file.cpp | 16 ++++++++++------ src/support/file.h | 1 - src/wasm-io.h | 9 ++++++--- src/wasm/wasm-io.cpp | 3 ++- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/support/file.cpp b/src/support/file.cpp index 1ddf8a1b728..81161ef874b 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -48,7 +48,8 @@ template<> std::string do_read_stdin::operator()() { } template -T wasm::read_file(const std::filesystem::path& filename, Flags::BinaryOption binary) { +T wasm::read_file(const std::filesystem::path& filename, + Flags::BinaryOption binary) { if (filename == "-") { return do_read_stdin{}(); } @@ -96,11 +97,13 @@ std::string wasm::read_possible_response_file(const std::string& input) { } // Explicit instantiations for the explicit specializations. -template std::string wasm::read_file<>(const std::filesystem::path&, Flags::BinaryOption); +template std::string wasm::read_file<>(const std::filesystem::path&, + Flags::BinaryOption); template std::vector wasm::read_file<>(const std::filesystem::path&, Flags::BinaryOption); -wasm::Output::Output(const std::filesystem::path& filename, Flags::BinaryOption binary) +wasm::Output::Output(const std::filesystem::path& filename, + Flags::BinaryOption binary) : outfile(), out([this, filename, binary]() { // Ensure a single return at the very end, to avoid clang-tidy warnings // about the types of different returns here. @@ -122,7 +125,8 @@ wasm::Output::Output(const std::filesystem::path& filename, Flags::BinaryOption return buffer; }()) {} -void wasm::copy_file(std::filesystem::path input, std::filesystem::path output) { - std::filesystem::copy_file(input, output, std::filesystem::copy_options::overwrite_existing); +void wasm::copy_file(std::filesystem::path input, + std::filesystem::path output) { + std::filesystem::copy_file( + input, output, std::filesystem::copy_options::overwrite_existing); } - diff --git a/src/support/file.h b/src/support/file.h index 279200c0b73..3d5081a640e 100644 --- a/src/support/file.h +++ b/src/support/file.h @@ -73,7 +73,6 @@ class Output { // Copies a file to another file, overwriting if the fiel exists void copy_file(std::filesystem::path input, std::filesystem::path output); - } // namespace wasm #endif // wasm_support_file_h diff --git a/src/wasm-io.h b/src/wasm-io.h index 11a22043ce3..fc4995a8119 100644 --- a/src/wasm-io.h +++ b/src/wasm-io.h @@ -65,8 +65,9 @@ class ModuleReader : public ModuleIOBase { std::filesystem::path sourceMapFilename = ""); // read text or binary, checking the contents for what it is. If `filename` is // empty, read from stdin. - void - read(std::filesystem::path filename, Module& wasm, std::filesystem::path sourceMapFilename = ""); + void read(std::filesystem::path filename, + Module& wasm, + std::filesystem::path sourceMapFilename = ""); // check whether a file is a wasm binary bool isBinaryFile(std::filesystem::path filename); @@ -100,7 +101,9 @@ class ModuleWriter : public ModuleIOBase { ModuleWriter() { setDebugInfo(false); } void setBinary(bool binary_) { binary = binary_; } - void setSymbolMap(std::filesystem::path symbolMap_) { symbolMap = symbolMap_; } + void setSymbolMap(std::filesystem::path symbolMap_) { + symbolMap = symbolMap_; + } void setSourceMapFilename(std::filesystem::path sourceMapFilename_) { sourceMapFilename = sourceMapFilename_; } diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp index 88ea992b60f..ab466125cee 100644 --- a/src/wasm/wasm-io.cpp +++ b/src/wasm/wasm-io.cpp @@ -119,7 +119,8 @@ void ModuleReader::read(std::filesystem::path filename, // TODO: reading into a vector then copying into a string is unnecessarily // inefficient. It would be better to read just once into a stringstream. -void ModuleReader::readStdin(Module& wasm, std::filesystem::path sourceMapFilename) { +void ModuleReader::readStdin(Module& wasm, + std::filesystem::path sourceMapFilename) { std::vector input = read_stdin(); if (input.size() >= 4 && input[0] == '\0' && input[1] == 'a' && input[2] == 's' && input[3] == 'm') { From 7158017673fd7823bea14314387d152f3074a0f2 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 09:58:46 -0700 Subject: [PATCH 03/12] temporarily bump OSX target --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 10ee9f16d5f..59a7be06a7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.10.2) # Needed for C++17 (std::variant) # TODO(https://github.com/WebAssembly/binaryen/issues/4299): We need # to reduce this for compatability with emsdk. -set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14" CACHE STRING "Minimum OS X deployment version") +set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version") project(binaryen LANGUAGES C CXX VERSION 112) include(GNUInstallDirs) From cac02d407c69af30f7e4c679f777d5618f51d4fc Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 10:26:33 -0700 Subject: [PATCH 04/12] convert path usage in tools --- src/support/path.cpp | 12 ++++++------ src/support/path.h | 9 +++++---- src/tools/wasm-as.cpp | 8 ++++---- src/tools/wasm-dis.cpp | 2 +- src/tools/wasm-emscripten-finalize.cpp | 14 +++++++------- src/tools/wasm-metadce.cpp | 4 ++-- src/tools/wasm-opt.cpp | 8 ++++---- src/tools/wasm-reduce.cpp | 24 +++++++++++++----------- src/wasm-io.h | 2 +- 9 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/support/path.cpp b/src/support/path.cpp index 1858fe1e931..fff7e971000 100644 --- a/src/support/path.cpp +++ b/src/support/path.cpp @@ -61,7 +61,7 @@ std::string getBaseName(const std::string& path) { return path; } -std::string getBinaryenRoot() { +std::filesystem::path getBinaryenRoot() { auto* envVar = getenv("BINARYEN_ROOT"); if (envVar) { return envVar; @@ -71,15 +71,15 @@ std::string getBinaryenRoot() { static std::string binDir; -std::string getBinaryenBinDir() { +std::filesystem::path getBinaryenBinDir() { if (binDir.empty()) { - return getBinaryenRoot() + getPathSeparator() + "bin" + getPathSeparator(); + return getBinaryenRoot() / "bin" / ""; } else { return binDir; } } -void setBinaryenBinDir(const std::string& dir) { +void setBinaryenBinDir(const std::filesystem::path& dir) { binDir = dir; if (binDir.empty() || binDir.back() != getPathSeparator()) { binDir += getPathSeparator(); @@ -87,8 +87,8 @@ void setBinaryenBinDir(const std::string& dir) { } // Gets the path to a binaryen binary tool, like wasm-opt -std::string getBinaryenBinaryTool(const std::string& name) { - return getBinaryenBinDir() + name; +std::filesystem::path getBinaryenBinaryTool(const std::string& name) { + return getBinaryenBinDir().append(name); } } // namespace wasm::Path diff --git a/src/support/path.h b/src/support/path.h index 78e85ca5cad..656b44f3f91 100644 --- a/src/support/path.h +++ b/src/support/path.h @@ -22,6 +22,7 @@ #define wasm_support_path_h #include +#include #include namespace wasm::Path { @@ -31,16 +32,16 @@ std::string getDirName(const std::string& path); std::string getBaseName(const std::string& path); // Get the binaryen root dor. -std::string getBinaryenRoot(); +std::filesystem::path getBinaryenRoot(); // Get the binaryen bin dir. -std::string getBinaryenBinDir(); +std::filesystem::path getBinaryenBinDir(); // Set the binaryen bin dir (allows tools to change it based on user input). -void setBinaryenBinDir(const std::string& dir); +void setBinaryenBinDir(const std::filesystem::path& dir); // Gets the path to a binaryen binary tool, like wasm-opt. -std::string getBinaryenBinaryTool(const std::string& name); +std::filesystem::path getBinaryenBinaryTool(const std::string& name); } // namespace wasm::Path diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp index cc4f6fda2f9..cdfc4e5c6e5 100644 --- a/src/tools/wasm-as.cpp +++ b/src/tools/wasm-as.cpp @@ -31,8 +31,8 @@ using namespace wasm; int main(int argc, const char* argv[]) { bool debugInfo = false; - std::string symbolMap; - std::string sourceMapFilename; + std::filesystem::path symbolMap; + std::filesystem::path sourceMapFilename; std::string sourceMapUrl; const std::string WasmAsOption = "wasm-as options"; @@ -142,11 +142,11 @@ int main(int argc, const char* argv[]) { ModuleWriter writer; writer.setBinary(true); writer.setDebugInfo(debugInfo); - if (sourceMapFilename.size()) { + if (!sourceMapFilename.empty()) { writer.setSourceMapFilename(sourceMapFilename); writer.setSourceMapUrl(sourceMapUrl); } - if (symbolMap.size() > 0) { + if (!symbolMap.empty() > 0) { writer.setSymbolMap(symbolMap); } writer.write(wasm, options.extra["output"]); diff --git a/src/tools/wasm-dis.cpp b/src/tools/wasm-dis.cpp index f9f30335963..7c37866db50 100644 --- a/src/tools/wasm-dis.cpp +++ b/src/tools/wasm-dis.cpp @@ -27,7 +27,7 @@ using namespace wasm; int main(int argc, const char* argv[]) { - std::string sourceMapFilename; + std::filesystem::path sourceMapFilename; const std::string WasmDisOption = "wasm-dis options"; diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp index 0f63e8112d1..039de59a8ad 100644 --- a/src/tools/wasm-emscripten-finalize.cpp +++ b/src/tools/wasm-emscripten-finalize.cpp @@ -39,12 +39,12 @@ using namespace wasm; int main(int argc, const char* argv[]) { const uint64_t INVALID_BASE = -1; - std::string infile; - std::string outfile; - std::string inputSourceMapFilename; - std::string outputSourceMapFilename; + std::filesystem::path infile; + std::filesystem::path outfile; + std::filesystem::path inputSourceMapFilename; + std::filesystem::path outputSourceMapFilename; std::string outputSourceMapUrl; - std::string dataSegmentFile; + std::filesystem::path dataSegmentFile; bool emitBinary = true; bool debugInfo = false; bool DWARF = false; @@ -213,7 +213,7 @@ int main(int argc, const char* argv[]) { // We will write the modified wasm if the user asked us to, either by // specifying an output file or requesting text output (which goes to stdout // by default). - auto writeOutput = outfile.size() > 0 || !emitBinary; + auto writeOutput = !outfile.empty() || !emitBinary; Module wasm; options.applyFeatures(wasm); @@ -314,7 +314,7 @@ int main(int argc, const char* argv[]) { writer.setDebugInfo(debugInfo); // writer.setSymbolMap(symbolMap); writer.setBinary(emitBinary); - if (outputSourceMapFilename.size()) { + if (!outputSourceMapFilename.empty()) { writer.setSourceMapFilename(outputSourceMapFilename); writer.setSourceMapUrl(outputSourceMapUrl); } diff --git a/src/tools/wasm-metadce.cpp b/src/tools/wasm-metadce.cpp index 029d4aae8ba..cc3020e48f9 100644 --- a/src/tools/wasm-metadce.cpp +++ b/src/tools/wasm-metadce.cpp @@ -421,7 +421,7 @@ int main(int argc, const char* argv[]) { std::vector passes; bool emitBinary = true; bool debugInfo = false; - std::string graphFile; + std::filesystem::path graphFile; bool dump = false; const std::string WasmMetaDCEOption = "wasm-opt options"; @@ -512,7 +512,7 @@ int main(int argc, const char* argv[]) { }); options.parse(argc, argv); - if (graphFile.size() == 0) { + if (!graphFile.empty()) { Fatal() << "no graph file provided."; } diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp index 0d279dc0864..6070bad903e 100644 --- a/src/tools/wasm-opt.cpp +++ b/src/tools/wasm-opt.cpp @@ -88,8 +88,8 @@ int main(int argc, const char* argv[]) { std::string emitJSWrapper; std::string emitSpecWrapper; std::string emitWasm2CWrapper; - std::string inputSourceMapFilename; - std::string outputSourceMapFilename; + std::filesystem::path inputSourceMapFilename; + std::filesystem::path outputSourceMapFilename; std::string outputSourceMapUrl; const std::string WasmOptOption = "wasm-opt options"; @@ -266,7 +266,7 @@ int main(int argc, const char* argv[]) { // down in TranslateToFuzzReader, but there is also an optional initial fuzz // file that if it exists we read it, then add more fuzz on top. if (!translateToFuzz || initialFuzz.size()) { - std::string inputFile = + std::filesystem::path inputFile = translateToFuzz ? initialFuzz : options.extra["infile"]; ModuleReader reader; // Enable DWARF parsing if we were asked for debug info, and were not @@ -412,7 +412,7 @@ int main(int argc, const char* argv[]) { ModuleWriter writer; writer.setBinary(emitBinary); writer.setDebugInfo(options.passOptions.debugInfo); - if (outputSourceMapFilename.size()) { + if (!outputSourceMapFilename.empty()) { writer.setSourceMapFilename(outputSourceMapFilename); writer.setSourceMapUrl(outputSourceMapUrl); } diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index 0cc620fb802..d472fea287b 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -298,7 +298,8 @@ struct Reducer // try both combining with a generic shrink (so minor pass overhead is // compensated for), and without for (auto pass : passes) { - std::string currCommand = Path::getBinaryenBinaryTool("wasm-opt") + " "; + std::string currCommand = + Path::getBinaryenBinaryTool("wasm-opt").append(" "); currCommand += working + " -o " + test + " " + pass + " " + extraFlags; if (!binary) { currCommand += " -S "; @@ -1186,9 +1187,10 @@ struct Reducer // int main(int argc, const char* argv[]) { - std::string input, test, working, command; + std::filesystem::path input, test, working; + std::string command; // By default, look for binaries alongside our own binary. - std::string binDir = Path::getDirName(argv[0]); + std::filesystem::path binDir = Path::getDirName(argv[0]); bool binary = true, deNan = false, verbose = false, debugInfo = false, force = false; @@ -1300,10 +1302,10 @@ int main(int argc, const char* argv[]) { WASM_UNREACHABLE("unexpected type system"); } - if (test.size() == 0) { + if (test.empty()) { Fatal() << "test file not provided\n"; } - if (working.size() == 0) { + if (working.empty()) { Fatal() << "working file not provided\n"; } @@ -1321,7 +1323,7 @@ int main(int argc, const char* argv[]) { std::cerr << "|extra flags: " << extraFlags << '\n'; // get the expected output - copy_file(input, test); + wasm::copy_file(input, test); expected.getFromExecution(command); std::cerr << "|expected result:\n" << expected << '\n'; @@ -1363,7 +1365,7 @@ int main(int argc, const char* argv[]) { << "running the command on the given input gives the same result as " "when running it on either a trivial valid wasm or a file with " "nonsense in it. does the script not look at the test file (" + - test + ")? (use -f to ignore this check)"; + test.string() + ")? (use -f to ignore this check)"; } } } @@ -1372,8 +1374,8 @@ int main(int argc, const char* argv[]) { "(read-written) binary\n"; { // read and write it - auto cmd = Path::getBinaryenBinaryTool("wasm-opt") + " " + input + " -o " + - test + " " + extraFlags; + auto cmd = Path::getBinaryenBinaryTool("wasm-opt").string() + " " + + input.string() + " -o " + test.string() + " " + extraFlags; if (!binary) { cmd += " -S "; } @@ -1390,7 +1392,7 @@ int main(int argc, const char* argv[]) { } } - copy_file(input, working); + wasm::copy_file(input, working); auto workingSize = file_size(working); std::cerr << "|input size: " << workingSize << "\n"; @@ -1484,5 +1486,5 @@ int main(int argc, const char* argv[]) { << '\n'; } std::cerr << "|finished, final size: " << file_size(working) << "\n"; - copy_file(working, test); // just to avoid confusion + wasm::copy_file(working, test); // just to avoid confusion } diff --git a/src/wasm-io.h b/src/wasm-io.h index fc4995a8119..48a93ab46d6 100644 --- a/src/wasm-io.h +++ b/src/wasm-io.h @@ -93,7 +93,7 @@ class ModuleWriter : public ModuleIOBase { std::filesystem::path symbolMap; std::filesystem::path sourceMapFilename; - std::filesystem::path sourceMapUrl; + std::string sourceMapUrl; public: // Writing defaults to not storing the names section. Storing it is a user- From e536457819cc8e63bd8987f785e0bb792c8ecd68 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 11:05:10 -0700 Subject: [PATCH 05/12] fix empty check --- src/tools/wasm-metadce.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/wasm-metadce.cpp b/src/tools/wasm-metadce.cpp index cc3020e48f9..d592a4463d0 100644 --- a/src/tools/wasm-metadce.cpp +++ b/src/tools/wasm-metadce.cpp @@ -512,7 +512,7 @@ int main(int argc, const char* argv[]) { }); options.parse(argc, argv); - if (!graphFile.empty()) { + if (graphFile.empty()) { Fatal() << "no graph file provided."; } From d36180631c64de62f96510950796ef4897adad81 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 17:17:14 -0700 Subject: [PATCH 06/12] fix bad path append --- src/tools/wasm-reduce.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index d472fea287b..7e7fc41fd0a 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -298,9 +298,9 @@ struct Reducer // try both combining with a generic shrink (so minor pass overhead is // compensated for), and without for (auto pass : passes) { - std::string currCommand = - Path::getBinaryenBinaryTool("wasm-opt").append(" "); - currCommand += working + " -o " + test + " " + pass + " " + extraFlags; + std::string currCommand = Path::getBinaryenBinaryTool("wasm-opt"); + currCommand += + " " + working + " -o " + test + " " + pass + " " + extraFlags; if (!binary) { currCommand += " -S "; } From 80d2528646e0c71653f7c1272cdab18c28c7a503 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 17:19:05 -0700 Subject: [PATCH 07/12] review suggestion --- src/support/file.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/support/file.h b/src/support/file.h index 3d5081a640e..a5040ce2898 100644 --- a/src/support/file.h +++ b/src/support/file.h @@ -70,7 +70,7 @@ class Output { std::ostream out; }; -// Copies a file to another file, overwriting if the fiel exists +// Copies a file to another file, overwriting if the file exists void copy_file(std::filesystem::path input, std::filesystem::path output); } // namespace wasm From 3b5f5cdcdf0becc2b43a938fee9513e398d6f087 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 18:15:57 -0700 Subject: [PATCH 08/12] use binDir path in path.cpp --- src/support/path.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/support/path.cpp b/src/support/path.cpp index fff7e971000..c21b02c67fa 100644 --- a/src/support/path.cpp +++ b/src/support/path.cpp @@ -69,7 +69,7 @@ std::filesystem::path getBinaryenRoot() { return "."; } -static std::string binDir; +static std::filesystem::path binDir; std::filesystem::path getBinaryenBinDir() { if (binDir.empty()) { @@ -81,7 +81,7 @@ std::filesystem::path getBinaryenBinDir() { void setBinaryenBinDir(const std::filesystem::path& dir) { binDir = dir; - if (binDir.empty() || binDir.back() != getPathSeparator()) { + if (binDir.empty() || binDir.string().back() != getPathSeparator()) { binDir += getPathSeparator(); } } From 6d0deda65c14fee147001877c86b232b04ab3dba Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 18:19:34 -0700 Subject: [PATCH 09/12] use more paths, explicit conversions to string --- src/tools/wasm-reduce.cpp | 15 ++++++++------- src/wasm/wasm-io.cpp | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index 7e7fc41fd0a..ff229e87964 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -232,15 +232,16 @@ static std::unordered_set functionsWeTriedToRemove; struct Reducer : public WalkerPass>> { - std::string command, test, working; + std::string command; + std::filesystem::path test, working; bool binary, deNan, verbose, debugInfo; ToolOptions& toolOptions; // test is the file we write to that the command will operate on // working is the current temporary state, the reduction so far Reducer(std::string command, - std::string test, - std::string working, + std::filesystem::path test, + std::filesystem::path working, bool binary, bool deNan, bool verbose, @@ -298,9 +299,9 @@ struct Reducer // try both combining with a generic shrink (so minor pass overhead is // compensated for), and without for (auto pass : passes) { - std::string currCommand = Path::getBinaryenBinaryTool("wasm-opt"); + std::string currCommand = Path::getBinaryenBinaryTool("wasm-opt").string(); currCommand += - " " + working + " -o " + test + " " + pass + " " + extraFlags; + " " + working.string() + " -o " + test.string() + " " + pass + " " + extraFlags; if (!binary) { currCommand += " -S "; } @@ -315,7 +316,7 @@ struct Reducer if (ProgramResult(command) == expected) { std::cerr << "| command \"" << currCommand << "\" succeeded, reduced size to " << newSize << '\n'; - copy_file(test, working); + wasm::copy_file(test, working); more = true; oldSize = newSize; } @@ -462,7 +463,7 @@ struct Reducer void noteReduction(size_t amount = 1) { reduced += amount; - copy_file(test, working); + wasm::copy_file(test, working); } // tests a reduction on an arbitrary child diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp index ab466125cee..5a71d59001f 100644 --- a/src/wasm/wasm-io.cpp +++ b/src/wasm/wasm-io.cpp @@ -162,7 +162,7 @@ void ModuleWriter::writeBinary(Module& wasm, Output& output) { writer.setSourceMap(sourceMapStream.get(), sourceMapUrl); } if (!symbolMap.empty()) { - writer.setSymbolMap(symbolMap); + writer.setSymbolMap(symbolMap.string()); } writer.write(); buffer.writeTo(output); From 002e647e1c66fcc04d69605f5783eda90a132a44 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 20:44:13 -0700 Subject: [PATCH 10/12] clang-format --- src/tools/wasm-reduce.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index ff229e87964..a8841dd8a8d 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -299,9 +299,10 @@ struct Reducer // try both combining with a generic shrink (so minor pass overhead is // compensated for), and without for (auto pass : passes) { - std::string currCommand = Path::getBinaryenBinaryTool("wasm-opt").string(); - currCommand += - " " + working.string() + " -o " + test.string() + " " + pass + " " + extraFlags; + std::string currCommand = + Path::getBinaryenBinaryTool("wasm-opt").string(); + currCommand += " " + working.string() + " -o " + test.string() + " " + + pass + " " + extraFlags; if (!binary) { currCommand += " -S "; } From 2fd367b15f6a6ccb1a57e3a0c4a84686fdac7238 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 22:35:33 -0700 Subject: [PATCH 11/12] use fs abstraction --- CMakeLists.txt | 2 +- src/support/file.cpp | 17 +++++------ src/support/file.h | 12 ++++---- src/support/path.cpp | 10 +++---- src/support/path.h | 10 ++++--- src/tools/wasm-as.cpp | 4 +-- src/tools/wasm-dis.cpp | 2 +- src/tools/wasm-emscripten-finalize.cpp | 10 +++---- src/tools/wasm-metadce.cpp | 2 +- src/tools/wasm-opt.cpp | 6 ++-- src/tools/wasm-reduce.cpp | 14 ++++----- src/wasm-io.h | 30 +++++++++---------- src/wasm/wasm-io.cpp | 22 +++++++------- .../include/llvm/Support/FileSystem.h | 2 +- 14 files changed, 73 insertions(+), 70 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 59a7be06a7c..10ee9f16d5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.10.2) # Needed for C++17 (std::variant) # TODO(https://github.com/WebAssembly/binaryen/issues/4299): We need # to reduce this for compatability with emsdk. -set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version") +set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14" CACHE STRING "Minimum OS X deployment version") project(binaryen LANGUAGES C CXX VERSION 112) include(GNUInstallDirs) diff --git a/src/support/file.cpp b/src/support/file.cpp index 81161ef874b..d8663146fdf 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -20,7 +20,6 @@ #include #include -#include #include #include @@ -48,7 +47,7 @@ template<> std::string do_read_stdin::operator()() { } template -T wasm::read_file(const std::filesystem::path& filename, +T wasm::read_file(const fs::path& filename, Flags::BinaryOption binary) { if (filename == "-") { return do_read_stdin{}(); @@ -97,12 +96,12 @@ std::string wasm::read_possible_response_file(const std::string& input) { } // Explicit instantiations for the explicit specializations. -template std::string wasm::read_file<>(const std::filesystem::path&, +template std::string wasm::read_file<>(const fs::path&, Flags::BinaryOption); -template std::vector wasm::read_file<>(const std::filesystem::path&, +template std::vector wasm::read_file<>(const fs::path&, Flags::BinaryOption); -wasm::Output::Output(const std::filesystem::path& filename, +wasm::Output::Output(const fs::path& filename, Flags::BinaryOption binary) : outfile(), out([this, filename, binary]() { // Ensure a single return at the very end, to avoid clang-tidy warnings @@ -125,8 +124,8 @@ wasm::Output::Output(const std::filesystem::path& filename, return buffer; }()) {} -void wasm::copy_file(std::filesystem::path input, - std::filesystem::path output) { - std::filesystem::copy_file( - input, output, std::filesystem::copy_options::overwrite_existing); +void wasm::copy_file(fs::path input, + fs::path output) { + fs::copy_file( + input, output, fs::copy_options::overwrite_existing); } diff --git a/src/support/file.h b/src/support/file.h index a5040ce2898..85153547ba1 100644 --- a/src/support/file.h +++ b/src/support/file.h @@ -27,6 +27,8 @@ #include #include +namespace fs = std::filesystem; + namespace wasm { namespace Flags { @@ -36,12 +38,12 @@ enum BinaryOption { Binary, Text }; std::vector read_stdin(); template -T read_file(const std::filesystem::path& filename, Flags::BinaryOption binary); +T read_file(const fs::path& filename, Flags::BinaryOption binary); // Declare the valid explicit specializations. -extern template std::string read_file<>(const std::filesystem::path&, +extern template std::string read_file<>(const fs::path&, Flags::BinaryOption); -extern template std::vector read_file<>(const std::filesystem::path&, +extern template std::vector read_file<>(const fs::path&, Flags::BinaryOption); // Given a string which may be a response file (i.e., a filename starting @@ -52,7 +54,7 @@ std::string read_possible_response_file(const std::string&); class Output { public: // An empty filename or "-" will open stdout instead. - Output(const std::filesystem::path& filename, Flags::BinaryOption binary); + Output(const fs::path& filename, Flags::BinaryOption binary); ~Output() = default; template std::ostream& operator<<(const T& v) { return out << v; } @@ -71,7 +73,7 @@ class Output { }; // Copies a file to another file, overwriting if the file exists -void copy_file(std::filesystem::path input, std::filesystem::path output); +void copy_file(fs::path input, fs::path output); } // namespace wasm diff --git a/src/support/path.cpp b/src/support/path.cpp index c21b02c67fa..4ddd186016f 100644 --- a/src/support/path.cpp +++ b/src/support/path.cpp @@ -61,7 +61,7 @@ std::string getBaseName(const std::string& path) { return path; } -std::filesystem::path getBinaryenRoot() { +fs::path getBinaryenRoot() { auto* envVar = getenv("BINARYEN_ROOT"); if (envVar) { return envVar; @@ -69,9 +69,9 @@ std::filesystem::path getBinaryenRoot() { return "."; } -static std::filesystem::path binDir; +static fs::path binDir; -std::filesystem::path getBinaryenBinDir() { +fs::path getBinaryenBinDir() { if (binDir.empty()) { return getBinaryenRoot() / "bin" / ""; } else { @@ -79,7 +79,7 @@ std::filesystem::path getBinaryenBinDir() { } } -void setBinaryenBinDir(const std::filesystem::path& dir) { +void setBinaryenBinDir(const fs::path& dir) { binDir = dir; if (binDir.empty() || binDir.string().back() != getPathSeparator()) { binDir += getPathSeparator(); @@ -87,7 +87,7 @@ void setBinaryenBinDir(const std::filesystem::path& dir) { } // Gets the path to a binaryen binary tool, like wasm-opt -std::filesystem::path getBinaryenBinaryTool(const std::string& name) { +fs::path getBinaryenBinaryTool(const std::string& name) { return getBinaryenBinDir().append(name); } diff --git a/src/support/path.h b/src/support/path.h index 656b44f3f91..eda3136ee9c 100644 --- a/src/support/path.h +++ b/src/support/path.h @@ -25,6 +25,8 @@ #include #include +namespace fs = std::filesystem; + namespace wasm::Path { char getPathSeparator(); @@ -32,16 +34,16 @@ std::string getDirName(const std::string& path); std::string getBaseName(const std::string& path); // Get the binaryen root dor. -std::filesystem::path getBinaryenRoot(); +fs::path getBinaryenRoot(); // Get the binaryen bin dir. -std::filesystem::path getBinaryenBinDir(); +fs::path getBinaryenBinDir(); // Set the binaryen bin dir (allows tools to change it based on user input). -void setBinaryenBinDir(const std::filesystem::path& dir); +void setBinaryenBinDir(const fs::path& dir); // Gets the path to a binaryen binary tool, like wasm-opt. -std::filesystem::path getBinaryenBinaryTool(const std::string& name); +fs::path getBinaryenBinaryTool(const std::string& name); } // namespace wasm::Path diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp index cdfc4e5c6e5..427c88feab6 100644 --- a/src/tools/wasm-as.cpp +++ b/src/tools/wasm-as.cpp @@ -31,8 +31,8 @@ using namespace wasm; int main(int argc, const char* argv[]) { bool debugInfo = false; - std::filesystem::path symbolMap; - std::filesystem::path sourceMapFilename; + fs::path symbolMap; + fs::path sourceMapFilename; std::string sourceMapUrl; const std::string WasmAsOption = "wasm-as options"; diff --git a/src/tools/wasm-dis.cpp b/src/tools/wasm-dis.cpp index 7c37866db50..e4b7e4e149c 100644 --- a/src/tools/wasm-dis.cpp +++ b/src/tools/wasm-dis.cpp @@ -27,7 +27,7 @@ using namespace wasm; int main(int argc, const char* argv[]) { - std::filesystem::path sourceMapFilename; + fs::path sourceMapFilename; const std::string WasmDisOption = "wasm-dis options"; diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp index 039de59a8ad..a535ac3bc88 100644 --- a/src/tools/wasm-emscripten-finalize.cpp +++ b/src/tools/wasm-emscripten-finalize.cpp @@ -39,12 +39,12 @@ using namespace wasm; int main(int argc, const char* argv[]) { const uint64_t INVALID_BASE = -1; - std::filesystem::path infile; - std::filesystem::path outfile; - std::filesystem::path inputSourceMapFilename; - std::filesystem::path outputSourceMapFilename; + fs::path infile; + fs::path outfile; + fs::path inputSourceMapFilename; + fs::path outputSourceMapFilename; std::string outputSourceMapUrl; - std::filesystem::path dataSegmentFile; + fs::path dataSegmentFile; bool emitBinary = true; bool debugInfo = false; bool DWARF = false; diff --git a/src/tools/wasm-metadce.cpp b/src/tools/wasm-metadce.cpp index d592a4463d0..7ac82f709f8 100644 --- a/src/tools/wasm-metadce.cpp +++ b/src/tools/wasm-metadce.cpp @@ -421,7 +421,7 @@ int main(int argc, const char* argv[]) { std::vector passes; bool emitBinary = true; bool debugInfo = false; - std::filesystem::path graphFile; + fs::path graphFile; bool dump = false; const std::string WasmMetaDCEOption = "wasm-opt options"; diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp index 6070bad903e..2af2c0af309 100644 --- a/src/tools/wasm-opt.cpp +++ b/src/tools/wasm-opt.cpp @@ -88,8 +88,8 @@ int main(int argc, const char* argv[]) { std::string emitJSWrapper; std::string emitSpecWrapper; std::string emitWasm2CWrapper; - std::filesystem::path inputSourceMapFilename; - std::filesystem::path outputSourceMapFilename; + fs::path inputSourceMapFilename; + fs::path outputSourceMapFilename; std::string outputSourceMapUrl; const std::string WasmOptOption = "wasm-opt options"; @@ -266,7 +266,7 @@ int main(int argc, const char* argv[]) { // down in TranslateToFuzzReader, but there is also an optional initial fuzz // file that if it exists we read it, then add more fuzz on top. if (!translateToFuzz || initialFuzz.size()) { - std::filesystem::path inputFile = + fs::path inputFile = translateToFuzz ? initialFuzz : options.extra["infile"]; ModuleReader reader; // Enable DWARF parsing if we were asked for debug info, and were not diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index a8841dd8a8d..f5a6a773b69 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -25,7 +25,6 @@ #include #include -#include #include #include "ir/branch-utils.h" @@ -72,7 +71,8 @@ std::string GetLastErrorStdStr() { return std::string(); } #endif -using std::filesystem::file_size; + +using fs::file_size; using namespace wasm; // A timeout on every execution of the command. @@ -233,15 +233,15 @@ static std::unordered_set functionsWeTriedToRemove; struct Reducer : public WalkerPass>> { std::string command; - std::filesystem::path test, working; + fs::path test, working; bool binary, deNan, verbose, debugInfo; ToolOptions& toolOptions; // test is the file we write to that the command will operate on // working is the current temporary state, the reduction so far Reducer(std::string command, - std::filesystem::path test, - std::filesystem::path working, + fs::path test, + fs::path working, bool binary, bool deNan, bool verbose, @@ -1189,10 +1189,10 @@ struct Reducer // int main(int argc, const char* argv[]) { - std::filesystem::path input, test, working; + fs::path input, test, working; std::string command; // By default, look for binaries alongside our own binary. - std::filesystem::path binDir = Path::getDirName(argv[0]); + fs::path binDir = Path::getDirName(argv[0]); bool binary = true, deNan = false, verbose = false, debugInfo = false, force = false; diff --git a/src/wasm-io.h b/src/wasm-io.h index 48a93ab46d6..88169514d0c 100644 --- a/src/wasm-io.h +++ b/src/wasm-io.h @@ -58,18 +58,18 @@ class ModuleReader : public ModuleIOBase { } // read text - void readText(std::filesystem::path filename, Module& wasm); + void readText(fs::path filename, Module& wasm); // read binary - void readBinary(std::filesystem::path filename, + void readBinary(fs::path filename, Module& wasm, - std::filesystem::path sourceMapFilename = ""); + fs::path sourceMapFilename = ""); // read text or binary, checking the contents for what it is. If `filename` is // empty, read from stdin. - void read(std::filesystem::path filename, + void read(fs::path filename, Module& wasm, - std::filesystem::path sourceMapFilename = ""); + fs::path sourceMapFilename = ""); // check whether a file is a wasm binary - bool isBinaryFile(std::filesystem::path filename); + bool isBinaryFile(fs::path filename); private: bool DWARF = false; @@ -78,11 +78,11 @@ class ModuleReader : public ModuleIOBase { bool skipFunctionBodies = false; - void readStdin(Module& wasm, std::filesystem::path sourceMapFilename); + void readStdin(Module& wasm, fs::path sourceMapFilename); void readBinaryData(std::vector& input, Module& wasm, - std::filesystem::path sourceMapFilename); + fs::path sourceMapFilename); }; class ModuleWriter : public ModuleIOBase { @@ -91,8 +91,8 @@ class ModuleWriter : public ModuleIOBase { // TODO: Remove `emitModuleName`. See the comment in wasm-binary.h bool emitModuleName = false; - std::filesystem::path symbolMap; - std::filesystem::path sourceMapFilename; + fs::path symbolMap; + fs::path sourceMapFilename; std::string sourceMapUrl; public: @@ -101,10 +101,10 @@ class ModuleWriter : public ModuleIOBase { ModuleWriter() { setDebugInfo(false); } void setBinary(bool binary_) { binary = binary_; } - void setSymbolMap(std::filesystem::path symbolMap_) { + void setSymbolMap(fs::path symbolMap_) { symbolMap = symbolMap_; } - void setSourceMapFilename(std::filesystem::path sourceMapFilename_) { + void setSourceMapFilename(fs::path sourceMapFilename_) { sourceMapFilename = sourceMapFilename_; } void setSourceMapUrl(std::string sourceMapUrl_) { @@ -114,15 +114,15 @@ class ModuleWriter : public ModuleIOBase { // write text void writeText(Module& wasm, Output& output); - void writeText(Module& wasm, std::filesystem::path filename); + void writeText(Module& wasm, fs::path filename); // write binary void writeBinary(Module& wasm, Output& output); - void writeBinary(Module& wasm, std::filesystem::path filename); + void writeBinary(Module& wasm, fs::path filename); // write text or binary, defaulting to binary unless setBinary(false), // and unless there is no output file (in which case we write text // to stdout). void write(Module& wasm, Output& output); - void write(Module& wasm, std::filesystem::path filename); + void write(Module& wasm, fs::path filename); }; } // namespace wasm diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp index 5a71d59001f..a380dabe58e 100644 --- a/src/wasm/wasm-io.cpp +++ b/src/wasm/wasm-io.cpp @@ -51,7 +51,7 @@ static void readTextData(std::string& input, Module& wasm, IRProfile profile) { } } -void ModuleReader::readText(std::filesystem::path filename, Module& wasm) { +void ModuleReader::readText(fs::path filename, Module& wasm) { BYN_TRACE("reading text from " << filename << "\n"); auto input(read_file(filename, Flags::Text)); readTextData(input, wasm, profile); @@ -59,7 +59,7 @@ void ModuleReader::readText(std::filesystem::path filename, Module& wasm) { void ModuleReader::readBinaryData(std::vector& input, Module& wasm, - std::filesystem::path sourceMapFilename) { + fs::path sourceMapFilename) { std::unique_ptr sourceMapStream; // Assume that the wasm has had its initial features applied, and use those // while parsing. @@ -78,15 +78,15 @@ void ModuleReader::readBinaryData(std::vector& input, } } -void ModuleReader::readBinary(std::filesystem::path filename, +void ModuleReader::readBinary(fs::path filename, Module& wasm, - std::filesystem::path sourceMapFilename) { + fs::path sourceMapFilename) { BYN_TRACE("reading binary from " << filename << "\n"); auto input(read_file>(filename, Flags::Binary)); readBinaryData(input, wasm, sourceMapFilename); } -bool ModuleReader::isBinaryFile(std::filesystem::path filename) { +bool ModuleReader::isBinaryFile(fs::path filename) { std::ifstream infile; std::ios_base::openmode flags = std::ifstream::in | std::ifstream::binary; infile.open(filename, flags); @@ -97,9 +97,9 @@ bool ModuleReader::isBinaryFile(std::filesystem::path filename) { buffer[3] == 'm'; } -void ModuleReader::read(std::filesystem::path filename, +void ModuleReader::read(fs::path filename, Module& wasm, - std::filesystem::path sourceMapFilename) { + fs::path sourceMapFilename) { // empty filename or "-" means read from stdin if (filename.empty() || filename == "-") { readStdin(wasm, sourceMapFilename); @@ -120,7 +120,7 @@ void ModuleReader::read(std::filesystem::path filename, // TODO: reading into a vector then copying into a string is unnecessarily // inefficient. It would be better to read just once into a stringstream. void ModuleReader::readStdin(Module& wasm, - std::filesystem::path sourceMapFilename) { + fs::path sourceMapFilename) { std::vector input = read_stdin(); if (input.size() >= 4 && input[0] == '\0' && input[1] == 'a' && input[2] == 's' && input[3] == 'm') { @@ -141,7 +141,7 @@ void ModuleWriter::writeText(Module& wasm, Output& output) { output.getStream() << wasm; } -void ModuleWriter::writeText(Module& wasm, std::filesystem::path filename) { +void ModuleWriter::writeText(Module& wasm, fs::path filename) { BYN_TRACE("writing text to " << filename << "\n"); Output output(filename, Flags::Text); writeText(wasm, output); @@ -171,7 +171,7 @@ void ModuleWriter::writeBinary(Module& wasm, Output& output) { } } -void ModuleWriter::writeBinary(Module& wasm, std::filesystem::path filename) { +void ModuleWriter::writeBinary(Module& wasm, fs::path filename) { BYN_TRACE("writing binary to " << filename << "\n"); Output output(filename, Flags::Binary); writeBinary(wasm, output); @@ -185,7 +185,7 @@ void ModuleWriter::write(Module& wasm, Output& output) { } } -void ModuleWriter::write(Module& wasm, std::filesystem::path filename) { +void ModuleWriter::write(Module& wasm, fs::path filename) { if (binary && !filename.empty()) { writeBinary(wasm, filename); } else { diff --git a/third_party/llvm-project/include/llvm/Support/FileSystem.h b/third_party/llvm-project/include/llvm/Support/FileSystem.h index a29a9d78794..3fd7f1dc107 100644 --- a/third_party/llvm-project/include/llvm/Support/FileSystem.h +++ b/third_party/llvm-project/include/llvm/Support/FileSystem.h @@ -1213,7 +1213,7 @@ class directory_entry { // when traversing a directory. The design of this class wraps most of the // information in basic_file_status, so on platforms where we can't populate // that whole structure, callers end up paying for a stat(). - // std::filesystem::directory_entry may be a better model. + // fs::directory_entry may be a better model. std::string Path; file_type Type = file_type::type_unknown; // Most platforms can provide this. bool FollowSymlinks = true; // Affects the behavior of status(). From 7695817692784aaea5af3bf0f9cb40b499408ee8 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 5 Apr 2023 22:57:30 -0700 Subject: [PATCH 12/12] exp --- src/support/file.h | 4 ++-- src/support/path.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/support/file.h b/src/support/file.h index 85153547ba1..254da502156 100644 --- a/src/support/file.h +++ b/src/support/file.h @@ -21,13 +21,13 @@ #ifndef wasm_support_file_h #define wasm_support_file_h -#include +#include #include #include #include #include -namespace fs = std::filesystem; +namespace fs = std::experimental::filesystem; namespace wasm { diff --git a/src/support/path.h b/src/support/path.h index eda3136ee9c..3bfcf6aac3f 100644 --- a/src/support/path.h +++ b/src/support/path.h @@ -22,10 +22,10 @@ #define wasm_support_path_h #include -#include +#include #include -namespace fs = std::filesystem; +namespace fs = std::experimental::filesystem; namespace wasm::Path {