[PATCH] D64678: [Sema] Fix -Wuninitialized for struct assignment from GNU C statement expression

Nathan Huckleberry via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 12 16:20:16 PDT 2019


Nathan-Huckleberry updated this revision to Diff 209636.
Nathan-Huckleberry added a comment.

- Adding fix


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64678/new/

https://reviews.llvm.org/D64678

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/Sema/warn-uninitialized-statement-expression.c


Index: clang/test/Sema/warn-uninitialized-statement-expression.c
===================================================================
--- /dev/null
+++ clang/test/Sema/warn-uninitialized-statement-expression.c
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -fsyntax-only -Wuninitialized -verify %s
+
+void init(int*);
+
+void foo(void) {
+    int i = ({
+        int z = i; // expected-warning{{variable 'i' is uninitialized when used within its own initialization}}
+        init(&i);
+        i;
+    });
+}
+
+struct widget {
+    int x, y;
+};
+void init2(struct widget*);
+
+void bar(void) {
+    struct widget my_widget = ({
+        struct widget z = my_widget; // expected-warning{{variable 'my_widget' is uninitialized when used within its own initialization}}
+        int x = my_widget.x;
+        init2(&my_widget);
+        my_widget;
+    });
+}
+
+void baz(void) {
+  struct widget a = ({
+    struct widget b = ({
+      b = a;  // expected-warning{{variable 'a' is uninitialized when used within its own initialization}}
+    });
+    a;
+  });
+}
+
+void f(void) {
+  struct widget* a = ({
+    init2(a);  // expected-warning{{variable 'a' is uninitialized when used within its own initialization}}
+    a;
+  });
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -10886,6 +10886,10 @@
             if (DRE->getDecl() == OrigDecl)
               return;
 
+    if (cast<VarDecl>(OrigDecl)->getType()->isRecordType() && dyn_cast_or_null<StmtExpr>(E)) {
+      return;
+    }
+
     SelfReferenceChecker(S, OrigDecl).CheckExpr(E);
   }
 } // end anonymous namespace


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64678.209636.patch
Type: text/x-patch
Size: 1694 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190712/a5ddb928/attachment.bin>


More information about the cfe-commits mailing list