【问题标题】:Suppress -Wconversion for specific lines of code抑制特定代码行的 -Wconversion
【发布时间】:2014-01-10 17:27:34
【问题描述】:

我使用提供内联函数的头文件。就 GCC -Wconversion 检查而言,这些函数并不总是保存。

现在我想对 我的代码 使用 -Wconversion 检查,但想抑制我收到的包含文件的警告。 编辑当我只是将转换检查添加到编译器选项中时,我会得到诊断,省略 -Wconversion 会给我一个干净的编译器运行。

对应于this question,我用一些pragma包围了include:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#include <lpc177x_8x_crc.h>
#pragma GCC diagnostic pop

不幸的是,这不会抑制警告。

warning: conversion to 'int32_t' from 'uint32_t' may change the sign of the result [-Wsign-conversion]

为了方便检查,如果您没有可用的 CMSIS,您甚至可以试试这个:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
int32_t foo(void)
{
    uint32_t result;
    return result;
}
#pragma GCC diagnostic pop

编译器命令行参数是:

arm-none-eabi-gcc.exe -mthumb -Wshadow -Winit-self -Wredundant-decls -Wcast-align -Wunreachable-code -W -Wextra -Wall -Wformat=0 -Wconversion -g -O0 -ffunction-sections -fdata-sections -g3 -mcpu=cortex-m3 -c foo.c -o foo.o

我使用 arm-none-abi-gcc 版本:

gcc version 4.7.3 20121207 (release) [ARM/embedded-4_7-branch revision 194305] (GNU Tools for ARM Embedded Processors)

【问题讨论】:

  • 我在 Linux 上尝试使用 gcc 4.8,但无法重现此问题。您能否提供您收到的确切消息?会不会是另一个,与 -Wconversion 无关?

标签: c++ c gcc gcc-warning integer-overflow


【解决方案1】:

由于警告消息将相关标志标识为-Wsign-conversion,您应该将其添加到编译指示中。

#include <stdint.h>
extern int32_t foo(void);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wsign-conversion"
int32_t foo(void)
{
    uint32_t result = 0;
    return result;
}
#pragma GCC diagnostic pop

如果你注释掉第二个ignored 并用-Wconversion 编译,你会得到错误:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror -Wconversion -c cvt.c
cvt.c: In function ‘foo’:
cvt.c:9:5: error: conversion to ‘int32_t’ from ‘uint32_t’ may change the sign of the result [-Werror=sign-conversion]
     return result;
     ^
cc1: all warnings being treated as errors
$

如果取消注释该 pragma,则不会收到警告或错误。

(在 Mac OS X 10.9.1 Mavericks 和 GCC 4.8.2 上测试 — YMMV!)我注意到 Apple 提供的 clang(版本“Apple LLVM 版本 5.0 (clang-500.2.79))(基于 LLVM 3.3svn)') 不反对第二个 ignored pragma 注释掉,-Wconversion-Wsign-conversion,我尝试使用传递 uint32_t 参数的代码进行 dinking,并在返回它之前将其分配给结果,等等(因此,认识到 0 是特殊的,或者返回未初始化的变量是未定义的行为等,这不仅仅是高级优化):

#include <stdint.h>
extern int32_t foo(uint32_t v);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
//#pragma GCC diagnostic ignored "-Wsign-conversion"
int32_t foo(uint32_t v)
{
    uint32_t result = v;
    return result;
}
#pragma GCC diagnostic pop

【讨论】:

  • 它也适用于我的编译器版本。很奇怪,我需要禁用更多的诊断(符号转换+转换)而不是在命令行启用(转换)。
  • 昨晚我没有检查,但今天早上,只使用-Wsign-conversion pragma 就足够了。查看Warning Options — 请注意,该列表不是按字母顺序排列的。它描述了-Wconversion 并说-Wno-sign-conversion 将关闭有关C 中有符号到无符号整数转换的警告(除非明确启用,否则它在C++ 中是关闭的)。并且 g++ (4.8.2) 即使使用 -Wsign-conversion 和两个 pragma 作为 cmets 也不会生成警告。
  • 同意,-Wsign-conversion 就足够了。但令我惊讶的是-Wconversion 也启用了-Wsign-conversion,我没有在命令行启用它。现在我使用#pragma GCC ignored "-Wsign-conversion 来恢复我使用命令行选项-Wconversion 所做的更改。奇怪,但您链接到的页面记录了这一点。
猜你喜欢
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 2015-04-13
  • 2011-02-20
  • 1970-01-01
  • 2010-10-22
  • 2014-07-22
  • 1970-01-01
相关资源
最近更新 更多