1cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Compile-time assert-like macros. 2cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 305436638acc7c010349a69c3395f1a57c642dc62Ying Wang Copyright (C) 2005-2006, 2009-2012 Free Software Foundation, Inc. 4cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 505436638acc7c010349a69c3395f1a57c642dc62Ying Wang This program is free software: you can redistribute it and/or modify 6cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project it under the terms of the GNU General Public License as published by 705436638acc7c010349a69c3395f1a57c642dc62Ying Wang the Free Software Foundation; either version 3 of the License, or 805436638acc7c010349a69c3395f1a57c642dc62Ying Wang (at your option) any later version. 9cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 10cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project This program is distributed in the hope that it will be useful, 11cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project but WITHOUT ANY WARRANTY; without even the implied warranty of 12cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project GNU General Public License for more details. 14cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 15cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project You should have received a copy of the GNU General Public License 1605436638acc7c010349a69c3395f1a57c642dc62Ying Wang along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 18cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */ 19cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 2005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#ifndef _GL_VERIFY_H 2105436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_VERIFY_H 2205436638acc7c010349a69c3395f1a57c642dc62Ying Wang 2305436638acc7c010349a69c3395f1a57c642dc62Ying Wang 2405436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Define _GL_HAVE__STATIC_ASSERT to 1 if _Static_assert works as per C11. 2505436638acc7c010349a69c3395f1a57c642dc62Ying Wang This is supported by GCC 4.6.0 and later, in C mode, and its use 2605436638acc7c010349a69c3395f1a57c642dc62Ying Wang here generates easier-to-read diagnostics when verify (R) fails. 2705436638acc7c010349a69c3395f1a57c642dc62Ying Wang 2805436638acc7c010349a69c3395f1a57c642dc62Ying Wang Define _GL_HAVE_STATIC_ASSERT to 1 if static_assert works as per C++11. 2905436638acc7c010349a69c3395f1a57c642dc62Ying Wang This will likely be supported by future GCC versions, in C++ mode. 3005436638acc7c010349a69c3395f1a57c642dc62Ying Wang 3105436638acc7c010349a69c3395f1a57c642dc62Ying Wang Use this only with GCC. If we were willing to slow 'configure' 3205436638acc7c010349a69c3395f1a57c642dc62Ying Wang down we could also use it with other compilers, but since this 3305436638acc7c010349a69c3395f1a57c642dc62Ying Wang affects only the quality of diagnostics, why bother? */ 3405436638acc7c010349a69c3395f1a57c642dc62Ying Wang# if (4 < __GNUC__ || (__GNUC__ == 4 && 6 <= __GNUC_MINOR__)) && !defined __cplusplus 3505436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_HAVE__STATIC_ASSERT 1 3605436638acc7c010349a69c3395f1a57c642dc62Ying Wang# endif 3705436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* The condition (99 < __GNUC__) is temporary, until we know about the 3805436638acc7c010349a69c3395f1a57c642dc62Ying Wang first G++ release that supports static_assert. */ 3905436638acc7c010349a69c3395f1a57c642dc62Ying Wang# if (99 < __GNUC__) && defined __cplusplus 4005436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_HAVE_STATIC_ASSERT 1 4105436638acc7c010349a69c3395f1a57c642dc62Ying Wang# endif 42cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 43cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Each of these macros verifies that its argument R is nonzero. To 44cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project be portable, R should be an integer constant expression. Unlike 45cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project assert (R), there is no run-time overhead. 46cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 4705436638acc7c010349a69c3395f1a57c642dc62Ying Wang If _Static_assert works, verify (R) uses it directly. Similarly, 4805436638acc7c010349a69c3395f1a57c642dc62Ying Wang _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct 4905436638acc7c010349a69c3395f1a57c642dc62Ying Wang that is an operand of sizeof. 50cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 5105436638acc7c010349a69c3395f1a57c642dc62Ying Wang The code below uses several ideas for C++ compilers, and for C 5205436638acc7c010349a69c3395f1a57c642dc62Ying Wang compilers that do not support _Static_assert: 53cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 54cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project * The first step is ((R) ? 1 : -1). Given an expression R, of 55cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project integral or boolean or floating-point type, this yields an 56cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project expression of integral type, whose value is later verified to be 57cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project constant and nonnegative. 58cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 59cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project * Next this expression W is wrapped in a type 6005436638acc7c010349a69c3395f1a57c642dc62Ying Wang struct _gl_verify_type { 6105436638acc7c010349a69c3395f1a57c642dc62Ying Wang unsigned int _gl_verify_error_if_negative: W; 6205436638acc7c010349a69c3395f1a57c642dc62Ying Wang }. 63cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project If W is negative, this yields a compile-time error. No compiler can 64cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project deal with a bit-field of negative size. 65cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 66cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project One might think that an array size check would have the same 67cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project effect, that is, that the type struct { unsigned int dummy[W]; } 68cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project would work as well. However, inside a function, some compilers 69cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project (such as C++ compilers and GNU C) allow local parameters and 70cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project variables inside array size expressions. With these compilers, 71cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project an array size check would not properly diagnose this misuse of 72cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project the verify macro: 73cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 74cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project void function (int n) { verify (n < 0); } 75cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 7605436638acc7c010349a69c3395f1a57c642dc62Ying Wang * For the verify macro, the struct _gl_verify_type will need to 77cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project somehow be embedded into a declaration. To be portable, this 78cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project declaration must declare an object, a constant, a function, or a 79cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project typedef name. If the declared entity uses the type directly, 80cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project such as in 81cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 82cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project struct dummy {...}; 83cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project typedef struct {...} dummy; 84cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern struct {...} *dummy; 85cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern void dummy (struct {...} *); 86cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern struct {...} *dummy (void); 87cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 88cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project two uses of the verify macro would yield colliding declarations 89cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project if the entity names are not disambiguated. A workaround is to 90cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project attach the current line number to the entity name: 91cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 9205436638acc7c010349a69c3395f1a57c642dc62Ying Wang #define _GL_CONCAT0(x, y) x##y 9305436638acc7c010349a69c3395f1a57c642dc62Ying Wang #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y) 9405436638acc7c010349a69c3395f1a57c642dc62Ying Wang extern struct {...} * _GL_CONCAT (dummy, __LINE__); 95cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 96cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project But this has the problem that two invocations of verify from 97cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project within the same macro would collide, since the __LINE__ value 9805436638acc7c010349a69c3395f1a57c642dc62Ying Wang would be the same for both invocations. (The GCC __COUNTER__ 9905436638acc7c010349a69c3395f1a57c642dc62Ying Wang macro solves this problem, but is not portable.) 100cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 101cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project A solution is to use the sizeof operator. It yields a number, 102cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project getting rid of the identity of the type. Declarations like 103cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 104cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern int dummy [sizeof (struct {...})]; 105cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern void dummy (int [sizeof (struct {...})]); 106cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern int (*dummy (void)) [sizeof (struct {...})]; 107cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 108cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project can be repeated. 109cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 110cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project * Should the implementation use a named struct or an unnamed struct? 111cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project Which of the following alternatives can be used? 112cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 113cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern int dummy [sizeof (struct {...})]; 11405436638acc7c010349a69c3395f1a57c642dc62Ying Wang extern int dummy [sizeof (struct _gl_verify_type {...})]; 115cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern void dummy (int [sizeof (struct {...})]); 11605436638acc7c010349a69c3395f1a57c642dc62Ying Wang extern void dummy (int [sizeof (struct _gl_verify_type {...})]); 117cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern int (*dummy (void)) [sizeof (struct {...})]; 11805436638acc7c010349a69c3395f1a57c642dc62Ying Wang extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})]; 119cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 120cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project In the second and sixth case, the struct type is exported to the 121cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project outer scope; two such declarations therefore collide. GCC warns 122cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project about the first, third, and fourth cases. So the only remaining 123cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project possibility is the fifth case: 124cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 125cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project extern int (*dummy (void)) [sizeof (struct {...})]; 126cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 12705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * GCC warns about duplicate declarations of the dummy function if 12805436638acc7c010349a69c3395f1a57c642dc62Ying Wang -Wredundant-decls is used. GCC 4.3 and later have a builtin 12905436638acc7c010349a69c3395f1a57c642dc62Ying Wang __COUNTER__ macro that can let us generate unique identifiers for 13005436638acc7c010349a69c3395f1a57c642dc62Ying Wang each dummy function, to suppress this warning. 13105436638acc7c010349a69c3395f1a57c642dc62Ying Wang 13205436638acc7c010349a69c3395f1a57c642dc62Ying Wang * This implementation exploits the fact that older versions of GCC, 13305436638acc7c010349a69c3395f1a57c642dc62Ying Wang which do not support _Static_assert, also do not warn about the 13405436638acc7c010349a69c3395f1a57c642dc62Ying Wang last declaration mentioned above. 135cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 13605436638acc7c010349a69c3395f1a57c642dc62Ying Wang * GCC warns if -Wnested-externs is enabled and verify() is used 13705436638acc7c010349a69c3395f1a57c642dc62Ying Wang within a function body; but inside a function, you can always 13805436638acc7c010349a69c3395f1a57c642dc62Ying Wang arrange to use verify_expr() instead. 139cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 140cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project * In C++, any struct definition inside sizeof is invalid. 141cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project Use a template type to work around the problem. */ 142cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 14305436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Concatenate two preprocessor tokens. */ 14405436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y) 14505436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_CONCAT0(x, y) x##y 146cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 14705436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* _GL_COUNTER is an integer, preferably one that changes each time we 14805436638acc7c010349a69c3395f1a57c642dc62Ying Wang use it. Use __COUNTER__ if it works, falling back on __LINE__ 14905436638acc7c010349a69c3395f1a57c642dc62Ying Wang otherwise. __LINE__ isn't perfect, but it's better than a 15005436638acc7c010349a69c3395f1a57c642dc62Ying Wang constant. */ 15105436638acc7c010349a69c3395f1a57c642dc62Ying Wang# if defined __COUNTER__ && __COUNTER__ != __COUNTER__ 15205436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_COUNTER __COUNTER__ 15305436638acc7c010349a69c3395f1a57c642dc62Ying Wang# else 15405436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_COUNTER __LINE__ 15505436638acc7c010349a69c3395f1a57c642dc62Ying Wang# endif 15605436638acc7c010349a69c3395f1a57c642dc62Ying Wang 15705436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Generate a symbol with the given prefix, making it unique if 15805436638acc7c010349a69c3395f1a57c642dc62Ying Wang possible. */ 15905436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER) 16005436638acc7c010349a69c3395f1a57c642dc62Ying Wang 16105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Verify requirement R at compile-time, as an integer constant expression 16205436638acc7c010349a69c3395f1a57c642dc62Ying Wang that returns 1. If R is false, fail at compile-time, preferably 16305436638acc7c010349a69c3395f1a57c642dc62Ying Wang with a diagnostic that includes the string-literal DIAGNOSTIC. */ 16405436638acc7c010349a69c3395f1a57c642dc62Ying Wang 16505436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \ 16605436638acc7c010349a69c3395f1a57c642dc62Ying Wang (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC))) 167cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 168cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project# ifdef __cplusplus 16905436638acc7c010349a69c3395f1a57c642dc62Ying Wang# if !GNULIB_defined_struct__gl_verify_type 170cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projecttemplate <int w> 17105436638acc7c010349a69c3395f1a57c642dc62Ying Wang struct _gl_verify_type { 17205436638acc7c010349a69c3395f1a57c642dc62Ying Wang unsigned int _gl_verify_error_if_negative: w; 17305436638acc7c010349a69c3395f1a57c642dc62Ying Wang }; 17405436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define GNULIB_defined_struct__gl_verify_type 1 17505436638acc7c010349a69c3395f1a57c642dc62Ying Wang# endif 17605436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \ 17705436638acc7c010349a69c3395f1a57c642dc62Ying Wang _gl_verify_type<(R) ? 1 : -1> 17805436638acc7c010349a69c3395f1a57c642dc62Ying Wang# elif defined _GL_HAVE__STATIC_ASSERT 17905436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \ 18005436638acc7c010349a69c3395f1a57c642dc62Ying Wang struct { \ 18105436638acc7c010349a69c3395f1a57c642dc62Ying Wang _Static_assert (R, DIAGNOSTIC); \ 18205436638acc7c010349a69c3395f1a57c642dc62Ying Wang int _gl_dummy; \ 18305436638acc7c010349a69c3395f1a57c642dc62Ying Wang } 184cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project# else 18505436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \ 18605436638acc7c010349a69c3395f1a57c642dc62Ying Wang struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; } 187cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project# endif 188cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 189cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Verify requirement R at compile-time, as a declaration without a 19005436638acc7c010349a69c3395f1a57c642dc62Ying Wang trailing ';'. If R is false, fail at compile-time, preferably 19105436638acc7c010349a69c3395f1a57c642dc62Ying Wang with a diagnostic that includes the string-literal DIAGNOSTIC. 19205436638acc7c010349a69c3395f1a57c642dc62Ying Wang 19305436638acc7c010349a69c3395f1a57c642dc62Ying Wang Unfortunately, unlike C11, this implementation must appear as an 19405436638acc7c010349a69c3395f1a57c642dc62Ying Wang ordinary declaration, and cannot appear inside struct { ... }. */ 19505436638acc7c010349a69c3395f1a57c642dc62Ying Wang 19605436638acc7c010349a69c3395f1a57c642dc62Ying Wang# ifdef _GL_HAVE__STATIC_ASSERT 19705436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_VERIFY _Static_assert 19805436638acc7c010349a69c3395f1a57c642dc62Ying Wang# else 19905436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _GL_VERIFY(R, DIAGNOSTIC) \ 20005436638acc7c010349a69c3395f1a57c642dc62Ying Wang extern int (*_GL_GENSYM (_gl_verify_function) (void)) \ 20105436638acc7c010349a69c3395f1a57c642dc62Ying Wang [_GL_VERIFY_TRUE (R, DIAGNOSTIC)] 20205436638acc7c010349a69c3395f1a57c642dc62Ying Wang# endif 20305436638acc7c010349a69c3395f1a57c642dc62Ying Wang 20405436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */ 20505436638acc7c010349a69c3395f1a57c642dc62Ying Wang# ifdef _GL_STATIC_ASSERT_H 20605436638acc7c010349a69c3395f1a57c642dc62Ying Wang# if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert 20705436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC) 20805436638acc7c010349a69c3395f1a57c642dc62Ying Wang# endif 20905436638acc7c010349a69c3395f1a57c642dc62Ying Wang# if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert 21005436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define static_assert _Static_assert /* C11 requires this #define. */ 21105436638acc7c010349a69c3395f1a57c642dc62Ying Wang# endif 21205436638acc7c010349a69c3395f1a57c642dc62Ying Wang# endif 21305436638acc7c010349a69c3395f1a57c642dc62Ying Wang 21405436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* @assert.h omit start@ */ 21505436638acc7c010349a69c3395f1a57c642dc62Ying Wang 21605436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Each of these macros verifies that its argument R is nonzero. To 21705436638acc7c010349a69c3395f1a57c642dc62Ying Wang be portable, R should be an integer constant expression. Unlike 21805436638acc7c010349a69c3395f1a57c642dc62Ying Wang assert (R), there is no run-time overhead. 21905436638acc7c010349a69c3395f1a57c642dc62Ying Wang 22005436638acc7c010349a69c3395f1a57c642dc62Ying Wang There are two macros, since no single macro can be used in all 22105436638acc7c010349a69c3395f1a57c642dc62Ying Wang contexts in C. verify_true (R) is for scalar contexts, including 22205436638acc7c010349a69c3395f1a57c642dc62Ying Wang integer constant expression contexts. verify (R) is for declaration 22305436638acc7c010349a69c3395f1a57c642dc62Ying Wang contexts, e.g., the top level. */ 22405436638acc7c010349a69c3395f1a57c642dc62Ying Wang 22505436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Verify requirement R at compile-time, as an integer constant expression. 22605436638acc7c010349a69c3395f1a57c642dc62Ying Wang Return 1. This is equivalent to verify_expr (R, 1). 22705436638acc7c010349a69c3395f1a57c642dc62Ying Wang 22805436638acc7c010349a69c3395f1a57c642dc62Ying Wang verify_true is obsolescent; please use verify_expr instead. */ 22905436638acc7c010349a69c3395f1a57c642dc62Ying Wang 23005436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")") 23105436638acc7c010349a69c3395f1a57c642dc62Ying Wang 23205436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Verify requirement R at compile-time. Return the value of the 23305436638acc7c010349a69c3395f1a57c642dc62Ying Wang expression E. */ 23405436638acc7c010349a69c3395f1a57c642dc62Ying Wang 23505436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define verify_expr(R, E) \ 23605436638acc7c010349a69c3395f1a57c642dc62Ying Wang (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E)) 23705436638acc7c010349a69c3395f1a57c642dc62Ying Wang 23805436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Verify requirement R at compile-time, as a declaration without a 239cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project trailing ';'. */ 240cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 24105436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define verify(R) _GL_VERIFY (R, "verify (" #R ")") 24205436638acc7c010349a69c3395f1a57c642dc62Ying Wang 24305436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* @assert.h omit end@ */ 244cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project 245cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#endif 246