• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 
17 // Various macros related to function inlining.
18 
19 #ifndef NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_BASE_ATTRIBUTES_H_
20 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_BASE_ATTRIBUTES_H_
21 
22 // SAFTM_HAVE_ATTRIBUTE
23 //
24 // A function-like feature checking macro that is a wrapper around
25 // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
26 // nonzero constant integer if the attribute is supported or 0 if not.
27 //
28 // It evaluates to zero if `__has_attribute` is not defined by the compiler.
29 //
30 // GCC: https://gcc.gnu.org/gcc-5/changes.html
31 // Clang: https://clang.llvm.org/docs/LanguageExtensions.html
32 #ifdef __has_attribute
33 #define SAFTM_HAVE_ATTRIBUTE(x) __has_attribute(x)
34 #else
35 #define SAFTM_HAVE_ATTRIBUTE(x) 0
36 #endif
37 
38 // SAFTM_MUST_USE_RESULT
39 //
40 // Tells the compiler to warn about unused return values for functions declared
41 // with this macro. The macro must appear as the very first part of a function
42 // declaration or definition:
43 //
44 // Example:
45 //
46 //   SAFTM_MUST_USE_RESULT Sprocket* AllocateSprocket();
47 //
48 // This placement has the broadest compatibility with GCC, Clang, and MSVC, with
49 // both defs and decls, and with GCC-style attributes, MSVC declspec, C++11
50 // and C++17 attributes.
51 //
52 // SAFTM_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
53 // warning. For that, warn_unused_result is used only for clang but not for gcc.
54 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
55 #if SAFTM_HAVE_ATTRIBUTE(nodiscard)
56 #define SAFTM_MUST_USE_RESULT [[nodiscard]]
57 #elif defined(__clang__) && SAFTM_HAVE_ATTRIBUTE(warn_unused_result)
58 #define SAFTM_MUST_USE_RESULT __attribute__((warn_unused_result))
59 #else
60 #define SAFTM_MUST_USE_RESULT
61 #endif
62 
63 #if defined(__GNUC__) && \
64     (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
65 
66 // For functions we want to force inline.
67 // Introduced in gcc 3.1.
68 #define SAFTM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
69 
70 // For functions we don't want to inline, e.g., to keep code size small.
71 #define SAFTM_ATTRIBUTE_NOINLINE __attribute__((noinline))
72 
73 #elif defined(_MSC_VER)
74 #define SAFTM_ATTRIBUTE_ALWAYS_INLINE __forceinline
75 #else
76 
77 // Other compilers will have to figure it out for themselves.
78 #define SAFTM_ATTRIBUTE_ALWAYS_INLINE
79 #define SAFTM_ATTRIBUTE_NOINLINE
80 #endif  // big condition on two lines.
81 
82 #endif  // NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_BASE_ATTRIBUTES_H_
83