[clang] Fix crash on template-specialization (PR #142338)
Mark de Wever via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 2 00:01:36 PDT 2025
https://github.com/mark-de-wever-sonarsource created https://github.com/llvm/llvm-project/pull/142338
This applies the name restoration as suggested by Richard Smith.
Fixes: #54279
>From 930e073e4d5bfbde8028fbb8330e6b91348cc20b Mon Sep 17 00:00:00 2001
From: Mark de Wever <mark.dewever at sonarsource.com>
Date: Mon, 2 Jun 2025 08:57:59 +0200
Subject: [PATCH] Fix crash on template-specialization
This applies the name restoration as suggested by Richard Smith.
Fixes: #54279
---
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 5 +++++
clang/test/CodeGenCXX/constructor-init.cpp | 2 +-
clang/test/SemaTemplate/default-arguments.cpp | 9 +++++++++
3 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 174c8fc59e4fa..9853466e01496 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -34,6 +34,7 @@
#include "clang/Sema/SemaSwift.h"
#include "clang/Sema/Template.h"
#include "clang/Sema/TemplateInstCallback.h"
+#include "llvm/ADT/ScopeExit.h"
#include "llvm/Support/TimeProfiler.h"
#include <optional>
@@ -5120,6 +5121,10 @@ bool Sema::addInstantiatedParametersToScope(
// Simple case: not a parameter pack.
assert(FParamIdx < Function->getNumParams());
ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
+ DeclarationName name = FunctionParam->getDeclName();
+ auto _ = llvm::make_scope_exit([&]() {
+ FunctionParam->setDeclName(name);
+ });
FunctionParam->setDeclName(PatternParam->getDeclName());
// If the parameter's type is not dependent, update it to match the type
// in the pattern. They can differ in top-level cv-qualifiers, and we want
diff --git a/clang/test/CodeGenCXX/constructor-init.cpp b/clang/test/CodeGenCXX/constructor-init.cpp
index f191599f360e7..b5f4cc0b3d6c8 100644
--- a/clang/test/CodeGenCXX/constructor-init.cpp
+++ b/clang/test/CodeGenCXX/constructor-init.cpp
@@ -160,7 +160,7 @@ template<typename T> struct X;
// Make sure that the instantiated constructor initializes start and
// end properly.
-// CHECK-LABEL: define linkonce_odr void @_ZN1XIiEC2ERKS0_(ptr {{[^,]*}} %this, ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %other) unnamed_addr
+// CHECK-LABEL: define linkonce_odr void @_ZN1XIiEC2ERKS0_(ptr {{[^,]*}} %this, ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %0) unnamed_addr
// CHECK: {{store.*null}}
// CHECK: {{store.*null}}
// CHECK: ret
diff --git a/clang/test/SemaTemplate/default-arguments.cpp b/clang/test/SemaTemplate/default-arguments.cpp
index 5ea34c0254ec1..a366c3d8ab722 100644
--- a/clang/test/SemaTemplate/default-arguments.cpp
+++ b/clang/test/SemaTemplate/default-arguments.cpp
@@ -283,3 +283,12 @@ static_assert(S<short *>().SizeOfT<char>() == sizeof(short *), "");
} // namespace GH68490
#endif
+
+namespace PR54279 {
+// Using a different name for the argument when there is a default argument
+// caused a crash.
+template <typename T> void f(const T &a, int c = 0);
+template <> void f(const int &unused, int) {
+ f(42);
+}
+}
More information about the cfe-commits
mailing list