GCCとClangの独自拡張 returns_twice 属性についてメモ。
対象関数から「2回以上制御が戻ってくる可能性」*1をコンパイラに伝える属性。コンパイラによる一部の最適化処理を抑止する。
The
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.htmlreturns_twice
attribute tells the compiler that a function may return more than one time. The compiler ensures that all registers are dead before calling such a function and emits a warning about the variables that may be clobbered after the second return from the function. Examples of such functions aresetjmp
andvfork
. Thelongjmp
-like counterpart of such function, if any, might need to be marked with thenoreturn
attribute.
https://llvm.org/docs/LangRef.html
returns_twice
This attribute indicates that this function can return twice. The Csetjmp
is an example of such a function. The compiler disables some optimizations (like tail calls) in the caller of these functions.
関連URL
- c - What sense do these clobbered variable warnings make? - Stack Overflow
- c++ - function attribute returns_twice - Stack Overflow
- c - What is the difference between assembler generated for functions with and without returns_twice and noreturn? - Stack Overflow
*1:関数から制御が戻ってこないことを表明するnoreturn属性と対をなす。