Skip to content

Commit

Permalink
refactor(cli): prefer Bump_Vector over std::vector/std::string
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 2aac3c5 commit 4baa187
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
9 changes: 6 additions & 3 deletions src/quick-lint-js/cli/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ bool Options::has_stdin() const {
}

bool Options::dump_errors(Output_Stream& out) const {
Monotonic_Allocator temporary_allocator("dump_errors");

bool have_errors = false;
if (this->lsp_server) {
if (this->exit_fail_on.is_user_provided()) {
Expand Down Expand Up @@ -269,14 +271,15 @@ bool Options::dump_errors(Output_Stream& out) const {
out.append_copy(u8'\n');
have_errors = true;
}
for (const std::string& error :
this->exit_fail_on.parse_errors("--exit-fail-on")) {
for (std::string_view error : this->exit_fail_on.parse_errors(
"--exit-fail-on", &temporary_allocator)) {
out.append_copy(u8"error: "_sv);
out.append_copy(to_string8_view(error));
out.append_copy(u8'\n');
have_errors = true;
}
for (const std::string& warning : this->exit_fail_on.parse_warnings()) {
for (std::string_view warning :
this->exit_fail_on.parse_warnings(&temporary_allocator)) {
out.append_copy(u8"warning: "_sv);
out.append_copy(to_string8_view(warning));
out.append_copy(u8'\n');
Expand Down
40 changes: 26 additions & 14 deletions src/quick-lint-js/diag/diag-code-list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,31 @@ void Compiled_Diag_Code_List::add(const Parsed_Diag_Code_List& diag_code_list) {
}
}

std::vector<std::string> Compiled_Diag_Code_List::parse_errors(
std::string_view cli_option_name) const {
std::vector<std::string> errors;
Span<std::string_view> Compiled_Diag_Code_List::parse_errors(
std::string_view cli_option_name, Monotonic_Allocator* allocator) const {
Bump_Vector<std::string_view, Monotonic_Allocator> errors("errors",
allocator);
if (this->has_missing_predicate_error_) {
errors.emplace_back(std::string(cli_option_name) +
" must be given at least one category or code");
// TODO(#1102): Make this code pretty.
Bump_Vector<char, Monotonic_Allocator> error("error", allocator);
error += cli_option_name;
error += " must be given at least one category or code"sv;
errors.emplace_back(error.release_to_string_view());
}
return errors;
return errors.release_to_span();
}

std::vector<std::string> Compiled_Diag_Code_List::parse_warnings() const {
std::vector<std::string> warnings;
auto check_category = [&warnings](std::string_view category) {
Span<std::string_view> Compiled_Diag_Code_List::parse_warnings(
Monotonic_Allocator* allocator) const {
Bump_Vector<std::string_view, Monotonic_Allocator> warnings("warnings",
allocator);
auto check_category = [&](std::string_view category) {
if (category != "all") {
warnings.emplace_back("unknown error category: ");
warnings.back().append(category);
// TODO(#1102): Make this code pretty.
Bump_Vector<char, Monotonic_Allocator> warning("warning", allocator);
warning += "unknown error category: "sv;
warning += category;
warnings.emplace_back(warning.release_to_string_view());
}
};

Expand All @@ -167,11 +176,14 @@ std::vector<std::string> Compiled_Diag_Code_List::parse_warnings() const {
}

for (std::string_view code : this->unknown_codes_) {
warnings.emplace_back("unknown error code: ");
warnings.back().append(code);
// TODO(#1102): Make this code pretty.
Bump_Vector<char, Monotonic_Allocator> warning("warning", allocator);
warning += "unknown error code: "sv;
warning += code;
warnings.emplace_back(warning.release_to_string_view());
}

return warnings;
return warnings.release_to_span();
}

bool Compiled_Diag_Code_List::is_present(Diag_Type type) const {
Expand Down
5 changes: 3 additions & 2 deletions src/quick-lint-js/diag/diag-code-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class Compiled_Diag_Code_List {
// Retains references to the std::string_view-s.
void add(const Parsed_Diag_Code_List&);

std::vector<std::string> parse_errors(std::string_view cli_option_name) const;
std::vector<std::string> parse_warnings() const;
Span<std::string_view> parse_errors(std::string_view cli_option_name,
Monotonic_Allocator* allocator) const;
Span<std::string_view> parse_warnings(Monotonic_Allocator* allocator) const;

bool is_present(Diag_Type) const;

Expand Down
34 changes: 18 additions & 16 deletions test/test-diag-code-list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ TEST_F(Test_Diag_Code_List, compiled_default_matches_all_errors) {
QLJS_X_DIAG_TYPE_NAMES
#undef QLJS_DIAG_TYPE_NAME

EXPECT_THAT(errors.parse_errors("--testoption"), IsEmpty());
EXPECT_THAT(errors.parse_warnings(), IsEmpty());
EXPECT_THAT(errors.parse_errors("--testoption", &this->allocator), IsEmpty());
EXPECT_THAT(errors.parse_warnings(&this->allocator), IsEmpty());
}

TEST_F(Test_Diag_Code_List, compiled_excluded_error_by_code) {
Expand All @@ -44,8 +44,8 @@ TEST_F(Test_Diag_Code_List, compiled_excluded_error_by_code) {
errors.is_present(Diag_Type::Diag_Big_Int_Literal_Contains_Decimal_Point))
<< "E0005 should be enabled";

EXPECT_THAT(errors.parse_errors("--testoption"), IsEmpty());
EXPECT_THAT(errors.parse_warnings(), IsEmpty());
EXPECT_THAT(errors.parse_errors("--testoption", &this->allocator), IsEmpty());
EXPECT_THAT(errors.parse_warnings(&this->allocator), IsEmpty());
}

TEST_F(Test_Diag_Code_List, compiled_excluded_then_included_error_by_code) {
Expand Down Expand Up @@ -85,8 +85,8 @@ TEST_F(Test_Diag_Code_List, compiled_exclude_all_matches_no_errors) {
QLJS_X_DIAG_TYPE_NAMES
#undef QLJS_DIAG_TYPE_NAME

EXPECT_THAT(errors.parse_errors("--testoption"), IsEmpty());
EXPECT_THAT(errors.parse_warnings(), IsEmpty());
EXPECT_THAT(errors.parse_errors("--testoption", &this->allocator), IsEmpty());
EXPECT_THAT(errors.parse_warnings(&this->allocator), IsEmpty());
}

TEST_F(Test_Diag_Code_List,
Expand Down Expand Up @@ -162,10 +162,11 @@ TEST_F(Test_Diag_Code_List, compiling_invalid_category_is_an_error) {
.excluded_categories = Span<const std::string_view>({"strawberry"}),
});

EXPECT_THAT(errors.parse_warnings(), UnorderedElementsAreArray({
"unknown error category: banana",
"unknown error category: strawberry",
}));
EXPECT_THAT(errors.parse_warnings(&this->allocator),
UnorderedElementsAreArray({
"unknown error category: banana",
"unknown error category: strawberry",
}));
}

TEST_F(Test_Diag_Code_List, compiling_invalid_code_is_an_error) {
Expand All @@ -175,10 +176,11 @@ TEST_F(Test_Diag_Code_List, compiling_invalid_code_is_an_error) {
.excluded_codes = Span<const std::string_view>({"E0000"}),
});

EXPECT_THAT(errors.parse_warnings(), UnorderedElementsAreArray({
"unknown error code: E9999",
"unknown error code: E0000",
}));
EXPECT_THAT(errors.parse_warnings(&this->allocator),
UnorderedElementsAreArray({
"unknown error code: E9999",
"unknown error code: E0000",
}));
}

TEST_F(Test_Diag_Code_List, compiling_empty_parsed_diag_code_list_is_an_error) {
Expand All @@ -191,9 +193,9 @@ TEST_F(Test_Diag_Code_List, compiling_empty_parsed_diag_code_list_is_an_error) {
.excluded_codes = Span<const std::string_view>({"E0003"}),
});

EXPECT_THAT(errors.parse_warnings(), IsEmpty());
EXPECT_THAT(errors.parse_warnings(&this->allocator), IsEmpty());
EXPECT_THAT(
errors.parse_errors("--testoption"),
errors.parse_errors("--testoption", &this->allocator),
ElementsAreArray(
{"--testoption must be given at least one category or code"}));
}
Expand Down

0 comments on commit 4baa187

Please sign in to comment.