Discussion:
new module 'noreturn'
(too old to reply)
Bruno Haible
2017-04-26 21:53:14 UTC
Permalink
Raw Message
Here's a proposed module 'noreturn' (attached).
The most interesting file is this one:

================================ lib/noreturn.h ================================
/* Macros for declaring functions as non-returning.

Copyright (C) 2017 Free Software Foundation, Inc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

/* Written by Paul Eggert and Bruno Haible. */

#ifndef _NORETURN_H
#define _NORETURN_H 1

/* A "non-returning" function is a function which cannot return normally.
It can transfer control only through longjmp(), throw (in C++), or similar
mechanisms.

This file defines two macros _GL_NORETURN_FUNC and _GL_NORETURN_FUNCPTR,
that declare a function to be non-returning.
_GL_NORETURN_FUNC is for use in function declarations and function
definitions.
_GL_NORETURN_FUNCPTR is for use on function pointers.
*/

/* This file is based on the following compiler dependent language features:
- '_Noreturn' - standardized by ISO C11, available in C only (not in C++),
and not applicable to pointers.
- '[[noreturn]]' - standardized by ISO C++11, available in C++ only,
and not applicable to pointers.
- '__attribute__((__noreturn__))' - available in GCC and clang only,
both in C and C++.
- '__declspec(noreturn)' - available in MSVC only,
both in C and C++, not applicable to pointers.
*/

/* Declares that a function is nonreturning.
Use in C only code:
_GL_NORETURN_FUNC extern void func (void);
extern _GL_NORETURN_FUNC void func (void);
extern void _GL_NORETURN_FUNC func (void);
Use in C++ only code for a function with C linkage:
extern "C" { _GL_NORETURN_FUNC void func (void); }
Use in C++ only code for a function with C++ linkage:
_GL_NORETURN_FUNC extern void func (void);
Use in C & C++ code for a function with C linkage:
#ifdef __cplusplus
extern "C" {
#endif
_GL_NORETURN_FUNC void func (void);
#ifdef __cplusplus
}
#endif
Use in C & C++ code for a function with current language linkage:
_GL_NORETURN_FUNC extern void func (void);
*/
#if (3 <= __GNUC__ || (__GNUC__ == 2 && 8 <= __GNUC_MINOR__)) \
|| (0x5110 <= __SUNPRO_C)
/* For compatibility with _GL_NORETURN_FUNCPTR on clang, use
__attribute__((__noreturn__)), not _Noreturn. */
# define _GL_NORETURN_FUNC __attribute__ ((__noreturn__))
#elif 1200 <= _MSC_VER
/* Use MSVC specific syntax. */
# define _GL_NORETURN_FUNC __declspec (noreturn)
#elif defined __cplusplus
/* Use ISO C++11 syntax when the compiler supports it. */
# if (__cplusplus >= 201103 && !(__GNUC__ == 4 && __GNUC_MINOR__ == 7)) \
|| (_MSC_VER >= 1900)
# define _GL_NORETURN_FUNC [[noreturn]]
# else
# define _GL_NORETURN_FUNC /* empty */
# endif
#else
/* Use ISO C11 syntax when the compiler supports it. */
# if __STDC_VERSION__ >= 201112 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
# define _GL_NORETURN_FUNC _Noreturn
# else
# define _GL_NORETURN_FUNC /* empty */
# endif
#endif

/* Declares that a function is nonreturning.
Use in types and declarations that involve function pointers:
_GL_NORETURN_FUNCPTR void (*funcptr) (void);
*/
#if (3 <= __GNUC__ || (__GNUC__ == 2 && 8 <= __GNUC_MINOR__)) \
|| (0x5110 <= __SUNPRO_C)
# define _GL_NORETURN_FUNCPTR __attribute__ ((__noreturn__))
#else
# define _GL_NORETURN_FUNCPTR /* empty */
#endif

#endif /* _NORETURN_H */
Paul Eggert
2017-04-28 15:05:55 UTC
Permalink
Raw Message
I thought we had established that in some cases one should apply both
_Noreturn and __attribute__ ((__noreturn__)) to the same declaration? I
don't see that in this module.

Also, I hope we're not envisioning a lot of replacement of _Noreturn
with _GL_NORETURN_FUNC in other modules. As I understand it,
_GL_NORETURN_FUNC is needed only for functions that one wants to take
addresses of, and to store these addresses in _GL_NORETURN_FUNCPTR
variables. Most existing modules don't need to do this, and so can
continue to use _Noreturn. This point should be made in the comments.
Bruno Haible
2017-04-28 16:40:21 UTC
Permalink
Raw Message
Hi Paul,
Post by Paul Eggert
I thought we had established that in some cases one should apply both
_Noreturn and __attribute__ ((__noreturn__)) to the same declaration? I
don't see that in this module.
Apparently we've been thinking at different solutions of the clang problem
(which, for type checking / assignment purposes, considers _Noreturn to
be different from __attribute__((__noreturn__))).

My solution in the proposed noreturn.h is to
#define _GL_NORETURN_FUNC __attribute__ ((__noreturn__))
#define _GL_NORETURN_FUNCPTR __attribute__ ((__noreturn__))
in this case. So clang won't warn.

You have been thinking of this?
#define _GL_NORETURN_FUNC _Noreturn
#define _GL_NORETURN_FUNCPTR _Noreturn __attribute__ ((__noreturn__))
Post by Paul Eggert
Also, I hope we're not envisioning a lot of replacement of _Noreturn
with _GL_NORETURN_FUNC in other modules.
No, most of these functions (dfaerror, openat_save_fail, _Exit, xalloc_die, ...)
are not likely to be stored in function pointers.
Post by Paul Eggert
As I understand it,
_GL_NORETURN_FUNC is needed only for functions that one wants to take
addresses of, and to store these addresses in _GL_NORETURN_FUNCPTR
variables. Most existing modules don't need to do this, and so can
continue to use _Noreturn. This point should be made in the comments.
Good point. And for C++ support (other than g++ and MSVC) as well. I'm squashing
in this comment change:

--- a/lib/noreturn.h
+++ b/lib/noreturn.h
@@ -29,6 +29,16 @@
_GL_NORETURN_FUNC is for use in function declarations and function
definitions.
_GL_NORETURN_FUNCPTR is for use on function pointers.
+
+ Comparison of this file with <stdnoreturn.h>:
+ <stdnoreturn.h> defines a macro (or keyword) _Noreturn that declares
+ a function to be non-returning. _Noreturn is only for use in function
+ declarations and function definitions.
+ Therefore, if the non-returning functions you have to declare are unlikely
+ to be accessed through function pointers, and if the efficiency with C++
+ compilers other than g++, clang, MSVC++ is not an issue to you, you can use
+ module 'stdnoreturn' instead of this one, and _Noreturn instead of
+ _GL_NORETURN_FUNC.
*/

/* Declares that a function is nonreturning.
Bruno Haible
2017-04-28 22:21:23 UTC
Permalink
Raw Message
Post by Bruno Haible
You have been thinking of this?
#define _GL_NORETURN_FUNC _Noreturn
#define _GL_NORETURN_FUNCPTR _Noreturn __attribute__ ((__noreturn__))
This approach does not work:

===============================================================================
void func1 (void) { for (;;); }
_Noreturn void func2 (void) { for (;;); }
__attribute__ ((__noreturn__)) void func3 (void) { for (;;); }
_Noreturn __attribute__ ((__noreturn__)) void func4 (void) { for (;;); }

void (*fptr11) (void) = func1; /* GCC: OK clang: OK */
void (*fptr12) (void) = func2; /* GCC: OK clang: OK */
void (*fptr13) (void) = func3; /* GCC: OK clang: OK */
void (*fptr14) (void) = func4; /* GCC: OK clang: OK */

_Noreturn void (*fptr21) (void) = func1; /* GCC: warning clang: error */
_Noreturn void (*fptr22) (void) = func2; /* GCC: warning clang: error */
_Noreturn void (*fptr23) (void) = func3; /* GCC: warning clang: error */
_Noreturn void (*fptr24) (void) = func4; /* GCC: warning clang: error */

__attribute__ ((__noreturn__)) void (*fptr31) (void) = func1; /* GCC: warning clang: warning */
__attribute__ ((__noreturn__)) void (*fptr32) (void) = func2; /* GCC: OK clang: warning */
__attribute__ ((__noreturn__)) void (*fptr33) (void) = func3; /* GCC: OK clang: OK */
__attribute__ ((__noreturn__)) void (*fptr34) (void) = func4; /* GCC: OK clang: OK */

_Noreturn __attribute__ ((__noreturn__)) void (*fptr41) (void) = func1; /* GCC: warning clang: error */
_Noreturn __attribute__ ((__noreturn__)) void (*fptr42) (void) = func2; /* GCC: warning clang: error */
_Noreturn __attribute__ ((__noreturn__)) void (*fptr43) (void) = func3; /* GCC: warning clang: error */
_Noreturn __attribute__ ((__noreturn__)) void (*fptr44) (void) = func4; /* GCC: warning clang: error */
===============================================================================

I'm pushing the new module now.

Bruno
Paul Eggert
2017-04-29 00:04:09 UTC
Permalink
Raw Message
Post by Bruno Haible
You have been thinking of this?
#define _GL_NORETURN_FUNC _Noreturn
#define _GL_NORETURN_FUNCPTR _Noreturn __attribute__ ((__noreturn__))
It was more like this:

#define _GL_NORETURN_FUNC _Noreturn __attribute_noreturn__
#define _GL_NORETURN_FUNCPTR __attribute_noreturn__

where __attribute_noreturn__ expands to either __attribute__ ((__noreturn__)) or to nothing (much as _Noreturn already does). Perhaps your draft has an identical effect to this; I didn't look at its #ifdef forest all that carefully.
Bruno Haible
2017-04-29 10:51:18 UTC
Permalink
Raw Message
Post by Paul Eggert
#define _GL_NORETURN_FUNC _Noreturn __attribute_noreturn__
#define _GL_NORETURN_FUNCPTR __attribute_noreturn__
Per [1] (compare fptr33 and fptr34), this has identical effects as

#define _GL_NORETURN_FUNC __attribute_noreturn__
#define _GL_NORETURN_FUNCPTR __attribute_noreturn__

In other words, when __attribute__ ((__noreturn__)) works, there is
no point in even looking whether _Noreturn works as well.
Post by Paul Eggert
Perhaps your draft has an identical effect to this
Yes, it what I pushed has identical effects.

Bruno

[1] https://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00152.html
Loading...