[C++] – GCC和LLVM对方法 warning: non-void function does not return a value [-Wreturn-type] 的处理差异

  最近做一个C++开源项目发现一个奇怪问题,通过clang编译链接执行程序每到有一个就崩溃了,gcc下则没有此问题。

后来通过调试,发现原因是bool返回的方法是没有return语句!问题是为啥还能通过编译呢?

列子如下:

#include <iostream>  class Test { public:   bool yes(); };  bool Test::yes() {   std::cout << "yes" << std::endl;   // return false; };  int main() {   Test *t = new Test;   bool r = t->yes();   std::cout << "yes->" << r << std::endl;   return 0; }

 

用g++编译得到警告但是通过了,并且执行得到正确预期(但是值为啥是64?没有找到原因!)。

1 gaojie@root-host:~$ g++ bool.cpp  2 bool.cpp: In member functionbool Test::yes()’: 3 bool.cpp:11:1: warning: no return statement in function returning non-void [-Wreturn-type] 4    11 | }; 5       | ^ 6 gaojie@root-host:~$ ./a.out  7 yes 8 yes->64

 

用clang++编译同样类似警告也通过了,但执行出现异常指令。

gaojie@root-host:~$ clang++ bool.cpp  bool.cpp:12:1: warning: non-void function does not return a value [-Wreturn-type] }; ^ 1 warning generated. gaojie@root-host:~$ ./a.out  yes 非法指令 (核心已转储)

 

本着好奇的心理,就想知其原因为啥会有不一样的结果呢?就想通过汇编语法查询差异。

通过 https://godbolt.org/得到如下:

g++汇编指令如下:

[C++] - GCC和LLVM对方法 warning: non-void function does not return a value [-Wreturn-type] 的处理差异

 

 clang++汇编指令如下:

[C++] - GCC和LLVM对方法 warning: non-void function does not return a value [-Wreturn-type] 的处理差异

 

通过yes方法发现差异了,

gcc汇编有return指令可以正常返回。

而clang就ud2指令结束了,查阅相关资料得到,UD2是一种让CPU产生invalid opcode exception的软件指令.  内核发现CPU出现这个异常, 会立即停止运行.

 

问题原因找到了就是gcc和llvm对编译申明返回值方法而没有返回语句的处理结果是不一样的,g++保证通过而clang则认为无法处理(给了异常指令)退出程序。

 

发表评论

评论已关闭。

相关文章