强制 GCC 4.x 将 -Wreturn-type 视为错误而不启用 -Werror?

发布于 2024-10-03 02:14:48 字数 2186 浏览 5 评论 0原文

假设我们有以下代码:

#if !defined(__cplusplus)
#  error This file should be compiled as C++
#endif

#include <stdio.h>
#include <string>

//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
    SomeClass() {}
    ~SomeClass() {}
    std::string GetSomeString()
    {
        // case #1
    }
};
#endif // USE_CXX_CLASS

int foo()
{
    // case #2
}

int
main (int argc, char *argv[])
{
    (void)argc;
    (void)argv;
#ifdef USE_CXX_CLASS
    SomeClass someInstance;
    someInstance.GetSomeString();
#endif // USE_CXX_CLASS
    foo();
    return 0;
}

并假设它是从 GCC 版本 4.2.1 中使用选项 -Wreturn-type -Werror=return-type。如果上面的代码按原样编译,而没有先取消注释上面的 //#define USE_CXX_CLASS 行,那么您将看到一条警告但没有错误

.../gcc-4.2.1/bin/g++   -g    -fPIC -Wreturn-type -Werror=return-type    test.cpp -c -o test.o
test.cpp: In function 'int foo()':
test.cpp:26: warning: control reaches end of non-void function

但是如果 //#define USE_CXX_CLASS 行取消注释,则警告视为错误:

.../gcc-4.2.1/bin/g++   -g    -fPIC -Wreturn-type -Werror=return-type    test.cpp -c -o test.o
test.cpp: In member function 'std::string SomeClass::GetSomeString()':
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1

是的,一个是非成员函数(情况 #2),另一个是 C++函数(案例#1)。海事组织,这应该不重要。我希望这两个条件都被视为错误,并且我不想在此时添加 -Werror-Wall (可能稍后会这样做,但那超出了这个问题的范围)。

我的子问题是:

  1. 是否有一些我缺少的 GCC 开关应该可以工作? (不,我不想使用#pragma。)
  2. 这是一个已在更新版本的 GCC 中解决的错误吗?

作为参考,我已经提出了其他类似的问题,包括以下内容:

Suppose we have the following code:

#if !defined(__cplusplus)
#  error This file should be compiled as C++
#endif

#include <stdio.h>
#include <string>

//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
    SomeClass() {}
    ~SomeClass() {}
    std::string GetSomeString()
    {
        // case #1
    }
};
#endif // USE_CXX_CLASS

int foo()
{
    // case #2
}

int
main (int argc, char *argv[])
{
    (void)argc;
    (void)argv;
#ifdef USE_CXX_CLASS
    SomeClass someInstance;
    someInstance.GetSomeString();
#endif // USE_CXX_CLASS
    foo();
    return 0;
}

And suppose that it were to be compiled the C++ compiler (and not the C compiler) from GCC version 4.2.1 with the options -Wreturn-type -Werror=return-type. If the above code is compiled as is without first uncommenting the //#define USE_CXX_CLASS line above, then you will see a warning but no error:

.../gcc-4.2.1/bin/g++   -g    -fPIC -Wreturn-type -Werror=return-type    test.cpp -c -o test.o
test.cpp: In function 'int foo()':
test.cpp:26: warning: control reaches end of non-void function

But if the //#define USE_CXX_CLASS line is uncommented, then the warning is treated as an error:

.../gcc-4.2.1/bin/g++   -g    -fPIC -Wreturn-type -Werror=return-type    test.cpp -c -o test.o
test.cpp: In member function 'std::string SomeClass::GetSomeString()':
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1

Yes, one is a non-member function (case #2), and the other is a C++ function (case #1). IMO, that should not matter. I want both conditions treated as an error, and I don't want to add -Werror or -Wall at this point in time (probably will do so later, but that is out of scope of this question).

My sub-questions are:

  1. Is there some GCC switch that I am missing that should work? (No I do not want to use #pragma's.)
  2. Is this a bug that has been addressed in a more recent version of GCC?

For reference, I have already poured through other similar questions already, including the following:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不知所踪 2024-10-10 02:14:48

它已被修复,它与 g++ 9.3 配合良好:成员函数和自由函数都被 -Wall -Werror=return-type 视为错误

It has been fixed, it works well with g++ 9.3: both member functions and free functions are treated as error with -Wall -Werror=return-type

墨落画卷 2024-10-10 02:14:48

即使没有 USE_CXX_CLASS 标志,我也确实看到了错误。即g++对于类成员函数和非成员函数的错误都是一致的。
g++ (GCC) 4.4.3 20100127(红帽 4.4.3-4)

I do see an error even w/o the USE_CXX_CLASS flag. i.e. g++ is consistent with the error for both class member functions and non member functions.
g++ (GCC) 4.4.3 20100127 (Red Hat 4.4.3-4)

压抑⊿情绪 2024-10-10 02:14:48

在我看来,你需要的是一个围绕 gcc 的 shell 脚本包装器。

  • 将其命名为 gcc-wrapperg++-wrapper
  • 在 Makefile 中将 CC 和 CXX 设置为包装器。
  • 让包装器调用 GCC 并将其输出传送到另一个程序,该程序将搜索您想要的警告字符串。
  • 当搜索程序发现警告时,让其退出并返回错误。

It seems to me that what you need is a shell script wrapper around gcc.

  • Name it something like gcc-wrapper and g++-wrapper.
  • In your Makefile set CC and CXX to the wrappers.
  • Have the wrapper invoke GCC and pipe its output to another program which will search for your desired warning strings.
  • Have the search program exit with an error when it finds the warning.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文