diff options
Diffstat (limited to 'assert')
-rw-r--r-- | assert/Makefile | 13 | ||||
-rw-r--r-- | assert/__assert.c | 2 | ||||
-rw-r--r-- | assert/assert-perr.c | 4 | ||||
-rw-r--r-- | assert/assert.c | 2 | ||||
-rw-r--r-- | assert/assert.h | 35 | ||||
-rw-r--r-- | assert/tst-assert-c++.cc | 78 | ||||
-rw-r--r-- | assert/tst-assert-g++.cc | 19 |
7 files changed, 141 insertions, 12 deletions
diff --git a/assert/Makefile b/assert/Makefile index cd0d809220..e2141e1200 100644 --- a/assert/Makefile +++ b/assert/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 1991-2016 Free Software Foundation, Inc. +# Copyright (C) 1991-2018 Free Software Foundation, Inc. # This file is part of the GNU C Library. # The GNU C Library is free software; you can redistribute it and/or @@ -25,6 +25,15 @@ include ../Makeconfig headers := assert.h routines := assert assert-perr __assert -tests := test-assert test-assert-perr +tests := test-assert test-assert-perr tst-assert-c++ tst-assert-g++ + +ifeq ($(have-cxx-thread_local),yes) +CFLAGS-tst-assert-c++.o = -std=c++11 +LDLIBS-tst-assert-c++ = -lstdc++ +CFLAGS-tst-assert-g++.o = -std=gnu++11 +LDLIBS-tst-assert-g++ = -lstdc++ +else +tests-unsupported += tst-assert-c++ tst-assert-g++ +endif include ../Rules diff --git a/assert/__assert.c b/assert/__assert.c index 75c6852bcf..c18bbc1a12 100644 --- a/assert/__assert.c +++ b/assert/__assert.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2000-2016 Free Software Foundation, Inc. +/* Copyright (C) 2000-2018 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or diff --git a/assert/assert-perr.c b/assert/assert-perr.c index f4fc08fcab..4b4fe883fd 100644 --- a/assert/assert-perr.c +++ b/assert/assert-perr.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1994-2016 Free Software Foundation, Inc. +/* Copyright (C) 1994-2018 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -32,7 +32,7 @@ __assert_perror_fail (int errnum, char errbuf[1024]; char *e = __strerror_r (errnum, errbuf, sizeof errbuf); - __assert_fail_base (_("%s%s%s:%u: %s%sUnexpected error: %s.\n"), + __assert_fail_base (_("%s%s%s:%u: %s%sUnexpected error: %s.\n%n"), e, file, line, function); } libc_hidden_def (__assert_perror_fail) diff --git a/assert/assert.c b/assert/assert.c index 8e8e29833b..8ed691bd32 100644 --- a/assert/assert.c +++ b/assert/assert.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991-2016 Free Software Foundation, Inc. +/* Copyright (C) 1991-2018 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or diff --git a/assert/assert.h b/assert/assert.h index 729edeb949..5468c16c2c 100644 --- a/assert/assert.h +++ b/assert/assert.h @@ -1,4 +1,4 @@ -/* Copyright (C) 1991-2016 Free Software Foundation, Inc. +/* Copyright (C) 1991-2018 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -82,10 +82,33 @@ extern void __assert (const char *__assertion, const char *__file, int __line) __END_DECLS -# define assert(expr) \ - ((expr) \ - ? __ASSERT_VOID_CAST (0) \ - : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION)) +/* When possible, define assert so that it does not add extra + parentheses around EXPR. Otherwise, those added parentheses would + suppress warnings we'd expect to be detected by gcc's -Wparentheses. */ +# if defined __cplusplus +# define assert(expr) \ + (static_cast <bool> (expr) \ + ? void (0) \ + : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION)) +# elif !defined __GNUC__ || defined __STRICT_ANSI__ +# define assert(expr) \ + ((expr) \ + ? __ASSERT_VOID_CAST (0) \ + : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION)) +# else +/* The first occurrence of EXPR is not evaluated due to the sizeof, + but will trigger any pedantic warnings masked by the __extension__ + for the second occurrence. The ternary operator is required to + support function pointers and bit fields in this context, and to + suppress the evaluation of variable length arrays. */ +# define assert(expr) \ + ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \ + if (expr) \ + ; /* empty */ \ + else \ + __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \ + })) +# endif # ifdef __USE_GNU # define assert_perror(errnum) \ @@ -100,7 +123,7 @@ __END_DECLS C9x has a similar variable called __func__, but prefer the GCC one since it demangles C++ function names. */ # if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4) -# define __ASSERT_FUNCTION __PRETTY_FUNCTION__ +# define __ASSERT_FUNCTION __extension__ __PRETTY_FUNCTION__ # else # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L # define __ASSERT_FUNCTION __func__ diff --git a/assert/tst-assert-c++.cc b/assert/tst-assert-c++.cc new file mode 100644 index 0000000000..fa83c5dc37 --- /dev/null +++ b/assert/tst-assert-c++.cc @@ -0,0 +1,78 @@ +/* Tests for interactions between C++ and assert. + Copyright (C) 2017-2018 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#include <assert.h> + +/* The C++ standard requires that if the assert argument is a constant + subexpression, then the assert itself is one, too. */ +constexpr int +check_constexpr () +{ + return (assert (true), 1); +} + +/* Objects of this class can be contextually converted to bool, but + cannot be compared to int. */ +struct no_int +{ + no_int () = default; + no_int (const no_int &) = delete; + + explicit operator bool () const + { + return true; + } + + bool operator! () const; /* No definition. */ + template <class T> bool operator== (T) const; /* No definition. */ + template <class T> bool operator!= (T) const; /* No definition. */ +}; + +/* This class tests that operator== is not used by assert. */ +struct bool_and_int +{ + bool_and_int () = default; + bool_and_int (const no_int &) = delete; + + explicit operator bool () const + { + return true; + } + + bool operator! () const; /* No definition. */ + template <class T> bool operator== (T) const; /* No definition. */ + template <class T> bool operator!= (T) const; /* No definition. */ +}; + +static int +do_test () +{ + { + no_int value; + assert (value); + } + + { + bool_and_int value; + assert (value); + } + + return 0; +} + +#include <support/test-driver.c> diff --git a/assert/tst-assert-g++.cc b/assert/tst-assert-g++.cc new file mode 100644 index 0000000000..d5a07252db --- /dev/null +++ b/assert/tst-assert-g++.cc @@ -0,0 +1,19 @@ +/* Tests for interactions between C++ and assert. GNU C++11 version. + Copyright (C) 2017-2018 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#include <tst-assert-c++.cc> |