cpphs 是错误的还是在 if 定义的表达式中带有参数的宏的行为未定义?
Is cpphs wrong or is the behavior of macros with arguments in if defined expressions undefined?
我有以下代码,它在 if defined 预处理器表达式中使用带参数的宏:
预处理器-games.c:
#define EXAMPLE_MACRO(arg1,arg2) (\
arg1 > arg2)
#if defined(EXAMPLE_MACRO)
#endif
我用 GCC 对其进行了预处理,并且没有错误:
$ gcc -E -undef -traditional -x assembler-with-cpp preprocessor-games.c -o preprocessed-games.c
$ echo $?
0
$
结果是可接受的输出:
# 1 "preprocessor-games.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 17 "/usr/include/stdc-predef.h" 3 4
# 1 "<command-line>" 2
# 1 "preprocessor-games.c"
我使用替代预处理器对其进行预处理 - cpphs(旨在尽可能模拟 GCC 的预处理器以用于 Haskell) - 我得到一个空输出文件和一条错误消息:
$ cpphs --cpp -E -undef -traditional -x assembler-with-cpp preprocessor-games.c -o preprocessed-games.c
cpphs: macro EXAMPLE_MACRO expected 2 arguments, but was given 0
$ echo $?
1
$
是 cpphs 错误还是 macros with arguments in if defined expressions 的行为未定义?
行为已定义。在 C99 6.10.3/7 宏替换:
The identifier immediately following the define
is called the macro name.
(以下段落继续定义object-like宏和function-like宏。但是宏名称是通用的。)
回到 6.10.1 条件包含:
The expression that controls conditional [...] and it may contain unary operator expressions of the form
defined identifier
or
defined ( identifier )
which evaluate to 1 if the identifier is currently defined as a macro name (that is, if it is predefined or if it has been the subject of a #define
preprocessing directive without an intervening #undef
directive with the same subject identifier), 0 if it is not.
object-like 或 function-like 宏之间没有区别:defined
检查已定义的 宏名称 ,这适用于两者。
我有以下代码,它在 if defined 预处理器表达式中使用带参数的宏:
预处理器-games.c:
#define EXAMPLE_MACRO(arg1,arg2) (\
arg1 > arg2)
#if defined(EXAMPLE_MACRO)
#endif
我用 GCC 对其进行了预处理,并且没有错误:
$ gcc -E -undef -traditional -x assembler-with-cpp preprocessor-games.c -o preprocessed-games.c
$ echo $?
0
$
结果是可接受的输出:
# 1 "preprocessor-games.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 17 "/usr/include/stdc-predef.h" 3 4
# 1 "<command-line>" 2
# 1 "preprocessor-games.c"
我使用替代预处理器对其进行预处理 - cpphs(旨在尽可能模拟 GCC 的预处理器以用于 Haskell) - 我得到一个空输出文件和一条错误消息:
$ cpphs --cpp -E -undef -traditional -x assembler-with-cpp preprocessor-games.c -o preprocessed-games.c
cpphs: macro EXAMPLE_MACRO expected 2 arguments, but was given 0
$ echo $?
1
$
是 cpphs 错误还是 macros with arguments in if defined expressions 的行为未定义?
行为已定义。在 C99 6.10.3/7 宏替换:
The identifier immediately following the
define
is called the macro name.
(以下段落继续定义object-like宏和function-like宏。但是宏名称是通用的。)
回到 6.10.1 条件包含:
The expression that controls conditional [...] and it may contain unary operator expressions of the form
defined identifier
or
defined ( identifier )
which evaluate to 1 if the identifier is currently defined as a macro name (that is, if it is predefined or if it has been the subject of a
#define
preprocessing directive without an intervening#undef
directive with the same subject identifier), 0 if it is not.
object-like 或 function-like 宏之间没有区别:defined
检查已定义的 宏名称 ,这适用于两者。