2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * GCC compatible wrappers around clang attributes.
20 * @author Dominik Gabi
25 #ifndef __has_cpp_attribute
26 #define FOLLY_HAS_CPP_ATTRIBUTE(x) 0
28 #define FOLLY_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
31 #ifndef __has_extension
32 #define FOLLY_HAS_EXTENSION(x) 0
34 #define FOLLY_HAS_EXTENSION(x) __has_extension(x)
38 * Fallthrough to indicate that `break` was left out on purpose in a switch
43 * case 33: // no warning: no statements between case labels
45 * case 44: // warning: unannotated fall-through
47 * FOLLY_FALLTHROUGH; // no warning: annotated fall-through
50 #if FOLLY_HAS_CPP_ATTRIBUTE(clang::fallthrough)
51 #define FOLLY_FALLTHROUGH [[clang::fallthrough]]
53 #define FOLLY_FALLTHROUGH
57 * Maybe_unused indicates that a function, variable or parameter might or
58 * might not be used, e.g.
60 * int foo(FOLLY_MAYBE_UNUSED int x) {
68 #if FOLLY_HAS_CPP_ATTRIBUTE(maybe_unused)
69 #define FOLLY_MAYBE_UNUSED [[maybe_unused]]
70 #elif FOLLY_HAS_CPP_ATTRIBUTE(gnu::unused)
71 #define FOLLY_MAYBE_UNUSED [[gnu::unused]]
73 #define FOLLY_MAYBE_UNUSED
77 * Nullable indicates that a return value or a parameter may be a `nullptr`,
80 * int* FOLLY_NULLABLE foo(int* a, int* FOLLY_NULLABLE b) {
81 * if (*a > 0) { // safe dereference
84 * if (*b < 0) { // unsafe dereference
87 * if (b != nullptr && *b == 1) { // safe checked dereference
93 #if FOLLY_HAS_EXTENSION(nullability)
94 #define FOLLY_NULLABLE _Nullable
96 #define FOLLY_NULLABLE