Skip to content

Commit

Permalink
refactor(diag): allow Diag_List to be moved
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Sep 22, 2024
1 parent 4550cae commit 1783b39
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/quick-lint-js/diag/diag-list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ static_assert(alignof(Diag_List::Node) == alignof(Diag_List::Node_Base),

Diag_List::Diag_List(Memory_Resource *memory) : memory_(memory) {}

Diag_List::Diag_List(Diag_List &&other)
: memory_(other.memory_), first_(other.first_), last_(other.last_) {
other.clear();
}

Diag_List::~Diag_List() { this->clear(); }

void Diag_List::add_many(const Diag_List &other) {
Expand Down
7 changes: 5 additions & 2 deletions src/quick-lint-js/diag/diag-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ class Diag_List {

explicit Diag_List(Memory_Resource *);

Diag_List(Diag_List &&) = delete;
Diag_List &operator=(Diag_List &&) = delete;
Diag_List(const Diag_List &) = delete;
Diag_List &operator=(const Diag_List &) = delete;

Diag_List(Diag_List &&);
Diag_List &operator=(Diag_List &&) = delete; // TODO(strager)

~Diag_List();

Expand Down
36 changes: 36 additions & 0 deletions test/test-diag-list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <quick-lint-js/diag-matcher.h>
#include <quick-lint-js/diag/diag-list.h>
#include <quick-lint-js/diag/diagnostic-types.h>
#include <quick-lint-js/diagnostic-assertion.h>
#include <quick-lint-js/fe/lex.h>
#include <quick-lint-js/identifier-support.h>
#include <type_traits>
Expand Down Expand Up @@ -189,6 +190,41 @@ TEST(Test_Diag_List, pretty_print_one_diag) {
EXPECT_EQ(ss.str(), "1 diagnostic: {\n Diag_Let_With_No_Bindings,\n}");
}
}

TEST(Test_Diag_List, moving_diag_list_clears_original) {
Linked_Bump_Allocator memory("test");
Diag_List diags_1(&memory);
Padded_String code(u8"hello"_sv);
diags_1.add(Diag_Let_With_No_Bindings{.where = span_of(code)});
Diag_List diags_2 = std::move(diags_1);
EXPECT_EQ(diags_1.size(), 0);
}

TEST(Test_Diag_List, moving_diag_list_keeps_single_diag) {
Linked_Bump_Allocator memory("test");
Diag_List diags_1(&memory);
Padded_String code(u8"hello"_sv);
diags_1.add(Diag_Let_With_No_Bindings{.where = span_of(code)});

Diag_List diags_2 = std::move(diags_1);
EXPECT_EQ(diags_2.size(), 1);
}

TEST(Test_Diag_List, moving_diag_list_keeps_multiple_diags) {
Linked_Bump_Allocator memory("test");
Diag_List diags_1(&memory);
Padded_String code(u8"hello"_sv);
diags_1.add(Diag_Let_With_No_Bindings{.where = span_of(code)});
diags_1.add(Diag_Expected_Parenthesis_Around_If_Condition{
.where = span_of(code),
.token = u8')',
});

Diag_List diags_2 = std::move(diags_1);
assert_diagnostics(&code, diags_2,
{u8"Diag_Let_With_No_Bindings"_diag,
u8"Diag_Expected_Parenthesis_Around_If_Condition"_diag});
}
}
}

Expand Down

0 comments on commit 1783b39

Please sign in to comment.