llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Devon Loehr (DKLoehr)

<details>
<summary>Changes</summary>

This turns on the unnecessary-virtual-specifier warning in genera, but disables 
it when building LLVM. It also tweaks the warning description to be slightly 
more accurate.

Background: I've been working on cleaning up this warning in two codebases: 
LLVM and chromium (plus its dependencies). The chromium cleanup has been 
straightforward. Git archaeology shows that there are two reasons for the 
warnings: classes to which `final` was added after they were initially 
committed, and classes with virtual destructors that nobody remarks on. 
Presumably the latter case is because people are just very used to destructors 
being virtual.

The LLVM cleanup was more surprising: I discovered that we have an [old 
policy](https://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers)
 about including out-of-line virtual functions in every class with a vtable, 
even `final` ones. This means our codebase has many virtual "anchor" functions 
which do nothing except control where the vtable is emitted, and which trigger 
the warning. I looked into alternatives to satisfy the policy, such as using 
destructors instead of introducing a new function, but it wasn't clear if they 
had larger implications.

Overall, it seems like the warning is genuinely useful in most codebases 
(evidenced by chromium and its dependencies), and LLVM is an unusual case. 
Therefore we should enable the warning by default, and turn it off only for 
LLVM builds.

---
Full diff: https://github.com/llvm/llvm-project/pull/133265.diff


3 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticGroups.td (+3-4) 
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+1-1) 
- (modified) llvm/cmake/modules/HandleLLVMOptions.cmake (+1-1) 


``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index b9f08d96151c9..e6e9ebbc2c304 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -377,13 +377,12 @@ def CXX11WarnSuggestOverride : 
DiagGroup<"suggest-override">;
 def WarnUnnecessaryVirtualSpecifier : 
DiagGroup<"unnecessary-virtual-specifier"> {
   code Documentation = [{
 Warns when a ``final`` class contains a virtual method (including virtual
-destructors). Since ``final`` classes cannot be subclassed, their methods
-cannot be overridden, and hence the ``virtual`` specifier is useless.
+destructors) that does not override anything. Since ``final`` classes cannot be
+subclassed, their methods cannot be overridden, so there is no point to
+introducing new ``virtual`` methods.
 
 The warning also detects virtual methods in classes whose destructor is
 ``final``, for the same reason.
-
-The warning does not fire on virtual methods which are also marked 
``override``.
   }];
 }
 
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index c77cde297dc32..05ded7d9ecef1 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2733,7 +2733,7 @@ def note_final_dtor_non_final_class_silence : Note<
   "mark %0 as '%select{final|sealed}1' to silence this warning">;
 def warn_unnecessary_virtual_specifier : Warning<
   "virtual method %0 is inside a 'final' class and can never be overridden">,
-  InGroup<WarnUnnecessaryVirtualSpecifier>, DefaultIgnore;
+  InGroup<WarnUnnecessaryVirtualSpecifier>;
 
 // C++11 attributes
 def err_repeat_attribute : Error<"%0 attribute cannot be repeated">;
diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake 
b/llvm/cmake/modules/HandleLLVMOptions.cmake
index 185c9b63aada3..f2c5cf4241e3d 100644
--- a/llvm/cmake/modules/HandleLLVMOptions.cmake
+++ b/llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -689,7 +689,7 @@ if ( LLVM_COMPILER_IS_GCC_COMPATIBLE OR 
CMAKE_CXX_COMPILER_ID MATCHES "XL" )
 endif( LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL" )
 
 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
-  append("-Werror=unguarded-availability-new" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+  append("-Werror=unguarded-availability-new 
-Wno-unnecessary-virtual-specifier" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
 endif()
 
 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND LLVM_ENABLE_LTO)

``````````

</details>


https://github.com/llvm/llvm-project/pull/133265
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to