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

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

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

问题

  1. #include <stdio.h>
  2. struct file_format_a_header
  3. {
  4. int i;
  5. };
  6. struct file_format_b_header
  7. {
  8. int i;
  9. };
  10. int main()
  11. {
  12. int type = 1;
  13. size_t len = 0;
  14. if (1 == type)
  15. {
  16. len = sizeof(struct file_format_a_header ); // sizeof file_format_a_header and file_format_b_header is SAME but they are different structures
  17. }
  18. else
  19. {
  20. len = sizeof(struct file_format_b_header );
  21. }
  22. printf("len = %zu\n", len);
  23. return 0;
  24. }
  1. <source>: In function 'main':
  2. <source>:18:8: error: this condition has identical branches [-Werror=duplicated-branches]
  3. 18 | if (1 == type)
  4. | ^
  5. cc1: all warnings being treated as errors
  6. ASM generation compiler returned: 1
  7. <source>: In function 'main':
  8. <source>:18:8: error: this condition has identical branches [-Werror=duplicated-branches]
  9. 18 | if (1 == type)
  10. | ^
  11. cc1: all warnings being treated as errors
  12. Execution build compiler returned: 1

GCC开关:

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

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

英文:

https://godbolt.org/z/YK5PPvz7d

  1. #include &lt;stdio.h&gt;
  2. struct file_format_a_header
  3. {
  4. int i;
  5. };
  6. struct file_format_b_header
  7. {
  8. int i;
  9. };
  10. int main()
  11. {
  12. int type = 1;
  13. size_t len = 0;
  14. if (1 == type)
  15. {
  16. len = sizeof(struct file_format_a_header ); // sizeof file_format_a_header and file_format_b_header is SAME but they are different structures
  17. }
  18. else
  19. {
  20. len = sizeof(struct file_format_b_header );
  21. }
  22. printf(&quot;len = %zu\n&quot;, len);
  23. return 0;
  24. }
  25. &lt;source&gt;: In function &#39;main&#39;:
  26. &lt;source&gt;:18:8: error: this condition has identical branches [-Werror=duplicated-branches]
  27. 18 | if (1 == type)
  28. | ^
  29. cc1: all warnings being treated as errors
  30. ASM generation compiler returned: 1
  31. &lt;source&gt;: In function &#39;main&#39;:
  32. &lt;source&gt;:18:8: error: this condition has identical branches [-Werror=duplicated-branches]
  33. 18 | if (1 == type)
  34. | ^
  35. cc1: all warnings being treated as errors
  36. Execution build compiler returned: 1

GCC switches:

  1. -std=gnu11
  2. -Wall
  3. -Werror
  4. -Wextra
  5. -Wduplicated-branches

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

答案1

得分: 3

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

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

  1. if (1 == type)
  2. {
  3. len = 4;
  4. }
  5. else
  6. {
  7. len = 4;
  8. }

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

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

  1. #pragma GCC diagnostic push
  2. #pragma GCC diagnostic ignored "-Wduplicated-branches"
  3. if (1 == type)
  4. {
  5. len = sizeof(struct ABC);
  6. }
  7. else
  8. {
  9. len = sizeof(struct abc);
  10. }
  11. #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:

  1. if (1 == type)
  2. {
  3. len = 4;
  4. }
  5. else
  6. {
  7. len = 4;
  8. }

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:

  1. #pragma GCC diagnostic push
  2. #pragma GCC diagnostic ignored &quot;-Wduplicated-branches&quot;
  3. if (1 == type)
  4. {
  5. len = sizeof(struct ABC);
  6. }
  7. else
  8. {
  9. len = sizeof(struct abc);
  10. }
  11. #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:

确定