[clang] [clang] more useful error message for decomposition declaration missing initializer (PR #127924)

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 19 17:08:51 PST 2025


https://github.com/kbrav updated https://github.com/llvm/llvm-project/pull/127924

>From 7f7b9b3f2e7324bd290decb7151c9432875b1dd6 Mon Sep 17 00:00:00 2001
From: kbrav <kevin at halys.dev>
Date: Wed, 19 Feb 2025 19:05:05 -0500
Subject: [PATCH 1/2] [clang] more useful error message for decomposition
 declaration missing initializer (#90107)

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td |  2 +-
 clang/lib/Sema/SemaDecl.cpp                      | 12 +++++++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index feef50812eca9..ad36ae898b147 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -555,7 +555,7 @@ def err_decomp_decl_template : Error<
 def err_decomp_decl_not_alone : Error<
   "decomposition declaration must be the only declaration in its group">;
 def err_decomp_decl_requires_init : Error<
-  "decomposition declaration %0 requires an initializer">;
+  "decomposition declaration %0 requires an initializer, but got %1 instead">;
 def err_decomp_decl_wrong_number_bindings : Error<
   "type %0 decomposes into %3 %plural{1:element|:elements}2, but "
   "%select{%plural{0:no|:only %1}1|%1}4 "
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 362df485a025c..c62041c8c5e93 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14058,7 +14058,17 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
 
     // C++1z [dcl.dcl]p1 grammar implies that an initializer is mandatory.
     if (isa<DecompositionDecl>(RealDecl)) {
-      Diag(Var->getLocation(), diag::err_decomp_decl_requires_init) << Var;
+      Preprocessor  &PP = getPreprocessor();
+      SourceManager &SM = Context.getSourceManager();
+      LangOptions    LO = Context.getLangOpts();
+
+      // Lexer previously checked for '=' and didn't find it
+      // Highlight the token found in its place in the error message
+      Token Tok;
+      Lexer::getRawToken(PP.getLastCachedTokenLocation(), Tok, SM, LO);
+
+      Diag(Tok.getLocation(), diag::err_decomp_decl_requires_init)
+        << Var << Lexer::getSpelling(Tok, SM, LO);
       Var->setInvalidDecl();
       return;
     }

>From cd657462ec40663896fa60fdabf565625188958f Mon Sep 17 00:00:00 2001
From: kbrav <kevin at halys.dev>
Date: Wed, 19 Feb 2025 19:51:19 -0500
Subject: [PATCH 2/2] run clang-format

---
 clang/lib/Sema/SemaDecl.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c62041c8c5e93..724da72b8115e 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14058,9 +14058,9 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
 
     // C++1z [dcl.dcl]p1 grammar implies that an initializer is mandatory.
     if (isa<DecompositionDecl>(RealDecl)) {
-      Preprocessor  &PP = getPreprocessor();
+      Preprocessor &PP = getPreprocessor();
       SourceManager &SM = Context.getSourceManager();
-      LangOptions    LO = Context.getLangOpts();
+      LangOptions LO = Context.getLangOpts();
 
       // Lexer previously checked for '=' and didn't find it
       // Highlight the token found in its place in the error message
@@ -14068,7 +14068,7 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
       Lexer::getRawToken(PP.getLastCachedTokenLocation(), Tok, SM, LO);
 
       Diag(Tok.getLocation(), diag::err_decomp_decl_requires_init)
-        << Var << Lexer::getSpelling(Tok, SM, LO);
+          << Var << Lexer::getSpelling(Tok, SM, LO);
       Var->setInvalidDecl();
       return;
     }



More information about the cfe-commits mailing list