[PATCH] D115976: [clang] Fix a crash case when reporting an uninitialized field.

Michael Liao via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 17 15:06:41 PST 2021


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

- As the initializer list's getSyntacticForm() may return NULL, need to consider the current semantic initalizer list itself as the synatic form. Wrap that together into a new `getSourceRange` method.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115976

Files:
  clang/include/clang/AST/Expr.h
  clang/lib/Sema/SemaInit.cpp
  clang/test/SemaCXX/uninitialized.cpp


Index: clang/test/SemaCXX/uninitialized.cpp
===================================================================
--- clang/test/SemaCXX/uninitialized.cpp
+++ clang/test/SemaCXX/uninitialized.cpp
@@ -1472,3 +1472,20 @@
   };
 };
 Outer<int>::Inner outerinner;
+
+namespace crash_on_report_uninitialized_field {
+struct S {
+  union {
+    struct {
+      const int &f0;
+      const float &f1; // expected-note{{uninitialized reference member is here}}
+    };
+    double d;
+  };
+};
+void bar(struct S &s);
+void foo(int i, float f) {
+  S s = { .f0 = i }; // expected-error{{reference member of type 'const float &' uninitialized}}
+  bar(s);
+}
+}
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -695,8 +695,7 @@
         //   member of reference type uninitialized, the program is
         //   ill-formed.
         SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
-          << Field->getType()
-          << ILE->getSyntacticForm()->getSourceRange();
+            << Field->getType() << ILE->getSourceRange();
         SemaRef.Diag(Field->getLocation(),
                      diag::note_uninit_reference_member);
       }
Index: clang/include/clang/AST/Expr.h
===================================================================
--- clang/include/clang/AST/Expr.h
+++ clang/include/clang/AST/Expr.h
@@ -4964,6 +4964,9 @@
 
   SourceLocation getBeginLoc() const LLVM_READONLY;
   SourceLocation getEndLoc() const LLVM_READONLY;
+  SourceRange getSourceRange() const LLVM_READONLY {
+    return SourceRange(getBeginLoc(), getEndLoc());
+  }
 
   static bool classof(const Stmt *T) {
     return T->getStmtClass() == InitListExprClass;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115976.395228.patch
Type: text/x-patch
Size: 1776 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211217/f60eefcd/attachment.bin>


More information about the cfe-commits mailing list