[clang-tools-extra] [clang-tidy] Fix crash in `cppcoreguidelines-pro-type-member-init` with alias template in constructor initializer (PR #192786)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 18 05:24:27 PDT 2026
https://github.com/flovent updated https://github.com/llvm/llvm-project/pull/192786
>From a6697685185835cbe5102f1f8bd0af347d3e000f Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 18 Apr 2026 19:18:06 +0800
Subject: [PATCH 1/5] [clang-tidy] Fix crash in
`cppcoreguidelines-pro-type-member-init` with alias template in constructor
initializer
---
.../ProTypeMemberInitCheck.cpp | 10 +++++++---
clang-tools-extra/docs/ReleaseNotes.rst | 9 ++++++---
.../pro-type-member-init-no-crash.cpp | 19 +++++++++++++++++++
3 files changed, 32 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
index 07d71968a07b8..74cd62cd869f8 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
@@ -571,9 +571,13 @@ void ProTypeMemberInitCheck::checkMissingBaseClassInitializer(
return;
for (const CXXCtorInitializer *Init : Ctor->inits())
- if (Init->isBaseInitializer() && Init->isWritten())
- BasesToInit.erase(
- Init->getBaseClass()->getAsCXXRecordDecl()->getCanonicalDecl());
+ if (Init->isBaseInitializer() && Init->isWritten()) {
+ // In template AST BaseInitializer could be generated too even if it's
+ // not target to base class.
+ if (const CXXRecordDecl *CRD =
+ Init->getBaseClass()->getAsCXXRecordDecl())
+ BasesToInit.erase(CRD->getCanonicalDecl());
+ }
}
if (BasesToInit.empty())
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 95ed0061d654c..980939c6ae0a3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -344,9 +344,12 @@ Changes in existing checks
lambda captures such as ``[t{std::forward<T>(t)}]``.
- Improved :doc:`cppcoreguidelines-pro-type-member-init
- <clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check by fixing
- a false positive when a base class has a forward declaration before its
- definition.
+ <clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check:
+
+ - Fixed a false positive when a base class has a forward declaration before
+ its definition.
+
+ - Fixed a crash with alias template in constructor initializer.
- Improved :doc:`cppcoreguidelines-pro-type-vararg
<clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check by no longer
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
index 2e2964dda1daf..f5ba3a3bd134f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
@@ -13,3 +13,22 @@ template <typename T> class NoCrash {
template <typename U> B(U u) {}
};
};
+
+namespace gh192510 {
+ template<typename T>
+ struct C {
+
+ };
+
+ struct Base {
+ int x;
+ };
+
+ template<typename T>
+ class X: public Base {
+ using INT = C<T>;
+
+ X(INT i) : INT(i) {}
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: constructor does not initialize these bases: Base
+ };
+}
\ No newline at end of file
>From 76b8651c5563d4b527c362c29eb3f89876f8ee8a Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 18 Apr 2026 19:20:10 +0800
Subject: [PATCH 2/5] Add extra line
---
.../cppcoreguidelines/pro-type-member-init-no-crash.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
index f5ba3a3bd134f..c454874503eac 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
@@ -31,4 +31,4 @@ namespace gh192510 {
X(INT i) : INT(i) {}
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: constructor does not initialize these bases: Base
};
-}
\ No newline at end of file
+}
>From fae5dc96c4ac09d741cb815d723cbf3929f762b5 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 18 Apr 2026 19:40:56 +0800
Subject: [PATCH 3/5] Remove check-message since it's not applied to windows
---
.../cppcoreguidelines/pro-type-member-init-no-crash.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
index c454874503eac..a01789c2fea61 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
@@ -28,7 +28,6 @@ namespace gh192510 {
class X: public Base {
using INT = C<T>;
- X(INT i) : INT(i) {}
- // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: constructor does not initialize these bases: Base
+ X(INT i) : INT(i) {} // no crash
};
}
>From 00268e8dd6779d651b73686d7711529cab3e8a3d Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 18 Apr 2026 20:22:45 +0800
Subject: [PATCH 4/5] Move the test case to main testfile
---
.../pro-type-member-init-no-crash.cpp | 18 ------------------
.../pro-type-member-init.cpp | 19 +++++++++++++++++++
2 files changed, 19 insertions(+), 18 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
index a01789c2fea61..2e2964dda1daf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
@@ -13,21 +13,3 @@ template <typename T> class NoCrash {
template <typename U> B(U u) {}
};
};
-
-namespace gh192510 {
- template<typename T>
- struct C {
-
- };
-
- struct Base {
- int x;
- };
-
- template<typename T>
- class X: public Base {
- using INT = C<T>;
-
- X(INT i) : INT(i) {} // no crash
- };
-}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.cpp
index 7468ce04434b0..55963eae2c1b5 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.cpp
@@ -635,3 +635,22 @@ namespace PR155416 {
Ct() : St{0} {}
};
}
+
+namespace gh192510 {
+ template<typename T>
+ struct C {
+
+ };
+
+ struct Base {
+ int x;
+ };
+
+ template<typename T>
+ class X: public Base {
+ using INT = C<T>;
+
+ X(INT i) : INT(i) {} // no crash
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: constructor does not initialize these bases: Base
+ };
+}
>From f3aefd4d6e98f2af61a7717a3166c28affad0f95 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 18 Apr 2026 20:24:12 +0800
Subject: [PATCH 5/5] rm release note
---
clang-tools-extra/docs/ReleaseNotes.rst | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 980939c6ae0a3..95ed0061d654c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -344,12 +344,9 @@ Changes in existing checks
lambda captures such as ``[t{std::forward<T>(t)}]``.
- Improved :doc:`cppcoreguidelines-pro-type-member-init
- <clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check:
-
- - Fixed a false positive when a base class has a forward declaration before
- its definition.
-
- - Fixed a crash with alias template in constructor initializer.
+ <clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check by fixing
+ a false positive when a base class has a forward declaration before its
+ definition.
- Improved :doc:`cppcoreguidelines-pro-type-vararg
<clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check by no longer
More information about the cfe-commits
mailing list