如何为Clang定义具有多个编译指示的宏? [英] How do I define a macro with multiple pragmas for Clang?

查看:283
本文介绍了如何为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屋!

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