Chinaunix首页 | 论坛 | 博客
  • 博客访问: 345620
  • 博文数量: 69
  • 博客积分: 3077
  • 博客等级: 中校
  • 技术积分: 602
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-11 09:40
个人简介

或以为孤权重,妄相忖度

文章分类

全部博文(69)

文章存档

2012年(1)

2011年(10)

2010年(39)

2009年(19)

我的朋友

分类: C/C++

2010-12-25 18:53:38

5.40 Function Names as Strings

GCC provides three magic variables which hold the name of the current function, as a string. The first of these is __func__, which is part of the C99 standard:

     The identifier __func__ is implicitly declared by the translator
     as if, immediately following the opening brace of each function
     definition, the declaration
     
          static const char __func__[] = "function-name";
     

appeared, where function-name is the name of the lexically-enclosing function. This name is the unadorned name of the function.

__FUNCTION__ is another name for __func__. Older versions of GCC recognize only this name. However, it is not standardized. For maximum portability, we recommend you use __func__, but provide a fallback definition with the preprocessor:

     #if __STDC_VERSION__ < 199901L
     # if __GNUC__ >= 2
     #  define __func__ __FUNCTION__
     # else
     #  define __func__ ""
     # endif
     #endif

In C, __PRETTY_FUNCTION__ is yet another name for __func__. However, in C++, __PRETTY_FUNCTION__ contains the type signature of the function as well as its bare name. For example, this program:

     extern "C" {
     extern int printf (char *, ...);
     }
     
     class a {
      public:
       void sub (int i)
         {
           printf ("__FUNCTION__ = %s\n", __FUNCTION__);
           printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
         }
     };
     
     int
     main (void)
     {
       a ax;
       ax.sub (0);
       return 0;
     }

gives this output:

     __FUNCTION__ = sub
     __PRETTY_FUNCTION__ = void a::sub(int)
These identifiers are not preprocessor macros. In GCC 3.3 and earlier, in C only, __FUNCTION__ and __PRETTY_FUNCTION__ were treated as string literals; they could be used to initialize char arrays, and they could be concatenated with other string literals. GCC 3.4 and later treat them as variables, like __func__. In C++, __FUNCTION__ and __PRETTY_FUNCTION__ have always been variables.
 
 
PS:linux中经常会看到__func__"关键字",这是C99标准的一部分,他的作用是将函数名作为字符串输出到控制台等设备,例如在调试linux 2.6.34.7的触摸屏驱动的时候,只要点击屏幕,就会在终端看到如下输出:
s3c24xx-ts s3c2440-ts: stylus_irq: count=4
在驱动代码s3c2410_ts.c的188行可以见到:
dev_info(ts.dev, "%s: count=%d\n", __func__, ts.count);
 
这是内核打印的一个设备信息,应该是可以注释的。
 
阅读(1929) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~