From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Subject: Avoid clang warnings regarding [[__maybe_unused__]]
Date: Sat, 28 Jan 2023 09:39:31 +0100 [thread overview]
Message-ID: <8503261.SR6Mq5HPT2@nimes> (raw)
Building a Gnulib testdir on FreeBSD 12.0, which uses clang 6.0.1, I see
many warnings such as
../../gltests/test-dirent-c++.cc:28:1: warning: unknown attribute '__maybe_unused__' ignored [-Wunknown-attributes]
The cause is that in this version of clang, __has_c_attribute(__maybe_unused__)
apparently returns true, and the use of [[__maybe_unused__]] does not elicit a
syntax error. But it elicits a warning. Here's how it depends on the clang
version:
clang result
----- -------
4.0.0 error
5.0.2 error
6.0.1 warning
7.0.1 warning
8.0.0 warning
9.0.0 warning
10.0.0 ok
This patch fixes the warnings, by choosing a different (more conservative)
expansion of _GL_ATTRIBUTE_MAYBE_UNUSED.
2023-01-28 Bruno Haible <bruno@clisp.org>
Avoid clang warnings regarding [[__maybe_unused__]].
* m4/gnulib-common.m4 (gl_COMMON_BODY): For clang versions ≥ 6, < 10,
in C++ mode, ignore the __has_c_attribute value and define
_GL_ATTRIBUTE_MAYBE_UNUSED to __attribute__ ((__unused__)), not
[[__maybe_unused__]].
diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4
index 0f1a6ac1ea..cf0fc5a1e7 100644
--- a/m4/gnulib-common.m4
+++ b/m4/gnulib-common.m4
@@ -1,4 +1,4 @@
-# gnulib-common.m4 serial 78
+# gnulib-common.m4 serial 79
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,
@@ -351,9 +351,15 @@ AC_DEFUN([gl_COMMON_BODY], [
in C++ also: class. */
/* In C++ and C23, this is spelled [[__maybe_unused__]].
GCC's syntax is __attribute__ ((__unused__)).
- clang supports both syntaxes. */
+ clang supports both syntaxes. Except that with clang ≥ 6, < 10, in C++ mode,
+ __has_c_attribute (__maybe_unused__) yields true but the use of
+ [[__maybe_unused__]] nevertheless produces a warning. */
#ifndef _GL_ATTRIBUTE_MAYBE_UNUSED
-# ifdef __has_c_attribute
+# if defined __clang__ && defined __cplusplus
+# if __clang_major__ >= 10
+# define _GL_ATTRIBUTE_MAYBE_UNUSED [[__maybe_unused__]]
+# endif
+# elif defined __has_c_attribute
# if __has_c_attribute (__maybe_unused__)
# define _GL_ATTRIBUTE_MAYBE_UNUSED [[__maybe_unused__]]
# endif
reply other threads:[~2023-01-28 8:40 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8503261.SR6Mq5HPT2@nimes \
--to=bruno@clisp.org \
--cc=bug-gnulib@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).