bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sr #111072] "[[]] attributes are a C23 extension warning" with clan


From: Paul Eggert
Subject: Re: [sr #111072] "[[]] attributes are a C23 extension warning" with clang
Date: Sat, 8 Jun 2024 12:38:00 -0700
User-agent: Mozilla Thunderbird

(Moving this from <https://savannah.gnu.org/support/?111072> to <mailto:bug-gnulib@gnu.org> because it's Gnulib, not Autoconf.]

Recently, I started getting warnings like:


./slist.h:170:1: warning: [[]] attributes are a C23 extension
[-Wc23-extensions]
   170 | NODISCARD
       | ^
../lib/attribute.h:143:19: note: expanded from macro 'NODISCARD'
   143 | #define NODISCARD _GL_ATTRIBUTE_NODISCARD
       |                   ^
./config.h:1392:37: note: expanded from macro '_GL_ATTRIBUTE_NODISCARD'
  1392 | #    define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
       |                                     ^

It looks like -Wc23-extensions is the problem. If you stop using it (or if you also use -std=gnu23), you shouldn't get those warnings. At least, that's the behavior I see with clang 18.1.6 (a bit later than your clang) on Fedora.

-Wc23-extensions is more trouble than it's worth when programs (as is commonly the case) use C23 extensions only when available.


Specifically, the value defined for NODISCARD defined in config.h as generated
by configure as generated by autoconf causes the warning.

The relevant section from config.h is:


#ifndef _GL_ATTRIBUTE_NODISCARD
# ifndef _GL_BRACKET_BEFORE_ATTRIBUTE
#  if defined __clang__ && defined __cplusplus
   /* With clang up to 15.0.6 (at least), in C++ mode, [[__nodiscard__]]
produces
      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
#  elif _GL_HAVE___HAS_C_ATTRIBUTE
#   if __has_c_attribute (__nodiscard__)
#    define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
#   endif
#  endif
# endif
# if !defined _GL_ATTRIBUTE_NODISCARD && _GL_HAS_ATTRIBUTE
(warn_unused_result)
#  define _GL_ATTRIBUTE_NODISCARD __attribute__ ((__warn_unused_result__))
# endif
# ifndef _GL_ATTRIBUTE_NODISCARD
#  define _GL_ATTRIBUTE_NODISCARD
# endif
#endif


I think the problem is this line:


#  if defined __clang__ && defined __cplusplus


My project is a C project, not C++, so __cplusplus is not defined so that #if
fails which means this part happens:


#  elif _GL_HAVE___HAS_C_ATTRIBUTE
#   if __has_c_attribute (__nodiscard__)
#    define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
#   endif


In this block, no check for __clang_major__ is done, so it always defines
_GL_ATTRIBUTE_NODISCARD to [[__nodiscard__]] and clang warns.

I don't think the && __cplusplus should be there at all. Attributes using [[
are supported in C23 or later or C++11 or later. Why not use
__STDC_VERSION__?

In practice __STDC_VERSION__ has been less reliable than the more-detailed checks.



FYI:


$ clang --version
FreeBSD clang version 18.1.5 (https://github.com/llvm/llvm-project.git
llvmorg-18.1.5-0-g617a15a9eac9)
Target: x86_64-unknown-freebsd14.1
Thread model: posix
InstalledDir: /usr/bin

$ autoconf --version
autoconf (GNU Autoconf) 2.72
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+/Autoconf: GNU GPL version 3 or later
<https://gnu.org/licenses/gpl.html>,
<https://gnu.org/licenses/exceptions.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David J. MacKenzie and Akim Demaille.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]