From 65ab3ab1976c9e1e2642dd2e79316ff4129d8bc9 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin (Intel)" Date: Tue, 30 Jun 2020 10:14:21 -0700 Subject: clang: mark inline functions unused and add warning options clang, unlike gcc, will warn on inline functions which are unused. This can happen if a function is either intended to be used in the future, or it is only used under certain config options. Mark those functions with the "unused" attribute; not only does it quiet the warning, but it also documents it for the user. Shuffle around the warning options in configure and add a few more that are specific to clang. Signed-off-by: H. Peter Anvin (Intel) --- asm/parser.c | 3 ++- asm/preproc.c | 6 ++++-- config/unconfig.h | 30 ++++++++++++++++++++++++++++++ configure.ac | 38 +++++++++++++++++++++++++------------- include/compiler.h | 25 +++++++++++++++++++++++-- include/strlist.h | 8 ++++---- 6 files changed, 88 insertions(+), 22 deletions(-) diff --git a/asm/parser.c b/asm/parser.c index de24103e..3daca0c6 100644 --- a/asm/parser.c +++ b/asm/parser.c @@ -234,7 +234,8 @@ static bool parse_braces(decoflags_t *decoflags) } } -static inline const expr *next_expr(const expr *e, const expr **next_list) +static inline unused +const expr *next_expr(const expr *e, const expr **next_list) { e++; if (!e->type) { diff --git a/asm/preproc.c b/asm/preproc.c index a2c57883..14ca17d3 100644 --- a/asm/preproc.c +++ b/asm/preproc.c @@ -345,7 +345,8 @@ static inline bool tok_text_match(const struct Token *a, const struct Token *b) return a->len == b->len && !memcmp(tok_text(a), tok_text(b), a->len); } -static inline bool tok_match(const struct Token *a, const struct Token *b) +static inline unused bool +tok_match(const struct Token *a, const struct Token *b) { return a->type == b->type && tok_text_match(a, b); } @@ -763,7 +764,8 @@ static const char *unquote_token_cstr(Token *t) * TOK_STRING tokens. */ static Token *quote_any_token(Token *t); -static inline Token *quote_token(Token *t) +static inline unused +Token *quote_token(Token *t) { if (likely(!tok_is(t, TOK_INTERNAL_STRING))) return t; diff --git a/config/unconfig.h b/config/unconfig.h index d01c2b3e..c7d900fc 100644 --- a/config/unconfig.h +++ b/config/unconfig.h @@ -60,6 +60,12 @@ /* Define to 1 if compiled with the `-Wc90-c99-compat' compiler flag */ /* #undef CFLAGS_WC90_C99_COMPAT */ +/* Define to 1 if compiled with the `-Wc99-compat' compiler flag */ +/* #undef CFLAGS_WC99_COMPAT */ + +/* Define to 1 if compiled with the `-Wc99-extensions' compiler flag */ +/* #undef CFLAGS_WC99_EXTENSIONS */ + /* Define to 1 if compiled with the `-Werror' compiler flag */ /* #undef CFLAGS_WERROR */ @@ -225,6 +231,10 @@ functions */ /* #undef HAVE_FUNC_ATTRIBUTE_SENTINEL */ +/* Define to 1 if your compiler supports __attribute__((unused)) on functions + */ +/* #undef HAVE_FUNC_ATTRIBUTE_UNUSED */ + /* Define to 1 if your compiler supports __attribute__((alloc_size)) on function pointers */ /* #undef HAVE_FUNC_PTR_ATTRIBUTE1_ALLOC_SIZE */ @@ -265,6 +275,10 @@ pointers */ /* #undef HAVE_FUNC_PTR_ATTRIBUTE_SENTINEL */ +/* Define to 1 if your compiler supports __attribute__((unused)) on function + pointers */ +/* #undef HAVE_FUNC_PTR_ATTRIBUTE_UNUSED */ + /* Define to 1 if you have the `getgid' function. */ /* #undef HAVE_GETGID */ @@ -714,6 +728,22 @@ # endif #endif +#ifndef unused_func +# ifdef HAVE_FUNC_ATTRIBUTE_UNUSED +# define unused_func __attribute__((unused)) +# else +# define unused_func +# endif +#endif + +#ifndef unused_func_ptr +# ifdef HAVE_FUNC_PTR_ATTRIBUTE_UNUSED +# define unused_func_ptr __attribute__((unused)) +# else +# define unused_func_ptr +# endif +#endif + #ifndef never_null # ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL # define never_null __attribute__((returns_nonnull)) diff --git a/configure.ac b/configure.ac index 85393d03..d9d45837 100644 --- a/configure.ac +++ b/configure.ac @@ -23,12 +23,6 @@ AC_USE_SYSTEM_EXTENSIONS AC_SYS_LARGEFILE AC_PROG_CC AC_PROG_CC_STDC -AC_PROG_LN_S -AC_PROG_MAKE_SET -AC_PROG_INSTALL -AC_PROG_MKDIR_P - -pa_no_optimize=false dnl If the user did not specify a CFLAGS default, change default dnl to -O0 for debugging @@ -36,6 +30,19 @@ PA_ARG_DISABLED([optimization], [compile without optimization (-O0) to help debugging], [pa_no_optimize=true]) +dnl LLVM doesn't error out on invalid -W options unless this option is +dnl specified first. Enable this so this script can actually discover +dnl which -W options are possible for this compiler. +PA_ADD_CFLAGS([-Werror=unknown-warning-option]) + +dnl Other programs +AC_PROG_LN_S +AC_PROG_MAKE_SET +AC_PROG_INSTALL +AC_PROG_MKDIR_P + +pa_no_optimize=false + dnl Compile and link with dwarf debug PA_ARG_ENABLED([gdb], [disable optimization and compile with extra debug information for GDB debugger], @@ -282,6 +289,7 @@ PA_FUNC_ATTRIBUTE(format, [printf,1,2], int, [const char *, ...], ["%d",1]) PA_FUNC_ATTRIBUTE(const) PA_FUNC_ATTRIBUTE(pure) PA_FUNC_ATTRIBUTE(cold,,,,,unlikely_func) +PA_FUNC_ATTRIBUTE(unused) PA_FUNC_ATTRIBUTE_ERROR dnl @@ -320,22 +328,25 @@ PA_ARG_ENABLED([sanitizer], dnl dnl Don't make symbols visible, there is no point and it just -dnl makes the code slower. +dnl makes the code slower. This mainly affects ELF. dnl PA_ADD_CFLAGS([-fvisibility=hidden]) -dnl If we have gcc, add appropriate code cleanliness options +dnl +dnl If we have gcc, add appropriate code cleanliness options. Do this +dnl here at the end, because configure relies on being able to use +dnl some very, very old C constructs. +dnl PA_ADD_CFLAGS([-Wall]) PA_ARG_DISABLED([pedantic], [disable some extra paranoid compiler warnings], [], [PA_ADD_CFLAGS([-W]) PA_ADD_CFLAGS([-pedantic]) - PA_ADD_CFLAGS([-Wc90-c99-compat])]) -dnl LLVM doesn't error out on invalid -W options unless this option is -dnl specified first. Enable this so this script can actually discover -dnl which -W options are possible for this compiler. -PA_ADD_CFLAGS([-Werror=unknown-warning-option]) + PA_ADD_CFLAGS([-Wc90-c99-compat]) + PA_ADD_CFLAGS([-Wc99-compat]) + PA_ADD_CFLAGS([-Wc99-extensions]) +]) dnl Suppress format warning on Windows targets due to their PA_ADD_CFLAGS([-Wpedantic-ms-format],[-Wno-pedantic-ms-format]) PA_ADD_CFLAGS([-Wlong-long],[-Wno-long-long]) @@ -347,6 +358,7 @@ dnl on compilers with gcc-like command line syntax we pass the -fwrapv dnl option for exactly that reason. PA_ADD_CFLAGS([-Wshift-negative-value],[-Wno-shift-negative-value]) +dnl Want to turn this on at some point... dnl PA_ADD_CFLAGS([-Wwrite-strings]) PA_ARG_ENABLED([werror], [compile with -Werror to error out on any warning], diff --git a/include/compiler.h b/include/compiler.h index 43984338..a1905d27 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -1,5 +1,5 @@ /* ----------------------------------------------------------------------- * - * + * * Copyright 2007-2018 The NASM Authors - All Rights Reserved * See the file AUTHORS included with the NASM distribution for * the specific copyright holders. @@ -14,7 +14,7 @@ * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF @@ -310,6 +310,27 @@ static inline void *mempcpy(void *dst, const void *src, size_t n) #define printf_func(fmt, list) format_func3(printf,fmt,list) #define printf_func_ptr(fmt, list) format_func3_ptr(printf,fmt,list) +/* + * A static [inline] function which either is currently unused but + * likely to be used in the future, or used only under some #if + * combinations. Mark with this option to suppress compiler + * warnings. + * + * This is better than #if(def) because it still lets the compiler + * analyze the function for validity, and it works even for the + * conditional use case. + * + * The macro UNUSED is set to 1 if the unused macro is meaningful, + * otherwise 0; this may be useful in some #if statements. + */ +#ifdef HAVE_FUNC_ATTRIBUTE_UNUSED +# define unused __attribute__((unused)) +# define UNUSED 1 +#else +# define unused +# define UNUSED 0 +#endif + /* Determine probabilistically if something is a compile-time constant */ #ifdef HAVE___BUILTIN_CONSTANT_P # if defined(__GNUC__) && (__GNUC__ >= 5) diff --git a/include/strlist.h b/include/strlist.h index 2c80d0be..f1775abd 100644 --- a/include/strlist.h +++ b/include/strlist.h @@ -1,6 +1,6 @@ /* ----------------------------------------------------------------------- * * - * Copyright 1996-2018 The NASM Authors - All Rights Reserved + * Copyright 1996-2020 The NASM Authors - All Rights Reserved * See the file AUTHORS included with the NASM distribution for * the specific copyright holders. * @@ -78,10 +78,10 @@ static inline size_t strlist_size(const struct strlist *list) } struct strlist safe_alloc *strlist_alloc(bool uniq); -const struct strlist_entry * never_null strlist_add(struct strlist *list, const char *str); -const struct strlist_entry * printf_func(2, 3) never_null +const struct strlist_entry *strlist_add(struct strlist *list, const char *str); +const struct strlist_entry * printf_func(2, 3) strlist_printf(struct strlist *list, const char *fmt, ...); -const struct strlist_entry * never_null +const struct strlist_entry * strlist_vprintf(struct strlist *list, const char *fmt, va_list ap); const struct strlist_entry * strlist_find(const struct strlist *list, const char *str); -- cgit v1.2.1