抑制比较始终是真正的警告宏? [英] Suppress comparison always true warning for Macros?

查看:235
本文介绍了抑制比较始终是真正的警告宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个简单/正确的方法来获取gcc停止抛出这个错误,当比较的对立目标是一个宏。是的,我知道,通过 this 宏的特定定义,比较总是正确的,但它显然不会扩展到一般情况:

  #define ROMBOT 0x00000000 
#define ROMTOP 0x00002000
...
if(addr> = ROMBOT& addr< ROMTOP ){

simulator.c:517​​:2:error:无符号表达式比较> = 0总是为真

一个解决方案是:

  #if ROMBOT == 0 
if(addr #else
if(addr> = ROMBOT& addr< ROMTOP){
#endif
/ code>

但这看起来确实是非常难看的/高度维护。



相关:

  #define CHECK_ROM_BOT(_addr)(... etc)



但是很快就升级为很多丑陋/不必要的宏。



解决方案

一种可能的方法是使用两个变量,这些变量或许可以改变:

  uintptr_t rombot = ROMBOT; 
uintptr_t romtop = ROMTOP;

if(addr> = rombot& addr< romtop)

如果这些变量在当前源文件之外可见,则它们可能在编译器无法从编译此文件中看到的情况下更改,因此不能合法地警告比较。

  #include< stdint.h> 

#define ROMBOT 0x00000000
#define ROMTOP 0x00002000

uintptr_t rombot = ROMBOT;
uintptr_t romtop = ROMTOP;

extern int check_addr(uintptr_t addr);

int check_addr(uintptr_t addr)
{
if(addr> = ROMBOT& addr< ROMTOP)
return 1;
if(addr> = rombot&& addr< romtop)
return 2;
return 0;
}
$ make xx.o
/ usr / bin / gcc -g -std = c99 -Wall -Wextra -c -o xx.o xx.c
xx。 c:在函数'check_addr'中:
xx.c:13:warning:无符号表达式比较> = 0总是真的
$

警告(因为我没有使用 -Werror 编译)出现为原始代码,而不是建议更换代码。你需要考虑变量的类型,但 uintptr_t 可能是合理的。


I'm wondering if there's a simple / sane way to get gcc to stop throwing this error when the opposing target of the comparison is a macro. Yes, I recognize that with this particular definition of the macro, the comparison is always true, but it obviously doesn't extend to the general case:

#define ROMBOT 0x00000000
#define ROMTOP 0x00002000
...
if (addr >= ROMBOT && addr < ROMTOP) {

simulator.c:517:2: error: comparison of unsigned expression >= 0 is always true

One solution would be something to the effect of:

#if ROMBOT == 0
if (addr < ROMTOP) {
#else
if (addr >= ROMBOT && addr < ROMTOP) {
#endif

But that seems really ungainly / high-maintenance.

Or the related:

#define CHECK_ROM_BOT(_addr) (... etc)

But that quickly escalates into a lot of ugly / unnecessary macros.

Anyone have any ideas?

解决方案

One possible way is to use two variables which could, perhaps, be changed, thus:

uintptr_t rombot = ROMBOT;
uintptr_t romtop = ROMTOP;

if (addr >= rombot && addr < romtop)

If these variables are visible outside the current source file, they could change under circumstances that the compiler cannot see from compiling this file, so it cannot legitimately warn about the comparison.

#include <stdint.h>

#define ROMBOT 0x00000000
#define ROMTOP 0x00002000

uintptr_t rombot = ROMBOT;
uintptr_t romtop = ROMTOP;

extern int check_addr(uintptr_t addr);

int check_addr(uintptr_t addr)
{
    if (addr >= ROMBOT && addr < ROMTOP) 
        return 1;
    if (addr >= rombot && addr < romtop) 
        return 2;
    return 0;
}
$ make xx.o
/usr/bin/gcc -g -std=c99 -Wall -Wextra -c -o xx.o xx.c
xx.c: In function ‘check_addr’:
xx.c:13: warning: comparison of unsigned expression >= 0 is always true
$

The warning (since I didn't compile with -Werror) appears for your original code and not for the suggested replacement code. You'll need to think about the types of the variables, but uintptr_t is probably reasonable.

这篇关于抑制比较始终是真正的警告宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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