[clang] [clang][Diagnostic] Clarify error message for auto (PR #149781)
Emily Dror via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 21 02:09:45 PDT 2025
https://github.com/emily-dror created https://github.com/llvm/llvm-project/pull/149781
Show line and file for auto template error in -std=c++20 or -std=c++23
>From 90ae570afa7b323be99fd36fd833902f5de9918e Mon Sep 17 00:00:00 2001
From: Emily Dror <emilydror01 at gmail.com>
Date: Mon, 21 Jul 2025 12:00:08 +0300
Subject: [PATCH] [clang][Diagnostic] Clarify error message for auto
Show line and file for auto template error in -std=c++20 or -std=c++23
---
clang/lib/Sema/SemaTemplate.cpp | 24 ++++++++++++++++++------
1 file changed, 18 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index b76619fc50268..4505d5f7f2226 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8238,12 +8238,24 @@ Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) {
// C++ [temp.mem]p2:
// A local class shall not have member templates.
- if (RD->isLocalClass())
- return Diag(TemplateParams->getTemplateLoc(),
- diag::err_template_inside_local_class)
- << TemplateParams->getSourceRange();
- else
- return false;
+ if (RD->isLocalClass()) {
+ SourceLocation DiagLoc = TemplateParams->getTemplateLoc();
+ if (DiagLoc.isInvalid()) {
+ for (const NamedDecl *Param : *TemplateParams) {
+ if (Param && Param->getLocation().isValid()) {
+ DiagLoc = Param->getLocation();
+ break;
+ }
+ }
+ }
+ if (DiagLoc.isInvalid()) {
+ // Still no good location? Fall back to the class declaration itself
+ DiagLoc = RD->getLocation();
+ }
+ return Diag(DiagLoc, diag::err_template_inside_local_class)
+ << TemplateParams->getSourceRange();
+ }
+ return false;
}
}
More information about the cfe-commits
mailing list