如何使Clang忽略特定块中的特定警告? [英] How to make Clang to ignore specific warning in specific block?

查看:221
本文介绍了如何使Clang忽略特定块中的特定警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用类型traits检查数值范围,无符号类型生成警告。

I am checking numeric value range with type traits, and unsigned types generate warning.

Comparison of unsigned expression >= 0 is always true

如何在特定代码范围禁用某些警告?我使用GCC风格 #pragma 与Clang,但这不工作。
这里是我的代码。

How to disable some warning at specific code range? I used GCC style #pragma with Clang but this doesn't work. Here's my code.

template<typename originT, typename destinationT>
void
assertForNumericRange(const originT value)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored  "-Wtype-limits"
    assertWithReason(value >= std::numeric_limits<destinationT>::min());
    assertWithReason(value <= std::numeric_limits<destinationT>::max());
#pragma GCC diagnostic pop
}

注意

目前,我将断言分为三组,浮点数,unsigned int,signed int。但我希望尽可能将它们整合为一个。

Currently, I divided the assertion into three groups, floating-point, unsigned int, signed int. But I wish to integrate them into one if possible.

我使用Xcode 5.0 beta。在命令行中,它报告:
Apple LLVM版本

I am using Xcode 5.0 beta. In command-line, it reports this: Apple LLVM version

5.0 (clang-500.1.58) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.3.0
Thread model: posix


推荐答案

你使用的是Clig的哪个版本?从诽谤用户手册,它应该工作方式你做了。但是你的范围断言将不会按你可能希望他们工作的方式工作:

Wich version of Clang are you using? From the Clang User Manual it should work exaclty the way you did. But your range assertions won't work the way you probably want them to work:

第一个断言本身没有什么意义,如果 destinationT 是无符号的,所以min给出0. originT 也是无符号的,那么它显然不是负面的,这是编译器警告你的。或者 originT 被签名,比较将把操作数中的一个或两个转换为其他类型,例如。可能会将 value 转换为无符号(因此为正值)表示。

That first assertion itself does not make much sense, if destinationT is unsigned, so min gives 0. Either originT is unsigned as well, then it's clearly not negative, wich is what the compiler warns you about. Or originT is signed, the comparison will convert one or both of the operand to other types, e.g. possibly converting value to an unsigned (and thus positive) representation.

考虑例如

 assertForNumericRange<signed char, unsigned long>( (signed char)-1);  

(signed char)-1 unsigned long 将会提升-1到 unsigned long ,有效地给出32位长的以下断言:

The comparisons between (signed char)-1 and unsigned long will promote the -1 to unsigned long, effectively giving following assertions for 32bit long:

assertWithReason((unsigned long)0xFFFFFFFF >= std::numeric_limits<destinationT>::min());
assertWithReason((unsigned long)0xFFFFFFFF <= std::numeric_limits<destinationT>::max());

两个比较都会显示为true,而-1显然不是 范围 unsigned long 的值。

Both comparisons will give true, while -1 is clearly not in the range of unsigned long's values.

这篇关于如何使Clang忽略特定块中的特定警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆