[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:19:13 PDT 2025
https://github.com/PiotrZSL created https://github.com/llvm/llvm-project/pull/150290
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.
Fixes #139031.
>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] [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();
+}
+
+}
More information about the cfe-commits
mailing list