简体   繁体   English

如何避免由于编译器错误导致的无法访问的代码警告?

[英]How to avoid an unreachable code warning due to a compiler bug?

The cut down reproducible test case, for VS2015 update 2, is the following. 对于VS2015更新2,减少的可重现测试用例如下。 We were attempting to build with "Warning Level 4" and we treat warnings as fatal errors to improve the chances of noticing said warnings. 我们试图建立“警告级别4”,我们将警告视为致命错误,以提高注意到警告的可能性。 I am hoping someone in the community has already run into this and has found a reasonable workaround. 我希望社区中的某个人已经遇到过这种情况并找到了合理的解决方法。

This may prove to be too localised if noone else has seen an equivalent issue, so I'd like to note that the underlying question is "how badly should one mangle the codebase to evade poor compiler warnings", or equivalently, "how should one report bugs to a compiler vendor that loses bug reports". 如果没有其他人看到过相同的问题,这可能会被证明过于局部化,所以我想指出一个基本问题是“应该多么严重地破坏代码库以逃避糟糕的编译器警告”,或者等效地说,“应该怎么做?向缺少错误报告的编译器供应商报告错误“。

#ifdef _WIN32
#pragma warning( push )
#pragma warning( disable : 4702 ) // As suggested by comments
#endif
template <class Type>
void func (Type t)
{
    t.func ();
    t.func ();
}
#ifdef _WIN32
#pragma warning( pop )
#endif

struct NoThrow
{
    void func () {}
};

struct Throws
{
    void func () {throw 1;}
};

int main ()
{
    NoThrow nt;
    func (nt);

    Throws t;
    func (t);
}

This triggers unreachable code warnings. 这会触发无法访问的代码警告。 The templated function looks reasonable by itself, but for one particular instantiation the compiler is able to determine that the second t.func() is dead, so it warns about unreachable code. 模板化函数本身看起来很合理,但对于一个特定的实例化,编译器能够确定第二个t.func()已死,因此它会警告无法访问的代码。

This seems a fairly clear cut quality of implementation issue for VS2015, so we opened a bug report here . 对于VS2015来说,这似乎是一个相当明确的实施问题,所以我们在这里打开了一个错误报告。

We received some guidance from Microsoft, 我们收到了微软的一些指导,

Unfortunately the backend of the compiler (where this warning originates) has no real concept of template function instantiations as instances of a single template function. 不幸的是,编译器的后端(此警告源自此处)没有模板函数实例化的真实概念作为单个模板函数的实例。 They're all just functions with very similar looking names, if it ever bothered to compare the (decorated) names to eachother. 它们都只是具有非常相似的名字的函数,如果它曾经打算将(装饰的)名称与彼此进行比较。 Which it never does. 它永远不会。 So what it sees here is one function with some unreachable code, which it warns about, and another function with no unreachable code, which it does not. 所以它在这里看到的是一个函数,它有一些无法访问的代码,它警告它,另一个函数没有无法访问的代码,它没有。 This concept of a "cross function warning" you propose, where we gather up and compare data across different template instiantiations and only warn if this or that, is extrodinarily difficult under a pure LTCG binary and impossible otherwise. 您提出的这种“交叉功能警告”的概念,我们在不同的模板实例中收集和比较数据,并且仅在纯LTCG二进制文件的情况下仅在此情况下发出警告,并且在其他情况下是不可能的。

Consider a template Foo in a header foo.h. 考虑标题foo.h中的模板Foo。 Suppose a.cpp includes it and creates Foo, with unreachable code, and then b.cpp includes it and creates Foo with no unreachable code. 假设a.cpp包含它并创建具有无法访问代码的Foo,然后b.cpp包含它并创建没有无法访问代码的Foo。 You propose that in a.cpp Foo should not warn, dispite the unreachable code, because Foo exists with no unreachable code. 你建议在a.cpp中Foo不应该警告,调用无法访问的代码,因为Foo存在没有无法访问的代码。 But, Foo is compiled in a different file, in a different process, in a different invocation of cl.exe, and most inconveniently in the future. 但是,Foo是在不同的文件中,在不同的进程中,在不同的cl.exe调用中编译的,并且在将来最不方便。 Clearly the compiler has no ability to reach into the future to a yet unborn process and pull in information needed to calcuate if it should warn or not. 很明显,编译器没有能力将未来的过程扩展到未出生的过程并提取计算是否需要警告的信息。

The only real viable option we have here is to turn off unreachable code warning for templates all together, which I'll be honest, isn't going to happen until we are sure the harm done is greater than the good being done (it's a net bad). 我们这里唯一真正可行的选择是关闭所有模板的无法访问的代码警告,我会诚实地说,直到我们确定所造成的伤害大于所做的好处时才会发生(这是一个净坏)。 Warnings have false positives, it happens. 警告有误报,它会发生。 I'll try to think of other options, and look into the line number/file thing. 我会尝试考虑其他选项,并查看行号/文件。

The above link may not be available, but as the internet comes with caches you can see a copy here or find your own using 2744730 and incorrect-unreachable-code-warning-in-template-instantiation. 上面的链接可能不可用,但随着互联网附带缓存,您可以在此处查看副本或使用2744730找到您自己的副本,并使用不正确的无法访问代码警告模板实例化。

So, if we assume the underlying template instantiation model is to stamp out copies of functions then subject them to the same warnings analysis as any other, how might we go about avoiding dead code warnings? 因此,如果我们假设底层模板实例化模型是要删除函数的副本,然后对它们进行相同的警告分析,那么我们如何避免死代码警告呢? I'm currently torn between adding tag dispatching to the dozen or so templates which are currently affected or switching off the warnings globally. 我目前正在将标签调度添加到当前受影响的十几个模板或全局关闭警告之间。

edit: The direct link seems to be live again 编辑:直接链接似乎再次生效

I would say that this is the correct behavior, annoying as it may be. 我会说这是正确的行为,尽管很烦人。 The implementer of 的实施者

template <class Type>
void func (Type t)
{
    t.func ();
    t.func ();
}

could be quite happy about this warning. 对这个警告感到非常高兴。 Also, it's not often possible for the compiler to sort out if there would be none, some, all Type that would produce invalid or valid code. 此外,编译器通常不可能排除是否存在会产生无效或有效代码的所有Type ,一些Type Therefore, it waits until Type is specified and continues mostly as if the code was entered with that fixed type. 因此,它等待直到指定Type并继续,就像使用该固定类型输入代码一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免“无法访问代码中的动态初始化”警告? - How can I avoid the “dynamic initialization in unreachable code” warning? 在C ++中禁止使用未使用的变量警告=&gt;编译器错误或代码错误? - Suppress unused variable warning in C++ => Compiler bug or code bug? 如何避免由于编译时计算的值导致的“无法到达的语句”? - How to avoid an “unreachable statement” due to compile-time-computed values? 警告级别4中的代码不可达警告 - Unreachable code warning in warning Level 4 由于未定义的行为或编译器错误导致C ++代码崩溃? - Crash in C++ code due to undefined behaviour or compiler bug? 为什么该程序显示“无法访问的代码”警告? 以及我如何抑制它? - Why is this program showing “unreachable code” warning? And how do I suppress it? 编译器尝试在constexpr if之后评估无法访问的代码 - compiler tries to evaluate unreachable code after constexpr if unreachable-code-loop-increment 警告规则 - unreachable-code-loop-increment warning rules 如何保持无法访问的代码? - How to keep unreachable code? 这是编译器错误还是我的代码? - Is this a compiler bug or it's my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM