GCC:严格走样警告精度 [英] GCC: accuracy of strict aliasing warnings

查看:146
本文介绍了GCC:严格走样警告精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的一些code的严格走样违法行为,但它
看起来像我错过的东西,而试图了解严格别名
规则。

I'm trying to check some of my code for strict aliasing violations, but it looks like I've missed something while trying to understand the strict aliasing rule.

想象一下以下code:

Imagine the following code:

#include <stdio.h>

int main( void )
{
    unsigned long l;

    l = 0;

    *( ( unsigned short * )&l ) = 1;

    printf( "%lu\n", l );

    return 0;
}

经典和基本的例子。随着GCC 4.9( -Wall -fstrict走样-Wstrict走样-O3 ),它实际上报告错误:

Classic and basic example. With GCC 4.9 (-Wall -fstrict-aliasing -Wstrict-aliasing -O3), it actually reports the error:

 dereferencing type-punned pointer will break strict-aliasing rules

但以下罚款编译:

But the following compiles fine:

#include <stdio.h>

int main( void )
{
    unsigned long    l;
    unsigned short * sp;

    l       = 0;
    sp      = ( unsigned short * )&l;
    *( sp ) = 1;

    printf( "%lu\n", l );

    return 0;
}

在我的理解,第二个例子也违反了结构走样规则。结果
那么,为什么它编译?它是海湾合作委员会中的精度问题,或者有我错过了
一些严格走样?

In my understanding, the second example also violates the struct aliasing rule.
So why does it compile? Is it an accuracy issue in GCC, or have I missed something with strict aliasing?

我还发现了以下主题: <一个href=\"http://stackoverflow.com/questions/3817474/why-are-no-strict-aliasing-warnings-generated-for-this-$c$c\">Why对于本code产生没有严格走样警告?

I also found the following topic: Why are no strict-aliasing warnings generated for this code?

-Wstrict走样= 2 -Wstrict走样= 3 没什么区别编译。

-Wstrict走样= 1 并汇报第二个例子中的错误。

Compiling with -Wstrict-aliasing=2 or -Wstrict-aliasing=3 makes no difference.
But -Wstrict-aliasing=1 does report the error on the second example.

GCC文件说,1级是最准确的,并能产生大量的
的假阳性,而第3级是最准确...

GCC documentation says level 1 is the least accurate, and can produce a lot of false positives, while level 3 is the most accurate...

所以,这里发生了什么?以我自己的理解问题或与海湾合作委员会的问题吗?

So what's happening here? An issue with my own understanding or an issue with GCC?

奖金问题

我通常在海湾合作委员会为我的项目preFER铛/ LLVM,但似乎锵
不发出关于严格别名任何警告。结果
有谁知道为什么吗?结果
是,因为它是不能检测违规,或者是因为它不遵循
规则产生code当?

I usually prefer Clang/LLVM over GCC for my projects, but it seems Clang doesn't issue any warning about strict aliasing.
Does anyone knows why?
Is it because it is not able to detect violations, or because it does not follow the rule when generating code?

推荐答案

你的理解是正确的。别名分析一般是复杂的,在这种情况下,显然单纯使用演员和间接引用之间的临时指针就足以把它关闭。出人意料的是,GCC 4.8.2确实对这个code一份更好的工作,在 -Wstrict走样= 2 以及1级预警,所以这是一个倒退。

Your understanding is correct. Alias analysis is generally complicated and in this case apparently the mere use of a temporary pointer between the cast and dereference was enough to throw it off. Surprisingly, GCC 4.8.2 does a better job on this code, warning at -Wstrict-aliasing=2 as well as level 1, so this is a regression.

至于铛,它只是目前还没有设施,以警告走样违法行为。它完全采取优化规则的优势。要看到这个动作,从C标准的直拿这个例子(N1570§6.5.2.39))

As for clang, it simply does not currently have the facility to warn about aliasing violations. It does absolutely take advantage of the rule in optimization. To see this in action, take this example straight from the C standard (N1570 §6.5.2.3 9))

struct t1 { int m; };
struct t2 { int m; };

int f(struct t1 *p1, struct t2 *p2) {
    if (p1->m < 0)
        p2->m = -p2->m;
    return p1->m;
}

如果p1和p2指向同一个结构,锵(和GCC)仍将返回 P1-&gt;中值;否定先于M ,因为他们可能承担P2不别名p1和因此previous否定从不影响的结果。 下面是完整的例子并输出带和不带 -fstrict走样。有关更多示例,请参阅这里和经常被引用的什么每一个C程序应该知道未定义行为;严格别名优化是介绍性文章的最后一个主题。

If p1 and p2 point to the same struct, Clang (and GCC) will nevertheless return the value of p1->m before negation, since they may assume p2 does not alias p1 and therefore the previous negation never affects the result. Here's the full example and output with and without -fstrict-aliasing. For more examples, see here and the oft-cited What Every C Programmer Should Know About Undefined Behavior; strict aliasing optimizations are the final topic of the introductory post.

至于何时警告将被执行,的开发者安静,但他们提到的<一个href=\"http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/warning-flags-tree.c?view=markup&pathrev=190972\"相对=nofollow>在铿锵的测试套件,其中列出了 -Wstrict走样= X 标题(重点煤矿)在

As for when warnings will be implemented, the devs are quiet, but they are mentioned in clang's test suite, which lists -Wstrict-aliasing=X under the title (emphasis mine)

这些标志的目前的未实现;测试我们将其输出
  反正。

These flags are currently unimplemented; test that we output them anyway.

所以,它很可能在某个时候发生的。

So it seems likely to happen at some point.

这篇关于GCC:严格走样警告精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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