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 typedef or function indicating it's ok if it's not used.
11 // Use like:
12 //   typedef Foo Bar ALLOW_UNUSED_TYPE;
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 #if ((!defined(V8_CC_GNU) && !defined(V8_TARGET_ARCH_MIPS) &&        \
98       !defined(V8_TARGET_ARCH_MIPS64) && !defined(V8_TARGET_ARCH_PPC) && \
99       !defined(V8_TARGET_ARCH_PPC64)) ||                                 \
100      (defined(__clang__) && __cplusplus > 201300L))
101 #define V8_NOEXCEPT noexcept
102 #else
103 #define V8_NOEXCEPT
104 #endif
105 
106 #endif  // V8_BASE_COMPILER_SPECIFIC_H_
107