From 95c98a0b5ff71caca6654327bf341719c6587766 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Apr 2021 18:16:29 +0200 Subject: [PATCH] Work around -Wuggest-override for event table macros from gcc 11 Disabling -Wsuggest-override inside macros is broken in gcc, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578, and has started affecting wxWARNING_SUPPRESS_MISSING_OVERRIDE since gcc 11, i.e. this macro doesn't have any effect any more and the warning is still given. Avoid it by actually specifying "override" for gcc 11 (as doing it for all compilers would result in -Winconsistent-missing-override from clang) and check that we don't get this warning in the allheaders test. Also don't use wxDECLARE_ABSTRACT_CLASS() inside wxObject itself, now that it uses "override", which is not appropriate for the base class version. This is arguably more clear and should have been done like this since the beginning anyhow. --- include/wx/defs.h | 11 +++++++++++ include/wx/event.h | 4 ++-- include/wx/object.h | 5 +++-- include/wx/rtti.h | 2 +- tests/allheaders.cpp | 27 +++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/include/wx/defs.h b/include/wx/defs.h index 07533d00f6..629706b1c8 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -740,6 +740,17 @@ typedef short int WXTYPE; # define wxWARNING_RESTORE_MISSING_OVERRIDE() #endif +/* + Macros above don't work with gcc 11 due to a compiler bug, unless we also + use "override" in the function declaration -- but this breaks other + compilers, so define a specific macro for gcc 11 only. + */ +#if wxCHECK_GCC_VERSION(11, 0) +# define wxDUMMY_OVERRIDE wxOVERRIDE +#else +# define wxDUMMY_OVERRIDE +#endif + /* Combination of the two variants above: should be used for deprecated functions which are defined inline and are used by wxWidgets itself. diff --git a/include/wx/event.h b/include/wx/event.h index c14b5def97..c649d2fb8f 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -4322,8 +4322,8 @@ typedef void (wxEvtHandler::*wxPressAndTapEventFunction)(wxPressAndTapEvent&); static const wxEventTableEntry sm_eventTableEntries[]; \ protected: \ wxWARNING_SUPPRESS_MISSING_OVERRIDE() \ - const wxEventTable* GetEventTable() const; \ - wxEventHashTable& GetEventHashTable() const; \ + const wxEventTable* GetEventTable() const wxDUMMY_OVERRIDE; \ + wxEventHashTable& GetEventHashTable() const wxDUMMY_OVERRIDE; \ wxWARNING_RESTORE_MISSING_OVERRIDE() \ static const wxEventTable sm_eventTable; \ static wxEventHashTable sm_eventHashTable diff --git a/include/wx/object.h b/include/wx/object.h index e4baee1396..4ec2bc457f 100644 --- a/include/wx/object.h +++ b/include/wx/object.h @@ -368,8 +368,6 @@ private: class WXDLLIMPEXP_BASE wxObject { - wxDECLARE_ABSTRACT_CLASS(wxObject); - public: wxObject() { m_refData = NULL; } virtual ~wxObject() { UnRef(); } @@ -392,6 +390,7 @@ public: bool IsKindOf(const wxClassInfo *info) const; + virtual wxClassInfo *GetClassInfo() const; // Turn on the correct set of new and delete operators @@ -453,6 +452,8 @@ protected: virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const; wxObjectRefData *m_refData; + + static wxClassInfo ms_classInfo; }; inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo) diff --git a/include/wx/rtti.h b/include/wx/rtti.h index 09faa06d51..0f54a0b914 100644 --- a/include/wx/rtti.h +++ b/include/wx/rtti.h @@ -140,7 +140,7 @@ WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxString& name); #define wxDECLARE_ABSTRACT_CLASS(name) \ public: \ wxWARNING_SUPPRESS_MISSING_OVERRIDE() \ - virtual wxClassInfo *GetClassInfo() const; \ + virtual wxClassInfo *GetClassInfo() const wxDUMMY_OVERRIDE; \ wxWARNING_RESTORE_MISSING_OVERRIDE() \ static wxClassInfo ms_classInfo diff --git a/tests/allheaders.cpp b/tests/allheaders.cpp index 522885b636..2663573da1 100644 --- a/tests/allheaders.cpp +++ b/tests/allheaders.cpp @@ -387,6 +387,33 @@ #include "allheaders.h" +// Check that using wx macros doesn't result in -Wsuggest-override or +// equivalent warnings in classes using and not using "override". +struct Base : wxEvtHandler +{ + virtual ~Base() { } + + virtual void Foo() { } +}; + +struct DerivedWithoutOverride : Base +{ + void OnIdle(wxIdleEvent&) { } + + wxDECLARE_DYNAMIC_CLASS_NO_COPY(DerivedWithoutOverride); + wxDECLARE_EVENT_TABLE(); +}; + +struct DerivedWithOverride : Base +{ + virtual void Foo() wxOVERRIDE { } + + void OnIdle(wxIdleEvent&) { } + + wxDECLARE_DYNAMIC_CLASS_NO_COPY(DerivedWithOverride); + wxDECLARE_EVENT_TABLE(); +}; + #ifdef GCC_TURN_OFF // Just using REQUIRE() below triggers -Wparentheses, so avoid it. GCC_TURN_OFF(parentheses)