__attribute__
2021年10月5日 · 701 字 · 2 分钟
The keyword attribute allows you to specify special properties of variables, function parameters, or structure, union, and, in C++, class members. This attribute keyword is followed by an attribute specification enclosed in double parentheses. Some attributes are currently defined generically for variables. Other attributes are defined for variables on particular target systems. Other attributes are available for functions (see Function Attributes), labels (see Label Attributes), enumerators (see Enumerator Attributes), statements (see Statement Attributes), and for types (see Type Attributes). Other front ends might define more attributes (see Extensions to the C++ Language).
Specifying Attributes of Variables
__noreturn__
首次见于: (webrtc: rtc_bash/check.h)
介绍说明见:Declaring Attributes of Functions
A few standard library functions, such as abort and exit, cannot return. GCC knows this automatically. Some programs define their own functions that never return. You can declare them noreturn to tell the compiler this fact. For example,
void fatal () __attribute__ ((noreturn));
void fatal (/* ... */)
{
/* ... */ /* Print error message. */ /* ... */
exit (1);
}
The noreturn keyword tells the compiler to assume that fatal cannot return. It can then optimize without regard to what would happen if fatal ever did return. This makes slightly better code. More importantly, it helps avoid spurious warnings of uninitialized variables.
The noreturn keyword does not affect the exceptional path when that applies: a noreturn-marked function may still return to the caller by throwing an exception or calling longjmp.
Do not assume that registers saved by the calling function are restored before calling the noreturn function.
It does not make sense for a noreturn function to have a return type other than void.
The attribute noreturn is not implemented in GCC versions earlier than 2.5. An alternative way to declare that a function does not return, which works in the current version and in some older versions, is as follows:
typedef void voidfn ();
volatile voidfn fatal;
This approach does not work in GNU C++.
- noreturn: The noreturn attribute is supposed to be used for functions that don’t return to the caller.
- void : functions which do return to the caller - they just don’t return a value
webrtc 的使用场景是,程序内部遇到致命错误,打印错误日志的函数标记为 __noreturn__
。打印函数中最后调用 abort()
方法,所以打印函数不会返回到调用者(caller)
#if defined(_MSC_VER)
#define RTC_NORETURN __declspec(noreturn)
#elif defined(__GNUC__)
#define RTC_NORETURN __attribute__((__noreturn__))
#else
#define RTC_NORETURN
#endif
// ...
RTC_NORETURN RTC_EXPORT void FatalLog(const char* file,
int line,
const char* message,
const CheckArgType* fmt,
...);
__always_inline__
(webrtc: rtc_base/system/inline.h)
对编译器:
- inline : 建议编译器内联,实际是否内联由编译器决定(根据优化等级);
- __attribute__((__always_inline__)):强制编译器将函数当做内联函数;
__noinline__
(webrtc: rtc_base/system/inline.h)
阻止inline,可以使用函数属性 __noinline__ 。即使使用了建议inline关键字修饰,__noinline__ 也会阻止真正内联。
REF
1. Specifying Attributes of Variables
2. C > gcc内联函数 inline和__attribute__((__always_inline__))的区别