Skip to content

Commit

Permalink
refactor(cli): work around GCC false positive warning
Browse files Browse the repository at this point in the history
GCC complains that arg_var might not be initialized:

    vector.h:238:23: error: '*((void*)& next_vim_file_bufnr +8)' may be used uninitialized in this function [-Werror=maybe-uninitialized]
    238 |     this->data_end_ = new (this->data_end_) T(std::forward<Args>(args)...);
        |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    options.cpp:36:5: note: '*((void*)& next_vim_file_bufnr +8)' was declared here
    36 |   } next_vim_file_bufnr;
        |     ^~~~~~~~~~~~~~~~~~~

This is a false positive in GCC. Whenever arg_var is pushed onto a
Vector, we check if number is non-null. Whenever number is non-null,
arg_var is initialized.

Work around the false positive by refactoring the code such that
arg_var and number are initialized together.
  • Loading branch information
strager committed Nov 5, 2023
1 parent f8fab2a commit 0f73dff
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/quick-lint-js/cli/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) {

Monotonic_Allocator temporary_allocator("parse_options");

struct {
std::optional<int> number;
struct Next_Vim_File_Bufnr {
int number;
const char* arg_var;
} next_vim_file_bufnr;
};
std::optional<Next_Vim_File_Bufnr> next_vim_file_bufnr;

Bump_Vector<File_To_Lint> files_to_lint("files_to_lint", allocator);
Bump_Vector<const char*> error_unrecognized_options(
Expand All @@ -53,12 +54,16 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) {
if (is_stdin && has_stdin) {
o.has_multiple_stdin = true;
} else {
File_To_Lint file{.path = path,
.config_file = active_config_file,
.path_for_config_search = next_path_for_config_search,
.language = language,
.is_stdin = is_stdin,
.vim_bufnr = next_vim_file_bufnr.number};
File_To_Lint file{
.path = path,
.config_file = active_config_file,
.path_for_config_search = next_path_for_config_search,
.language = language,
.is_stdin = is_stdin,
.vim_bufnr = next_vim_file_bufnr.has_value()
? std::optional<int>(next_vim_file_bufnr->number)
: std::nullopt,
};
files_to_lint.emplace_back(file);
}
if (is_stdin) {
Expand All @@ -67,7 +72,7 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) {

unused_language_option = nullptr;
next_path_for_config_search = nullptr;
next_vim_file_bufnr.number = std::nullopt;
next_vim_file_bufnr = std::nullopt;
};

auto add_stdin_file = [&]() { add_file("<stdin>", /*is_stdin=*/true); };
Expand Down Expand Up @@ -160,12 +165,14 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) {
error_unrecognized_options.emplace_back(arg_value);
continue;
}
if (next_vim_file_bufnr.number != std::nullopt) {
if (next_vim_file_bufnr.has_value()) {
warning_vim_bufnr_without_file.emplace_back(
next_vim_file_bufnr.arg_var);
next_vim_file_bufnr->arg_var);
}
next_vim_file_bufnr.number = bufnr;
next_vim_file_bufnr.arg_var = arg_value;
next_vim_file_bufnr = Next_Vim_File_Bufnr{
.number = bufnr,
.arg_var = arg_value,
};
}

QLJS_OPTION(const char* arg_value, "--exit-fail-on"sv) {
Expand All @@ -189,8 +196,8 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) {
if (unused_language_option) {
warning_language_without_file.emplace_back(unused_language_option);
}
if (next_vim_file_bufnr.number != std::nullopt) {
warning_vim_bufnr_without_file.emplace_back(next_vim_file_bufnr.arg_var);
if (next_vim_file_bufnr.has_value()) {
warning_vim_bufnr_without_file.emplace_back(next_vim_file_bufnr->arg_var);
}
if (o.path_for_stdin != nullptr) {
for (File_To_Lint& file : files_to_lint) {
Expand Down

0 comments on commit 0f73dff

Please sign in to comment.