* Avoid clang warnings regarding [[__nodiscard__]]
@ 2023-01-28 10:57 Bruno Haible
2023-01-28 19:06 ` Paul Eggert
0 siblings, 1 reply; 3+ messages in thread
From: Bruno Haible @ 2023-01-28 10:57 UTC (permalink / raw)
To: bug-gnulib
Building a Gnulib testdir on FreeBSD 12.0, which uses clang 6.0.1, I see
several warnings such as
../../gltests/../gllib/gl_list.h:642:1: warning: unknown attribute '__nodiscard__' ignored [-Wunknown-attributes]
The cause is that in this version of clang, __has_c_attribute(__nodiscard__)
apparently returns true, but the use of [[__nodiscard__]] elicits a warning.
Here's how it depends on the clang version:
clang result
----- -------
5.0.2 error
6.0.1 warning
7.0.1 warning
8.0.0 warning
9.0.0 warning
10.0.0 warning
11.0.0 warning
12.0.1 warning
13.0.0 warning
14.0.0 warning
15.0.6 warning
This patch fixes the warnings, by choosing a different (more conservative)
expansion of _GL_ATTRIBUTE_NODISCARD.
2023-01-28 Bruno Haible <bruno@clisp.org>
Avoid clang warnings regarding [[__nodiscard__]].
* m4/gnulib-common.m4 (gl_COMMON_BODY): For clang, in C++ mode, ignore
the __has_c_attribute value and define _GL_ATTRIBUTE_NODISCARD to
__attribute__ ((__warn_unused_result__)), not [[__nodiscard__]].
diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4
index cf0fc5a1e7..d632819864 100644
--- a/m4/gnulib-common.m4
+++ b/m4/gnulib-common.m4
@@ -1,4 +1,4 @@
-# gnulib-common.m4 serial 79
+# gnulib-common.m4 serial 80
dnl Copyright (C) 2007-2023 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -379,7 +379,13 @@ AC_DEFUN([gl_COMMON_BODY], [
the return value, unless the caller uses something like ignore_value. */
/* Applies to: function, enumeration, class. */
#ifndef _GL_ATTRIBUTE_NODISCARD
-# ifdef __has_c_attribute
+# if defined __clang__ && defined __cplusplus
+ /* With clang up to 15.0.6 (at least), in C++ mode, [[__nodiscard__]] produces
+ a warning. */
+# if __clang_major__ >= 1000
+# define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
+# endif
+# elif defined __has_c_attribute
# if __has_c_attribute (__nodiscard__)
# define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
# endif
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: Avoid clang warnings regarding [[__nodiscard__]]
2023-01-28 10:57 Avoid clang warnings regarding [[__nodiscard__]] Bruno Haible
@ 2023-01-28 19:06 ` Paul Eggert
2023-01-28 19:22 ` Bruno Haible
0 siblings, 1 reply; 3+ messages in thread
From: Paul Eggert @ 2023-01-28 19:06 UTC (permalink / raw)
To: Bruno Haible; +Cc: bug-gnulib
[-- Attachment #1: Type: text/plain, Size: 387 bytes --]
On 2023-01-28 02:57, Bruno Haible wrote:
> + /* With clang up to 15.0.6 (at least), in C++ mode, [[__nodiscard__]] produces
> + a warning. */
> +# if __clang_major__ >= 1000
I found this a little hard to follow, as I don't know __clang_major__ by
heart and at first misread that as version 10.0.0. How about something
like the attached, to simplify this for Clang non-experts?
[-- Attachment #2: nodiscard.diff --]
[-- Type: text/x-patch, Size: 876 bytes --]
diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4
index d632819864..bb5a2029af 100644
--- a/m4/gnulib-common.m4
+++ b/m4/gnulib-common.m4
@@ -379,14 +379,11 @@ AC_DEFUN([gl_COMMON_BODY], [
the return value, unless the caller uses something like ignore_value. */
/* Applies to: function, enumeration, class. */
#ifndef _GL_ATTRIBUTE_NODISCARD
-# if defined __clang__ && defined __cplusplus
/* With clang up to 15.0.6 (at least), in C++ mode, [[__nodiscard__]] produces
a warning. */
-# if __clang_major__ >= 1000
-# define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
-# endif
-# elif defined __has_c_attribute
-# if __has_c_attribute (__nodiscard__)
+# ifdef __has_c_attribute
+# if (__has_c_attribute (__nodiscard__) \
+ && ! (defined __clang__ && defined __cplusplus))
# define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
# endif
# endif
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: Avoid clang warnings regarding [[__nodiscard__]]
2023-01-28 19:06 ` Paul Eggert
@ 2023-01-28 19:22 ` Bruno Haible
0 siblings, 0 replies; 3+ messages in thread
From: Bruno Haible @ 2023-01-28 19:22 UTC (permalink / raw)
To: Paul Eggert; +Cc: bug-gnulib
Paul Eggert wrote:
> > + /* With clang up to 15.0.6 (at least), in C++ mode, [[__nodiscard__]] produces
> > + a warning. */
> > +# if __clang_major__ >= 1000
>
> I found this a little hard to follow
Indeed, I wondered whether I should add a comment. Guess I should have :)
> How about something like the attached, to simplify this for Clang non-experts?
I wanted the code to be prepared for the day when clang++ actually adds
support for this syntax [[__nodiscard__]]. The code you are proposing
optimizes the current needs, but not the future needs.
I'm adding this comment instead. I hope that's clear enough.
2023-01-28 Bruno Haible <bruno@clisp.org>
Clarify _GL_ATTRIBUTE_NODISCARD code.
Reported by Paul Eggert.
* m4/gnulib-common.m4 (gl_COMMON_BODY): Add comment regarding
_GL_ATTRIBUTE_NODISCARD definition.
diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4
index d632819864..c0181abdc5 100644
--- a/m4/gnulib-common.m4
+++ b/m4/gnulib-common.m4
@@ -381,7 +381,10 @@ AC_DEFUN([gl_COMMON_BODY], [
#ifndef _GL_ATTRIBUTE_NODISCARD
# if defined __clang__ && defined __cplusplus
/* With clang up to 15.0.6 (at least), in C++ mode, [[__nodiscard__]] produces
- a warning. */
+ a warning.
+ The 1000 below means a yet unknown threshold. When clang++ version X
+ starts supporting [[__nodiscard__]] without warning about it, you can
+ replace the 1000 with X. */
# if __clang_major__ >= 1000
# define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
# endif
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-01-28 19:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-28 10:57 Avoid clang warnings regarding [[__nodiscard__]] Bruno Haible
2023-01-28 19:06 ` Paul Eggert
2023-01-28 19:22 ` Bruno Haible
Code repositories for project(s) associated with this public inbox
https://public-inbox.org/mirrors/gnulib.git/
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).