[llvm] 8132359 - [CMake] Don't add -Wnon-virtual-dtor if affected by GCC PR102168

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 7 23:36:52 PDT 2021


Author: Fangrui Song
Date: 2021-09-07T23:36:47-07:00
New Revision: 813235947d07890ea55a6de039261d0c409c8b42

URL: https://github.com/llvm/llvm-project/commit/813235947d07890ea55a6de039261d0c409c8b42
DIFF: https://github.com/llvm/llvm-project/commit/813235947d07890ea55a6de039261d0c409c8b42.diff

LOG: [CMake] Don't add -Wnon-virtual-dtor if affected by GCC PR102168

See the discussion on
https://reviews.llvm.org/rG4852c770fe8703145dd2a35395985646ce57a454
The GCC behavior (https://gcc.gnu.org/PR102168) seems unhelpful.

Unconditional -Wnon-virtual-dtor led to other unnecessary workarounds like
6df09d6ccbc0cb72d3278cafb592e9bc0e6b84a1

This patches uses a variant of the 4bb5f44c701402462cb93ef00d46d52382f39f11
check to detect GCC PR102168.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D109404

Added: 
    

Modified: 
    llvm/cmake/modules/HandleLLVMOptions.cmake

Removed: 
    


################################################################################
diff  --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake
index af7dee903e6da..6df81f4c6f740 100644
--- a/llvm/cmake/modules/HandleLLVMOptions.cmake
+++ b/llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -736,7 +736,18 @@ if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
   check_cxx_compiler_flag("-Wnoexcept-type" CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG)
   append_if(CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG "-Wno-noexcept-type" CMAKE_CXX_FLAGS)
 
-  append("-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
+  # Check if -Wnon-virtual-dtor warns for a class marked final, when it has a
+  # friend declaration. If it does, don't add -Wnon-virtual-dtor. The case is
+  # considered unhelpful (https://gcc.gnu.org/PR102168).
+  set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
+  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=non-virtual-dtor")
+  CHECK_CXX_SOURCE_COMPILES("class f {};
+                             class base {friend f; public: virtual void anchor();protected: ~base();};
+                             int main() { return 0; }"
+                            CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR)
+  set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
+  append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
+
   append("-Wdelete-non-virtual-dtor" CMAKE_CXX_FLAGS)
 
   # Enable -Wsuggest-override if it's available, and only if it doesn't


        


More information about the llvm-commits mailing list