如何禁用几行代码的GCC警告

问题描述 投票:186回答:9

在Visual C ++中,可以使用#pragma warning (disable: ...)。我也发现在GCC你可以override per file compiler flags。我如何为“下一行”执行此操作,或使用GCC围绕代码区域进行推/弹语义?

c gcc compiler-warnings pragma
9个回答
195
投票

看来这是can be done。我无法确定它添加的GCC版本,但它是在2010年6月之前的某个时间。

这是一个例子:

#pragma GCC diagnostic error "-Wuninitialized"
    foo(a);         /* error is given for this one */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
    foo(b);         /* no diagnostic for this one */
#pragma GCC diagnostic pop
    foo(c);         /* error is given for this one */
#pragma GCC diagnostic pop
    foo(d);         /* depends on command line options */

92
投票

为了解决所有问题,这是暂时禁用警告的示例:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
    write(foo, bar, baz);
#pragma GCC diagnostic pop

您可以查看GCC documentation on diagnostic pragmas了解更多详情。


19
投票

TL; DR:如果有效,请避免或使用像__attribute__这样的说明符,否则使用_Pragma

这是我的博客文章Suppressing Warnings in GCC and Clang的简短版本。

考虑以下Makefile

CPPFLAGS:=-std=c11 -W -Wall -pedantic -Werror

.PHONY: all
all: puts

用于构建以下puts.c源代码

#include <stdio.h>

int main(int argc, const char *argv[])
{
    while (*++argv) puts(*argv);
    return 0;
}

它不会编译,因为argc未使用,并且设置是硬核(-W -Wall -pedantic -Werror)。

你可以做5件事:

  • 如果可能,改进源代码
  • 使用声明说明符,如__attribute__
  • 使用_Pragma
  • 使用#pragma
  • 使用命令行选项。

Improving the source

第一次尝试应该检查是否可以改进源代码以消除警告。在这种情况下,我们不想仅仅因为这个而改变算法,因为argc!*argvNULL在最后一个元素之后)是多余的。

Using a declaration specifier, like __attribute__

#include <stdio.h>

int main(__attribute__((unused)) int argc, const char *argv[])
{
    while (*++argv) puts(*argv);
    return 0;
}

如果你很幸运,标准会为你的情况提供一个说明符,比如_Noreturn

__attribute__是专有的GCC扩展(由Clang和其他一些编译器支持,如armcc),许多其他编译器也不会理解。如果你想要可移植代码,请将__attribute__((unused))放在宏中。

_Pragma operator

_Pragma可以作为#pragma的替代品。

#include <stdio.h>

_Pragma("GCC diagnostic push")
_Pragma("GCC diagnostic ignored \"-Wunused-parameter\"")

int main(int argc, const char *argv[])
{
    while (*++argv) puts(*argv);
    return 0;
}
_Pragma("GCC diagnostic pop") \

_Pragma运算符的主要优点是你可以把它放在宏中,这是#pragma指令无法实现的。

缺点:这几乎是一个战术核武器,因为它是基于行的而不是基于声明的。

_Pragma算子在C99中引入。

#pragma directive.

我们可以更改源代码以禁止代码区域的警告,通常是整个函数:

#include <stdio.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
int main(int argc, const char *argv[])
{
    while (*++argc) puts(*argv);
    return 0;
}
#pragma GCC diagnostic pop

缺点:这几乎是一个战术核武器,因为它是基于行的而不是基于声明的。

请注意,clang中存在类似的语法。

Suppressing the warning on the command line for a single file

我们可以在Makefile中添加以下行来专门为puts抑制警告:

CPPFLAGS:=-std=c11 -W -Wall -pedantic -Werror

.PHONY: all
all: puts

puts.o: CPPFLAGS+=-Wno-unused-parameter

在您的特定情况下,这可能不是您想要的,但它可能有助于处于类似情况的其他读取。


17
投票
#define DIAG_STR(s) #s
#define DIAG_JOINSTR(x,y) DIAG_STR(x ## y)
#ifdef _MSC_VER
#define DIAG_DO_PRAGMA(x) __pragma (#x)
#define DIAG_PRAGMA(compiler,x) DIAG_DO_PRAGMA(warning(x))
#else
#define DIAG_DO_PRAGMA(x) _Pragma (#x)
#define DIAG_PRAGMA(compiler,x) DIAG_DO_PRAGMA(compiler diagnostic x)
#endif
#if defined(__clang__)
# define DISABLE_WARNING(gcc_unused,clang_option,msvc_unused) DIAG_PRAGMA(clang,push) DIAG_PRAGMA(clang,ignored DIAG_JOINSTR(-W,clang_option))
# define ENABLE_WARNING(gcc_unused,clang_option,msvc_unused) DIAG_PRAGMA(clang,pop)
#elif defined(_MSC_VER)
# define DISABLE_WARNING(gcc_unused,clang_unused,msvc_errorcode) DIAG_PRAGMA(msvc,push) DIAG_DO_PRAGMA(warning(disable:##msvc_errorcode))
# define ENABLE_WARNING(gcc_unused,clang_unused,msvc_errorcode) DIAG_PRAGMA(msvc,pop)
#elif defined(__GNUC__)
#if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
# define DISABLE_WARNING(gcc_option,clang_unused,msvc_unused) DIAG_PRAGMA(GCC,push) DIAG_PRAGMA(GCC,ignored DIAG_JOINSTR(-W,gcc_option))
# define ENABLE_WARNING(gcc_option,clang_unused,msvc_unused) DIAG_PRAGMA(GCC,pop)
#else
# define DISABLE_WARNING(gcc_option,clang_unused,msvc_unused) DIAG_PRAGMA(GCC,ignored DIAG_JOINSTR(-W,gcc_option))
# define ENABLE_WARNING(gcc_option,clang_option,msvc_unused) DIAG_PRAGMA(GCC,warning DIAG_JOINSTR(-W,gcc_option))
#endif
#endif

这应该是gcc,clang和msvc的诀窍

可以用例如:

DISABLE_WARNING(unused-variable,unused-variable,42)
[.... some code with warnings in here ....]
ENABLE_WARNING(unused-variable,unused-variable,42)

有关详细信息,请参阅https://gcc.gnu.org/onlinedocs/cpp/Pragmas.htmlhttp://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmashttps://msdn.microsoft.com/de-DE/library/d9x1s805.aspx

您至少需要4.02版才能为gcc使用这些编译指示,不确定msvc和clang版本。

看起来像gcc的push pop pragma处理有点破。如果再次启用警告,仍会收到DISABLE_WARNING / ENABLE_WARNING块内的块的警告。对于某些版本的gcc,它可以工作,但有些版本没有。


16
投票
#pragma GCC diagnostic ignored "-Wformat"

将“-Wformat”替换为警告标志的名称。

AFAIK没有办法为此选项使用推/弹语义。


5
投票

我对ROS头等外部库也有同样的问题。我喜欢在CMakeLists.txt中使用以下选项进行更严格的编译:

set(CMAKE_CXX_FLAGS "-std=c++0x -Wall -Wextra -Wstrict-aliasing -pedantic -Werror -Wunreachable-code ${CMAKE_CXX_FLAGS}")

但是,这样做会导致外部包含的库中的所有类型的迂腐错误。解决方案是在包含外部库并重新启用之前禁用所有迂腐警告:

//save compiler switches
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"

//Bad headers with problem goes here
#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>

//restore compiler switches
#pragma GCC diagnostic pop

3
投票

gcc样式通常使用标准C构造或__attribute__扩展来告诉编译器更多关于你的意图,而不是使警告静音。例如,通过将赋值括在括号中来抑制关于用作条件的赋值的警告,即if ((p=malloc(cnt)))而不是if (p=malloc(cnt))。关于未使用的函数参数的警告可以通过我永远不会记住的一些奇怪的__attribute__或者通过自我赋值等来抑制。但是通常我更喜欢全局禁用任何警告选项,该警告选项会对正确代码中发生的事情生成警告。


3
投票

对于那些发现此页面寻找在IAR中执行此操作的方法的人,请尝试以下方法:

#pragma diag_suppress=Pe177
void foo1( void )
{
   /* The following line of code would normally provoke diagnostic 
      message #177-D: variable "x" was declared but never referenced.
      Instead, we have suppressed this warning throughout the entire 
      scope of foo1(). 
   */
   int x;
}
#pragma diag_default=Pe177

请参阅http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472m/chr1359124244797.html以供参考。


2
投票

我知道问题是关于GCC,但对于那些在其他和/或多个编译器中寻找如何做到这一点的人...

TL;DR

您可能想看看Hedley,这是我写的一个公共域单C / C ++标题,它为您做了很多这样的事情。我将在本文末尾简要介绍如何使用Hedley。

Disabling the warning

#pragma warning (disable: …)在大多数编译器中具有等价物:

  • MSVC:#pragma warning(disable:4996)
  • 海湾合作委员会:#pragma GCC diagnostic ignored "-W…"省略号是警告的名称;例如,#pragma GCC diagnostic ignored "-Wdeprecated-declarations
  • clang:#pragma clang diagnostic ignored "-W…"。语法基本上与GCC相同,并且许多警告名称是相同的(尽管许多不是)。
  • 英特尔C编译器:使用MSVC语法,但请记住警告编号完全不同。示例:#pragma warning(disable:1478 1786)
  • PGI:有一个diag_suppress编曲:#pragma diag_suppress 1215,1444
  • TI:有一个diag_suppress编译指示具有相同的语法(但不同的警告数字!)作为PGI:pragma diag_suppress 1291,1718
  • Oracle Developer Studio(suncc):有一个error_messages编译指示。令人讨厌的是,C和C ++编译器的警告是不同的。这两个禁用基本相同的警告: C:#pragma error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS) C ++:#pragma error_messages(off,symdeprecated,symdeprecated2)
  • IAR:也使用像PGI和TI这样的diag_suppress,但语法不同。一些警告号码是相同的,但我的其他人有分歧:#pragma diag_suppress=Pe1444,Pe1215
  • Pelles C:类似于MSVC,尽管数字是不同的#pragma warn(disable:2241)

对于大多数编译器,在尝试禁用它之前检查编译器版本通常是个好主意,否则你最终会触发另一个警告。例如,GCC 7增加了对-Wimplicit-fallthrough警告的支持,因此如果您在7之前关心GCC,您应该做类似的事情

#if defined(__GNUC__) && (__GNUC__ >= 7)
#  pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#endif

对于基于clang的clang和编译器,例如较新版本的XL C / C ++和armclang,您可以使用__has_warning()宏检查编译器是否知道特定警告。

#if __has_warning("-Wimplicit-fallthrough")
#  pragma clang diagnostic ignored "-Wimplicit-fallthrough"
#endif

当然,您还必须检查__has_warning()宏是否存在:

#if defined(__has_warning)
#  if __has_warning("-Wimplicit-fallthrough")
#    pragma clang diagnostic ignored "-Wimplicit-fallthrough"
#  endif
#endif

你可能想做类似的事情

#if !defined(__has_warning)
#  define __has_warning(warning)
#endif

所以你可以更容易地使用__has_warning。 Clang甚至在他们的手册中为__has_builtin()宏提出了类似的建议。不要这样做。其他代码可以检查__has_warning并回退检查编译器版本,如果它不存在,如果你定义__has_warning你将破坏他们的代码。正确的方法是在命名空间中创建一个宏。例如:

#if defined(__has_warning)
#  define MY_HAS_WARNING(warning) __has_warning(warning)
#else
#  define MY_HAS_WARNING(warning) (0)
#endif

然后你可以做类似的事情

#if MY_HAS_WARNING(warning)
#  pragma clang diagnostic ignored "-Wimplicit-fallthrough"
#elif defined(__GNUC__) && (__GNUC__ >= 7)
#  pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#endif

Pushing and popping

许多编译器还支持将警告推送到堆栈的方法。例如,这将在GCC上禁用一行代码的警告,然后将其返回到先前的状态:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
call_deprecated_function();
#pragma GCC diagnostic pop

当然,编译器之间没有很多关于语法的协议:

  • GCC 4.6+:#pragma GCC diagnostic push / #pragma GCC diagnostic pop
  • clang:#pragma clang diagnostic push / #pragma diagnostic pop
  • 英特尔13+(可能更早):#pragma warning(push) / #pragma warning(pop)
  • MSVC 15+(VS 9.0 / 2008):#pragma warning(push) / #pragma warning(pop)
  • ARM 5.6+:#pragma push / #pragma pop
  • TI 8.1+:#pragma diag_push / #pragma diag_pop
  • Pelles C 2.90+(可能更早):#pragma warning(push) / #pragma warning(pop)

如果内存服务,对于一些非常旧版本的GCC(如3.x,IIRC),push / pop pragma必须在函数之外。

Hiding the gory details

对于大多数编译器,可以使用在C99中引入的_Pragma来隐藏宏背后的逻辑。即使在非C99模式下,大多数编译器都支持_Pragma;最大的例外是MSVC,它有自己的__pragma关键字,语法不同。标准的_Pragma需要一个字符串,微软的版本没有:

#if defined(_MSC_VER)
#  define PRAGMA_FOO __pragma(foo)
#else
#  define PRAGMA_FOO _Pragma("foo")
#endif
PRAGMA_FOO

一旦经过预处理,大致相当于

#pragma foo

这让我们可以创建宏,这样我们就可以编写代码了

MY_DIAGNOSTIC_PUSH
MY_DIAGNOSTIC_DISABLE_DEPRECATED
call_deprecated_function();
MY_DIAGNOSTIC_POP

并隐藏宏定义中所有丑陋的版本检查。

The easy way: Hedley

现在您已经了解了如何在保持代码清洁的同时轻松地执行此类操作的机制,您了解我的一个项目,Hedley。您可以只使用Hedley(它是一个单一的公共域C / C ++标头),而不是通过大量文档和/或安装尽可能多的编译器版本来完成测试。例如:

#include "hedley.h"

HEDLEY_DIAGNOSTIC_PUSH
HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
call_deprecated();
HEDLEY_DIAGNOSTIC_POP

将禁用关于在GCC,clang,ICC,PGI,MSVC,TI,IAR,ODS,Pelles以及可能的其他人上调用已弃用函数的警告(我可能不会在更新Hedley时更新此答案)。而且,在未知的编译器上,宏将被预处理为空,因此您的代码将继续与任何编译器一起使用。当然HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED不是Hedley所知道的唯一警告,也不是Hedley所能做的所有禁用警告,但希望你能得到这个想法。

© www.soinside.com 2019 - 2024. All rights reserved.