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.
This commit is contained in:
Mahmoud Al-Qudsi 2017-08-12 09:54:26 -05:00 committed by Kurtis Rader
parent 3f6ed72a6b
commit 4fd458900b
6 changed files with 30 additions and 17 deletions

View file

@ -616,14 +616,14 @@ void debug_safe(int level, const char *msg, const char *param1, const char *para
const char *end = strchr(cursor, '%'); const char *end = strchr(cursor, '%');
if (end == NULL) end = cursor + strlen(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') { if (end[0] == '%' && end[1] == 's') {
// Handle a format string. // Handle a format string.
assert(param_idx < sizeof params / sizeof *params); assert(param_idx < sizeof params / sizeof *params);
const char *format = params[param_idx++]; const char *format = params[param_idx++];
if (!format) format = "(null)"; if (!format) format = "(null)";
(void)write(STDERR_FILENO, format, strlen(format)); ignore_result(write(STDERR_FILENO, format, strlen(format)));
cursor = end + 2; cursor = end + 2;
} else if (end[0] == '\0') { } else if (end[0] == '\0') {
// Must be at the end of the string. // 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. // We always append a newline.
(void)write(STDERR_FILENO, "\n", 1); ignore_result(write(STDERR_FILENO, "\n", 1));
errno = errno_old; errno = errno_old;
} }

View file

@ -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 // 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. // `__attribute__((noreturn))` on the exit_without_destructors() function.
// TODO: we use C++11 [[noreturn]] now, does that change things? // TODO: we use C++11 [[noreturn]] now, does that change things?
#define FATAL_EXIT() \ #define FATAL_EXIT() \
{ \ { \
char exit_read_buff; \ char exit_read_buff; \
show_stackframe(L'E'); \ show_stackframe(L'E'); \
(void)read(0, &exit_read_buff, 1); \ ignore_result(read(0, &exit_read_buff, 1)); \
exit_without_destructors(1); \ exit_without_destructors(1); \
} }
/// Exit the program at once after emitting an error message and stack trace if possible. /// 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 /// like an unrecognized flag, missing or too many arguments, an invalid integer, etc. But
STATUS_INVALID_ARGS = 121, 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 #endif

View file

@ -1209,7 +1209,7 @@ class universal_notifier_named_pipe_t : public universal_notifier_t {
// would cause us to hang! // would cause us to hang!
size_t read_amt = 64 * 1024; size_t read_amt = 64 * 1024;
void *buff = malloc(read_amt); void *buff = malloc(read_amt);
(void)read(this->pipe_fd, buff, read_amt); ignore_result(read(this->pipe_fd, buff, read_amt));
free(buff); free(buff);
} }
@ -1308,7 +1308,7 @@ class universal_notifier_named_pipe_t : public universal_notifier_t {
while (this->readback_amount > 0) { while (this->readback_amount > 0) {
char buff[64]; char buff[64];
size_t amt_to_read = mini(this->readback_amount, sizeof buff); 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; this->readback_amount -= amt_to_read;
} }
assert(this->readback_amount == 0); assert(this->readback_amount == 0);

View file

@ -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"The error was '%s'."), strerror(saved_errno));
debug(0, _(L"Please set $%ls to a directory where you have write access."), env_var); 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, static void path_create(wcstring &path, const wcstring &xdg_var, const wcstring &which_dir,

View file

@ -696,14 +696,14 @@ void reader_write_title(const wcstring &cmd, bool reset_cursor_position) {
for (size_t i = 0; i < lst.size(); i++) { for (size_t i = 0; i < lst.size(); i++) {
fputws(lst.at(i).c_str(), stdout); fputws(lst.at(i).c_str(), stdout);
} }
(void)write(STDOUT_FILENO, "\a", 1); ignore_result(write(STDOUT_FILENO, "\a", 1));
} }
proc_pop_interactive(); proc_pop_interactive();
set_color(rgb_color_t::reset(), rgb_color_t::reset()); set_color(rgb_color_t::reset(), rgb_color_t::reset());
if (reset_cursor_position && !lst.empty()) { if (reset_cursor_position && !lst.empty()) {
// Put the cursor back at the beginning of the line (issue #2453). // 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(); reader_repaint();
(void)write(STDOUT_FILENO, "\a", 1); ignore_result(write(STDOUT_FILENO, "\a", 1));
pollint.tv_sec = 0; pollint.tv_sec = 0;
pollint.tv_nsec = 100 * 1000000; pollint.tv_nsec = 100 * 1000000;
@ -3248,7 +3248,7 @@ const wchar_t *reader_readline(int nchars) {
reader_repaint_if_needed(); 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. // Ensure we have no pager contents when we exit.
if (!data->pager.empty()) { if (!data->pager.empty()) {

View file

@ -338,7 +338,7 @@ void safe_perror(const char *message) {
safe_append(buff, safe_strerror(err), sizeof buff); safe_append(buff, safe_strerror(err), sizeof buff);
safe_append(buff, "\n", 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; errno = err;
} }