clang 优化错误?
我一直在试图找出 clang 中的一个 bug,并且我认为我已经得到了它的一个相当小的再现。这是我的程序:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define x_Is_Digit(x) isdigit((unsigned char) (x))
void Odd_Behavior(char * version)
{
char * ptr, *tmp;
for (ptr = version; x_Is_Digit(*ptr); ptr++);
ptr++;
for (tmp = ptr; x_Is_Digit(*ptr); ptr++);
if (ptr == tmp)
printf("%08x == %08x! Really?\n", ptr, tmp);
}
int main()
{
char buffer[100];
strcpy(buffer, "3.8a");
Odd_Behavior(buffer);
return(0);
}
当我在 Xcode 下载中包含的 clang 中对其进行优化编译时(“Apple clang 2.1”):
clang++ -Os optimizebug.cpp
并运行它,它报告:
6b6f2be3 == 6b6f2be2! Really?
至少可以说,这让我觉得有点奇怪。如果我删除 x_Is_Digit 中的 (unsigned char) 转换,它可以正常工作。
我在 clang 中遇到了错误吗?或者我在这里做的事情导致了某种未定义的行为?如果我用 -O0 编译它,我就不会遇到问题。
I've been trying to track down what seems like a bug in clang, and I think I've got a reasonably minimal reproduction of it. Here's my program:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define x_Is_Digit(x) isdigit((unsigned char) (x))
void Odd_Behavior(char * version)
{
char * ptr, *tmp;
for (ptr = version; x_Is_Digit(*ptr); ptr++);
ptr++;
for (tmp = ptr; x_Is_Digit(*ptr); ptr++);
if (ptr == tmp)
printf("%08x == %08x! Really?\n", ptr, tmp);
}
int main()
{
char buffer[100];
strcpy(buffer, "3.8a");
Odd_Behavior(buffer);
return(0);
}
When I compile it with optimization, in the clang included with the Xcode download ("Apple clang 2.1"):
clang++ -Os optimizebug.cpp
And run it, it reports:
6b6f2be3 == 6b6f2be2! Really?
This strikes me as a tad odd, to say the least. If I remove the (unsigned char) cast in x_Is_Digit, it works properly.
Have I run into a bug in clang? Or am I doing something here that's causing some sort of undefined behavior? If I compile it with -O0, I don't get the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说当然看起来像一个错误。 Clang 主线不显示此内容(至少在 darwin/x86-64 上)。请在 llvm.org/bugs 上提交错误,并提供有关如何重现此问题的完整详细信息。堆栈溢出不是报告编译器错误的好地方:)
Certainly looks like a bug to me. Clang mainline doesn't display this (at least on darwin/x86-64). Please file a bug at llvm.org/bugs with full details on how to reproduce this. Stack overflow isn't a great place to report compiler bugs :)
绝对是一个错误。如果两个指针在
if
语句中相等,则它们在printf
语句中也必须相等。Definitively a bug. If the two pointers are equal at the
if
statement, they must also be equal in theprintf
statement.