Skip to content

Commit 274b6b0

Browse files
committed
Only enable -Wsuggest-override if it doesn't suggest adding override to functions that are already final
A previous patch added -Wsuggest-override using a simple add_flag_if_supported(). This causes lots of warnings in LLVM when building with older GCC versions (< 9.2) which suggest adding override to functions that are only marked final. The current flags in both GCC >=9.2 and Clang accept plain final as equivalent to override final. This patch adds logic to detect versions of -Wsuggest-override that warn on void foo() final and disables them to avoid warning spam in builds using older GCC's. This has the added minor benefit of getting rid of the useless C_SUPPORTS_SUGGEST_OVERRIDE_FLAG CMake cache variable which was set by add_flag_if_supported(). Differential Revision: https://reviews.llvm.org/D84292
1 parent a1d99af commit 274b6b0

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

llvm/cmake/modules/HandleLLVMOptions.cmake

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,20 @@ if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
672672
# Enable -Wdelete-non-virtual-dtor if available.
673673
add_flag_if_supported("-Wdelete-non-virtual-dtor" DELETE_NON_VIRTUAL_DTOR_FLAG)
674674

675-
# Enable -Wsuggest-override if available.
676-
add_flag_if_supported("-Wsuggest-override" SUGGEST_OVERRIDE_FLAG)
675+
# Enable -Wsuggest-override if it's available, and only if it doesn't
676+
# suggest adding 'override' to functions that are already marked 'final'
677+
# (which means it is disabled for GCC < 9.2).
678+
check_cxx_compiler_flag("-Wsuggest-override" CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG)
679+
if (CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG)
680+
set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
681+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=suggest-override")
682+
CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();};
683+
class derived : base {public: void anchor() final;};
684+
int main() { return 0; }"
685+
CXX_WSUGGEST_OVERRIDE_ALLOWS_ONLY_FINAL)
686+
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
687+
append_if(CXX_WSUGGEST_OVERRIDE_ALLOWS_ONLY_FINAL "-Wsuggest-override" CMAKE_CXX_FLAGS)
688+
endif()
677689

678690
# Check if -Wcomment is OK with an // comment ending with '\' if the next
679691
# line is also a // comment.

0 commit comments

Comments
 (0)