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