attribute is a compiler directive that specifies characteristics on declarations. They allow the compiler to perform advanced optimizations and enable new kinds of warnings for the analyzer.attribute attribute是一个编译器在声明上指定的特征的指令。它们允许编译器执行先进的优化。
The syntax for this keyword is attribute followed by two sets of parentheses. attribute directives are placed after function, variable, and type declarations. Inside the parentheses is a comma-delimited list of attributes.attribute 指令放在函数,变量和类型声明后面。括号内是以逗号分隔的属性列表。
format
The format attribute specifies that a function takes format arguments in the style of printf, scanf, strftime or strfmon.
extern int my_printf (void *my_object, const char *my_format, ...) __attribute__((format(printf, 2, 3)));
nonnull
The nonnull attribute specifies that some function parameters should be non-null pointers.表示函数的参数不为空。
extern void * my_memcpy (void *dest, const void *src, size_t len) __attribute__((nonnull (1, 2)));
Using nonnull codifies expectations about values into an explicit contract, which can help catch any NULL pointer bugs lurking in any calling code.
noreturn 函数返回值不为空
static BOOL different (NSString *a, NSString *b) __attribute__((noreturn))
pure / const
The pure attribute specifies that a function has no effects except the return value. That is to say, the return value of a pure function depends only on the parameters and/or global variables.
The const attribute specifies that a function does not examine any values except their arguments, and have no side-effects except the return value. Note that a function with pointer arguments or calls a non-const function usually should not be const.
int square(int n) __attribute__((const));
unused
This attribute, when attached to a function, denotes that the function is not meant to be used. GCC will not produce a warning for this function. 如果函数未使用,在函数后面加上unused,GCC就不会出现警告。 The same effect can be accomplished with the __unused keyword.
availability
Clang introduces the availability attribute, which can be placed on declarations to availability for particular operating system versions. Consider the function declaration for a hypothetical function f:
void f(void) __attribute__((availability(macosx, introduced=10.4,deprecated=10.6,obsoleted=10.7)));
This information is used by Clang to determine when it is safe to use f. If Clang is instructed to compile code for Mac OS X 10.5, a call to f() succeeds. If Clang is instructed to compile code for Mac OS X 10.6, the call succeeds but Clang emits a warning specifying that the function is deprecated. Finally, if Clang is instructed to compile code for Mac OS X 10.7, the call fails because f() is no longer available.
introduced: The first version in which this declaration was introduced.
deprecated: The first version in which this declaration was deprecated, meaning that users should migrate away from this API.
obsoleted: The first version in which this declaration was obsoleted, meaning that it was removed completely and can no longer be used.
unavailable: This declaration is never available on this platform.
message: Additional message text that Clang will provide when emitting a warning or error about use of a deprecated or obsoleted declaration. Useful for directing users to replacement APIs.
Supported Platforms
ios: Apple’s iOS operating system. The minimum deployment target is specified by the -mios-version- min=version or -miphoneos-version-min=version command-line arguments.
macosx: Apple’s Mac OS X operating system. The minimum deployment target is specified by the - mmacosx-version-min=version command-line argument.
overloadable
Clang provides support for C++ function overloading in C with the overloadable attribute.
#include <math.h>
float __attribute__((overloadable)) tgsin(float x)
{
return sinf(x);
}
double __attribute__((overloadable)) tgsin(double x)
{
return sin(x);
}
long double __attribute__((overloadable)) tgsin(long double x)
{
return sinl(x);
}