gpt4 book ai didi

c - 没有宏扩展的延迟字符串化

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:10 25 4
gpt4 key购买 nike

如果 deprecation_message 中的任何标识符标记被定义为对象,是否可以修改以下代码片段以防止 #pragma GCC warning 打印的诊断信息发生变化-在扩展dmacro 的地方使用类似宏,同时保留将symbol 替换到消息中的能力?丑陋没有问题,只要 clang 也实现了 GCC 扩展,GCC 扩展就是公平的游戏,但不能修改诊断的内容。

#define deprecation_message(symbol) \
#symbol will be removed from <header.h> in the next release\n\
of $LIBRARY. To use #symbol, include <moved/header.h> instead.

#define make_pw(...) make_pw_(__VA_ARGS__)
#define make_pw_(...) make_pw__(GCC warning #__VA_ARGS__)
#define make_pw__(...) _Pragma(#__VA_ARGS_)

#define dmacro(a,b,c) make_pw(deprecation_message(dmacro)) xmacro(a,b,c)

// Uncommenting any of the following #define lines should *not*
// change the text of the diagnostic in any way.
//#define header
//#define n f
//#define will won't'
dmacro(x,y,z)

(您可能想要实现字符串文字连接,但这不会起作用;_Pragma 本身和 #pragma GCC warning 都只接受一个字符串literal._Pragma("this""that") 是一个语法错误。)

最佳答案

也许我误解了这个问题,但下面的代码在 clanggcc 上都可以运行和编译,并用 -std=c99 进行了测试> 和 -std=c11:

#include <stdio.h>
#include <stdlib.h>

#define deprecation_message(symbol) \
#symbol " will be removed from <header.h> in the next release\n" \
"of $LIBRARY. To use " #symbol ", include <moved/header.h> instead."

#define make_pw__(...) _Pragma(#__VA_ARGS__)
#define make_pw_(...) make_pw__(GCC warning #__VA_ARGS__)
#define make_pw(...) make_pw_(__VA_ARGS__)

#define xmacro(a, b, c) puts("I'm working: " #a #b #c "!")
#define dmacro(a, b, c) make_pw(deprecation_message(dmacro)) xmacro(a, b, c)

int
main(void)
{
dmacro(x, y, z);
return EXIT_SUCCESS;
}

现在,上面的代码将使用 clang 展开它:

int
main(void)
{
#pragma GCC warning "\042dmacro\042 \042 will be removed from <header.h> in the next release\134n\042 \042of $LIBRARY. To use \042 \042dmacro\042 \042, include <moved/header.h> instead.\042"
puts("I'm working: " "x" "y" "z" "!");
return 0 /* Successful exit status. */;
}

并将在 clang 上产生以下警告:

src/main.c:18:5: warning: "dmacro" " will be removed from <header.h> in the next release\n" "of $LIBRARY. To use " "dmacro" ", include <moved/header.h> instead." [-W#pragma-messages]
dmacro(x, y, z);
^

gcc 上:

src/main.c:18:13: warning: "dmacro" " will be removed from <header.h> in the next release\n" "of $LIBRARY. To use " "dmacro" ", include <moved/header.h> instead."
dmacro(x, y, z);
^~~~~~~~

基本上我所做的就是将弃用消息放入引号中......


更新1:

现在,有趣的是,如果您从 make_pw_ 中删除字符串化,即:

#define make_pw_(...)  make_pw__(GCC warning __VA_ARGS__)

然后 clang 会给你这个:

src/main.c:18:5: warning: dmacro will be removed from <header.h> in the next releaseof $LIBRARY. To use dmacro, include <moved/header.h> instead. [-W#pragma-messages]
dmacro(x, y, z);
^

这很好,因为它没有不需要的引号,但是 GCC 只会给你这个:

src/main.c:18:13: warning: dmacro
dmacro(x, y, z);
^~~~~~~~

更奇怪的是,如果您将 deprecation_message 更改为:

#define deprecation_message(symbol) "" #symbol "..."

然后它会给你一个空警告,就像它只使用宏参数中的第一个非空白标记一样。

老实说,我不知道这是 GCC 的错误,还是这是定义明确的行为,clang 正在做一些额外的事情努力弄清楚这一点,尽管如此,这两个编译器的最新版本都会发生这种情况。 (如果我必须打赌,我会说,这是一个错误 :P)


更新2:

这是一个稍微修改过的版本,它适用于您在评论中描述的两种编译器。诀窍在于,宏 DEPRECATED 有两个阶段,而不是一个阶段。

#include <stdio.h>
#include <stdlib.h>

#define DEPRECATED_(...) #__VA_ARGS__
#define DEPRECATED(symbol) \
DEPRECATED_(#symbol will be removed from <header.h> in the next release\n \
of $LIBRARY. To use #symbol, include <moved/header.h> instead.)

#define PRAGMA_WARN_(message) _Pragma(#message)
#define PRAGMA_WARN(message) PRAGMA_WARN_(GCC warning message)

#define dmacro(a, b, c) \
PRAGMA_WARN(DEPRECATED(dmacro)) \
puts("I'm working: " #a #b #c "!")

int
main(void)
{
dmacro(x, y, z);
return EXIT_SUCCESS;
}

因此这将在 clang 中产生以下输出:

src/main.c:19:5: warning: "dmacro" will be removed from <header.h> in the next release of $LIBRARY. To use "dmacro", include <moved/header.h> instead. [-W#pragma-messages]
dmacro(x, y, z);
^

GCC 中的以下代码:

src/main.c:19:13: warning: "dmacro" will be removed from <header.h> in the next release
of $LIBRARY. To use "dmacro", include <moved/header.h> instead.
dmacro(x, y, z);
^~~~~~~~

注意:我强烈建议您从邮件中删除 \n 字符!

关于c - 没有宏扩展的延迟字符串化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39752164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com