[clang-tools-extra] [clang-tidy] Ignore pure-virtual in portability-template... (PR #150290)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 23 12:51:29 PDT 2025


https://github.com/PiotrZSL updated https://github.com/llvm/llvm-project/pull/150290

>From 76f481c6471f71151855082360416078c14675ea Mon Sep 17 00:00:00 2001
From: Piotr Zegar <me at piotrzegar.pl>
Date: Wed, 23 Jul 2025 19:11:28 +0000
Subject: [PATCH 1/2] [clang-tidy] Ignore pure-virtual in
 portability-template...

Ignore pure virtual member functions in
portability-template-virtual-member-function check.
Those functions will be represented in vtable
as __cxa_pure_virtual or something similar.
---
 .../TemplateVirtualMemberFunctionCheck.cpp      |  5 +++--
 clang-tools-extra/docs/ReleaseNotes.rst         |  6 +++++-
 .../template-virtual-member-function.cpp        | 17 +++++++++++++++++
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/portability/TemplateVirtualMemberFunctionCheck.cpp b/clang-tools-extra/clang-tidy/portability/TemplateVirtualMemberFunctionCheck.cpp
index 9c2f27f01f184..aaa23367a3825 100644
--- a/clang-tools-extra/clang-tidy/portability/TemplateVirtualMemberFunctionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/portability/TemplateVirtualMemberFunctionCheck.cpp
@@ -18,10 +18,11 @@ AST_MATCHER(CXXMethodDecl, isUsed) { return Node.isUsed(); }
 
 void TemplateVirtualMemberFunctionCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
-      cxxMethodDecl(ofClass(classTemplateSpecializationDecl(
+      cxxMethodDecl(isVirtual(),
+                    ofClass(classTemplateSpecializationDecl(
                                 unless(isExplicitTemplateSpecialization()))
                                 .bind("specialization")),
-                    isVirtual(), unless(isUsed()),
+                    unless(isUsed()), unless(isPure()),
                     unless(cxxDestructorDecl(isDefaulted())))
           .bind("method"),
       this);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index c5f756aeee6eb..c75d9ca71cad7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -121,7 +121,11 @@ Changes in existing checks
 
 - Improved :doc:`misc-header-include-cycle
   <clang-tidy/checks/misc/header-include-cycle>` check performance.
-  
+
+- Improved :doc:`portability-template-virtual-member-function
+  <clang-tidy/checks/portability/template-virtual-member-function>` check to
+  avoid false positives on pure virtual member functions.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/portability/template-virtual-member-function.cpp b/clang-tools-extra/test/clang-tidy/checkers/portability/template-virtual-member-function.cpp
index 94786ae93dd3f..4626365699888 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/portability/template-virtual-member-function.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/portability/template-virtual-member-function.cpp
@@ -171,3 +171,20 @@ struct NoInstantiation<int, U>{
     };
 };
 } // namespace PartialSpecializationNoInstantiation
+
+namespace PR139031 {
+
+template<typename T>
+struct Base {
+    virtual void foo() = 0;
+};
+
+struct Derived : public Base<int> {
+    void foo() {}
+};
+
+void test() {
+    Derived{}.foo();
+}
+
+}

>From 2227ab80ba1ee3aa3e157fd6bf7fb659f33125a5 Mon Sep 17 00:00:00 2001
From: Piotr Zegar <me at piotrzegar.pl>
Date: Wed, 23 Jul 2025 21:51:21 +0200
Subject: [PATCH 2/2] Update
 clang-tools-extra/test/clang-tidy/checkers/portability/template-virtual-member-function.cpp

Change namespace name

Co-authored-by: Baranov Victor <bar.victor.2002 at gmail.com>
---
 .../checkers/portability/template-virtual-member-function.cpp   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/test/clang-tidy/checkers/portability/template-virtual-member-function.cpp b/clang-tools-extra/test/clang-tidy/checkers/portability/template-virtual-member-function.cpp
index 4626365699888..2a67b79cb358a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/portability/template-virtual-member-function.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/portability/template-virtual-member-function.cpp
@@ -172,7 +172,7 @@ struct NoInstantiation<int, U>{
 };
 } // namespace PartialSpecializationNoInstantiation
 
-namespace PR139031 {
+namespace PureVirtual {
 
 template<typename T>
 struct Base {



More information about the cfe-commits mailing list