[PATCH] D53713: Add extension to always default-initialize nullptr_t.

Erich Keane via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 25 10:13:47 PDT 2018


erichkeane updated this revision to Diff 171128.
erichkeane added a comment.

Update the 2 failing tests.  Would love some help looking at the Analysis fix, it seems to no longer have to 'assume' stuff due to the this now being defined behavior.

Additionally, the other test is just an AST change, but seems to be exactly what I'm suggesting as an extension here.


https://reviews.llvm.org/D53713

Files:
  lib/Sema/SemaInit.cpp
  test/Analysis/nullptr.cpp
  test/SemaCXX/ast-print-crash.cpp
  test/SemaCXX/nullptr_t-init.cpp


Index: test/SemaCXX/nullptr_t-init.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/nullptr_t-init.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -ffreestanding -Wuninitialized %s
+// expected-no-diagnostics
+typedef decltype(nullptr) nullptr_t;
+
+// Ensure no 'uninitialized when used here' warnings (Wuninitialized), for 
+// nullptr_t always-initialized extension.
+nullptr_t default_init() {
+  nullptr_t a;
+  return a;
+}
Index: test/SemaCXX/ast-print-crash.cpp
===================================================================
--- test/SemaCXX/ast-print-crash.cpp
+++ test/SemaCXX/ast-print-crash.cpp
@@ -7,6 +7,6 @@
 
 // CHECK:      struct {
 // CHECK-NEXT: } dont_crash_on_syntax_error;
-// CHECK-NEXT: decltype(nullptr) p;
+// CHECK-NEXT: decltype(nullptr) p(/*implicit*/(decltype(nullptr))0);
 struct {
 } dont_crash_on_syntax_error /* missing ; */ decltype(nullptr) p;
Index: test/Analysis/nullptr.cpp
===================================================================
--- test/Analysis/nullptr.cpp
+++ test/Analysis/nullptr.cpp
@@ -125,21 +125,16 @@
 };
 
 void shouldNotCrash() {
-  decltype(nullptr) p; // expected-note{{'p' declared without an initial value}}
-  if (getSymbol()) // expected-note   {{Assuming the condition is false}}
-                   // expected-note at -1{{Taking false branch}}
-                   // expected-note at -2{{Assuming the condition is false}}
-                   // expected-note at -3{{Taking false branch}}
-                   // expected-note at -4{{Assuming the condition is true}}
-                   // expected-note at -5{{Taking true branch}}
-    invokeF(p); // expected-warning{{1st function call argument is an uninitialized value}}
-                // expected-note at -1{{1st function call argument is an uninitialized value}}
+  decltype(nullptr) p; // expected-note{{'p' initialized to a null pointer value}}
   if (getSymbol()) // expected-note   {{Assuming the condition is false}}
                    // expected-note at -1{{Taking false branch}}
                    // expected-note at -2{{Assuming the condition is true}}
                    // expected-note at -3{{Taking true branch}}
-    invokeF(nullptr); // expected-note   {{Calling 'invokeF'}}
-                      // expected-note at -1{{Passing null pointer value via 1st parameter 'x'}}
+    invokeF(p); // expected-note{{Passing null pointer value via 1st parameter 'x'}}
+                // expected-note at -1{{Calling 'invokeF'}}
+  if (getSymbol()) // expected-note   {{Assuming the condition is false}}
+                   // expected-note at -1{{Taking false branch}}
+    invokeF(nullptr);
   if (getSymbol()) {  // expected-note  {{Assuming the condition is true}}
                       // expected-note at -1{{Taking true branch}}
     X *xx = Type().x; // expected-note   {{Null pointer value stored to field 'x'}}
Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp
+++ lib/Sema/SemaInit.cpp
@@ -4877,6 +4877,13 @@
     return;
   }
 
+  // As an extension, and to fix Core issue 1013, zero initialize nullptr_t.
+  // Since there is only 1 valid value of nullptr_t, we can just use that.
+  if (DestType->isNullPtrType()) {
+    Sequence.AddZeroInitializationStep(Entity.getType());
+    return;
+  }
+
   //     - otherwise, no initialization is performed.
 
   //   If a program calls for the default initialization of an object of


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53713.171128.patch
Type: text/x-patch
Size: 3500 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181025/e7da2b5b/attachment.bin>


More information about the cfe-commits mailing list