~ubuntu-branches/ubuntu/trusty/grub2/trusty

« back to all changes in this revision

Viewing changes to grub-core/gnulib/verify.h

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Compile-time assert-like macros.
2
2
 
3
 
   Copyright (C) 2005-2006, 2009-2010 Free Software Foundation, Inc.
 
3
   Copyright (C) 2005-2006, 2009-2013 Free Software Foundation, Inc.
4
4
 
5
5
   This program is free software: you can redistribute it and/or modify
6
6
   it under the terms of the GNU General Public License as published by
17
17
 
18
18
/* Written by Paul Eggert, Bruno Haible, and Jim Meyering.  */
19
19
 
20
 
#ifndef VERIFY_H
21
 
# define VERIFY_H 1
 
20
#ifndef _GL_VERIFY_H
 
21
# define _GL_VERIFY_H
 
22
 
 
23
 
 
24
/* Define _GL_HAVE__STATIC_ASSERT to 1 if _Static_assert works as per C11.
 
25
   This is supported by GCC 4.6.0 and later, in C mode, and its use
 
26
   here generates easier-to-read diagnostics when verify (R) fails.
 
27
 
 
28
   Define _GL_HAVE_STATIC_ASSERT to 1 if static_assert works as per C++11.
 
29
   This will likely be supported by future GCC versions, in C++ mode.
 
30
 
 
31
   Use this only with GCC.  If we were willing to slow 'configure'
 
32
   down we could also use it with other compilers, but since this
 
33
   affects only the quality of diagnostics, why bother?  */
 
34
# if (4 < __GNUC__ || (__GNUC__ == 4 && 6 <= __GNUC_MINOR__)) && !defined __cplusplus
 
35
#  define _GL_HAVE__STATIC_ASSERT 1
 
36
# endif
 
37
/* The condition (99 < __GNUC__) is temporary, until we know about the
 
38
   first G++ release that supports static_assert.  */
 
39
# if (99 < __GNUC__) && defined __cplusplus
 
40
#  define _GL_HAVE_STATIC_ASSERT 1
 
41
# endif
22
42
 
23
43
/* Each of these macros verifies that its argument R is nonzero.  To
24
44
   be portable, R should be an integer constant expression.  Unlike
25
45
   assert (R), there is no run-time overhead.
26
46
 
27
 
   There are two macros, since no single macro can be used in all
28
 
   contexts in C.  verify_true (R) is for scalar contexts, including
29
 
   integer constant expression contexts.  verify (R) is for declaration
30
 
   contexts, e.g., the top level.
31
 
 
32
 
   Symbols ending in "__" are private to this header.
33
 
 
34
 
   The code below uses several ideas.
 
47
   If _Static_assert works, verify (R) uses it directly.  Similarly,
 
48
   _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
 
49
   that is an operand of sizeof.
 
50
 
 
51
   The code below uses several ideas for C++ compilers, and for C
 
52
   compilers that do not support _Static_assert:
35
53
 
36
54
   * The first step is ((R) ? 1 : -1).  Given an expression R, of
37
55
     integral or boolean or floating-point type, this yields an
39
57
     constant and nonnegative.
40
58
 
41
59
   * Next this expression W is wrapped in a type
42
 
     struct verify_type__ { unsigned int verify_error_if_negative_size__: W; }.
 
60
     struct _gl_verify_type {
 
61
       unsigned int _gl_verify_error_if_negative: W;
 
62
     }.
43
63
     If W is negative, this yields a compile-time error.  No compiler can
44
64
     deal with a bit-field of negative size.
45
65
 
53
73
 
54
74
       void function (int n) { verify (n < 0); }
55
75
 
56
 
   * For the verify macro, the struct verify_type__ will need to
 
76
   * For the verify macro, the struct _gl_verify_type will need to
57
77
     somehow be embedded into a declaration.  To be portable, this
58
78
     declaration must declare an object, a constant, a function, or a
59
79
     typedef name.  If the declared entity uses the type directly,
91
111
     Which of the following alternatives can be used?
92
112
 
93
113
       extern int dummy [sizeof (struct {...})];
94
 
       extern int dummy [sizeof (struct verify_type__ {...})];
 
114
       extern int dummy [sizeof (struct _gl_verify_type {...})];
95
115
       extern void dummy (int [sizeof (struct {...})]);
96
 
       extern void dummy (int [sizeof (struct verify_type__ {...})]);
 
116
       extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
97
117
       extern int (*dummy (void)) [sizeof (struct {...})];
98
 
       extern int (*dummy (void)) [sizeof (struct verify_type__ {...})];
 
118
       extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
99
119
 
100
120
     In the second and sixth case, the struct type is exported to the
101
121
     outer scope; two such declarations therefore collide.  GCC warns
105
125
       extern int (*dummy (void)) [sizeof (struct {...})];
106
126
 
107
127
   * GCC warns about duplicate declarations of the dummy function if
108
 
     -Wredundant_decls is used.  GCC 4.3 and later have a builtin
 
128
     -Wredundant-decls is used.  GCC 4.3 and later have a builtin
109
129
     __COUNTER__ macro that can let us generate unique identifiers for
110
130
     each dummy function, to suppress this warning.
111
131
 
112
 
   * This implementation exploits the fact that GCC does not warn about
113
 
     the last declaration mentioned above.  If a future version of GCC
114
 
     introduces a warning for this, the problem could be worked around
115
 
     by using code specialized to GCC, just as __COUNTER__ is already
116
 
     being used if available.
 
132
   * This implementation exploits the fact that older versions of GCC,
 
133
     which do not support _Static_assert, also do not warn about the
 
134
     last declaration mentioned above.
117
135
 
118
 
       #if 4 <= __GNUC__
119
 
       # define verify(R) [another version to keep GCC happy]
120
 
       #endif
 
136
   * GCC warns if -Wnested-externs is enabled and verify() is used
 
137
     within a function body; but inside a function, you can always
 
138
     arrange to use verify_expr() instead.
121
139
 
122
140
   * In C++, any struct definition inside sizeof is invalid.
123
141
     Use a template type to work around the problem.  */
140
158
   possible.  */
141
159
# define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
142
160
 
143
 
/* Verify requirement R at compile-time, as an integer constant expression.
144
 
   Return 1.  */
 
161
/* Verify requirement R at compile-time, as an integer constant expression
 
162
   that returns 1.  If R is false, fail at compile-time, preferably
 
163
   with a diagnostic that includes the string-literal DIAGNOSTIC.  */
 
164
 
 
165
# define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
 
166
    (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
145
167
 
146
168
# ifdef __cplusplus
 
169
#  if !GNULIB_defined_struct__gl_verify_type
147
170
template <int w>
148
 
  struct verify_type__ { unsigned int verify_error_if_negative_size__: w; };
149
 
#  define verify_true(R) \
150
 
     (!!sizeof (verify_type__<(R) ? 1 : -1>))
151
 
# else
152
 
#  define verify_true(R) \
153
 
     (!!sizeof \
154
 
      (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
155
 
# endif
 
171
  struct _gl_verify_type {
 
172
    unsigned int _gl_verify_error_if_negative: w;
 
173
  };
 
174
#   define GNULIB_defined_struct__gl_verify_type 1
 
175
#  endif
 
176
#  define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
 
177
    _gl_verify_type<(R) ? 1 : -1>
 
178
# elif defined _GL_HAVE__STATIC_ASSERT
 
179
#  define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
 
180
     struct {                                   \
 
181
       _Static_assert (R, DIAGNOSTIC);          \
 
182
       int _gl_dummy;                          \
 
183
     }
 
184
# else
 
185
#  define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
 
186
     struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
 
187
# endif
 
188
 
 
189
/* Verify requirement R at compile-time, as a declaration without a
 
190
   trailing ';'.  If R is false, fail at compile-time, preferably
 
191
   with a diagnostic that includes the string-literal DIAGNOSTIC.
 
192
 
 
193
   Unfortunately, unlike C11, this implementation must appear as an
 
194
   ordinary declaration, and cannot appear inside struct { ... }.  */
 
195
 
 
196
# ifdef _GL_HAVE__STATIC_ASSERT
 
197
#  define _GL_VERIFY _Static_assert
 
198
# else
 
199
#  define _GL_VERIFY(R, DIAGNOSTIC)                                    \
 
200
     extern int (*_GL_GENSYM (_gl_verify_function) (void))             \
 
201
       [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
 
202
# endif
 
203
 
 
204
/* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h.  */
 
205
# ifdef _GL_STATIC_ASSERT_H
 
206
#  if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
 
207
#   define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
 
208
#  endif
 
209
#  if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
 
210
#   define static_assert _Static_assert /* C11 requires this #define.  */
 
211
#  endif
 
212
# endif
 
213
 
 
214
/* @assert.h omit start@  */
 
215
 
 
216
/* Each of these macros verifies that its argument R is nonzero.  To
 
217
   be portable, R should be an integer constant expression.  Unlike
 
218
   assert (R), there is no run-time overhead.
 
219
 
 
220
   There are two macros, since no single macro can be used in all
 
221
   contexts in C.  verify_true (R) is for scalar contexts, including
 
222
   integer constant expression contexts.  verify (R) is for declaration
 
223
   contexts, e.g., the top level.  */
 
224
 
 
225
/* Verify requirement R at compile-time, as an integer constant expression.
 
226
   Return 1.  This is equivalent to verify_expr (R, 1).
 
227
 
 
228
   verify_true is obsolescent; please use verify_expr instead.  */
 
229
 
 
230
# define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
 
231
 
 
232
/* Verify requirement R at compile-time.  Return the value of the
 
233
   expression E.  */
 
234
 
 
235
# define verify_expr(R, E) \
 
236
    (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
156
237
 
157
238
/* Verify requirement R at compile-time, as a declaration without a
158
239
   trailing ';'.  */
159
240
 
160
 
# define verify(R) \
161
 
    extern int (* _GL_GENSYM (verify_function) (void)) [verify_true (R)]
 
241
# define verify(R) _GL_VERIFY (R, "verify (" #R ")")
 
242
 
 
243
/* @assert.h omit end@  */
162
244
 
163
245
#endif