From 4fd458900bf79c4b85b79e7bd61ab9badf09c20f Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sat, 12 Aug 2017 09:54:26 -0500 Subject: [PATCH] Silence unused result warnings on newer compilers Newer versions of GCC and Clang are not satisfied by a cast to void, this fix is adapted from glibc's solution. New wrapper function ignore_result should be used when a function with explicit _unused_attribute_ wrapper is called whose result will not be handled. --- src/common.cpp | 6 +++--- src/common.h | 25 +++++++++++++++++++------ src/env_universal_common.cpp | 4 ++-- src/path.cpp | 2 +- src/reader.cpp | 8 ++++---- src/wutil.cpp | 2 +- 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index b52dbf076..be5c6bf43 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -616,14 +616,14 @@ void debug_safe(int level, const char *msg, const char *param1, const char *para const char *end = strchr(cursor, '%'); if (end == NULL) end = cursor + strlen(cursor); - (void)write(STDERR_FILENO, cursor, end - cursor); + ignore_result(write(STDERR_FILENO, cursor, end - cursor)); if (end[0] == '%' && end[1] == 's') { // Handle a format string. assert(param_idx < sizeof params / sizeof *params); const char *format = params[param_idx++]; if (!format) format = "(null)"; - (void)write(STDERR_FILENO, format, strlen(format)); + ignore_result(write(STDERR_FILENO, format, strlen(format))); cursor = end + 2; } else if (end[0] == '\0') { // Must be at the end of the string. @@ -635,7 +635,7 @@ void debug_safe(int level, const char *msg, const char *param1, const char *para } // We always append a newline. - (void)write(STDERR_FILENO, "\n", 1); + ignore_result(write(STDERR_FILENO, "\n", 1)); errno = errno_old; } diff --git a/src/common.h b/src/common.h index aa0085378..46953359f 100644 --- a/src/common.h +++ b/src/common.h @@ -199,12 +199,12 @@ extern bool has_working_tty_timestamps; // from within a `switch` block. As of the time I'm writing this oclint doesn't recognize the // `__attribute__((noreturn))` on the exit_without_destructors() function. // TODO: we use C++11 [[noreturn]] now, does that change things? -#define FATAL_EXIT() \ - { \ - char exit_read_buff; \ - show_stackframe(L'E'); \ - (void)read(0, &exit_read_buff, 1); \ - exit_without_destructors(1); \ +#define FATAL_EXIT() \ + { \ + char exit_read_buff; \ + show_stackframe(L'E'); \ + ignore_result(read(0, &exit_read_buff, 1)); \ + exit_without_destructors(1); \ } /// Exit the program at once after emitting an error message and stack trace if possible. @@ -876,4 +876,17 @@ enum { /// like an unrecognized flag, missing or too many arguments, an invalid integer, etc. But STATUS_INVALID_ARGS = 121, }; + +/* Normally casting an expression to void discards its value, but GCC + versions 3.4 and newer have __attribute__ ((__warn_unused_result__)) + which may cause unwanted diagnostics in that case. Use __typeof__ + and __extension__ to work around the problem, if the workaround is + known to be needed. */ +#if 3 < __GNUC__ + (4 <= __GNUC_MINOR__) +# define ignore_result(x) \ + (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; })) +#else +# define ignore_result(x) ((void) (x)) +#endif + #endif diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index 00228113f..eac8b1e10 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -1209,7 +1209,7 @@ class universal_notifier_named_pipe_t : public universal_notifier_t { // would cause us to hang! size_t read_amt = 64 * 1024; void *buff = malloc(read_amt); - (void)read(this->pipe_fd, buff, read_amt); + ignore_result(read(this->pipe_fd, buff, read_amt)); free(buff); } @@ -1308,7 +1308,7 @@ class universal_notifier_named_pipe_t : public universal_notifier_t { while (this->readback_amount > 0) { char buff[64]; size_t amt_to_read = mini(this->readback_amount, sizeof buff); - (void)read(this->pipe_fd, buff, amt_to_read); + ignore_result(read(this->pipe_fd, buff, amt_to_read)); this->readback_amount -= amt_to_read; } assert(this->readback_amount == 0); diff --git a/src/path.cpp b/src/path.cpp index 7af742974..36997b76d 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -275,7 +275,7 @@ static void maybe_issue_path_warning(const wcstring &which_dir, const wcstring & debug(0, _(L"The error was '%s'."), strerror(saved_errno)); debug(0, _(L"Please set $%ls to a directory where you have write access."), env_var); } - (void)write(STDERR_FILENO, "\n", 1); + ignore_result(write(STDERR_FILENO, "\n", 1)); } static void path_create(wcstring &path, const wcstring &xdg_var, const wcstring &which_dir, diff --git a/src/reader.cpp b/src/reader.cpp index 1855fece9..c0f519be3 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -696,14 +696,14 @@ void reader_write_title(const wcstring &cmd, bool reset_cursor_position) { for (size_t i = 0; i < lst.size(); i++) { fputws(lst.at(i).c_str(), stdout); } - (void)write(STDOUT_FILENO, "\a", 1); + ignore_result(write(STDOUT_FILENO, "\a", 1)); } proc_pop_interactive(); set_color(rgb_color_t::reset(), rgb_color_t::reset()); if (reset_cursor_position && !lst.empty()) { // Put the cursor back at the beginning of the line (issue #2453). - (void)write(STDOUT_FILENO, "\r", 1); + ignore_result(write(STDOUT_FILENO, "\r", 1)); } } @@ -1291,7 +1291,7 @@ static void reader_flash() { } reader_repaint(); - (void)write(STDOUT_FILENO, "\a", 1); + ignore_result(write(STDOUT_FILENO, "\a", 1)); pollint.tv_sec = 0; pollint.tv_nsec = 100 * 1000000; @@ -3248,7 +3248,7 @@ const wchar_t *reader_readline(int nchars) { reader_repaint_if_needed(); } - (void)write(STDOUT_FILENO, "\n", 1); + ignore_result(write(STDOUT_FILENO, "\n", 1)); // Ensure we have no pager contents when we exit. if (!data->pager.empty()) { diff --git a/src/wutil.cpp b/src/wutil.cpp index fd1586950..d777555bf 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -338,7 +338,7 @@ void safe_perror(const char *message) { safe_append(buff, safe_strerror(err), sizeof buff); safe_append(buff, "\n", sizeof buff); - (void)write(STDERR_FILENO, buff, strlen(buff)); + ignore_result(write(STDERR_FILENO, buff, strlen(buff))); errno = err; }