类似函数的宏和变量

function-like macros and variables

出于某些难以理解的原因,在我的代码中我有类似的东西:

#define pippo(x) printf("%d",x)
...
... many lines down in the code
...
int pippo = 0;

相同的标识符 pippo 已用于类似函数的宏和变量名!除了这可能会引起可怜的维护者的困惑之外,我想知道这是否符合标准。

C99 和 C11(在 6.10.3.)都说:

10 [...] Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition [...]

他们没有说如果类函数宏名称是 not 后跟一个 '(' 会发生什么,我担心某些编译器可能会认为这是错误(或者可能只是发出警告)。

我是不是太担心了?

不替换后跟 ( 的类函数宏名称的实例。

这样使用名称并不违反 C 标准中的约束。该标准甚至给出了使用此行为的示例。 C 2018 7.1.4 1,讨论标准库函数及其作为类函数宏的潜在实现(除了作为函数的定义),说:

… Any macro definition of a function can be suppressed locally by enclosing the name of the function in parentheses, because the name is then not followed by the left parenthesis that indicates expansion of a macro function name. For the same syntactic reason, it is permitted to take the address of a library function even if it is also defined as a macro…

编译器可以发出警告(尽管当宏名称是上面 C 标准建议使用的库函数时它可能希望抑制此警告),但 GCC 9.2 和 Clang 11.0.0 都没有,即使启用了所有警告。

5.1.1.2,第 4 点,指定预处理器如何 "invoked" 如果您愿意:

Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed. [...] All preprocessing directives are then deleted.

所以这意味着预处理器未触及的任何内容都将被保留,包括任何未跟随 (pippo