xref: /third_party/gn/src/base/compiler_specific.h (revision 6d528ed9)
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 "util/build_config.h"
9
10#if defined(COMPILER_MSVC)
11
12// For _Printf_format_string_.
13#include <sal.h>
14
15#else  // Not MSVC
16
17#define _Printf_format_string_
18
19#endif  // COMPILER_MSVC
20
21#if defined(COMPILER_GCC) && defined(NDEBUG)
22#define ALWAYS_INLINE inline __attribute__((__always_inline__))
23#elif defined(COMPILER_MSVC) && defined(NDEBUG)
24#define ALWAYS_INLINE __forceinline
25#else
26#define ALWAYS_INLINE inline
27#endif
28
29// Tell the compiler a function is using a printf-style format string.
30// |format_param| is the one-based index of the format string parameter;
31// |dots_param| is the one-based index of the "..." parameter.
32// For v*printf functions (which take a va_list), pass 0 for dots_param.
33// (This is undocumented but matches what the system C headers do.)
34#if defined(COMPILER_GCC) || defined(__clang__)
35#define PRINTF_FORMAT(format_param, dots_param) \
36  __attribute__((format(printf, format_param, dots_param)))
37#else
38#define PRINTF_FORMAT(format_param, dots_param)
39#endif
40
41// Macro for hinting that an expression is likely to be false.
42#if !defined(UNLIKELY)
43#if defined(COMPILER_GCC) || defined(__clang__)
44#define UNLIKELY(x) __builtin_expect(!!(x), 0)
45#else
46#define UNLIKELY(x) (x)
47#endif  // defined(COMPILER_GCC)
48#endif  // !defined(UNLIKELY)
49
50#if !defined(LIKELY)
51#if defined(COMPILER_GCC) || defined(__clang__)
52#define LIKELY(x) __builtin_expect(!!(x), 1)
53#else
54#define LIKELY(x) (x)
55#endif  // defined(COMPILER_GCC)
56#endif  // !defined(LIKELY)
57
58// Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional.
59#if __cplusplus >= 201703L
60#define FALLTHROUGH [[fallthrough]]
61#elif defined(__clang__)
62#define FALLTHROUGH [[clang::fallthrough]]
63#else
64#define FALLTHROUGH
65#endif
66
67#endif  // BASE_COMPILER_SPECIFIC_H_
68