指令#warning和#ERROR作为宏 [英] #warning and #error as Macro

查看:967
本文介绍了指令#warning和#ERROR作为宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让宏强制警告和错误在编译时?

目前,我有这样的事情:

 #如果定义(__clang__)
#定义PRAGMA(x)的_Pragma(#X)
#elif指令定义(__GNUC__)
#定义PRAGMA(x)的_Pragma(#X)
#elif指令定义(_MSC_VER)
#定义PRAGMA(x)的__pragma(X)
#万一#定义STRINGISIZE(STR)#str
STR的#define(STR)STRINGISIZE(STR)#define所在行的STR(__LINE__)
#定义文件__FILE__
#定义FILE_LINE __FILE__(线)#定义INFO(资讯,MSG)\\
  PRAGMA(消息(FILE_LINE:#info:MSG))#定义消息(m)INFO(味精,M)
#定义警告(W)INFO(警告,W)
的#define ERROR(五)信息(误差e)
#定义TODO(T)INFO(TODO,T)诠释的main()
{
    消息(味精)
    TODO(TODO)
    警告(警告)
    错误(ERROR)
}

的Visual Studio 2013将会把这些宏为警告/错误,这个例子不会编译。
有没有GCC和Clang的?

等效

 #如果定义(_MSC_VER)
    #定义INFO(资讯,MSG)\\
        PRAGMA(消息(FILE_LINE:#info:MSG))
    #定义消息(m)INFO(资讯,M)
    #定义警告(W)INFO(警告,W)
    的#define ERROR(五)信息(误差e)
    #定义TODO(T)INFO(待办事项T)
#elif指令定义(__GNUC__)||定义(__clang__)
    #定义INFO(资讯,MSG)\\
        PRAGMA(#info:#msg))
    #定义消息(m)INFO(资讯,M)
    #定义警告(W)INFO(GCC警告,W)
    的#define ERROR(五)信息(GCC误差e)
    #定义TODO(T)INFO(待办事项T)
#万一


解决方案

是的,有。引述 GCC preprocessor文档


 的#pragma GCC警告
GCC的#pragma错误


  
  

的#pragma GCC警告消息导致preprocessor发出警告诊断与文本的消息。中包含的编译的消息必须是一个字符串。同样,的#pragma GCC错误消息发出错误消息。不同于指令#warning #ERROR '的指令,这些标记可以嵌入在preprocessor宏使用 _Pragma


测试表明,随着铿锵这些工作了。

请注意,你并不需要嵌入文件和行信息。该指令将作为常规诊断输出,以及所有诊断已经包括文件和行信息。

根据所涉及的特定的宏,另一个选择可能是迫使一个函数调用到标有警告错误属性。不同于编译指示,如果函数调用被称为是不可达的属性没有任何效果(例如,因为它出现在一个如果块,其中的条件已经在编译时检测一如既往的是假的),因此,如果在这种情况下你想要的警告或错误是燮pressed,他们可能会更合适。

Is there a way to have macros to force warnings and errors while compiling?

I currently have something like this:

#if defined( __clang__ )
#   define PRAGMA( x )                    _Pragma( #x )
#elif defined( __GNUC__ )
#   define PRAGMA( x )                    _Pragma( #x )
#elif defined( _MSC_VER )
#   define PRAGMA( x )                    __pragma( x )
#endif

#define STRINGISIZE( str )    #str
#define STR( str )            STRINGISIZE( str )

#define LINE                  STR( __LINE__ )
#define FILE                  __FILE__
#define FILE_LINE             __FILE__ "(" LINE ")"

#define INFO( info , msg ) \
  PRAGMA( message( FILE_LINE ": " #info ": " msg ) )

#define MESSAGE( m )          INFO( msg , m )
#define WARNING( w )          INFO( warning , w )
#define ERROR( e )            INFO( error , e )
#define TODO( t )             INFO( TODO , t )

int main()
{
    MESSAGE( "MSG" )
    TODO( "TODO" )
    WARNING( "WARN" )
    ERROR( "ERROR" )
}

Visual Studio 2013 will treat these macros as warnings/errors and this example will not compile. Is there an equivalent for GCC and Clang?


#if defined( _MSC_VER )
    #define INFO( info , msg ) \
        PRAGMA( message( FILE_LINE ": " #info ": " msg ) )
    #define MESSAGE( m )          INFO( info , m )
    #define WARNING( w )          INFO( warning , w )
    #define ERROR( e )            INFO( error , e )
    #define TODO( t )             INFO( todo t )
#elif defined( __GNUC__ ) || defined( __clang__ )
    #define INFO( info , msg ) \
        PRAGMA( #info " : " #msg ) )
    #define MESSAGE( m )          INFO( info , m )
    #define WARNING( w )          INFO( GCC warning , w )
    #define ERROR( e )            INFO( GCC error , e )
    #define TODO( t )             INFO( , "todo" t )
#endif

解决方案

Yes, there is. Quoting the GCC preprocessor documentation:

#pragma GCC warning
#pragma GCC error

#pragma GCC warning "message" causes the preprocessor to issue a warning diagnostic with the text ‘message’. The message contained in the pragma must be a single string literal. Similarly, #pragma GCC error "message" issues an error message. Unlike the ‘#warning’ and ‘#error’ directives, these pragmas can be embedded in preprocessor macros using ‘_Pragma’.

Testing shows that these work with clang too.

Note that you do not need to embed the file and line information. The directive will be output as a regular diagnostic, and all diagnostics include the file and line information already.

Depending on the specific macro in question, another option may be to force a function call to a function marked with the warning or error attributes. Unlike the pragmas, the attributes have no effect if the function call is known to be unreachable (for example because it appears in an if block where the condition has been detected at compile time as always being false), so if in that case you want the warning or error to be suppressed, they may be more appropriate.

这篇关于指令#warning和#ERROR作为宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆