How can we know the caller function's name?

后端 未结 10 1624
北海茫月
北海茫月 2020-12-02 10:26

In the C language, __FUNCTION__ can be used to get the current function\'s name. But if I define a function named a() and it is called

10条回答
  •  佛祖请我去吃肉
    2020-12-02 11:07

    If the function in question is in a different c file, you can do

    #define name_of_function(...) \
        printf("Function %s is parent\n", __FUNCTION__); \
        name_of_function(__VA_ARGS__);
    

    And at the top of the c file it lives in

    #ifdef name_of_function
    #undef name_of_function
    #endif
    

    If they're in the same file, you can wrap the function definition in the second macro, then redefine the first macro at the end. It's not terribly extensible because you can't generate new defines from other defines, but if you're trying to track down parents for a particular function it works without any nonsense.

    https://godbolt.org/z/f2jKOm

提交回复
热议问题