summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2017-04-26 23:25:11 +0200
committerBruno Haible <bruno@clisp.org>2017-04-29 00:20:16 +0200
commit62f2dba6c1959cefca00bf6e1c168819e3bd05b9 (patch)
tree3f6c716db74a0e1061ca65fccaf9156ecd6a4e49
parent2a0c08e48834ae056964922ee6d21ccd223d1d80 (diff)
downloadgnulib-62f2dba6c1959cefca00bf6e1c168819e3bd05b9.tar.gz
noreturn: New module.
* lib/noreturn.h: New file. * modules/noreturn: New file. * tests/test-noreturn.c: New file. * modules/noreturn-tests: New file. * tests/test-noreturn-c++.cc: New file. * modules/noreturn-c++-tests: New file.
-rw-r--r--ChangeLog10
-rw-r--r--lib/noreturn.h111
-rw-r--r--modules/noreturn20
-rw-r--r--modules/noreturn-c++-tests17
-rw-r--r--modules/noreturn-tests11
-rw-r--r--tests/test-noreturn-c++.cc60
-rw-r--r--tests/test-noreturn.c68
7 files changed, 297 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 43e6639592..00d9277aaa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2017-04-28 Bruno Haible <bruno@clisp.org>
+
+ noreturn: New module.
+ * lib/noreturn.h: New file.
+ * modules/noreturn: New file.
+ * tests/test-noreturn.c: New file.
+ * modules/noreturn-tests: New file.
+ * tests/test-noreturn-c++.cc: New file.
+ * modules/noreturn-c++-tests: New file.
+
2017-04-27 Bruno Haible <bruno@clisp.org>
wctype-h: Fix compilation error with the original mingw.org mingw.
diff --git a/lib/noreturn.h b/lib/noreturn.h
new file mode 100644
index 0000000000..353a1e41b0
--- /dev/null
+++ b/lib/noreturn.h
@@ -0,0 +1,111 @@
+/* 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.
+
+ 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.
+ 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
+
+/* Comments about the 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.
+ */
+
+#endif /* _NORETURN_H */
diff --git a/modules/noreturn b/modules/noreturn
new file mode 100644
index 0000000000..388ee0c325
--- /dev/null
+++ b/modules/noreturn
@@ -0,0 +1,20 @@
+Description:
+Macros for declaring functions as non-returning.
+
+Files:
+lib/noreturn.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+
+Include:
+<noreturn.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Bruno Haible
diff --git a/modules/noreturn-c++-tests b/modules/noreturn-c++-tests
new file mode 100644
index 0000000000..e8a346650b
--- /dev/null
+++ b/modules/noreturn-c++-tests
@@ -0,0 +1,17 @@
+Files:
+tests/test-noreturn-c++.cc
+
+Status:
+c++-test
+
+Depends-on:
+ansi-c++-opt
+
+configure.ac:
+
+Makefile.am:
+if ANSICXX
+TESTS += test-noreturn-c++
+check_PROGRAMS += test-noreturn-c++
+test_noreturn_c___SOURCES = test-noreturn-c++.cc
+endif
diff --git a/modules/noreturn-tests b/modules/noreturn-tests
new file mode 100644
index 0000000000..61ec22b557
--- /dev/null
+++ b/modules/noreturn-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-noreturn.c
+
+Depends-on:
+noreturn-c++-tests
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-noreturn
+check_PROGRAMS += test-noreturn
diff --git a/tests/test-noreturn-c++.cc b/tests/test-noreturn-c++.cc
new file mode 100644
index 0000000000..d576b66f1e
--- /dev/null
+++ b/tests/test-noreturn-c++.cc
@@ -0,0 +1,60 @@
+/* Test of 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 Bruno Haible <bruno@clisp.org>, 2017. */
+
+#include <config.h>
+
+#include <noreturn.h>
+
+/* Test _GL_NORETURN_FUNC on function declarations. */
+
+extern "C" { _GL_NORETURN_FUNC void func1_c (void); }
+_GL_NORETURN_FUNC extern void func1_cxx (void);
+
+/* Test _GL_NORETURN_FUNC on function definitions. */
+
+_GL_NORETURN_FUNC void funcd_cxx (void)
+{
+ for (;;)
+ ;
+}
+
+/* Test _GL_NORETURN_FUNCPTR. */
+
+_GL_NORETURN_FUNCPTR void (*func1_c_ptr) (void) = func1_c;
+_GL_NORETURN_FUNCPTR void (*func1_cxx_ptr) (void) = func1_cxx;
+_GL_NORETURN_FUNCPTR void (*funcd_cxx_ptr) (void) = funcd_cxx;
+
+/* These could also be defined in a separate compilation unit. */
+
+void func1_c (void)
+{
+ for (;;)
+ ;
+}
+
+void func1_cxx (void)
+{
+ for (;;)
+ ;
+}
+
+
+int
+main ()
+{
+}
diff --git a/tests/test-noreturn.c b/tests/test-noreturn.c
new file mode 100644
index 0000000000..e7c162a069
--- /dev/null
+++ b/tests/test-noreturn.c
@@ -0,0 +1,68 @@
+/* Test of 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 Bruno Haible <bruno@clisp.org>, 2017. */
+
+#include <config.h>
+
+#include <noreturn.h>
+
+/* Test _GL_NORETURN_FUNC on function declarations. */
+
+_GL_NORETURN_FUNC extern void func1 (void);
+extern _GL_NORETURN_FUNC void func2 (void);
+extern void _GL_NORETURN_FUNC func3 (void);
+
+/* Test _GL_NORETURN_FUNC on function definitions. */
+
+_GL_NORETURN_FUNC void funcd (void)
+{
+ for (;;)
+ ;
+}
+
+/* Test _GL_NORETURN_FUNCPTR. */
+
+_GL_NORETURN_FUNCPTR void (*func1_ptr) (void) = func1;
+_GL_NORETURN_FUNCPTR void (*func2_ptr) (void) = func2;
+_GL_NORETURN_FUNCPTR void (*func3_ptr) (void) = func3;
+_GL_NORETURN_FUNCPTR void (*funcd_ptr) (void) = funcd;
+
+/* These could also be defined in a separate compilation unit. */
+
+void func1 (void)
+{
+ for (;;)
+ ;
+}
+
+void func2 (void)
+{
+ for (;;)
+ ;
+}
+
+void func3 (void)
+{
+ for (;;)
+ ;
+}
+
+
+int
+main ()
+{
+}