apply clang-tidy modernize-use-override
[folly.git] / folly / CppAttributes.h
1 /*
2  * Copyright 2017 Facebook, 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
17 /**
18  * GCC compatible wrappers around clang attributes.
19  *
20  * @author Dominik Gabi
21  */
22
23 #pragma once
24
25 #ifndef __has_cpp_attribute
26 #define FOLLY_HAS_CPP_ATTRIBUTE(x) 0
27 #else
28 #define FOLLY_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
29 #endif
30
31 #ifndef __has_extension
32 #define FOLLY_HAS_EXTENSION(x) 0
33 #else
34 #define FOLLY_HAS_EXTENSION(x) __has_extension(x)
35 #endif
36
37 /**
38  * Fallthrough to indicate that `break` was left out on purpose in a switch
39  * statement, e.g.
40  *
41  * switch (n) {
42  *   case 22:
43  *   case 33:  // no warning: no statements between case labels
44  *     f();
45  *   case 44:  // warning: unannotated fall-through
46  *     g();
47  *     FOLLY_FALLTHROUGH; // no warning: annotated fall-through
48  * }
49  */
50 #if FOLLY_HAS_CPP_ATTRIBUTE(clang::fallthrough)
51 #define FOLLY_FALLTHROUGH [[clang::fallthrough]]
52 #else
53 #define FOLLY_FALLTHROUGH
54 #endif
55
56 /**
57  * Nullable indicates that a return value or a parameter may be a `nullptr`,
58  * e.g.
59  *
60  * int* FOLLY_NULLABLE foo(int* a, int* FOLLY_NULLABLE b) {
61  *   if (*a > 0) {  // safe dereference
62  *     return nullptr;
63  *   }
64  *   if (*b < 0) {  // unsafe dereference
65  *     return *a;
66  *   }
67  *   if (b != nullptr && *b == 1) {  // safe checked dereference
68  *     return new int(1);
69  *   }
70  *   return nullptr;
71  * }
72  */
73 #if FOLLY_HAS_EXTENSION(nullability)
74 #define FOLLY_NULLABLE _Nullable
75 #else
76 #define FOLLY_NULLABLE
77 #endif