未初始化的警告需要

未初始化的警告需要

本文介绍了未初始化的警告需要 -O2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译以下 C++ 程序时,我需要添加 -O2 标志以获取有关未初始化变量的警告.这是为什么?

When I compile the following C++ program I need to add the -O2 flag to get a warning about the uninitialized variable. Why is that?

unsigned long fac(unsigned long n)
{
  unsigned long product;

  while (n > 1)
  {
    product = product * n;
    n = n - 1;
  }

  return product;
}

➜  a g++ --version
g++ (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2)

为了澄清这个问题,我当然启用了警告.

edit: To clarify the question, I did of course enable the warning.

推荐答案

为了更深入地回答 why 问题,这样做主要是为了减少误报率和编译时间.产生这些警告的传递(-Wmaybe-uninitialized 风格)在编译管道中运行得很晚(参见 gcc/passes.def;相应的 pass_late_warn_uninitialized pass 在第 338 行左右).GCC 努力为这个警告产生尽可能少的误报.为此,它需要有关程序的更精确信息.要获得这些信息,需要事先进行一些分析/转换(例如,跳转线程有利).其中一些太昂贵而无法在 -O0 启用.

To answer the why question in a bit more depth, this is done so chiefly to reduce false positive rate and compilation time. The pass that produces these warnings (the -Wmaybe-uninitialized flavor) is run very late in the compilation pipeline (see gcc/passes.def; the corresponding pass_late_warn_uninitialized pass is on the line 338 or so). GCC tries hard to produce as little false positives for this warning as possible. To do so, it needs more precise information about the program. To get this information it needs some analyses/transformations performed beforehand (for example, jump threading is particularly beneficial). And some of them are too expensive to be enabled at -O0.

GCC 开发是公开完成的.所有的主要决定通常都在邮件列表和 bugzilla 中讨论.例如,请参阅此评论.

GCC development is done in the open. All the major decisions are normally discussed on the mailing lists and in the bugzilla. See, for example, this comment.

这篇关于未初始化的警告需要 -O2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:37