简体   繁体   English

如何将宏作为宏的操作结果进行字符串化?

[英]How do I stringify macros that are the results of operations on macros?

Here's a program that illustrates my problem: 这是一个说明我的问题的程序:

#include <stdio.h>

#define NUMERATOR 8
#define DENOMINATOR 2
#define QUOTIENT (NUMERATOR / DENOMINATOR)

#define ZSTR(x) XSTR(#x)
#define YSTR(x) XSTR(x)
#define XSTR(x) STR(x)
#define STR(x) #x

int main()
{
    printf("QUOTIENT:       %d\n", QUOTIENT);
    printf("STR(QUOTIENT):  %s\n", STR(QUOTIENT));
    printf("XSTR(QUOTIENT): %s\n", XSTR(QUOTIENT));
    printf("YSTR(QUOTIENT): %s\n", YSTR(QUOTIENT));
    printf("ZSTR(QUOTIENT): %s\n", ZSTR(QUOTIENT));
    return 0;
}

And here's its output: 这是它的输出:

$ gcc -g -Wall -o stringify stringify.c && ./stringify 
QUOTIENT:       4
STR(QUOTIENT):  QUOTIENT
XSTR(QUOTIENT): (8 / 2)
YSTR(QUOTIENT): (8 / 2)
ZSTR(QUOTIENT): "QUOTIENT"

I would like to have a the string literal "4" passed to the compiler, but I'm losing hope. 我想将一个字符串文字"4"传递给编译器,但我失去了希望。 This is related to this question , but adds a level. 这与此问题有关 ,但增加了一个级别。

You can define macros that paste together their arguments and then define a (large) number of other macros that do the evaluation as kind of a table lookup: 您可以定义将其参数粘贴在一起的宏,然后定义(大量)其他宏来执行评估作为表查找的类型:

#define DIV(X, Y)  DIV_(X, Y)
#define DIV_(X, Y) DIV_##X##_##Y
#define DIV_0_1  0
#define DIV_1_1  1
#define DIV_2_1  2
    :
#define DIV_8_2  4
    :

This is kind of tedious, but you can easily write a little program to generate a header file with the above stuff in it and run that as part of your build process. 这有点乏味,但您可以轻松编写一个小程序来生成包含上述内容的头文件,并将其作为构建过程的一部分运行。 Then you just need 那你就需要了

#define QUOTIENT  DIV(NUMERATOR, DENOMINATOR)

Note that his kind of thing only works for unsigned integers -- if you need negative numbers or floating point, it won't work 请注意,他的类型只适用于无符号整数 - 如果您需要负数或浮点数,它将无法工作

With some tricks you can implement basic arithmetic in a C99 conforming preprocessor. 通过一些技巧,您可以在符合C99的预处理器中实现基本算法。 P99 implements arithmetic and logic for small decimal numbers. P99十进制数实现算术和逻辑。 Eg 例如

P99_IF_GT(1,0)(true)(false)
P99_ADD(3, 7)
P99_MUL(7, 2)
P99_DIV(7, 2)

would be preprocessed to something like 将被预处理为类似的东西

1
10
14
3

These macros can be processed further, stringified and everthing you like. 这些宏可以进一步处理,字符串化和你喜欢的everthing。

P99_STRINGIFY(P99_PASTE2(XXX_, P99_ADD(3, 7)))

leads to "XXX_10" as the result of preprocessing. 导致"XXX_10"作为预处理的结果。

The best you can do is stringify the expansion of the macro which is done with your XSTR and YSTR examples. 您可以做的最好的事情是使用XSTRYSTR示例对宏的扩展进行字符串化 Although it may compile to 4 with optimizations all the pre processor will be able see is (8 / 2) 尽管它可以通过优化编译为4 ,但所有处理器都能看到(8 / 2)

Well, I'm a little reluctant to admit I know one way to make this work. 好吧,我有点不愿意承认我知道一种方法来完成这项工作。

Attack it from the other direction. 从另一个方向攻击它。 You want your compiler to see something like this. 您希望编译器看到类似这样的内容。

#define QUOTIENT 4
#include <stdio.h>

int main(void)
{
  printf("QUOTIENT:       %d\n", QUOTIENT);
  return 0;
}

How do we do that without using a literal "4" in the definition of QUOTIENT, since the macro processor won't help us? 如果不在QUOTIENT的定义中使用文字“4”,我们如何做到这一点,因为宏处理器不会帮助我们? By using an additional preprocessor. 通过使用额外的预处理器。 Write a source file, stringify.c.awk, like this. 像这样写一个源文件stringify.c.awk。

/* stringify.c.awk --  Source file for stringify.c  
   (Put build instructions here.)  
 */
#define QUOTIENT NUMERATOR/DENOMINATOR
#include <stdio.h>

int main(void)
{
  printf("QUOTIENT:       %d\n", QUOTIENT);
  return 0;
}

Write the secondary preprocessor in awk. 在awk中编写辅助预处理器。 I deliberately used a really tight regular expression. 我故意用一个非常紧凑的正则表达式。 I think it's the most likely regex to fail if there are changes to the source file, and I think that's usually what you want. 如果源文件有变化,我认为这是最有可能失败的正则表达式,我认为这通常是你想要的。 (I usually want to discourage cosmetic changes to the #define.) (我通常想阻止对#define进行外观修饰。)

# stringify.awk -- calculate and substitute the value for #define QUOTIENT.
BEGIN {
  NUMERATOR = 8;
  DENOMINATOR = 2;
}
{
  if ($0~/^#define QUOTIENT NUMERATOR\/DENOMINATOR$/) {
    sub(/NUMERATOR\/DENOMINATOR/, NUMERATOR/DENOMINATOR);
  } 
  print $0;
}

Now you can build stringify.c from the stringify.c.awk file. 现在,您可以从stringify.c.awk文件构建stringify.c。

$ awk -f stringify.awk stringify.c.awk > stringify.c
$ gcc -Wall -o stringify stringify.c
$ ./stringify
QUOTIENT:       4

A makefile and generous comments takes a lot of the pain away. makefile和慷慨的评论带来了很多痛苦。

(m4 won't help for more or less the same reasons the C preprocessor won't help.) (m4对C预处理器没有帮助的原因或多或少有所帮助。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM