如何修复GCC中的“此条件具有相同的分支”错误?

huangapple go评论106阅读模式
英文:

How to fix "this condition has identical branches" in GCC?

问题

#include <stdio.h>

struct file_format_a_header 
{
    int i;
};

struct file_format_b_header 
{
    int i;
};

int main()
{
    int type = 1;
    size_t len = 0;

    if (1 == type)
    {
        len = sizeof(struct file_format_a_header ); // sizeof file_format_a_header and file_format_b_header  is SAME but they are different structures
    }
    else
    {
        len = sizeof(struct file_format_b_header );
    }

    printf("len = %zu\n", len);
    return 0;
}
<source>: In function 'main':
<source>:18:8: error: this condition has identical branches [-Werror=duplicated-branches]
   18 |     if (1 == type)
      |        ^
cc1: all warnings being treated as errors
ASM generation compiler returned: 1
<source>: In function 'main':
<source>:18:8: error: this condition has identical branches [-Werror=duplicated-branches]
   18 |     if (1 == type)
      |        ^
cc1: all warnings being treated as errors
Execution build compiler returned: 1

GCC开关:

-std=gnu11
-Wall
-Werror
-Wextra
-Wduplicated-branches

问题:是否有办法解决这个GCC警告?

英文:

https://godbolt.org/z/YK5PPvz7d

#include &lt;stdio.h&gt;

struct file_format_a_header 
{
    int i;
};

struct file_format_b_header 
{
    int i;
};

int main()
{
    int type = 1;
    size_t len = 0;

    if (1 == type)
    {
        len = sizeof(struct file_format_a_header ); // sizeof file_format_a_header and file_format_b_header  is SAME but they are different structures
    }
    else
    {
        len = sizeof(struct file_format_b_header );
    }

    printf(&quot;len = %zu\n&quot;, len);
    return 0;
}


&lt;source&gt;: In function &#39;main&#39;:
&lt;source&gt;:18:8: error: this condition has identical branches [-Werror=duplicated-branches]
   18 |     if (1 == type)
      |        ^
cc1: all warnings being treated as errors
ASM generation compiler returned: 1
&lt;source&gt;: In function &#39;main&#39;:
&lt;source&gt;:18:8: error: this condition has identical branches [-Werror=duplicated-branches]
   18 |     if (1 == type)
      |        ^
cc1: all warnings being treated as errors
Execution build compiler returned: 1

GCC switches:

-std=gnu11
-Wall
-Werror
-Wextra
-Wduplicated-branches

Question> Is there a way that I can fix this GCC warning?

答案1

得分: 3

gcc可能会在这里生成警告,因为sizeof操作符的结果是一个编译时常量(除非其操作数是变长数组)。

因此,它实际上看到了这样的情况:

if (1 == type)
{
    len = 4;
}
else
{
    len = 4;
}

即使涉及的两个结构体具有不同的成员名称、不同的成员类型或不同数量的成员,只要它们的大小相同,这种情况也会发生。

您可以通过在特定范围内添加pragma来忽略特定的警告来解决此问题:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wduplicated-branches"
if (1 == type)
{
    len = sizeof(struct ABC);
}
else
{
    len = sizeof(struct abc);
}
#pragma GCC diagnostic pop

这样可以抑制特定警告。

英文:

gcc is likely generating a warning here because the result of the sizeof operator is a compile time constant (unless its operand is a variable length array).

So effectively it sees this:

    if (1 == type)
    {
        len = 4;
    }
    else
    {
        len = 4;
    }

This is happening even if the two structs in question have differently named members, differing member types, or a differing number of members, as long as the sizes are the same.

You can work around this by adding pragma to ignore this specific warning in a particular scope:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored &quot;-Wduplicated-branches&quot;
    if (1 == type)
    {
        len = sizeof(struct ABC);
    }
    else
    {
        len = sizeof(struct abc);
    }
#pragma GCC diagnostic pop

huangapple
  • 本文由 发表于 2023年6月9日 03:17:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定