blob: 402bc5ddb691d1b030cfd029f3e0eb33dc2a107d [file] [log] [blame]
eroman@chromium.orgf1ab5fe2012-02-14 10:12:12 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
mmentovai@google.comfa5f9932008-08-22 07:26:06 +09004
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
brucedawson9a84a6a2015-10-20 14:39:00 +090012// For _Printf_format_string_.
13#include <sal.h>
14
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090015// 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_SUPPRESS_WARNING disables warning |n| for the remainder of the line and
27// for the next line of the source file.
28#define MSVC_SUPPRESS_WARNING(n) __pragma(warning(suppress:n))
29
30// MSVC_PUSH_DISABLE_WARNING pushes |n| onto a stack of warnings to be disabled.
31// The warning remains disabled until popped by MSVC_POP_WARNING.
32#define MSVC_PUSH_DISABLE_WARNING(n) __pragma(warning(push)) \
33 __pragma(warning(disable:n))
evanm@google.com5756c4f2008-09-17 04:28:06 +090034
35// MSVC_PUSH_WARNING_LEVEL pushes |n| as the global warning level. The level
36// remains in effect until popped by MSVC_POP_WARNING(). Use 0 to disable all
37// warnings.
38#define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n))
39
40// Pop effects of innermost MSVC_PUSH_* macro.
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090041#define MSVC_POP_WARNING() __pragma(warning(pop))
42
deanm@chromium.org520ada62008-11-22 03:48:52 +090043#define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off))
44#define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on))
45
rvargas@google.com1ed7f9a2011-05-19 03:29:36 +090046// Allows exporting a class that inherits from a non-exported base class.
47// This uses suppress instead of push/pop because the delimiter after the
48// declaration (either "," or "{") has to be placed before the pop macro.
49//
50// Example usage:
51// class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) {
52//
53// MSVC Compiler warning C4275:
54// non dll-interface class 'Bar' used as base for dll-interface class 'Foo'.
jam@chromium.org743e2e32012-02-09 15:48:09 +090055// Note that this is intended to be used only when no access to the base class'
56// static data is done through derived classes or inline methods. For more info,
57// see http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
rvargas@google.com1ed7f9a2011-05-19 03:29:36 +090058#define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \
59 code
60
agl@chromium.org19583012008-11-22 08:58:09 +090061#else // Not MSVC
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090062
brucedawson9a84a6a2015-10-20 14:39:00 +090063#define _Printf_format_string_
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090064#define MSVC_SUPPRESS_WARNING(n)
65#define MSVC_PUSH_DISABLE_WARNING(n)
evanm@google.com5756c4f2008-09-17 04:28:06 +090066#define MSVC_PUSH_WARNING_LEVEL(n)
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090067#define MSVC_POP_WARNING()
deanm@chromium.org520ada62008-11-22 03:48:52 +090068#define MSVC_DISABLE_OPTIMIZE()
69#define MSVC_ENABLE_OPTIMIZE()
rvargas@google.com1ed7f9a2011-05-19 03:29:36 +090070#define NON_EXPORTED_BASE(code) code
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090071
72#endif // COMPILER_MSVC
73
license.botf003cfe2008-08-24 09:55:55 +090074
evan@chromium.org6b68db32010-10-09 01:20:32 +090075// Annotate a variable indicating it's ok if the variable is not used.
76// (Typically used to silence a compiler warning when the assignment
77// is important for some other reason.)
78// Use like:
pkastingd36037a2014-10-17 08:49:24 +090079// int x = ...;
80// ALLOW_UNUSED_LOCAL(x);
81#define ALLOW_UNUSED_LOCAL(x) false ? (void)x : (void)0
82
83// Annotate a typedef or function indicating it's ok if it's not used.
84// Use like:
85// typedef Foo Bar ALLOW_UNUSED_TYPE;
thakisee904722015-07-21 03:10:55 +090086#if defined(COMPILER_GCC) || defined(__clang__)
pkastingd36037a2014-10-17 08:49:24 +090087#define ALLOW_UNUSED_TYPE __attribute__((unused))
evan@chromium.org6b68db32010-10-09 01:20:32 +090088#else
pkastingd36037a2014-10-17 08:49:24 +090089#define ALLOW_UNUSED_TYPE
eroman@chromium.orgf1ab5fe2012-02-14 10:12:12 +090090#endif
91
92// Annotate a function indicating it should not be inlined.
93// Use like:
94// NOINLINE void DoStuff() { ... }
95#if defined(COMPILER_GCC)
96#define NOINLINE __attribute__((noinline))
97#elif defined(COMPILER_MSVC)
98#define NOINLINE __declspec(noinline)
99#else
davemoore@chromium.orgf895b372011-05-10 05:11:01 +0900100#define NOINLINE
evan@chromium.org6b68db32010-10-09 01:20:32 +0900101#endif
102
jbates@chromium.orgdde8e302012-02-24 02:52:20 +0900103// Specify memory alignment for structs, classes, etc.
104// Use like:
105// class ALIGNAS(16) MyClass { ... }
106// ALIGNAS(16) int array[4];
107#if defined(COMPILER_MSVC)
108#define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
109#elif defined(COMPILER_GCC)
110#define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
111#endif
112
thakise0742f52015-04-09 04:34:29 +0900113// Return the byte alignment of the given type (available at compile time).
jbates@chromium.orgdde8e302012-02-24 02:52:20 +0900114// Use like:
115// ALIGNOF(int32) // this would be 4
116#if defined(COMPILER_MSVC)
thakise0742f52015-04-09 04:34:29 +0900117#define ALIGNOF(type) __alignof(type)
jbates@chromium.orgdde8e302012-02-24 02:52:20 +0900118#elif defined(COMPILER_GCC)
119#define ALIGNOF(type) __alignof__(type)
120#endif
121
evan@chromium.org6b68db32010-10-09 01:20:32 +0900122// Annotate a function indicating the caller must examine the return value.
123// Use like:
124// int foo() WARN_UNUSED_RESULT;
toyoshime1879342015-07-03 17:38:07 +0900125// To explicitly ignore a result, see |ignore_result()| in base/macros.h.
dcheng7043e3c2015-10-10 10:17:26 +0900126// TODO(dcheng): Update //third_party/webrtc's macro definition to match.
127#undef WARN_UNUSED_RESULT
128#if defined(COMPILER_GCC) || defined(__clang__)
agl@chromium.org19583012008-11-22 08:58:09 +0900129#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
evan@chromium.org6b68db32010-10-09 01:20:32 +0900130#else
131#define WARN_UNUSED_RESULT
132#endif
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900133
134// Tell the compiler a function is using a printf-style format string.
135// |format_param| is the one-based index of the format string parameter;
136// |dots_param| is the one-based index of the "..." parameter.
137// For v*printf functions (which take a va_list), pass 0 for dots_param.
138// (This is undocumented but matches what the system C headers do.)
evan@chromium.org6b68db32010-10-09 01:20:32 +0900139#if defined(COMPILER_GCC)
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900140#define PRINTF_FORMAT(format_param, dots_param) \
141 __attribute__((format(printf, format_param, dots_param)))
evan@chromium.org6b68db32010-10-09 01:20:32 +0900142#else
143#define PRINTF_FORMAT(format_param, dots_param)
144#endif
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900145
146// WPRINTF_FORMAT is the same, but for wide format strings.
evan@chromium.org6b68db32010-10-09 01:20:32 +0900147// This doesn't appear to yet be implemented in any compiler.
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900148// See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
149#define WPRINTF_FORMAT(format_param, dots_param)
150// If available, it would look like:
151// __attribute__((format(wprintf, format_param, dots_param)))
152
eugenis@chromium.org5eff8312013-03-21 06:18:22 +0900153// MemorySanitizer annotations.
earthdok@chromium.org815eb602014-02-08 03:54:44 +0900154#if defined(MEMORY_SANITIZER) && !defined(OS_NACL)
earthdok@chromium.org0446a672014-02-04 04:51:17 +0900155#include <sanitizer/msan_interface.h>
eugenis@chromium.org5eff8312013-03-21 06:18:22 +0900156
157// Mark a memory region fully initialized.
158// Use this to annotate code that deliberately reads uninitialized data, for
159// example a GC scavenging root set pointers from the stack.
thestig2913d852015-03-17 07:36:55 +0900160#define MSAN_UNPOISON(p, size) __msan_unpoison(p, size)
161
162// Check a memory region for initializedness, as if it was being used here.
163// If any bits are uninitialized, crash with an MSan report.
164// Use this to sanitize data which MSan won't be able to track, e.g. before
165// passing data to another process via shared memory.
166#define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \
167 __msan_check_mem_is_initialized(p, size)
eugenis@chromium.org5eff8312013-03-21 06:18:22 +0900168#else // MEMORY_SANITIZER
thestig2913d852015-03-17 07:36:55 +0900169#define MSAN_UNPOISON(p, size)
170#define MSAN_CHECK_MEM_IS_INITIALIZED(p, size)
eugenis@chromium.org5eff8312013-03-21 06:18:22 +0900171#endif // MEMORY_SANITIZER
172
jochen@chromium.org74e19e22013-12-19 02:42:27 +0900173// Macro useful for writing cross-platform function pointers.
174#if !defined(CDECL)
175#if defined(OS_WIN)
176#define CDECL __cdecl
177#else // defined(OS_WIN)
178#define CDECL
179#endif // defined(OS_WIN)
180#endif // !defined(CDECL)
181
skyostil@chromium.org98e93c02014-02-14 01:11:04 +0900182// Macro for hinting that an expression is likely to be false.
183#if !defined(UNLIKELY)
184#if defined(COMPILER_GCC)
185#define UNLIKELY(x) __builtin_expect(!!(x), 0)
186#else
187#define UNLIKELY(x) (x)
188#endif // defined(COMPILER_GCC)
189#endif // !defined(UNLIKELY)
190
agl@chromium.org19583012008-11-22 08:58:09 +0900191#endif // BASE_COMPILER_SPECIFIC_H_