英文:
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 <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 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 "-Wduplicated-branches"
if (1 == type)
{
len = sizeof(struct ABC);
}
else
{
len = sizeof(struct abc);
}
#pragma GCC diagnostic pop
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论