• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_BASE_COMPILER_SPECIFIC_H_
6 #define V8_BASE_COMPILER_SPECIFIC_H_
7 
8 #include "include/v8config.h"
9 
10 // Annotate a  using ALLOW_UNUSED_TYPE = or function indicating it's ok if it's
11 // not used. Use like:
12 //    using Bar = Foo;
13 #if V8_HAS_ATTRIBUTE_UNUSED
14 #define ALLOW_UNUSED_TYPE __attribute__((unused))
15 #else
16 #define ALLOW_UNUSED_TYPE
17 #endif
18 
19 // Tell the compiler a function is using a printf-style format string.
20 // |format_param| is the one-based index of the format string parameter;
21 // |dots_param| is the one-based index of the "..." parameter.
22 // For v*printf functions (which take a va_list), pass 0 for dots_param.
23 // (This is undocumented but matches what the system C headers do.)
24 #if defined(__GNUC__)
25 #define PRINTF_FORMAT(format_param, dots_param) \
26   __attribute__((format(printf, format_param, dots_param)))
27 #else
28 #define PRINTF_FORMAT(format_param, dots_param)
29 #endif
30 
31 // The C++ standard requires that static const members have an out-of-class
32 // definition (in a single compilation unit), but MSVC chokes on this (when
33 // language extensions, which are required, are enabled). (You're only likely to
34 // notice the need for a definition if you take the address of the member or,
35 // more commonly, pass it to a function that takes it as a reference argument --
36 // probably an STL function.) This macro makes MSVC do the right thing. See
37 // http://msdn.microsoft.com/en-us/library/34h23df8(v=vs.100).aspx for more
38 // information. Use like:
39 //
40 // In .h file:
41 //   struct Foo {
42 //     static const int kBar = 5;
43 //   };
44 //
45 // In .cc file:
46 //   STATIC_CONST_MEMBER_DEFINITION const int Foo::kBar;
47 #if V8_HAS_DECLSPEC_SELECTANY
48 #define STATIC_CONST_MEMBER_DEFINITION __declspec(selectany)
49 #else
50 #define STATIC_CONST_MEMBER_DEFINITION
51 #endif
52 
53 #if V8_CC_MSVC
54 
55 #include <sal.h>
56 
57 // Macros for suppressing and disabling warnings on MSVC.
58 //
59 // Warning numbers are enumerated at:
60 // http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx
61 //
62 // The warning pragma:
63 // http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
64 //
65 // Using __pragma instead of #pragma inside macros:
66 // http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
67 
68 // MSVC_SUPPRESS_WARNING disables warning |n| for the remainder of the line and
69 // for the next line of the source file.
70 #define MSVC_SUPPRESS_WARNING(n) __pragma(warning(suppress : n))
71 
72 // Allows exporting a class that inherits from a non-exported base class.
73 // This uses suppress instead of push/pop because the delimiter after the
74 // declaration (either "," or "{") has to be placed before the pop macro.
75 //
76 // Example usage:
77 // class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) {
78 //
79 // MSVC Compiler warning C4275:
80 // non dll-interface class 'Bar' used as base for dll-interface class 'Foo'.
81 // Note that this is intended to be used only when no access to the base class'
82 // static data is done through derived classes or inline methods. For more info,
83 // see http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
84 #define NON_EXPORTED_BASE(code) \
85   MSVC_SUPPRESS_WARNING(4275)   \
86   code
87 
88 #else  // Not MSVC
89 
90 #define MSVC_SUPPRESS_WARNING(n)
91 #define NON_EXPORTED_BASE(code) code
92 
93 #endif  // V8_CC_MSVC
94 
95 // Allowing the use of noexcept by removing the keyword on older compilers that
96 // do not support adding noexcept to default members.
97 // Disabled on MSVC because constructors of standard containers are not noexcept
98 // there.
99 #if ((!defined(V8_CC_GNU) && !defined(V8_CC_MSVC) &&                      \
100       !defined(V8_TARGET_ARCH_MIPS) && !defined(V8_TARGET_ARCH_MIPS64) && \
101       !defined(V8_TARGET_ARCH_PPC) && !defined(V8_TARGET_ARCH_PPC64)) ||  \
102      (defined(__clang__) && __cplusplus > 201300L))
103 #define V8_NOEXCEPT noexcept
104 #else
105 #define V8_NOEXCEPT
106 #endif
107 
108 // Specify memory alignment for structs, classes, etc.
109 // Use like:
110 //   class ALIGNAS(16) MyClass { ... }
111 //   ALIGNAS(16) int array[4];
112 //
113 // In most places you can use the C++11 keyword "alignas", which is preferred.
114 //
115 // But compilers have trouble mixing __attribute__((...)) syntax with
116 // alignas(...) syntax.
117 //
118 // Doesn't work in clang or gcc:
119 //   struct alignas(16) __attribute__((packed)) S { char c; };
120 // Works in clang but not gcc:
121 //   struct __attribute__((packed)) alignas(16) S2 { char c; };
122 // Works in clang and gcc:
123 //   struct alignas(16) S3 { char c; } __attribute__((packed));
124 //
125 // There are also some attributes that must be specified *before* a class
126 // definition: visibility (used for exporting functions/classes) is one of
127 // these attributes. This means that it is not possible to use alignas() with a
128 // class that is marked as exported.
129 #if defined(V8_CC_MSVC)
130 #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
131 #else
132 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
133 #endif
134 
135 #endif  // V8_BASE_COMPILER_SPECIFIC_H_
136