MongoDB C++ Driver  legacy-1.1.2
compiler_gcc.h
1 /*
2  * Copyright 2012 10gen Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
24 #pragma once
25 
26 #define MONGO_COMPILER_NORETURN __attribute__((__noreturn__))
27 
28 #define MONGO_COMPILER_VARIABLE_UNUSED __attribute__((__unused__))
29 
30 #define MONGO_COMPILER_ALIGN_TYPE(ALIGNMENT) __attribute__((__aligned__(ALIGNMENT)))
31 
32 #define MONGO_COMPILER_ALIGN_VARIABLE(ALIGNMENT) __attribute__((__aligned__(ALIGNMENT)))
33 
34 // NOTE(schwerin): These visibility and calling-convention macro definitions assume we're not using
35 // GCC/CLANG to target native Windows. If/when we decide to do such targeting, we'll need to change
36 // compiler flags on Windows to make sure we use an appropriate calling convention, and configure
37 // MONGO_COMPILER_API_EXPORT, MONGO_COMPILER_API_IMPORT and MONGO_COMPILER_API_CALLING_CONVENTION
38 // correctly. I believe "correctly" is the following:
39 //
40 // #ifdef _WIN32
41 // #define MONGO_COMIPLER_API_EXPORT __attribute__(( __dllexport__ ))
42 // #define MONGO_COMPILER_API_IMPORT __attribute__(( __dllimport__ ))
43 // #ifdef _M_IX86
44 // #define MONGO_COMPILER_API_CALLING_CONVENTION __attribute__((__cdecl__))
45 // #else
46 // #define MONGO_COMPILER_API_CALLING_CONVENTION
47 // #endif
48 // #else ... fall through to the definitions below.
49 
50 #define MONGO_COMPILER_API_EXPORT __attribute__((__visibility__("default")))
51 #define MONGO_COMPILER_API_IMPORT
52 #define MONGO_COMPILER_API_CALLING_CONVENTION
53 
54 // old versions of gcc and clang don't accept a message
55 #ifdef __clang__
56 
57 // for compatibility with older versions of clang
58 #ifndef __has_extension
59 #define __has_extension __has_feature
60 #endif
61 
62 // Technically __attribute__(deprecated) is supposed to come at the end of the declaration, but
63 // GCC and clang accept it at the start, which eases compatibility with MSVC
64 
65 #if __has_extension(attribute_deprecated_with_message)
66 #define MONGO_COMPILER_API_DEPRECATED(MSG) __attribute__((deprecated(MSG)))
67 #else // older clang doesn't support message
68 #define MONGO_COMPILER_API_DEPRECATED(MSG) __attribute__((deprecated))
69 #endif
70 
71 #else // we are using GCC
72 
73 #if __GNUC__ > 4 || ((__GNUC__ == 4) && __GNUC_MINOR > 5) // deprecation messages were added in 4.5
74 #define MONGO_COMPILER_API_DEPRECATED(MSG) __attribute__((deprecated(MSG)))
75 #else // Older GCC doesn't support message
76 #define MONGO_COMPILER_API_DEPRECATED(MSG) __attribute__((deprecated))
77 #endif
78 
79 #endif
80 
81 #define MONGO_likely(x) static_cast<bool>(__builtin_expect(static_cast<bool>(x), 1))
82 #define MONGO_unlikely(x) static_cast<bool>(__builtin_expect(static_cast<bool>(x), 0))