From 928c39937af6303909addf4cec8f6e7c0123be90 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sun, 3 Oct 2021 16:26:31 +0900 Subject: [PATCH] Refactor --- elf/cmdline.cc | 63 --------------------------------------------- elf/main.cc | 12 +-------- elf/mold.h | 7 ----- mold.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 81 deletions(-) diff --git a/elf/cmdline.cc b/elf/cmdline.cc index a1f06cc3..1e9649f3 100644 --- a/elf/cmdline.cc +++ b/elf/cmdline.cc @@ -151,65 +151,6 @@ Options: mold: supported targets: elf32-i386 elf64-x86-64 mold: supported emulations: elf_i386 elf_x86_64)"; -template -static std::vector -read_response_file(Context &ctx, std::string_view path) { - std::vector vec; - MappedFile> *mf = - MappedFile>::must_open(ctx, std::string(path)); - u8 *data = mf->data; - - auto read_quoted = [&](i64 i, char quote) { - std::string buf; - while (i < mf->size && data[i] != quote) { - if (data[i] == '\\') { - buf.append(1, data[i + 1]); - i += 2; - } else { - buf.append(1, data[i++]); - } - } - if (i >= mf->size) - Fatal(ctx) << path << ": premature end of input"; - vec.push_back(save_string(ctx, buf)); - return i + 1; - }; - - auto read_unquoted = [&](i64 i) { - std::string buf; - while (i < mf->size && !isspace(data[i])) - buf.append(1, data[i++]); - vec.push_back(save_string(ctx, buf)); - return i; - }; - - for (i64 i = 0; i < mf->size;) { - if (isspace(data[i])) - i++; - else if (data[i] == '\'') - i = read_quoted(i + 1, '\''); - else if (data[i] == '\"') - i = read_quoted(i + 1, '\"'); - else - i = read_unquoted(i); - } - return vec; -} - -template -std::vector -expand_response_files(Context &ctx, char **argv) { - std::vector vec; - - for (i64 i = 0; argv[i]; i++) { - if (argv[i][0] == '@') - append(vec, read_response_file(ctx, argv[i] + 1)); - else - vec.push_back(argv[i]); - } - return vec; -} - static std::vector add_dashes(std::string name) { // Multi-letter linker options can be preceded by either a single // dash or double dashes except ones starting with "o", which must @@ -861,10 +802,6 @@ void parse_nonpositional_args(Context &ctx, #define INSTANTIATE(E) \ template \ - std::vector \ - expand_response_files(Context &ctx, char **argv); \ - \ - template \ bool read_arg(Context &ctx, std::span &args, \ std::string_view &arg, \ std::string name); \ diff --git a/elf/main.cc b/elf/main.cc index 7dfb25cd..a552576b 100644 --- a/elf/main.cc +++ b/elf/main.cc @@ -13,15 +13,6 @@ namespace mold::elf { -template -std::string_view save_string(Context &ctx, const std::string &str) { - u8 *buf = new u8[str.size() + 1]; - memcpy(buf, str.data(), str.size()); - buf[str.size()] = '\0'; - ctx.string_pool.push_back(std::unique_ptr(buf)); - return {(char *)buf, str.size()}; -} - std::regex glob_to_regex(std::string_view pattern) { std::stringstream ss; for (u8 c : pattern) { @@ -654,8 +645,7 @@ int main(int argc, char **argv) { } #define INSTANTIATE(E) \ - template void read_file(Context &, MappedFile> *); \ - template std::string_view save_string(Context &, const std::string &); + template void read_file(Context &, MappedFile> *); INSTANTIATE(X86_64); INSTANTIATE(I386); diff --git a/elf/mold.h b/elf/mold.h index ea9e81e0..73a5d253 100644 --- a/elf/mold.h +++ b/elf/mold.h @@ -1094,10 +1094,6 @@ void process_run_subcommand(Context &ctx, int argc, char **argv); // commandline.cc // -template -std::vector -expand_response_files(Context &ctx, char **argv); - bool read_flag(std::span &args, std::string name); template @@ -1404,9 +1400,6 @@ MappedFile> *find_library(Context &ctx, std::string path); template void read_file(Context &ctx, MappedFile> *mf); -template -std::string_view save_string(Context &ctx, const std::string &str); - std::regex glob_to_regex(std::string_view pat); int main(int argc, char **argv); diff --git a/mold.h b/mold.h index b006100e..5c5af95a 100644 --- a/mold.h +++ b/mold.h @@ -248,6 +248,15 @@ inline i64 uleb_size(u64 val) { return i; } +template +std::string_view save_string(C &ctx, const std::string &str) { + u8 *buf = new u8[str.size() + 1]; + memcpy(buf, str.data(), str.size()); + buf[str.size()] = '\0'; + ctx.string_pool.push_back(std::unique_ptr(buf)); + return {(char *)buf, str.size()}; +} + // // Concurrent Map // @@ -820,4 +829,64 @@ read_archive_members(C &ctx, MappedFile *mf) { } } +// +// Command line processing +// + +template +static std::vector +read_response_file(C &ctx, std::string_view path) { + std::vector vec; + MappedFile *mf = MappedFile::must_open(ctx, std::string(path)); + u8 *data = mf->data; + + auto read_quoted = [&](i64 i, char quote) { + std::string buf; + while (i < mf->size && data[i] != quote) { + if (data[i] == '\\') { + buf.append(1, data[i + 1]); + i += 2; + } else { + buf.append(1, data[i++]); + } + } + if (i >= mf->size) + Fatal(ctx) << path << ": premature end of input"; + vec.push_back(save_string(ctx, buf)); + return i + 1; + }; + + auto read_unquoted = [&](i64 i) { + std::string buf; + while (i < mf->size && !isspace(data[i])) + buf.append(1, data[i++]); + vec.push_back(save_string(ctx, buf)); + return i; + }; + + for (i64 i = 0; i < mf->size;) { + if (isspace(data[i])) + i++; + else if (data[i] == '\'') + i = read_quoted(i + 1, '\''); + else if (data[i] == '\"') + i = read_quoted(i + 1, '\"'); + else + i = read_unquoted(i); + } + return vec; +} + +template +std::vector expand_response_files(C &ctx, char **argv) { + std::vector vec; + for (i64 i = 0; argv[i]; i++) { + if (argv[i][0] == '@') + append(vec, read_response_file(ctx, argv[i] + 1)); + else + vec.push_back(argv[i]); + } + return vec; +} + } // namespace mold