Hi,

there is an header file in the attachment -- ignore.h -- that defines three macros; ignore(), ignorep() and ignoreb(). These can be used to suppress warn_unused_result warnings safely without any direct consequences to the application itself.

Unlike assert(), ignore() displays warning for negative results (as most of the C library functions returns -1 on error), ignorep() (ignore pointer) for zero result (it actually casts it to (int) from pointer it should receive) and ignoreb() (ignore boolean) prints warning for false (0) expressions and none of these terminates application.

About fread/fwrite checks; C library does not set errno != 0 for cases when fread/fwrite reads/writes short item count. So I don't think it is necessary to check for these cases (they are pretty rare anyway).

        What do you think about using this header for HLFS build?

Note that even unlike their names suggests, these macros don't actually bury the return value. They use gcc extension -- compbound command in brackets -- to return original expression result (last command expression of block), so they can be used even as sanity checks. I mean `res = ignore(fread(...))` assigns `res` correct value.

- Mordae
#ifndef _IGNORE_H
#define _IGNORE_H 1

#include <stdio.h> /* for fprintf() */
#include <errno.h> /* for thread-safe errno */

/* Temporary variables to hold call result and errno that may be changed
 * by call to fprintf().
 */
static __thread int __ignore_res;
static __thread int __ignore_errno;

/* Glibc-provided program name.
 */
extern char *program_invocation_short_name;

/* Prints message for negative expressions.
 */
#define ignore(expr) ({                                                 \
        if (0 > (__ignore_res = (expr))) {                              \
                __ignore_errno = errno;                                 \
                fprintf(stderr,                                         \
                        "%s: Ignored failure in %s:%i\n (%s) == %i (%s)\n", \
                        program_invocation_short_name, __FILE__, __LINE__,   \
                        #expr, __ignore_res, strerror(errno));          \
                errno = __ignore_errno;                                 \
        }                                                               \
        __ignore_res;                                                   \
})

/* Prints message for NULL expressions.
 */
#define ignorep(expr) ({                                                \
        if (0 == (__ignore_res = (int)(expr))) {                        \
                __ignore_errno = errno;                                 \
                fprintf(stderr,                                         \
                        "%s: Ignored failure in %s:%i\n (%s) == %#x (%s)\n", \
                        program_invocation_short_name, __FILE__, __LINE__,   \
                        #expr, __ignore_res, strerror(errno));          \
                errno = __ignore_errno;                                 \
        }                                                               \
        (void *)__ignore_res;                                           \
})

/* Prints message for false (0) expressions.
 */
#define ignoreb(expr) ({                                                \
        if (! (__ignore_res = (expr))) {                                \
                __ignore_errno = errno;                                 \
                fprintf(stderr,                                         \
                        "%s: Ignored failure in %s:%i\n (%s) == %i (%s)\n", \
                        program_invocation_short_name, __FILE__, __LINE__,   \
                        #expr, __ignore_res, strerror(errno));          \
                errno = __ignore_errno;                                 \
        }                                                               \
        __ignore_res;                                                   \
})

#endif /* !_IGNORE_H */
-- 
http://linuxfromscratch.org/mailman/listinfo/hlfs-dev
FAQ: http://www.linuxfromscratch.org/faq/
Unsubscribe: See the above information page

Reply via email to