Author: arphaman
Date: Mon Oct 24 04:33:32 2016
New Revision: 284959

URL: http://llvm.org/viewvc/llvm-project?rev=284959&view=rev
Log:
[Sema][TreeTransform] Re-create DesignatedInitExpr when a field designator
has no field declaration.

This commit fixes an invalid Winitializer-overrides warning that's shown
when analyzing a second (or any after the first) instantiation of a designated
initializer. This invalid warning is fixed by making sure that a
DesignatedInitExpr is rebuilt by the tree transformer when it has a field
designator whose FieldDecl* hasn't been yet initialized. This ensures that a
different DesignatedInitExpr is processed by Sema for every instantiation, and
thus the invalid warning is avoided.

rdar://28768441

Differential Revision: https://reviews.llvm.org/D25777

Added:
    cfe/trunk/test/SemaCXX/designated-initializers.cpp
Modified:
    cfe/trunk/lib/Sema/TreeTransform.h

Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=284959&r1=284958&r2=284959&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Mon Oct 24 04:33:32 2016
@@ -8923,6 +8923,19 @@ TreeTransform<Derived>::TransformDesigna
       Desig.AddDesignator(Designator::getField(D.getFieldName(),
                                                D.getDotLoc(),
                                                D.getFieldLoc()));
+      if (D.getField()) {
+        FieldDecl *Field = cast_or_null<FieldDecl>(
+            getDerived().TransformDecl(D.getFieldLoc(), D.getField()));
+        if (Field != D.getField())
+          // Rebuild the expression when the transformed FieldDecl is
+          // different to the already assigned FieldDecl.
+          ExprChanged = true;
+      } else {
+        // Ensure that the designator expression is rebuilt when there isn't
+        // a resolved FieldDecl in the designator as we don't want to assign
+        // a FieldDecl to a pattern designator that will be instantiated again.
+        ExprChanged = true;
+      }
       continue;
     }
 

Added: cfe/trunk/test/SemaCXX/designated-initializers.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/designated-initializers.cpp?rev=284959&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/designated-initializers.cpp (added)
+++ cfe/trunk/test/SemaCXX/designated-initializers.cpp Mon Oct 24 04:33:32 2016
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Winitializer-overrides %s
+
+template <typename T> struct Foo {
+  struct SubFoo {
+    int bar1;
+    int bar2;
+  };
+
+  static void Test() { SubFoo sf = {.bar1 = 10, .bar2 = 20}; } // Expected no 
warning
+};
+
+void foo() {
+  Foo<int>::Test();
+  Foo<bool>::Test();
+  Foo<float>::Test();
+}
+
+template <typename T> struct Bar {
+  struct SubFoo {
+    int bar1;
+    int bar2;
+  };
+
+  static void Test() { SubFoo sf = {.bar1 = 10,    // expected-note 2 
{{previous initialization is here}}
+                                    .bar1 = 20}; } // expected-warning 2 
{{initializer overrides prior initialization of this subobject}}
+};
+
+void bar() {
+  Bar<int>::Test();  // expected-note {{in instantiation of member function 
'Bar<int>::Test' requested here}}
+  Bar<bool>::Test(); // expected-note {{in instantiation of member function 
'Bar<bool>::Test' requested here}}
+}


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to