[clang] 2cbf512 - [Clang] Improve designated inits diagnostic location
Bill Wendling via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 7 11:37:35 PDT 2023
Author: Bill Wendling
Date: 2023-04-07T11:37:05-07:00
New Revision: 2cbf5127d58553fd6224a1ec809536bccc5b8510
URL: https://github.com/llvm/llvm-project/commit/2cbf5127d58553fd6224a1ec809536bccc5b8510
DIFF: https://github.com/llvm/llvm-project/commit/2cbf5127d58553fd6224a1ec809536bccc5b8510.diff
LOG: [Clang] Improve designated inits diagnostic location
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
Reviewed By: shafik
Differential Revision: https://reviews.llvm.org/D147673
Added:
clang/test/SemaCXX/cxx2b-designated-initializers.cpp
Modified:
clang/lib/Sema/SemaInit.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 5fe417fa6351d..be46191a19b93 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2641,8 +2641,15 @@ InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
hadError = true;
} else {
// Typo correction didn't find anything.
- SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
- << FieldName << CurrentObjectType;
+ SourceLocation Loc = D->getFieldLoc();
+
+ // The loc can be invalid with a "null" designator (i.e. an anonymous
+ // union/struct). Do our best to approximate the location.
+ if (Loc.isInvalid())
+ Loc = IList->getBeginLoc();
+
+ SemaRef.Diag(Loc, diag::err_field_designator_unknown)
+ << FieldName << CurrentObjectType << DIE->getSourceRange();
++Index;
return true;
}
diff --git a/clang/test/SemaCXX/cxx2b-designated-initializers.cpp b/clang/test/SemaCXX/cxx2b-designated-initializers.cpp
new file mode 100644
index 0000000000000..3cddb5d6410ec
--- /dev/null
+++ b/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
More information about the cfe-commits
mailing list