bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* _Noreturn.h not respecting __STDC_VERSION__ (macOS, clang 10)
@ 2021-01-09  4:31 Joe Nelson
  2021-01-09 22:21 ` Paul Eggert
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Nelson @ 2021-01-09  4:31 UTC (permalink / raw)
  To: bug-gnulib

Hi, my project uses lib/_Noreturn.h, but gets a warning when compiled
with "-std=c99 -pedantic":

	warning: _Noreturn functions are a C11-specific feature

By placing an #error directive in the code below during testing, I
confirmed the preprocessor is incorrectly hitting the condition marked
/* _Noreturn works as-is.  */

	#ifndef _Noreturn
	# if (defined __cplusplus \
		  && ((201103 <= __cplusplus && !(__GNUC__ == 4 && __GNUC_MINOR__ == 7)) \
			  || (defined _MSC_VER && 1900 <= _MSC_VER)) \
		  && 0)
		/* [[noreturn]] is not practically usable, because with it the syntax
			 extern _Noreturn void func (...);
		   would not be valid; such a declaration would only be valid with 'extern'
		   and '_Noreturn' swapped, or without the 'extern' keyword.  However, some
		   AIX system header files and several gnulib header files use precisely
		   this syntax with 'extern'.  */
	#  define _Noreturn [[noreturn]]
	# elif ((!defined __cplusplus || defined __clang__)                     \
			&& (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0)  \
				|| 4 < __GNUC__ + (7 <= __GNUC_MINOR__) \
				|| (defined __apple_build_version__ \
					? 6000000 <= __apple_build_version__ \
					: 3 < __clang_major__ + (5 <= __clang_minor__))))
	   /* _Noreturn works as-is.  */
	# elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__) || 0x5110 <= __SUNPRO_C
	#  define _Noreturn __attribute__ ((__noreturn__))
	# elif 1200 <= (defined _MSC_VER ? _MSC_VER : 0)
	#  define _Noreturn __declspec (noreturn)
	# else
	#  define _Noreturn
	# endif
	#endif

The correct behavior would be to respect the value of __STDC_VERSION__
which I set with the -std=c99 compiler flag.

Here are the values of the relevant macros in my environment:

#define __GNUC__ 4
#define __GNUC_MINOR__ 2
#define __STDC_VERSION__ 199901L
#define __apple_build_version__ 10001145
#define __clang_major__ 10
#define __clang_minor__ 0

Here is my compiler version:
	Apple LLVM version 10.0.0 (clang-1000.11.45.5)
	Target: x86_64-apple-darwin17.7.0

Also tested that on my system it would be OK to define _Noreturn to be
"__attribute__ ((__noreturn__))".  That option causes no warnings.

Thanks for your help. Hope there's a way to simplify/fix the logic in
the header.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: _Noreturn.h not respecting __STDC_VERSION__ (macOS, clang 10)
  2021-01-09  4:31 _Noreturn.h not respecting __STDC_VERSION__ (macOS, clang 10) Joe Nelson
@ 2021-01-09 22:21 ` Paul Eggert
  2021-01-10  4:23   ` Joe Nelson
  0 siblings, 1 reply; 3+ messages in thread
From: Paul Eggert @ 2021-01-09 22:21 UTC (permalink / raw)
  To: Joe Nelson; +Cc: bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 354 bytes --]

On 1/8/21 8:31 PM, Joe Nelson wrote:
> Hi, my project uses lib/_Noreturn.h, but gets a warning when compiled
> with "-std=c99 -pedantic":
> 
> 	warning: _Noreturn functions are a C11-specific feature

-pedantic can be such a pain sometimes, as can Clang. Thanks for 
reporting the problem, I guess. I installed the attached patch; please 
give it a try.

[-- Attachment #2: 0001-snippet-_Noreturn-port-to-pedantic-clang.patch --]
[-- Type: text/x-patch, Size: 5988 bytes --]

From 077ffc1e416a6be980dd45979547201e572962f6 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sat, 9 Jan 2021 14:17:32 -0800
Subject: [PATCH] snippet/_Noreturn: port to pedantic clang

Problem reported by Joe Nelson in:
https://lists.gnu.org/r/bug-gnulib/2021-01/msg00152.html
* doc/noreturn.texi: Improve.
* lib/_Noreturn.h (_Noreturn):
* m4/gnulib-common.m4 (gl_COMMON_BODY):
Do not assume _Noreturn works as-is when __STRICT_ANSI__ is
defined, unless __STDC_VERSION__ indicates C11 or later.
* lib/_Noreturn.h (_Noreturn): Fall back on __attribute__
((__noreturn__)) if Clang; this merges the
2020-08-10T23:53:13Zbruno@clisp.org patch to m4/gnulib-common.m4.
---
 ChangeLog           | 14 ++++++++++++++
 doc/noreturn.texi   | 20 ++++++++++++--------
 lib/_Noreturn.h     | 16 +++++++++-------
 m4/gnulib-common.m4 | 11 ++++++-----
 4 files changed, 41 insertions(+), 20 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b713bedb4..4329b81d9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2021-01-09  Paul Eggert  <eggert@cs.ucla.edu>
+
+	snippet/_Noreturn: port to pedantic clang
+	Problem reported by Joe Nelson in:
+	https://lists.gnu.org/r/bug-gnulib/2021-01/msg00152.html
+	* doc/noreturn.texi: Improve.
+	* lib/_Noreturn.h (_Noreturn):
+	* m4/gnulib-common.m4 (gl_COMMON_BODY):
+	Do not assume _Noreturn works as-is when __STRICT_ANSI__ is
+	defined, unless __STDC_VERSION__ indicates C11 or later.
+	* lib/_Noreturn.h (_Noreturn): Fall back on __attribute__
+	((__noreturn__)) if Clang; this merges the
+	2020-08-10T23:53:13Zbruno@clisp.org patch to m4/gnulib-common.m4.
+
 2021-01-09  Darshit Shah  <darnir@gnu.org>
 
 	Allow setting CVS username for gnu-web-doc-update.
diff --git a/doc/noreturn.texi b/doc/noreturn.texi
index e119ff88a..86bcc351e 100644
--- a/doc/noreturn.texi
+++ b/doc/noreturn.texi
@@ -23,17 +23,14 @@ declared with a @code{void} return type.
 It helps the compiler's ability to emit sensible warnings, following
 data-flow analysis, to declare which functions are non-returning.
 
+To decorate function declarations and function definitions, you can
+use the @code{_Noreturn} keyword.  No modules are needed, as Gnulib
+arranges for @code{<config.h>} to define @code{_Noreturn} to an
+appropriate replacement on platforms lacking it.
+
 Gnulib has two modules that support such a declaration:
 
 @itemize @bullet
-@item
-The @samp{stdnoreturn} module.  It provides a way to put this
-declaration at function declarations and function definitions, but not
-in function pointer types.  The identifier to use is @code{_Noreturn}
-or @code{noreturn}; @code{_Noreturn} is to be preferred because
-@code{noreturn} is a no-op on some platforms.  The include file is
-@code{<stdnoreturn.h>}.
-
 @item
 The @samp{noreturn} module.  It provides a way to put this declaration
 at function declarations, at function definitions, and in function
@@ -47,6 +44,13 @@ definitions.
 @end itemize
 @noindent
 The include file is @code{<noreturn.h>}.
+
+@item
+The @samp{stdnoreturn} module.  This can improve readability by
+letting you use @code{noreturn} instead of @code{_Noreturn};
+unfortunately, @code{noreturn} is a no-op on some platforms even
+though @code{_Noreturn} works on them.  The include file is
+@code{<stdnoreturn.h>}.
 @end itemize
 
 Which of the two modules to use?  If the non-returning functions you
diff --git a/lib/_Noreturn.h b/lib/_Noreturn.h
index f5003b9be..cb72f2620 100644
--- a/lib/_Noreturn.h
+++ b/lib/_Noreturn.h
@@ -26,14 +26,16 @@
        AIX system header files and several gnulib header files use precisely
        this syntax with 'extern'.  */
 #  define _Noreturn [[noreturn]]
-# elif ((!defined __cplusplus || defined __clang__)                     \
-        && (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0)  \
-            || 4 < __GNUC__ + (7 <= __GNUC_MINOR__) \
-            || (defined __apple_build_version__ \
-                ? 6000000 <= __apple_build_version__ \
-                : 3 < __clang_major__ + (5 <= __clang_minor__))))
+# elif ((!defined __cplusplus || defined __clang__) \
+        && (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) \
+            || (!defined __STRICT_ANSI__ \
+                && (__4 < __GNUC__ + (7 <= __GNUC_MINOR__) \
+                    || (defined __apple_build_version__ \
+                        ? 6000000 <= __apple_build_version__ \
+                        : 3 < __clang_major__ + (5 <= __clang_minor__))))))
    /* _Noreturn works as-is.  */
-# elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__) || 0x5110 <= __SUNPRO_C
+# elif (2 < __GNUC__ + (8 <= __GNUC_MINOR__) || defined __clang__ \
+        || 0x5110 <= __SUNPRO_C)
 #  define _Noreturn __attribute__ ((__noreturn__))
 # elif 1200 <= (defined _MSC_VER ? _MSC_VER : 0)
 #  define _Noreturn __declspec (noreturn)
diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4
index 535359b2c..3d87fd840 100644
--- a/m4/gnulib-common.m4
+++ b/m4/gnulib-common.m4
@@ -39,11 +39,12 @@ AC_DEFUN([gl_COMMON_BODY], [
        this syntax with 'extern'.  */
 #  define _Noreturn [[noreturn]]
 # elif ((!defined __cplusplus || defined __clang__) \
-        && (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0)  \
-            || _GL_GNUC_PREREQ (4, 7) \
-            || (defined __apple_build_version__ \
-                ? 6000000 <= __apple_build_version__ \
-                : 3 < __clang_major__ + (5 <= __clang_minor__))))
+        && (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) \
+            || (!defined __STRICT_ANSI__ \
+                && (_GL_GNUC_PREREQ (4, 7) \
+                    || (defined __apple_build_version__ \
+                        ? 6000000 <= __apple_build_version__ \
+                        : 3 < __clang_major__ + (5 <= __clang_minor__))))))
    /* _Noreturn works as-is.  */
 # elif _GL_GNUC_PREREQ (2, 8) || defined __clang__ || 0x5110 <= __SUNPRO_C
 #  define _Noreturn __attribute__ ((__noreturn__))
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: _Noreturn.h not respecting __STDC_VERSION__ (macOS, clang 10)
  2021-01-09 22:21 ` Paul Eggert
@ 2021-01-10  4:23   ` Joe Nelson
  0 siblings, 0 replies; 3+ messages in thread
From: Joe Nelson @ 2021-01-10  4:23 UTC (permalink / raw)
  To: Paul Eggert; +Cc: bug-gnulib

Paul Eggert wrote:
> On 1/8/21 8:31 PM, Joe Nelson wrote:
> > Hi, my project uses lib/_Noreturn.h, but gets a warning when compiled
> > with "-std=c99 -pedantic":
> > 
> > 	warning: _Noreturn functions are a C11-specific feature
> 
> -pedantic can be such a pain sometimes, as can Clang. Thanks for reporting
> the problem, I guess. I installed the attached patch; please give it a try.

Thanks, Paul! Your patch worked properly on my machine.

I didn't try the m4 change in any way; tested only the changed
lib/_Noreturn.h


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-01-10  4:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-09  4:31 _Noreturn.h not respecting __STDC_VERSION__ (macOS, clang 10) Joe Nelson
2021-01-09 22:21 ` Paul Eggert
2021-01-10  4:23   ` Joe Nelson

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).