强制VS2008发出类似于“警告:有符号和无符号整数表达式之间的比较”的GCC警告

发布于 2024-10-06 22:03:51 字数 2197 浏览 10 评论 0原文

从 'int' 转换为 'size_t' 可能会更改结果的符号 - GCC 、 C 中描述的内容相同,我想确保在 32 位和 64 位编译下,我在 GCC 4.2.1 下收到的警告也在 VS2008 SP1 下被标记,而不是在 GCC 下禁用警告以适应 VS2008。

例如,在 64 位条件下的 GCC 4.2.1 下,我们在 -Wall 条件下看到以下警告:

#include <string.h>
bool foo()
{
    size_t len = 0;
    const char * someBuffer = "flubber";
    len = strlen(someBuffer);
    bool retval = false;
    for (int j = 0; j < len; j++) // <-- warning
    {
        if (someBuffer[j] != '0')
        {
            retval = true;
            break;
        }
    }
    return retval;
}

GCC 警告为:

warning: comparison between signed and unsigned integer expressions

但 VS2008 SP1 64 位编译器不会标记它,无论什么类型我尝试使用 /W3 或 /W4、/w3some_warning_number 或 /w4some_warning_number 等启用什么编译指示。

现在,我不想愚蠢降低 GCC 的警告级别或禁用警告,因为他们有充分的理由发出警告。因此,我希望 VS 构建在启用 /WX 时以类似的方式失败。这是我必须忍受的事情,还是有一些警告可以在 VS2008 上解决问题?

编辑:警告显示 32 位构建,但不使用 64 位构建,使用相同的编译器选项集,如下所示:

/O2
/Ob2
/D "WIN32"
/D "_WINDOWS"
/D "NDEBUG"
/D "_CRT_SECURE_NO_WARNINGS"
/D "__WIN32__"
/D "_SCL_SECURE_NO_WARNINGS"
/D "_BIND_TO_CURRENT_MFC_VERSION"
/D "_BIND_TO_CURRENT_CRT_VERSION"
/D "WINVER=0x0502"
/D "_WIN32_WINNT=0x0502"
/D "_WIN32"
/D "_NT"
/D "_CRT_NONSTDC_NO_WARNINGS"
/D "_MBCS"
/FD
/EHsc
/MD
/W3
/WX
/TP
/Zm1000

编辑 #1:因为这可能是 Visual Studio 的一个特性,我发布到 Visual C++ 开发人员中心 - 警告 C4018:' <' :在 64 位 Windows 上的 VS2008 SP1 下,对于 32 位编译,会产生有符号/无符号不匹配的情况,但不会产生 64 位编译的情况

编辑 #2:我被指示从 Visual C++ 开发者中心发帖,今天(2011-01-11)微软回复了我的问题,并验证为编译器问题:他们表示该问题将添加到积压工作将在未来版本中修复:Microsoft Connect - 警告 C4018:“<” :有符号/无符号不匹配仅在大小相似的类型之间发出

Along the same lines as to what was described in conversion to ‘size_t’ from ‘int’ may change the sign of the result - GCC , C, I would instead like to insure that the warning I receive under GCC 4.2.1 is also flagged under VS2008 SP1 under both 32-bit and 64-bit compilation, versus disabling warnings under GCC to suit VS2008.

For example, under GCC 4.2.1 under 64-bit conditions, we see the following as a warning under -Wall conditions:

#include <string.h>
bool foo()
{
    size_t len = 0;
    const char * someBuffer = "flubber";
    len = strlen(someBuffer);
    bool retval = false;
    for (int j = 0; j < len; j++) // <-- warning
    {
        if (someBuffer[j] != '0')
        {
            retval = true;
            break;
        }
    }
    return retval;
}

The GCC warning is:

warning: comparison between signed and unsigned integer expressions

But it is not flagged by the VS2008 SP1 64-bit compiler, no matter what types of pragmas I try and no matter what I enable using /W3 or /W4, or /w3some_warning_number or /w4some_warning_number, etc.

Now, I don't want to dumb down GCC's warning levels or disable the warning, since they had the warning in there for very good reasons. So I would like the VS builds to fail in similar ways when /WX is enabled. Is this something I have to live with, or is there some warning that would do the trick on VS2008?

EDIT: The warning shows up for 32-bit builds but not using 64-bit builds, using the same set of compiler options, shown below:

/O2
/Ob2
/D "WIN32"
/D "_WINDOWS"
/D "NDEBUG"
/D "_CRT_SECURE_NO_WARNINGS"
/D "__WIN32__"
/D "_SCL_SECURE_NO_WARNINGS"
/D "_BIND_TO_CURRENT_MFC_VERSION"
/D "_BIND_TO_CURRENT_CRT_VERSION"
/D "WINVER=0x0502"
/D "_WIN32_WINNT=0x0502"
/D "_WIN32"
/D "_NT"
/D "_CRT_NONSTDC_NO_WARNINGS"
/D "_MBCS"
/FD
/EHsc
/MD
/W3
/WX
/TP
/Zm1000

EDIT #1: Seeing as this might be a Visual Studio idiosyncrasy, I posted to Visual C++ Developer Center - warning C4018: '<' : signed/unsigned mismatch is emitted for 32-bit but not 64-bit compilation under VS2008 SP1 on 64-bit Windows.

EDIT #2: I was directed to post to Microsoft Connect from within the Visual C++ Developer Center posting, and today (2011-01-11) Microsoft replied to my question and verified it as a compiler issue: They stated that the issue will added to the backlog to be fixed in a future release: Microsoft Connect - warning C4018: '<' : signed/unsigned mismatch is only emitted between like-sized types.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2024-10-13 22:03:51

出了问题,该行代码应该并且将会生成所需的“警告 C4018:'<' :有符号/无符号不匹配”。

这是我的测试项目编译器设置,如果这些有帮助的话:

/Od /D "WIN32" /D "_D​​EBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /MDd /Yu "stdafx.h" /Fp"Debug\testproj1.pch" /Fo"Debug\" /Fd"Debug\vc90.pdb" /W3 /nologo /c /ZI /TP /errorReport:prompt

编辑:这是用 32 测试的位 Visual Studio,但您询问的是 64 位编译,因此此信息与您无关,请原谅。

Something's gone wrong, that line of code should and will generate the desired "warning C4018: '<' : signed/unsigned mismatch".

Here's my test project compiler settings, if these help at all:

/Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /MDd /Yu"stdafx.h" /Fp"Debug\testproj1.pch" /Fo"Debug\" /Fd"Debug\vc90.pdb" /W3 /nologo /c /ZI /TP /errorReport:prompt

EDIT: this as tested with 32-bit Visual Studio, but you're asking about 64-bit compile, so this info is not relevant to you, my pardon.

梦冥 2024-10-13 22:03:51

回答我自己的问题:请参阅原始问题区域中的我的编辑#2。 Microsoft 现在将其识别为编译器问题。

Answering my own question: See my EDIT #2 in the original question area. It is now recognized by Microsoft as a compiler issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文