xref: /dflybsd-src/contrib/grep/lib/ignore-value.h (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
1dc7c36e4SJohn Marino /* ignore a function return without a compiler warning.  -*- coding: utf-8 -*-
295b7b453SJohn Marino 
3*09d4459fSDaniel Fojt    Copyright (C) 2008-2020 Free Software Foundation, Inc.
495b7b453SJohn Marino 
595b7b453SJohn Marino    This program is free software: you can redistribute it and/or modify
695b7b453SJohn Marino    it under the terms of the GNU General Public License as published by
795b7b453SJohn Marino    the Free Software Foundation; either version 3 of the License, or
895b7b453SJohn Marino    (at your option) any later version.
995b7b453SJohn Marino 
1095b7b453SJohn Marino    This program is distributed in the hope that it will be useful,
1195b7b453SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1295b7b453SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1395b7b453SJohn Marino    GNU General Public License for more details.
1495b7b453SJohn Marino 
1595b7b453SJohn Marino    You should have received a copy of the GNU General Public License
16*09d4459fSDaniel Fojt    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
1795b7b453SJohn Marino 
18200fbe8dSJohn Marino /* Written by Jim Meyering, Eric Blake and Pádraig Brady.  */
1995b7b453SJohn Marino 
20200fbe8dSJohn Marino /* Use "ignore_value" to avoid a warning when using a function declared with
2195b7b453SJohn Marino    gcc's warn_unused_result attribute, but for which you really do want to
2295b7b453SJohn Marino    ignore the result.  Traditionally, people have used a "(void)" cast to
2395b7b453SJohn Marino    indicate that a function's return value is deliberately unused.  However,
2495b7b453SJohn Marino    if the function is declared with __attribute__((warn_unused_result)),
2595b7b453SJohn Marino    gcc issues a warning even with the cast.
2695b7b453SJohn Marino 
2795b7b453SJohn Marino    Caution: most of the time, you really should heed gcc's warning, and
2895b7b453SJohn Marino    check the return value.  However, in those exceptional cases in which
2995b7b453SJohn Marino    you're sure you know what you're doing, use this function.
3095b7b453SJohn Marino 
3195b7b453SJohn Marino    For the record, here's one of the ignorable warnings:
3295b7b453SJohn Marino    "copy.c:233: warning: ignoring return value of 'fchown',
3395b7b453SJohn Marino    declared with attribute warn_unused_result".  */
3495b7b453SJohn Marino 
35200fbe8dSJohn Marino #ifndef _GL_IGNORE_VALUE_H
36200fbe8dSJohn Marino #define _GL_IGNORE_VALUE_H
37200fbe8dSJohn Marino 
38680a9cb8SJohn Marino /* Normally casting an expression to void discards its value, but GCC
39680a9cb8SJohn Marino    versions 3.4 and newer have __attribute__ ((__warn_unused_result__))
40680a9cb8SJohn Marino    which may cause unwanted diagnostics in that case.  Use __typeof__
41680a9cb8SJohn Marino    and __extension__ to work around the problem, if the workaround is
42680a9cb8SJohn Marino    known to be needed.  */
43680a9cb8SJohn Marino #if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
44680a9cb8SJohn Marino # define ignore_value(x) \
45680a9cb8SJohn Marino     (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
46200fbe8dSJohn Marino #else
47680a9cb8SJohn Marino # define ignore_value(x) ((void) (x))
48200fbe8dSJohn Marino #endif
49200fbe8dSJohn Marino 
50200fbe8dSJohn Marino #endif
51