有没有办法让宏在编译时强制发出警告和错误?
我目前有这样的东西:
#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 会将这些宏视为警告/错误,此示例将不会编译。 GCC 和 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
最佳答案
是的,有。引用 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’.
测试表明这些也适用于 clang。
请注意,您不需要嵌入文件和行信息。该指令将作为常规诊断输出,所有诊断都已包含文件和行信息。
根据所讨论的特定宏,另一种选择可能是强制函数调用标记有 warning 或 error 属性的函数。与 pragma 不同,如果已知函数调用不可访问(例如,因为它出现在 if block 中,在编译时检测到条件始终为 false,则属性无效),因此,如果在这种情况下您希望抑制警告或错误,它们可能更合适。
关于c++ - #warning 和 #error 作为宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24225006/
我正在使用这个:4.times{|i|assert_not_equal("content#{i+2}".constantize,object.first_content)}我之前声明过局部变量content1content2content3content4content5我得到的错误NameError:wrongconstantnamecontent2这个错误是什么意思?我很确定我想要content2=\ 最佳答案 你必须用一个大字母来调用ruby常量:Content2而不是content2。Aconstantnamestart
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一
我在新的Debian6VirtualBoxVM上安装RVM时遇到问题。我已经安装了所有需要的包并使用下载了安装脚本(curl-shttps://rvm.beginrescueend.com/install/rvm)>rvm,但以单个用户身份运行时bashrvm我收到以下错误消息:ERROR:Unabletocheckoutbranch.安装在这里停止,并且(据我所知)没有安装RVM的任何文件。如果我以root身份运行脚本(对于多用户安装),我会收到另一条消息:Successfullycheckedoutbranch''安装程序继续并指示成功,但未添加.rvm目录,甚至在修改我的.bas
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
当我创建一个Rails应用程序时,控制台:railsnewfoo我的代码可以使用字符串“foo”吗?puts"Yourapp'snameis"+app_name_bar 最佳答案 Rails.application.class将为您提供应用程序的全名(例如YourAppName::Application)。从那里您可以使用Rails.application.class.parent获取模块名称。 关于ruby-on-rails-应用程序的名称是否可以作为变量使用?,我们在StackOve
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我在搜索我的值是方法的散列时遇到问题。我只是不想运行plan_type与键匹配的方法。defmethod(plan_type,plan,user){foo:plan_is_foo(plan,user),bar:plan_is_bar(plan,user),waa:plan_is_waa(plan,user),har:plan_is_har(user)}[plan_type]end目前如果我传入“bar”作为plan_type,所有方法都会运行,我怎么能只运行plan_is_bar方法呢? 最佳答案 这个变体怎么样?defmethod