如何为Clang定义具有多个编译指示的宏? [英] How do I define a macro with multiple pragmas for Clang?
问题描述
我想一些宏添加到放心(UN)周围设置程序,我们在内部自嘲的特定警告.
I'd like to add some macros to ease (un)setting a specific warning around routines that we are deprecating internally.
我想打开这样的:
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wdeprecated-declarations"
void Foo() __attribute__((deprecated("Warning: deprecated routine")))
#pragma clang diagnostic pop
对此:
MY_DEPRECATED_BEGIN
void Foo() MY_DEPRECATED
MY_DEPRECATED_END
MY_DEPRECATED_BEGIN
宏给我带来麻烦,因为我必须在单个宏中指定两个编译指示.能做到吗?
The MY_DEPRECATED_BEGIN
macro is giving me trouble as I have to specify two pragmas in a single macro. Can this be done?
(积分为仅仅使用MY_DEPRECATED宏达到相同效果的溶液!)
(Bonus points for a solution that achieves the same effect using only the MY_DEPRECATED macro!)
推荐答案
void Foo() __attribute__((deprecated));
#define MY_DEPRECATED_BEGIN \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic warning \"-Wdeprecated-declarations\"")
int main()
{
MY_DEPRECATED_BEGIN
Foo();
#pragma clang diagnostic pop
}
简短的回答您的技术问题是,C99提供了
The short answer to your technical question is that C99 provides the _Pragma("foo")
construct, which is equivalent to #pragma foo
but is processed slightly later, and doesn't need to be on a line by itself.
您另一个问题是,-Wdeprecated
的设置才有意义.
Your other problem is that -Wdeprecated-declarations
doesn't do what you think it does. Simply declaring a function as deprecated will never give you a diagnostic, because __attribute__((deprecated))
is supposed to be used (generally in header files). What causes the diagnostic is if you use a deprecated function — and it's at that point that the setting of -Wdeprecated
becomes relevant.
如果你真的只是想弃用<6>当且仅当<7>设置,然后以正确的方式做的是的就是
If you really just want to deprecate Foo
iff MY_DEPRECATED
is set, then the right way to do that is
#ifdef MY_DEPRECATED
#define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
#else
#define ATTRIBUTE_DEPRECATED
#endif
void Foo() ATTRIBUTE_DEPRECATED;
这篇关于如何为Clang定义具有多个编译指示的宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!