Skip to content

Commit

Permalink
refactor(cli): prefer Bump_Vector over std::vector
Browse files Browse the repository at this point in the history
Reduce our use of std::vector to reduce binary bloat:
#689
  • Loading branch information
strager committed Oct 29, 2023
1 parent 9a6740b commit 1723455
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
40 changes: 27 additions & 13 deletions src/quick-lint-js/cli/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ QLJS_WARNING_IGNORE_GCC("-Wshadow=local")
using namespace std::literals::string_view_literals;

namespace quick_lint_js {
Options parse_options(int argc, char** argv,
[[maybe_unused]] Monotonic_Allocator* allocator) {
Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) {
Options o;

Monotonic_Allocator temporary_allocator("parse_options");
Expand All @@ -36,6 +35,15 @@ Options parse_options(int argc, char** argv,
const char* arg_var;
} next_vim_file_bufnr;

Bump_Vector<File_To_Lint, Monotonic_Allocator> files_to_lint("files_to_lint",
allocator);
Bump_Vector<const char*, Monotonic_Allocator> error_unrecognized_options(
"error_unrecognized_options", allocator);
Bump_Vector<const char*, Monotonic_Allocator> warning_vim_bufnr_without_file(
"warning_vim_bufnr_without_file", allocator);
Bump_Vector<const char*, Monotonic_Allocator> warning_language_without_file(
"warning_language_without_file", allocator);

const char* next_path_for_config_search = nullptr;
const char* active_config_file = nullptr;
Raw_Input_File_Language language = Raw_Input_File_Language::default_;
Expand All @@ -52,7 +60,7 @@ Options parse_options(int argc, char** argv,
.language = language,
.is_stdin = is_stdin,
.vim_bufnr = next_vim_file_bufnr.number};
o.files_to_lint.emplace_back(file);
files_to_lint.emplace_back(file);
}
if (is_stdin) {
has_stdin = true;
Expand Down Expand Up @@ -91,7 +99,7 @@ Options parse_options(int argc, char** argv,
} else if (arg_value == "emacs-lisp"sv) {
o.output_format = Output_Format::emacs_lisp;
} else {
o.error_unrecognized_options.emplace_back(arg_value);
error_unrecognized_options.emplace_back(arg_value);
}
}

Expand All @@ -103,7 +111,7 @@ Options parse_options(int argc, char** argv,
} else if (arg_value == "never"sv) {
o.diagnostic_hyperlinks = Option_When::never;
} else {
o.error_unrecognized_options.emplace_back(arg_value);
error_unrecognized_options.emplace_back(arg_value);
}
}

Expand All @@ -115,7 +123,7 @@ Options parse_options(int argc, char** argv,
QLJS_OPTION(const char* arg_value, "--language"sv) {
o.has_language = true;
if (unused_language_option) {
o.warning_language_without_file.emplace_back(unused_language_option);
warning_language_without_file.emplace_back(unused_language_option);
}
unused_language_option = arg_value;
if (arg_value == "default"sv) {
Expand All @@ -133,7 +141,7 @@ Options parse_options(int argc, char** argv,
} else if (arg_value == "experimental-typescript-jsx"sv) {
language = Raw_Input_File_Language::typescript_jsx;
} else {
o.error_unrecognized_options.emplace_back(arg_value);
error_unrecognized_options.emplace_back(arg_value);
}
}

Expand All @@ -150,11 +158,11 @@ Options parse_options(int argc, char** argv,
int bufnr;
if (parse_integer_exact(std::string_view(arg_value), bufnr) !=
Parse_Integer_Exact_Error::ok) {
o.error_unrecognized_options.emplace_back(arg_value);
error_unrecognized_options.emplace_back(arg_value);
continue;
}
if (next_vim_file_bufnr.number != std::nullopt) {
o.warning_vim_bufnr_without_file.emplace_back(
warning_vim_bufnr_without_file.emplace_back(
next_vim_file_bufnr.arg_var);
}
next_vim_file_bufnr.number = bufnr;
Expand All @@ -173,26 +181,32 @@ Options parse_options(int argc, char** argv,
QLJS_FLAG("--stdin"sv, ""sv) { add_stdin_file(); }

QLJS_UNRECOGNIZED_OPTION(const char* unrecognized) {
o.error_unrecognized_options.emplace_back(unrecognized);
error_unrecognized_options.emplace_back(unrecognized);
goto done_parsing_options;
}
}
done_parsing_options:

if (unused_language_option) {
o.warning_language_without_file.emplace_back(unused_language_option);
warning_language_without_file.emplace_back(unused_language_option);
}
if (next_vim_file_bufnr.number != std::nullopt) {
o.warning_vim_bufnr_without_file.emplace_back(next_vim_file_bufnr.arg_var);
warning_vim_bufnr_without_file.emplace_back(next_vim_file_bufnr.arg_var);
}
if (o.path_for_stdin != nullptr) {
for (File_To_Lint& file : o.files_to_lint) {
for (File_To_Lint& file : files_to_lint) {
if (file.path_for_config_search == nullptr) {
file.path_for_config_search = o.path_for_stdin;
}
}
}

o.files_to_lint = files_to_lint.release_to_span();
o.error_unrecognized_options = error_unrecognized_options.release_to_span();
o.warning_vim_bufnr_without_file =
warning_vim_bufnr_without_file.release_to_span();
o.warning_language_without_file =
warning_language_without_file.release_to_span();
return o;
}

Expand Down
10 changes: 5 additions & 5 deletions src/quick-lint-js/cli/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <optional>
#include <quick-lint-js/container/monotonic-allocator.h>
#include <quick-lint-js/diag/diag-code-list.h>
#include <vector>
#include <quick-lint-js/port/span.h>

namespace quick_lint_js {
class Output_Stream;
Expand Down Expand Up @@ -65,13 +65,13 @@ struct Options {
bool snarky = false;
Output_Format output_format = Output_Format::default_format;
Option_When diagnostic_hyperlinks = Option_When::auto_;
std::vector<File_To_Lint> files_to_lint;
Span<const File_To_Lint> files_to_lint;
Compiled_Diag_Code_List exit_fail_on;
const char *path_for_stdin = nullptr;

std::vector<const char *> error_unrecognized_options;
std::vector<const char *> warning_vim_bufnr_without_file;
std::vector<const char *> warning_language_without_file;
Span<const char *const> error_unrecognized_options;
Span<const char *const> warning_vim_bufnr_without_file;
Span<const char *const> warning_language_without_file;
bool has_multiple_stdin = false;
bool has_config_file = false;
bool has_language = false;
Expand Down
22 changes: 13 additions & 9 deletions test/test-options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,16 +885,18 @@ TEST_F(Test_Options, invalid_option) {
TEST_F(Test_Options, dump_errors) {
{
Options o;
o.error_unrecognized_options.clear();
o.error_unrecognized_options = Span<const char *const>();

Dumped_Errors errors = dump_errors(o);
EXPECT_FALSE(errors.have_errors);
EXPECT_EQ(errors.output, u8"");
}

{
const char *unrecognized_options[] = {"--bad-option"};
Options o;
o.error_unrecognized_options.push_back("--bad-option");
o.error_unrecognized_options =
Span<const char *const>(unrecognized_options);

Dumped_Errors errors = dump_errors(o);
EXPECT_TRUE(errors.have_errors);
Expand Down Expand Up @@ -965,17 +967,19 @@ TEST_F(Test_Options, dump_errors) {
}

{
const File_To_Lint file = {
.path = "file.js",
.config_file = nullptr,
.language = Raw_Input_File_Language::default_,
.is_stdin = false,
.vim_bufnr = std::optional<int>(),
const File_To_Lint files[] = {
File_To_Lint{
.path = "file.js",
.config_file = nullptr,
.language = Raw_Input_File_Language::default_,
.is_stdin = false,
.vim_bufnr = std::optional<int>(),
},
};

Options o;
o.lsp_server = true;
o.files_to_lint.emplace_back(file);
o.files_to_lint = Span<const File_To_Lint>(files);

Dumped_Errors errors = dump_errors(o);
EXPECT_FALSE(errors.have_errors);
Expand Down

0 comments on commit 1723455

Please sign in to comment.