Skip to content

Commit

Permalink
refactor(fe): Diag_List_Diag_Reporter->Diag_List in Variable_Analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Aug 20, 2024
1 parent 6b22a19 commit 40a07f9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 84 deletions.
7 changes: 3 additions & 4 deletions benchmark/benchmark-lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void benchmark_lint(benchmark::State &state) {

for (auto _ : state) {
Monotonic_Allocator memory("benchmark");
Diag_List_Diag_Reporter diags(&memory);
Diag_List diags(&memory);
Variable_Analyzer l(&diags, &config.globals(), var_options);
visitor.copy_into(l);

Expand Down Expand Up @@ -73,8 +73,7 @@ void benchmark_parse_and_lint(benchmark::State &state) {
Configuration config;
for (auto _ : state) {
Parser p(&source, p_options);
Variable_Analyzer l(&p.diag_list_diag_reporter(), &config.globals(),
var_options);
Variable_Analyzer l(&p.diags(), &config.globals(), var_options);
p.parse_and_visit_module(l);
}
}
Expand Down Expand Up @@ -114,7 +113,7 @@ void benchmark_undeclared_variable_references(benchmark::State &state) {
Variable_Analyzer_Options var_options;
for (auto _ : state) {
Monotonic_Allocator memory("benchmark");
Diag_List_Diag_Reporter diags(&memory);
Diag_List diags(&memory);
Variable_Analyzer l(&diags, &globals, var_options);
for (Identifier &variable_use : variable_use_identifiers) {
l.visit_variable_use(variable_use);
Expand Down
2 changes: 1 addition & 1 deletion src/quick-lint-js/fe/linter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void parse_and_lint(Padded_String_View code, Diag_Reporter& reporter,

Parser p(code, parser_options);
Variable_Analyzer var_analyzer(
&p.diag_list_diag_reporter(), &options.configuration->globals(),
&p.diags(), &options.configuration->globals(),
Variable_Analyzer_Options{
.allow_deleting_typescript_variable = !parser_options.typescript,
.eval_can_declare_variables = !parser_options.typescript,
Expand Down
60 changes: 27 additions & 33 deletions src/quick-lint-js/fe/variable-analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,9 @@ bool is_runtime_and_type(Variable_Kind);
}

Variable_Analyzer::Variable_Analyzer(
Diag_List_Diag_Reporter *diag_reporter,
const Global_Declared_Variable_Set *global_variables,
Diag_List *out_diags, const Global_Declared_Variable_Set *global_variables,
Variable_Analyzer_Options options)
: global_scope_(global_variables),
diag_reporter_(diag_reporter),
options_(options) {}
: global_scope_(global_variables), diags_(out_diags), options_(options) {}

void Variable_Analyzer::visit_enter_block_scope() { this->scopes_.push(); }

Expand Down Expand Up @@ -438,10 +435,9 @@ void Variable_Analyzer::visit_variable_assertion_signature_use(
if (var) {
// FIXME(strager): Should we mark the parameter as used?
} else {
this->diag_reporter_->report(
Diag_Use_Of_Undeclared_Parameter_In_Assertion_Signature{
.name = name.span(),
});
this->diags_->add(Diag_Use_Of_Undeclared_Parameter_In_Assertion_Signature{
.name = name.span(),
});
}
}

Expand All @@ -464,7 +460,7 @@ void Variable_Analyzer::visit_variable_delete_use(
this->add_variable_use_to_current_scope(std::move(used_var));
}
} else {
this->diag_reporter_->report(Diag_TypeScript_Delete_Cannot_Delete_Variables{
this->diags_->add(Diag_TypeScript_Delete_Cannot_Delete_Variables{
.delete_expression =
Source_Code_Span(delete_keyword.begin(), name.span().end()),
});
Expand Down Expand Up @@ -502,10 +498,9 @@ void Variable_Analyzer::visit_variable_type_predicate_use(Identifier name) {
if (var) {
// FIXME(strager): Should we mark the parameter as used?
} else {
this->diag_reporter_->report(
Diag_Use_Of_Undeclared_Parameter_In_Type_Predicate{
.name = name.span(),
});
this->diags_->add(Diag_Use_Of_Undeclared_Parameter_In_Type_Predicate{
.name = name.span(),
});
}
}

Expand Down Expand Up @@ -612,22 +607,22 @@ void Variable_Analyzer::visit_end_of_module() {
if (!is_variable_declared(used_var)) {
switch (used_var.kind) {
case Used_Variable_Kind::assignment:
this->diag_reporter_->report(Diag_Assignment_To_Undeclared_Variable{
this->diags_->add(Diag_Assignment_To_Undeclared_Variable{
.assignment = used_var.name.span()});
break;
case Used_Variable_Kind::_delete:
// TODO(strager): Report a warning if the global variable is not
// deletable.
break;
case Used_Variable_Kind::type:
this->diag_reporter_->report(
this->diags_->add(
Diag_Use_Of_Undeclared_Type{.name = used_var.name.span()});
break;
case Used_Variable_Kind::_export:
case Used_Variable_Kind::_export_default:
case Used_Variable_Kind::use:
case Used_Variable_Kind::use_in_type:
this->diag_reporter_->report(
this->diags_->add(
Diag_Use_Of_Undeclared_Variable{.name = used_var.name.span()});
break;
case Used_Variable_Kind::_typeof:
Expand Down Expand Up @@ -780,7 +775,7 @@ void Variable_Analyzer::propagate_variable_declarations_to_parent_scope() {
(already_declared_variable->kind == Variable_Kind::_const ||
already_declared_variable->kind == Variable_Kind::_let ||
already_declared_variable->kind == Variable_Kind::_var)) {
this->diag_reporter_->report(Diag_Unused_Variable_Shadows{
this->diags_->add(Diag_Unused_Variable_Shadows{
.shadowing_declaration = var.declaration.span(),
.shadowed_declaration =
already_declared_variable->declaration.span(),
Expand Down Expand Up @@ -851,18 +846,18 @@ void Variable_Analyzer::report_error_if_assignment_is_illegal(
case Variable_Kind::_import_alias:
case Variable_Kind::_namespace:
if (is_global_variable) {
this->diag_reporter_->report(Diag_Assignment_To_Const_Global_Variable{
this->diags_->add(Diag_Assignment_To_Const_Global_Variable{
.assignment = assignment.span()});
} else {
if (is_assigned_before_declaration) {
this->diag_reporter_->report(
this->diags_->add(
Diag_Assignment_To_Const_Variable_Before_Its_Declaration{
.declaration = declaration->span(),
.assignment = assignment.span(),
.var_kind = kind,
});
} else {
this->diag_reporter_->report(Diag_Assignment_To_Const_Variable{
this->diags_->add(Diag_Assignment_To_Const_Variable{
.declaration = declaration->span(),
.assignment = assignment.span(),
.var_kind = kind});
Expand All @@ -879,7 +874,7 @@ void Variable_Analyzer::report_error_if_assignment_is_illegal(
// HACK(#1141): Avoid false positives in TypeScript by disabling this
// diagnostic for now.
if (!this->options_.import_variable_can_be_runtime_or_type) {
this->diag_reporter_->report(Diag_Assignment_To_Imported_Variable{
this->diags_->add(Diag_Assignment_To_Imported_Variable{
.declaration = declaration->span(),
.assignment = assignment.span(),
.var_kind = kind,
Expand All @@ -895,7 +890,7 @@ void Variable_Analyzer::report_error_if_assignment_is_illegal(
QLJS_WARNING_PUSH
QLJS_WARNING_IGNORE_GCC("-Wnull-dereference")

this->diag_reporter_->report(Diag_Assignment_Before_Variable_Declaration{
this->diags_->add(Diag_Assignment_Before_Variable_Declaration{
.assignment = assignment.span(),
.declaration = declaration->span(),
});
Expand Down Expand Up @@ -947,7 +942,7 @@ void Variable_Analyzer::report_errors_for_variable_use(
used_var.kind == Used_Variable_Kind::_delete) {
// TODO(strager): What if the variable was parenthesized? We should
// include the closing parenthesis.
this->diag_reporter_->report(Diag_Redundant_Delete_Statement_On_Variable{
this->diags_->add(Diag_Redundant_Delete_Statement_On_Variable{
.delete_expression = Source_Code_Span(used_var.delete_keyword_begin,
used_var.name.span().end()),
});
Expand All @@ -958,11 +953,10 @@ void Variable_Analyzer::report_errors_for_variable_use(
declared.declaration_scope ==
Declared_Variable_Scope::declared_in_descendant_scope &&
used_var.kind == Used_Variable_Kind::use) {
this->diag_reporter_->report(
Diag_Function_Call_Before_Declaration_In_Block_Scope{
.use = used_var.name.span(),
.declaration = declared.declaration.span(),
});
this->diags_->add(Diag_Function_Call_Before_Declaration_In_Block_Scope{
.use = used_var.name.span(),
.declaration = declared.declaration.span(),
});
}

if (use_is_before_declaration) {
Expand All @@ -981,7 +975,7 @@ void Variable_Analyzer::report_errors_for_variable_use(
if (declared.kind == Variable_Kind::_class ||
declared.kind == Variable_Kind::_const ||
declared.kind == Variable_Kind::_let) {
this->diag_reporter_->report(Diag_Variable_Used_Before_Declaration{
this->diags_->add(Diag_Variable_Used_Before_Declaration{
.use = used_var.name.span(),
.declaration = declared.declaration.span(),
});
Expand All @@ -995,7 +989,7 @@ void Variable_Analyzer::report_errors_for_variable_use(
break;
case Used_Variable_Kind::type:
if (declared.kind == Variable_Kind::_generic_parameter) {
this->diag_reporter_->report(Diag_Variable_Used_Before_Declaration{
this->diags_->add(Diag_Variable_Used_Before_Declaration{
.use = used_var.name.span(),
.declaration = declared.declaration.span(),
});
Expand Down Expand Up @@ -1220,11 +1214,11 @@ bool Variable_Analyzer::report_error_if_variable_declaration_conflicts(
bool already_declared_is_global_variable =
already_declared_var.name == nullptr;
if (already_declared_is_global_variable) {
this->diag_reporter_->report(Diag_Redeclaration_Of_Global_Variable{
this->diags_->add(Diag_Redeclaration_Of_Global_Variable{
.redeclaration = newly_declared_var.declaration.span(),
});
} else {
this->diag_reporter_->report(Diag_Redeclaration_Of_Variable{
this->diags_->add(Diag_Redeclaration_Of_Variable{
.redeclaration = newly_declared_var.declaration.span(),
.original_declaration = already_declared_var.name->span(),
});
Expand Down
7 changes: 3 additions & 4 deletions src/quick-lint-js/fe/variable-analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
#include <vector>

namespace quick_lint_js {
class Diag_List_Diag_Reporter;
class Diag_Reporter;
class Diag_List;
class Global_Declared_Variable_Set;
struct Global_Declared_Variable;

Expand Down Expand Up @@ -57,7 +56,7 @@ struct Variable_Analyzer_Options {
class Variable_Analyzer final : public Parse_Visitor_Base {
public:
explicit Variable_Analyzer(
Diag_List_Diag_Reporter *diag_reporter,
Diag_List *out_diags,
const Global_Declared_Variable_Set *global_variables,
Variable_Analyzer_Options options);

Expand Down Expand Up @@ -415,7 +414,7 @@ class Variable_Analyzer final : public Parse_Visitor_Base {
// with the 'declare' keyword.
unsigned typescript_ambient_context_depth_ = 0;

Diag_Reporter *diag_reporter_;
Diag_List *diags_;

Variable_Analyzer_Options options_;
};
Expand Down
3 changes: 1 addition & 2 deletions test/quick-lint-js/variable-analyzer-support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ void test_parse_and_analyze(String8_View input,
Padded_String code(input);

Parser p(&code, options.parse_options);
Variable_Analyzer var_analyzer(&p.diag_list_diag_reporter(), &globals,
options.analyze_options);
Variable_Analyzer var_analyzer(&p.diags(), &globals, options.analyze_options);
p.parse_and_visit_module(var_analyzer);

assert_diagnostics(&code, p.diags(), diag_assertions, caller);
Expand Down
Loading

0 comments on commit 40a07f9

Please sign in to comment.