mirror of
https://github.com/rui314/mold.git
synced 2024-09-20 09:27:45 +03:00
Refactor
This commit is contained in:
parent
001cf042d9
commit
928c39937a
@ -151,65 +151,6 @@ Options:
|
||||
mold: supported targets: elf32-i386 elf64-x86-64
|
||||
mold: supported emulations: elf_i386 elf_x86_64)";
|
||||
|
||||
template <typename E>
|
||||
static std::vector<std::string_view>
|
||||
read_response_file(Context<E> &ctx, std::string_view path) {
|
||||
std::vector<std::string_view> vec;
|
||||
MappedFile<Context<E>> *mf =
|
||||
MappedFile<Context<E>>::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 <typename E>
|
||||
std::vector<std::string_view>
|
||||
expand_response_files(Context<E> &ctx, char **argv) {
|
||||
std::vector<std::string_view> 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<std::string> 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<E> &ctx,
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template \
|
||||
std::vector<std::string_view> \
|
||||
expand_response_files(Context<E> &ctx, char **argv); \
|
||||
\
|
||||
template \
|
||||
bool read_arg(Context<E> &ctx, std::span<std::string_view> &args, \
|
||||
std::string_view &arg, \
|
||||
std::string name); \
|
||||
|
12
elf/main.cc
12
elf/main.cc
@ -13,15 +13,6 @@
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
template <typename E>
|
||||
std::string_view save_string(Context<E> &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<u8[]>(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<E> &, MappedFile<Context<E>> *); \
|
||||
template std::string_view save_string(Context<E> &, const std::string &);
|
||||
template void read_file(Context<E> &, MappedFile<Context<E>> *);
|
||||
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
|
@ -1094,10 +1094,6 @@ void process_run_subcommand(Context<E> &ctx, int argc, char **argv);
|
||||
// commandline.cc
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
std::vector<std::string_view>
|
||||
expand_response_files(Context<E> &ctx, char **argv);
|
||||
|
||||
bool read_flag(std::span<std::string_view> &args, std::string name);
|
||||
|
||||
template <typename E>
|
||||
@ -1404,9 +1400,6 @@ MappedFile<Context<E>> *find_library(Context<E> &ctx, std::string path);
|
||||
template <typename E>
|
||||
void read_file(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
|
||||
template <typename E>
|
||||
std::string_view save_string(Context<E> &ctx, const std::string &str);
|
||||
|
||||
std::regex glob_to_regex(std::string_view pat);
|
||||
|
||||
int main(int argc, char **argv);
|
||||
|
69
mold.h
69
mold.h
@ -248,6 +248,15 @@ inline i64 uleb_size(u64 val) {
|
||||
return i;
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
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<u8[]>(buf));
|
||||
return {(char *)buf, str.size()};
|
||||
}
|
||||
|
||||
//
|
||||
// Concurrent Map
|
||||
//
|
||||
@ -820,4 +829,64 @@ read_archive_members(C &ctx, MappedFile<C> *mf) {
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Command line processing
|
||||
//
|
||||
|
||||
template <typename C>
|
||||
static std::vector<std::string_view>
|
||||
read_response_file(C &ctx, std::string_view path) {
|
||||
std::vector<std::string_view> vec;
|
||||
MappedFile<C> *mf = MappedFile<C>::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 <typename C>
|
||||
std::vector<std::string_view> expand_response_files(C &ctx, char **argv) {
|
||||
std::vector<std::string_view> 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
|
||||
|
Loading…
Reference in New Issue
Block a user