cpp
#include <stdio.h>
#define _MESA_TRACE_SCOPE_VAR_CONCAT(name, suffix) name##suffix
#define _MESA_TRACE_SCOPE_VAR(suffix) \
_MESA_TRACE_SCOPE_VAR_CONCAT(_mesa_trace_scope_, suffix)
/* This must expand to a single non-scoped statement for
*
* if (cond)
* _MESA_TRACE_SCOPE(...)
*
* to work.
*/
#define _MESA_TRACE_SCOPE(name) \
int _MESA_TRACE_SCOPE_VAR(__LINE__) \
__attribute__((cleanup(_mesa_trace_scope_end), unused)) = \
_mesa_trace_scope_begin(name)
static inline int
_mesa_trace_scope_begin(const char *name)
{
printf("start\n");
return 0;
}
static inline void
_mesa_trace_scope_end(int *scope)
{
printf("end\n");
}
#define MESA_TRACE_FUNC() _MESA_TRACE_SCOPE(__func__)
int main() {
MESA_TRACE_FUNC();
printf("main\n");
return 0;
}
运行输出: