在重新定义时暂时禁用gcc警告

Jon*_*art 23 gcc c-preprocessor

我正在努力使这项工作(在GCC 4.6中)没有对我咆哮.

#define FOO  ""
#define BAR  ""

#if ....
    #define FOO    "Foo, good sir"
#endif

#if ...
    #define BAR    "Bar, my lady"
#endif
....

#define EVERYTHING      FOO BAR ...
Run Code Online (Sandbox Code Playgroud)

我会有很多这些.所以这样做而不是:

#if ...
    #define FOO    "Foo"
#else
    #define FOO    ""
#endif
Run Code Online (Sandbox Code Playgroud)

保存了大量代码,使其更具可读性.我得到的警告是:

警告:"FOO"重新定义[默认启用]

有没有办法在此特定部分的代码中禁用此警告?我发现Diagnostic Pragmas禁用了某些警告,但是我无法找到需要在此处禁用的警告(在此请求或抑制警告选项列表中).

有人知道怎么做吗?或者以不同的方式避免将#else #define所有这些都放到空字符串中?

Gre*_*ill 37

尝试使用#undef:

#define FOO ""

#if ....
    #undef FOO
    #define FOO "Foo, good sir"
#endif
Run Code Online (Sandbox Code Playgroud)


osg*_*sgx 8

此警告来自gcc中名为"cccp.c"的文件(截至2.95版本;此文件来自"苏联俄罗斯"?),并且无法关闭.即使在git head,gcc/libcpp/macro.c文件(同一文件的第2527 第2994行)中,仍然没有选项可以单独禁用此警告

我会引用一些消息来源.

2525 /* Returns nonzero if a macro redefinition warning is required.  */
2526 static bool
2527 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2528                       const cpp_macro *macro2)
2529 {
...
2537   /* Suppress warnings for builtins that lack the NODE_WARN flag.  */
..
2545   /* Redefinitions of conditional (context-sensitive) macros, on
2546      the other hand, must be allowed silently.  */
...
2550   /* Redefinition of a macro is allowed if and only if the old and new
2551      definitions are the same.  (6.10.3 paragraph 2).  */
...
2561   /* Check parameter spellings.  */
...
2566   /* Check the replacement text or tokens.  */
...
2573   for (i = 0; i < macro1->count; i++)
2574     if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2575       return true;
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下 warn_of_redefinition函数将返回true.这是真实的用法:

2989   if (node->type == NT_MACRO)
2990     {
2991       if (CPP_OPTION (pfile, warn_unused_macros))
2992         _cpp_warn_if_unused_macro (pfile, node, NULL);
2993 
2994       if (warn_of_redefinition (pfile, node, macro))
2995         {
2996           const int reason = (node->flags & NODE_BUILTIN)
2997                              ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
2998           bool warned;
2999 
3000           warned = cpp_pedwarning_with_line (pfile, reason,
3001                                              pfile->directive_line, 0,
3002                                              "\"%s\" redefined",
3003                                              NODE_NAME (node));
3004 
3005           if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3006             cpp_error_with_line (pfile, CPP_DL_NOTE,
3007                                  node->value.macro->line, 0,
3008                          "this is the location of the previous definition");
3009         }
3010     }
Run Code Online (Sandbox Code Playgroud)

所以,没有任何具体的选择.Greg的回答对于这种情况很有用,只需在重新定义之前取消定义空字符串.

  • 在苏联俄罗斯,宏重新定义了你! (12认同)