* [PATCH] system: Add WARN_UNUSED_RESULT
@ 2020-11-12 3:03 Marek Polacek
2020-11-12 16:03 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2020-11-12 3:03 UTC (permalink / raw)
To: GCC Patches
I'd like to have the option of marking functions with
__attribute__ ((__warn_unused_result__)), so this patch adds a macro.
And use it for maybe_wrap_with_location, it's always a bug if the
return value is not used, which happened to me and got me confused.
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
gcc/ChangeLog:
* system.h (WARN_UNUSED_RESULT): Define for GCC >= 3.4.
* tree.h (maybe_wrap_with_location): Add WARN_UNUSED_RESULT.
---
gcc/system.h | 6 ++++++
gcc/tree.h | 2 +-
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/gcc/system.h b/gcc/system.h
index b0f3f1dd019..6f6ab616a61 100644
--- a/gcc/system.h
+++ b/gcc/system.h
@@ -789,6 +789,12 @@ extern void fancy_abort (const char *, int, const char *)
#define ALWAYS_INLINE inline
#endif
+#if GCC_VERSION >= 3004
+#define WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
+#else
+#define WARN_UNUSED_RESULT
+#endif
+
/* Use gcc_unreachable() to mark unreachable locations (like an
unreachable default case of a switch. Do not use gcc_assert(0). */
#if (GCC_VERSION >= 4005) && !ENABLE_ASSERT_CHECKING
diff --git a/gcc/tree.h b/gcc/tree.h
index 684be10b440..9a713cdb0c7 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -1214,7 +1214,7 @@ get_expr_source_range (tree expr)
extern void protected_set_expr_location (tree, location_t);
extern void protected_set_expr_location_if_unset (tree, location_t);
-extern tree maybe_wrap_with_location (tree, location_t);
+WARN_UNUSED_RESULT extern tree maybe_wrap_with_location (tree, location_t);
extern int suppress_location_wrappers;
base-commit: 0f5f9ed5e5a041b636cc002451b1e8b2295f8e4f
--
2.28.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] system: Add WARN_UNUSED_RESULT
2020-11-12 3:03 [PATCH] system: Add WARN_UNUSED_RESULT Marek Polacek
@ 2020-11-12 16:03 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2020-11-12 16:03 UTC (permalink / raw)
To: Marek Polacek, GCC Patches
On 11/11/20 10:03 PM, Marek Polacek wrote:
> I'd like to have the option of marking functions with
> __attribute__ ((__warn_unused_result__)), so this patch adds a macro.
> And use it for maybe_wrap_with_location, it's always a bug if the
> return value is not used, which happened to me and got me confused.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
OK.
> gcc/ChangeLog:
>
> * system.h (WARN_UNUSED_RESULT): Define for GCC >= 3.4.
> * tree.h (maybe_wrap_with_location): Add WARN_UNUSED_RESULT.
> ---
> gcc/system.h | 6 ++++++
> gcc/tree.h | 2 +-
> 2 files changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/system.h b/gcc/system.h
> index b0f3f1dd019..6f6ab616a61 100644
> --- a/gcc/system.h
> +++ b/gcc/system.h
> @@ -789,6 +789,12 @@ extern void fancy_abort (const char *, int, const char *)
> #define ALWAYS_INLINE inline
> #endif
>
> +#if GCC_VERSION >= 3004
> +#define WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
> +#else
> +#define WARN_UNUSED_RESULT
> +#endif
> +
> /* Use gcc_unreachable() to mark unreachable locations (like an
> unreachable default case of a switch. Do not use gcc_assert(0). */
> #if (GCC_VERSION >= 4005) && !ENABLE_ASSERT_CHECKING
> diff --git a/gcc/tree.h b/gcc/tree.h
> index 684be10b440..9a713cdb0c7 100644
> --- a/gcc/tree.h
> +++ b/gcc/tree.h
> @@ -1214,7 +1214,7 @@ get_expr_source_range (tree expr)
> extern void protected_set_expr_location (tree, location_t);
> extern void protected_set_expr_location_if_unset (tree, location_t);
>
> -extern tree maybe_wrap_with_location (tree, location_t);
> +WARN_UNUSED_RESULT extern tree maybe_wrap_with_location (tree, location_t);
>
> extern int suppress_location_wrappers;
>
>
> base-commit: 0f5f9ed5e5a041b636cc002451b1e8b2295f8e4f
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-11-12 16:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-12 3:03 [PATCH] system: Add WARN_UNUSED_RESULT Marek Polacek
2020-11-12 16:03 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).