[PATCH] D147673: [Clang] Improve designated inits diagnostic location

Bill Wendling via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 5 17:20:20 PDT 2023


void created this revision.
void added a reviewer: rsmith.
Herald added a project: All.
void requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

A "null" designator won't have a valid location. Try to approximate this
location as best we can in that situation.

Closes 61118
Closes 46132


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147673

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/SemaCXX/cxx2b-designated-initializers.cpp


Index: clang/test/SemaCXX/cxx2b-designated-initializers.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/cxx2b-designated-initializers.cpp
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -std=c++2b -fsyntax-only -verify %s
+
+namespace PR61118 {
+
+union S {
+  struct {
+    int a;
+  };
+}; 
+
+void f(int x, auto) {
+  const S result { // expected-error {{field designator (null) does not refer to any field in type 'const S'}}
+    .a = x
+  };
+}
+
+void g(void) {
+  f(0, 0); // expected-note {{in instantiation of function template specialization 'PR61118::f<int>' requested here}}
+}
+
+} // end namespace PR61118
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -2641,8 +2641,13 @@
           hadError = true;
         } else {
           // Typo correction didn't find anything.
-          SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
-            << FieldName << CurrentObjectType;
+          SourceLocation Loc = D->getFieldLoc();
+          if (Loc.isInvalid())
+            // This could happen with a "null" designator (i.e. an anonymous
+            // union/struct). Do our best to approximate the location.
+            Loc = IList->getBeginLoc();
+          SemaRef.Diag(Loc, diag::err_field_designator_unknown)
+            << FieldName << CurrentObjectType << DIE->getSourceRange();
           ++Index;
           return true;
         }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147673.511240.patch
Type: text/x-patch
Size: 1551 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230406/8f660cc2/attachment.bin>


More information about the cfe-commits mailing list