-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(diag): create new Diag_List type; use in one spot
I don't like Buffering_Diag_Reporter. Create a new Diag_List type, decoupled from Diag_Reporter, which we can use to "just" store a list of diagnostics (hence its name). To test the waters, use Diag_List in place of Buffering_Diag_Reporter in one spot in the Lexer.
- Loading branch information
Showing
9 changed files
with
395 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (C) 2020 Matthew "strager" Glazar | ||
// See end of file for extended copyright information. | ||
|
||
#include <quick-lint-js/diag/diag-list.h> | ||
#include <quick-lint-js/port/memory-resource.h> | ||
#include <quick-lint-js/util/algorithm.h> | ||
|
||
namespace quick_lint_js { | ||
struct Diag_List::Node : public Node_Base { | ||
union Underlying_Diag { | ||
explicit Underlying_Diag() {} | ||
|
||
#define QLJS_DIAG_TYPE_NAME(name) \ | ||
::quick_lint_js::name name; \ | ||
static_assert(std::is_trivially_copyable_v<::quick_lint_js::name>, \ | ||
#name " should be trivially copyable"); | ||
QLJS_X_DIAG_TYPE_NAMES | ||
#undef QLJS_DIAG_TYPE_NAME | ||
}; | ||
|
||
Underlying_Diag data; | ||
}; | ||
|
||
Diag_List::Diag_List(Memory_Resource *memory) : memory_(memory) {} | ||
|
||
Diag_List::~Diag_List() { | ||
// Leak. this->memory should be a Linked_Bump_Allocator managed by the caller. | ||
} | ||
|
||
Diag_List::Rewind_State Diag_List::prepare_for_rewind() { | ||
return Rewind_State{ | ||
.first_ = this->first_, | ||
.last_ = this->last_, | ||
}; | ||
} | ||
|
||
void Diag_List::rewind(Rewind_State &&r) { | ||
// Leak nodes between r.last and this->last_. this->memory should be a | ||
// Linked_Bump_Allocator managed by the caller. | ||
this->first_ = r.first_; | ||
this->last_ = r.last_; | ||
} | ||
|
||
#define QLJS_DIAG_TYPE_NAME(name) \ | ||
void Diag_List::add(name diag) { \ | ||
this->add_impl<sizeof(diag)>(Diag_Type::name, &diag); \ | ||
} | ||
QLJS_X_DIAG_TYPE_NAMES | ||
#undef QLJS_DIAG_TYPE_NAME | ||
|
||
template <std::size_t Diag_Size> | ||
void Diag_List::add_impl(Diag_Type type, void *diag) { | ||
Node *node = this->memory_->new_object<Node>(); | ||
node->next = nullptr; | ||
node->type = type; | ||
std::memcpy(&node->data, diag, Diag_Size); | ||
if (this->last_ == nullptr) { | ||
this->first_ = node; | ||
} else { | ||
this->last_->next = node; | ||
} | ||
this->last_ = node; | ||
} | ||
|
||
bool Diag_List::reported_any_diagnostic_except_since( | ||
std::initializer_list<Diag_Type> ignored_types, const Rewind_State &r) { | ||
for (Node_Base *node = r.last_ == nullptr ? this->first_ : r.last_; | ||
node != nullptr; node = node->next) { | ||
if (!contains(ignored_types, node->type)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
// quick-lint-js finds bugs in JavaScript programs. | ||
// Copyright (C) 2020 Matthew "strager" Glazar | ||
// | ||
// This file is part of quick-lint-js. | ||
// | ||
// quick-lint-js is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// quick-lint-js is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (C) 2020 Matthew "strager" Glazar | ||
// See end of file for extended copyright information. | ||
|
||
#pragma once | ||
|
||
#include <quick-lint-js/diag/diagnostic-types.h> | ||
#include <quick-lint-js/port/memory-resource.h> | ||
|
||
namespace quick_lint_js { | ||
// Internally, Diag_List is implemented as a singly linked list. | ||
class Diag_List { | ||
private: | ||
// Node_Base is split from Node to improve compile times. The union inside | ||
// Node compiles slowly. | ||
// | ||
// Node_Base needs to be in the header for an efficient implementation of | ||
// for_each. | ||
struct Node_Base { | ||
Node_Base *next; | ||
Diag_Type type; | ||
|
||
// Returns &static_cast<Node*>(this)->data. | ||
void *get_data() { return &this[1]; } | ||
}; | ||
struct Node; | ||
|
||
public: | ||
struct Rewind_State { | ||
// Private to Diag_List. Do not use. | ||
Node_Base *first_; | ||
Node_Base *last_; | ||
}; | ||
|
||
explicit Diag_List(Memory_Resource *); | ||
|
||
Diag_List(Diag_List &&) = delete; | ||
Diag_List &operator=(Diag_List &&) = delete; | ||
|
||
~Diag_List(); | ||
|
||
#define QLJS_DIAG_TYPE_NAME(name) void add(name diag); | ||
QLJS_X_DIAG_TYPE_NAMES | ||
#undef QLJS_DIAG_TYPE_NAME | ||
|
||
Rewind_State prepare_for_rewind(); | ||
|
||
void rewind(Rewind_State &&r); | ||
|
||
// Func must have the following signature: | ||
// | ||
// void(Diag_Type type, void* diag); | ||
template <class Func> | ||
void for_each(Func &&callback) { | ||
for (Node_Base *node = this->first_; node != nullptr; node = node->next) { | ||
callback(node->type, node->get_data()); | ||
} | ||
} | ||
|
||
// reporter->report_impl must be a member function with the following | ||
// signature: | ||
// | ||
// void report_impl(Diag_Type type, void* diag); | ||
// | ||
// TODO(strager): Delete this function. | ||
template <class Reporter> | ||
void copy_into(Reporter *reporter) { | ||
this->for_each([&reporter](Diag_Type type, void *diag) -> void { | ||
reporter->report_impl(type, diag); | ||
}); | ||
} | ||
|
||
bool reported_any_diagnostic_except_since( | ||
std::initializer_list<Diag_Type> ignored_types, const Rewind_State &); | ||
|
||
private: | ||
template <std::size_t Diag_Size> | ||
void add_impl(Diag_Type type, void *diag); | ||
|
||
Memory_Resource *memory_; | ||
Node_Base *first_ = nullptr; | ||
Node_Base *last_ = nullptr; | ||
}; | ||
} | ||
|
||
// quick-lint-js finds bugs in JavaScript programs. | ||
// Copyright (C) 2020 Matthew "strager" Glazar | ||
// | ||
// This file is part of quick-lint-js. | ||
// | ||
// quick-lint-js is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// quick-lint-js is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.