[M73 Dev][Tizen] Fix compilation errors for TV profile
[platform/framework/web/chromium-efl.git] / base / compiler_specific.h
1 // Copyright (c) 2012 The Chromium 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 BASE_COMPILER_SPECIFIC_H_
6 #define BASE_COMPILER_SPECIFIC_H_
7
8 #include "build/build_config.h"
9
10 #if defined(COMPILER_MSVC)
11
12 // For _Printf_format_string_.
13 #include <sal.h>
14
15 // Macros for suppressing and disabling warnings on MSVC.
16 //
17 // Warning numbers are enumerated at:
18 // http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx
19 //
20 // The warning pragma:
21 // http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
22 //
23 // Using __pragma instead of #pragma inside macros:
24 // http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
25
26 // MSVC_PUSH_DISABLE_WARNING pushes |n| onto a stack of warnings to be disabled.
27 // The warning remains disabled until popped by MSVC_POP_WARNING.
28 #define MSVC_PUSH_DISABLE_WARNING(n) __pragma(warning(push)) \
29                                      __pragma(warning(disable:n))
30
31 // Pop effects of innermost MSVC_PUSH_* macro.
32 #define MSVC_POP_WARNING() __pragma(warning(pop))
33
34 #else  // Not MSVC
35
36 #define _Printf_format_string_
37 #define MSVC_PUSH_DISABLE_WARNING(n)
38 #define MSVC_POP_WARNING()
39 #define MSVC_DISABLE_OPTIMIZE()
40 #define MSVC_ENABLE_OPTIMIZE()
41
42 #endif  // COMPILER_MSVC
43
44 // These macros can be helpful when investigating compiler bugs or when
45 // investigating issues in local optimized builds, by temporarily disabling
46 // optimizations for a single function or file. These macros should never be
47 // used to permanently work around compiler bugs or other mysteries, and should
48 // not be used in landed changes.
49 #if !defined(OFFICIAL_BUILD)
50 #if defined(__clang__)
51 #define DISABLE_OPTIMIZE() __pragma(clang optimize off)
52 #define ENABLE_OPTIMIZE() __pragma(clang optimize on)
53 #elif defined(COMPILER_MSVC)
54 #define DISABLE_OPTIMIZE() __pragma(optimize("", off))
55 #define ENABLE_OPTIMIZE() __pragma(optimize("", on))
56 #else
57 // These macros are not currently available for other compiler options.
58 #endif
59 // These macros are not available in official builds.
60 #endif  // !defined(OFFICIAL_BUILD)
61
62 // Annotate a variable indicating it's ok if the variable is not used.
63 // (Typically used to silence a compiler warning when the assignment
64 // is important for some other reason.)
65 // Use like:
66 //   int x = ...;
67 //   ALLOW_UNUSED_LOCAL(x);
68 #define ALLOW_UNUSED_LOCAL(x) (void)x
69
70 // Annotate a typedef or function indicating it's ok if it's not used.
71 // Use like:
72 //   typedef Foo Bar ALLOW_UNUSED_TYPE;
73 #if defined(COMPILER_GCC) || defined(__clang__)
74 #define ALLOW_UNUSED_TYPE __attribute__((unused))
75 #else
76 #define ALLOW_UNUSED_TYPE
77 #endif
78
79 // Annotate a function indicating it should not be inlined.
80 // Use like:
81 //   NOINLINE void DoStuff() { ... }
82 #if defined(COMPILER_GCC)
83 #define NOINLINE __attribute__((noinline))
84 #elif defined(COMPILER_MSVC)
85 #define NOINLINE __declspec(noinline)
86 #else
87 #define NOINLINE
88 #endif
89
90 #if defined(COMPILER_GCC) && defined(NDEBUG)
91 #define ALWAYS_INLINE inline __attribute__((__always_inline__))
92 #elif defined(COMPILER_MSVC) && defined(NDEBUG)
93 #define ALWAYS_INLINE __forceinline
94 #else
95 #define ALWAYS_INLINE inline
96 #endif
97
98 // Specify memory alignment for structs, classes, etc.
99 // Use like:
100 //   class ALIGNAS(16) MyClass { ... }
101 //   ALIGNAS(16) int array[4];
102 //
103 // In most places you can use the C++11 keyword "alignas", which is preferred.
104 //
105 // But compilers have trouble mixing __attribute__((...)) syntax with
106 // alignas(...) syntax.
107 //
108 // Doesn't work in clang or gcc:
109 //   struct alignas(16) __attribute__((packed)) S { char c; };
110 // Works in clang but not gcc:
111 //   struct __attribute__((packed)) alignas(16) S2 { char c; };
112 // Works in clang and gcc:
113 //   struct alignas(16) S3 { char c; } __attribute__((packed));
114 //
115 // There are also some attributes that must be specified *before* a class
116 // definition: visibility (used for exporting functions/classes) is one of
117 // these attributes. This means that it is not possible to use alignas() with a
118 // class that is marked as exported.
119 #if defined(COMPILER_MSVC)
120 #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
121 #elif defined(COMPILER_GCC)
122 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
123 #endif
124
125 // Annotate a function indicating the caller must examine the return value.
126 // Use like:
127 //   int foo() WARN_UNUSED_RESULT;
128 // To explicitly ignore a result, see |ignore_result()| in base/macros.h.
129 #undef WARN_UNUSED_RESULT
130 #if defined(COMPILER_GCC) || defined(__clang__)
131 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
132 #else
133 #define WARN_UNUSED_RESULT
134 #endif
135
136 // Tell the compiler a function is using a printf-style format string.
137 // |format_param| is the one-based index of the format string parameter;
138 // |dots_param| is the one-based index of the "..." parameter.
139 // For v*printf functions (which take a va_list), pass 0 for dots_param.
140 // (This is undocumented but matches what the system C headers do.)
141 #if defined(COMPILER_GCC) || defined(__clang__)
142 #define PRINTF_FORMAT(format_param, dots_param) \
143     __attribute__((format(printf, format_param, dots_param)))
144 #else
145 #define PRINTF_FORMAT(format_param, dots_param)
146 #endif
147
148 // WPRINTF_FORMAT is the same, but for wide format strings.
149 // This doesn't appear to yet be implemented in any compiler.
150 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
151 #define WPRINTF_FORMAT(format_param, dots_param)
152 // If available, it would look like:
153 //   __attribute__((format(wprintf, format_param, dots_param)))
154
155 // Sanitizers annotations.
156 #if defined(__has_attribute)
157 #if __has_attribute(no_sanitize)
158 #define NO_SANITIZE(what) __attribute__((no_sanitize(what)))
159 #endif
160 #endif
161 #if !defined(NO_SANITIZE)
162 #define NO_SANITIZE(what)
163 #endif
164
165 // MemorySanitizer annotations.
166 #if defined(MEMORY_SANITIZER) && !defined(OS_NACL)
167 #include <sanitizer/msan_interface.h>
168
169 // Mark a memory region fully initialized.
170 // Use this to annotate code that deliberately reads uninitialized data, for
171 // example a GC scavenging root set pointers from the stack.
172 #define MSAN_UNPOISON(p, size)  __msan_unpoison(p, size)
173
174 // Check a memory region for initializedness, as if it was being used here.
175 // If any bits are uninitialized, crash with an MSan report.
176 // Use this to sanitize data which MSan won't be able to track, e.g. before
177 // passing data to another process via shared memory.
178 #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \
179     __msan_check_mem_is_initialized(p, size)
180 #else  // MEMORY_SANITIZER
181 #define MSAN_UNPOISON(p, size)
182 #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size)
183 #endif  // MEMORY_SANITIZER
184
185 // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons.
186 #if !defined(DISABLE_CFI_PERF)
187 #if defined(__clang__) && defined(OFFICIAL_BUILD)
188 #define DISABLE_CFI_PERF __attribute__((no_sanitize("cfi")))
189 #else
190 #define DISABLE_CFI_PERF
191 #endif
192 #endif
193
194 // Macro useful for writing cross-platform function pointers.
195 #if !defined(CDECL)
196 #if defined(OS_WIN)
197 #define CDECL __cdecl
198 #else  // defined(OS_WIN)
199 #define CDECL
200 #endif  // defined(OS_WIN)
201 #endif  // !defined(CDECL)
202
203 // Macro for hinting that an expression is likely to be false.
204 #if !defined(UNLIKELY)
205 #if defined(COMPILER_GCC) || defined(__clang__)
206 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
207 #else
208 #define UNLIKELY(x) (x)
209 #endif  // defined(COMPILER_GCC)
210 #endif  // !defined(UNLIKELY)
211
212 #if !defined(LIKELY)
213 #if defined(COMPILER_GCC) || defined(__clang__)
214 #define LIKELY(x) __builtin_expect(!!(x), 1)
215 #else
216 #define LIKELY(x) (x)
217 #endif  // defined(COMPILER_GCC)
218 #endif  // !defined(LIKELY)
219
220 // Compiler feature-detection.
221 // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
222 #if defined(__has_feature)
223 #define HAS_FEATURE(FEATURE) __has_feature(FEATURE)
224 #else
225 #define HAS_FEATURE(FEATURE) 0
226 #endif
227
228 // Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional.
229 #if defined(__clang__)
230 #define FALLTHROUGH [[clang::fallthrough]]
231 #else
232 #define FALLTHROUGH
233 #endif
234
235 #if defined(COMPILER_GCC)
236 #define PRETTY_FUNCTION __PRETTY_FUNCTION__
237 #elif defined(COMPILER_MSVC)
238 #define PRETTY_FUNCTION __FUNCSIG__
239 #else
240 // See https://en.cppreference.com/w/c/language/function_definition#func
241 #define PRETTY_FUNCTION __func__
242 #endif
243
244 #endif  // BASE_COMPILER_SPECIFIC_H_