【问题标题】:How to delete a specific warning gcc如何删除特定的警告 gcc
【发布时间】:2021-09-24 02:46:07
【问题描述】:

我有一个特定的警告,我想删除/隐藏,然后在之后启用它。您知道如何在不删除所有其他警告的情况下做到这一点吗?

这是我收到的警告

warning: enumeration value 'EtatCCSortie' not handled in switch [-Wswitch]

而且我不需要在交换机中实际使用该状态(它是一个状态机)。我有办法不再收到此警告,即通过将状态添加到开关,如下所示:

while (etatsImprimanteCasImpressionPeriodique != EtatIPSortie)
{
         switch (etatsImprimanteCasChangementCartouche)
         {
               ....
               case EtatCCSortie:
               break;

但我不必添加它,因为它永远不会执行(而!=)。这就是为什么我正在寻找一种方法来禁用此特定警告,然后在此切换后启用它。

Ps:我知道警告的重要性,我只需要一种方法来删除它。

【问题讨论】:

  • 你有明确的default: 分支吗?如果没有,插入一个,编译器无论如何都会实现它。最好是明确的。
  • 使代码更健壮...“价值永远不会发生”-“著名的遗言”。
  • 请发完整的minimal reproducible example
  • while 的值不同于case

标签: c gcc gcc-warning


【解决方案1】:

使用diagnostic pragmas:

enum somenum {
    EtatIPSortie,
    EtatCCSortie,
};
int etatsImprimanteCasImpressionPeriodique;
enum somenum etatsImprimanteCasChangementCartouche;

void func() {
    while (etatsImprimanteCasImpressionPeriodique != EtatIPSortie)
    {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
            switch (etatsImprimanteCasChangementCartouche)
            {
                case EtatCCSortie:
                break;
            }
#pragma GCC diagnostic pop
    }
}

【讨论】:

  • 你应该只添加一个default: break; 子句,而不是使用特定于编译器的#pragma
【解决方案2】:

使用:

#pragma GCC diagnostic ignored "-W<replace_this_with_the_warning>"

【讨论】:

    猜你喜欢
    • 2011-07-02
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2011-09-27
    • 2019-01-15
    • 2010-11-07
    相关资源
    最近更新 更多