]> granicus.if.org Git - clang/commitdiff
[Sema] Fix using old initializer during switch statement transformation.
authorVolodymyr Sapsai <vsapsai@apple.com>
Thu, 21 Sep 2017 17:58:27 +0000 (17:58 +0000)
committerVolodymyr Sapsai <vsapsai@apple.com>
Thu, 21 Sep 2017 17:58:27 +0000 (17:58 +0000)
It fixes a crash in CodeGen when we are trying to generate code for
initializer expression created before template instantiation, like

    CallExpr '<dependent type>'
    |-UnresolvedLookupExpr '<overloaded function type>' lvalue (ADL) = 'parse'
    `-DeclRefExpr 'Buffer<N>' lvalue ParmVar 'buffer' 'Buffer<N>'

rdar://problem/33888545

Reviewers: rsmith, ahatanak

Reviewed By: ahatanak

Subscribers: aemerson, kristof.beyls, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313896 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/TreeTransform.h
test/SemaCXX/cxx1z-init-statement-template.cpp [new file with mode: 0644]

index ee67e15b9ae428d1e3de03c8224efb0bfb8d00e0..6fb9eca9a9ce6832c52fb438a4a139a992f4790b 100644 (file)
@@ -6601,8 +6601,7 @@ TreeTransform<Derived>::TransformSwitchStmt(SwitchStmt *S) {
 
   // Rebuild the switch statement.
   StmtResult Switch
-    = getDerived().RebuildSwitchStmtStart(S->getSwitchLoc(),
-                                          S->getInit(), Cond);
+    = getDerived().RebuildSwitchStmtStart(S->getSwitchLoc(), Init.get(), Cond);
   if (Switch.isInvalid())
     return StmtError();
 
diff --git a/test/SemaCXX/cxx1z-init-statement-template.cpp b/test/SemaCXX/cxx1z-init-statement-template.cpp
new file mode 100644 (file)
index 0000000..cedd2c7
--- /dev/null
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -std=c++1z -verify -emit-llvm-only %s
+// expected-no-diagnostics
+
+// rdar://problem/33888545
+template <unsigned int BUFFER_SIZE> class Buffer {};
+
+class A {
+public:
+  int status;
+};
+
+template <unsigned int N> A parse(Buffer<N> buffer);
+
+template<unsigned int N>
+void init_in_if(Buffer<N> buffer) {
+  if (A a = parse(buffer); a.status > 0) {
+  }
+}
+
+template<unsigned int N>
+void init_in_switch(Buffer<N> buffer) {
+  switch (A a = parse(buffer); a.status) {
+    default:
+      break;
+  }
+}
+
+void test() {
+  Buffer<10> buffer;
+  init_in_if(buffer);
+  init_in_switch(buffer);
+}