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

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 7 17:04:05 PDT 2021


MaskRay created this revision.
MaskRay added a reviewer: dblaikie.
Herald added a subscriber: mgorny.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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

Unconditional -Wnon-virtual-dtor led to other unnecessary workarounds like
6df09d6ccbc0cb72d3278cafb592e9bc0e6b84a1 <https://reviews.llvm.org/rG6df09d6ccbc0cb72d3278cafb592e9bc0e6b84a1>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109404

Files:
  llvm/cmake/modules/HandleLLVMOptions.cmake


Index: llvm/cmake/modules/HandleLLVMOptions.cmake
===================================================================
--- llvm/cmake/modules/HandleLLVMOptions.cmake
+++ llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -736,7 +736,20 @@
   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 not useful (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();};
+                             class derived final : public base { public: ~derived();};
+                             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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109404.371220.patch
Type: text/x-patch
Size: 1447 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210908/b130ed25/attachment.bin>


More information about the llvm-commits mailing list