Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Side by Side Diff: third_party/macros.h

Issue 605273004: Use static_assert unconditionally. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Add to AUTHORS Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file contains macros and macro-like constructs (e.g., templates) that 5 // This file contains macros and macro-like constructs (e.g., templates) that
6 // are commonly used throughout Chromium source. (It may also contain things 6 // are commonly used throughout Chromium source. (It may also contain things
7 // that are closely related to things that are commonly used that belong in this 7 // that are closely related to things that are commonly used that belong in this
8 // file.) 8 // file.)
9 9
10 #ifndef BASE_MACROS_H_ 10 #ifndef BASE_MACROS_H_
11 #define BASE_MACROS_H_ 11 #define BASE_MACROS_H_
12 12
13 // The COMPILE_ASSERT macro can be used to verify that a compile time 13 // The COMPILE_ASSERT macro can be used to verify that a compile time
14 // expression is true. For example, you could use it to verify the 14 // expression is true. For example, you could use it to verify the
15 // size of a static array: 15 // size of a static array:
16 // 16 //
17 // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES, 17 // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
18 // content_type_names_incorrect_size); 18 // content_type_names_incorrect_size);
19 // 19 //
20 // or to make sure a struct is smaller than a certain size: 20 // or to make sure a struct is smaller than a certain size:
21 // 21 //
22 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); 22 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
23 // 23 //
24 // The second argument to the macro is the name of the variable. If 24 // The second argument to the macro is the name of the variable. If
25 // the expression is false, most compilers will issue a warning/error 25 // the expression is false, most compilers will issue a warning/error
26 // containing the name of the variable. 26 // containing the name of the variable.
27 27
28 #undef COMPILE_ASSERT 28 #undef COMPILE_ASSERT
29
30 #if __cplusplus >= 201103L
31
32 // Under C++11, just use static_assert.
33 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) 29 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
34 30
35 #else
36
37 template <bool>
38 struct CompileAssert {
39 };
40
41 // Annotate a variable indicating it's ok if the variable is not used.
42 // (Typically used to silence a compiler warning when the assignment
43 // is important for some other reason.)
44 // Use like:
45 // int x ALLOW_UNUSED = ...;
46 #if defined(COMPILER_GCC)
47 #define ALLOW_UNUSED __attribute__((unused))
48 #else
49 #define ALLOW_UNUSED
50 #endif
51
52 #define COMPILE_ASSERT(expr, msg) \
53 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ALLOW_UNUSED
54
55 // Implementation details of COMPILE_ASSERT:
56 //
57 // - COMPILE_ASSERT works by defining an array type that has -1
58 // elements (and thus is invalid) when the expression is false.
59 //
60 // - The simpler definition
61 //
62 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
63 //
64 // does not work, as gcc supports variable-length arrays whose sizes
65 // are determined at run-time (this is gcc's extension and not part
66 // of the C++ standard). As a result, gcc fails to reject the
67 // following code with the simple definition:
68 //
69 // int foo;
70 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
71 // // not a compile-time constant.
72 //
73 // - By using the type CompileAssert<(bool(expr))>, we ensures that
74 // expr is a compile-time constant. (Template arguments must be
75 // determined at compile-time.)
76 //
77 // - The outer parentheses in CompileAssert<(bool(expr))> are necessary
78 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
79 //
80 // CompileAssert<bool(expr)>
81 //
82 // instead, these compilers will refuse to compile
83 //
84 // COMPILE_ASSERT(5 > 0, some_message);
85 //
86 // (They seem to think the ">" in "5 > 0" marks the end of the
87 // template argument list.)
88 //
89 // - The array size is (bool(expr) ? 1 : -1), instead of simply
90 //
91 // ((expr) ? 1 : -1).
92 //
93 // This is to avoid running into a bug in MS VC 7.1, which
94 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
95
96 #endif
97
98 #endif // BASE_MACROS_H_ 31 #endif // BASE_MACROS_H_
OLDNEW
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698