Oolite
Loading...
Searching...
No Matches
OOFunctionAttributes.h
Go to the documentation of this file.
1#ifndef INCLUDED_OOFUNCTIONATTRIBUTES_h
2#define INCLUDED_OOFUNCTIONATTRIBUTES_h
3
4#ifndef GCC_ATTR
5#ifdef __GNUC__
6#define GCC_ATTR(x) __attribute__(x)
7#else
8#define GCC_ATTR(x)
9#endif
10#endif
11
12// Clang feature testing extensions.
13#ifndef __has_feature
14#define __has_feature(x) (0)
15#endif
16
17#ifndef __has_attribute
18#define __has_attribute(x) (0)
19#endif
20
21#ifndef __has_extension
22#define __has_extension(x) (0)
23#endif
24
25#if __cplusplus
26#define OOINLINE inline
27#else
28#define OOINLINE static inline
29#endif
30
31#if !OO_DEBUG
32#define ALWAYS_INLINE_FUNC GCC_ATTR((always_inline)) // Force inlining of function
33#else
34#define ALWAYS_INLINE_FUNC // Don't force inlining of function (because gdb is silly)
35#endif
36
37#define PURE_FUNC GCC_ATTR((pure)) // result dependent only on params and globals
38#define CONST_FUNC GCC_ATTR((const)) // pure + no pointer dereferences or globals
39#define NONNULL_FUNC GCC_ATTR((nonnull)) // Pointer parameters may not be NULL
40#define DEPRECATED_FUNC GCC_ATTR((deprecated)) // Warn if this function is used
41#define NO_RETURN_FUNC GCC_ATTR((noreturn)) // Function can never return
42#define NO_INLINE_FUNC GCC_ATTR((noinline)) // Function must never be inlined
43
44#define INLINE_PURE_FUNC ALWAYS_INLINE_FUNC PURE_FUNC
45#define INLINE_CONST_FUNC ALWAYS_INLINE_FUNC CONST_FUNC
46
47#if __has_extension(attribute_deprecated_with_message)
48#define DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
49#else
50#define DEPRECATED_MSG(msg) DEPRECATED_FUNC
51#endif
52
53#if __clang__
54#define DEPRECATED_METHOD(msg) DEPRECATED_MSG(msg)
55#else
56// GCC doesn't support attributes on Objective-C methods.
57#define DEPRECATED_METHOD(msg)
58#endif
59
60#ifdef __GNUC__
61#define EXPECT(x) __builtin_expect((x), 1)
62#define EXPECT_NOT(x) __builtin_expect((x), 0)
63#else
64#define EXPECT(x) (x)
65#define EXPECT_NOT(x) (x)
66#endif
67
68// OO_RETURNS_RETAINED: indicates the caller of a method owns a reference to the return value.
69#if __has_feature(attribute_ns_returns_retained)
70#define OO_RETURNS_RETAINED __attribute__((ns_returns_retained))
71#else
72#define OO_RETURNS_RETAINED
73#endif
74
75// OO_NS_CONSUMED: indicates that a reference to an object parameter is "consumed".
76#ifndef OO_NS_CONSUMED
77#if __has_feature(attribute_ns_consumed)
78#define OO_NS_CONSUMED __attribute__((ns_consumed))
79#else
80#define OO_NS_CONSUMED
81#endif
82#endif
83
84// OO_UNREACHABLE(): a statement that should never be executed (Clang optimization hint).
85#if __has_feature(__builtin_unreachable)
86#define OO_UNREACHABLE() __builtin_unreachable()
87#else
88#define OO_UNREACHABLE() \
89 do { \
90 } while (0)
91#endif
92
93/*
94 OO_TAKES_FORMAT_STRING(stringIndex, firstToCheck): marks a function that
95 applies [NSString stringWithFormat:]-type formatting to arguments.
96
97 According to the fine manuals, mainline GCC supports basic checking of
98 NSString format strings since 4.6, but doesn't validate the arguments.
99
100 Update: apparently GCC 4.6.3 doesn't recognize Objective-C string literals
101 as being string literals for this purpose, and errors out.
102 -- Ahruman 2012-10-06
103*/
104#if __has_attribute(format) /*|| (defined(OOLITE_GCC_VERSION) && OOLITE_GCC_VERSION >= 40600)*/
105#define OO_TAKES_FORMAT_STRING(stringIndex, firstToCheck) __attribute__((format(NSString, stringIndex, firstToCheck)))
106#else
107#define OO_TAKES_FORMAT_STRING(stringIndex, firstToCheck)
108#endif
109
110#if __OBJC__
111/* OOConsumeReference()
112 Decrements the Clang Static Analyzer's notion of an object's reference
113 count. This is used to work around cases where the analyzer claims an
114 object is being leaked but it actually isn't, due to a pattern the
115 analyzer doesn't understand (like singletons, or references being stored
116 in JavaScript objects' private field).
117 Do not use this blindly. If you aren't absolutely certain it's appropriate,
118 don't use it.
119 -- Ahruman 2011-01-28
120*/
121#if NDEBUG
124{
125 return value;
126}
127#else
128// Externed to work around analyzer being too "clever" and ignoring attributes
129// when it's inlined.
131#endif
132#endif
133
134#endif /* INCLUDED_OOFUNCTIONATTRIBUTES_h */
id OOConsumeReference(id OO_NS_CONSUMED value)
#define ALWAYS_INLINE_FUNC
#define OOINLINE
#define OO_NS_CONSUMED