https://github.com/emily-dror updated 
https://github.com/llvm/llvm-project/pull/149781

>From 90ae570afa7b323be99fd36fd833902f5de9918e Mon Sep 17 00:00:00 2001
From: Emily Dror <emilydro...@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;
     }
   }
 

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to