ahatanak created this revision.
ahatanak added a reviewer: rsmith.

The assertion fails when a function with a default argument that materializes a 
temporary is called more than once in an expression. The assertion fails in 
CallStackFrame::createTemporary when it searches map Temporaries using the 
default argument's expression (which is a MaterializeTemporaryExpr) as the key 
and it discovers that there is already an element with that key that has been 
initialized.

  constexpr bool equals(const float& arg = 1.0f) {
    return arg == 1.0f;
  }
  
  constexpr bool test_default_arg() {
    return equals() && equals();
  }

This patch removes the assertion and makes CallStackFrame::createTemporary 
reset the existing element and return it if the element is found in the map.

rdar://problem/36505742


https://reviews.llvm.org/D42776

Files:
  lib/AST/ExprConstant.cpp
  test/SemaCXX/constexpr-default-arg.cpp


Index: test/SemaCXX/constexpr-default-arg.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/constexpr-default-arg.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++1y -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+constexpr bool equals(const float& arg = 1.0f) {
+  return arg == 1.0f;
+}
+
+constexpr bool test_default_arg() {
+  return equals() && equals();
+}
Index: lib/AST/ExprConstant.cpp
===================================================================
--- lib/AST/ExprConstant.cpp
+++ lib/AST/ExprConstant.cpp
@@ -1162,8 +1162,15 @@
 
 APValue &CallStackFrame::createTemporary(const void *Key,
                                          bool IsLifetimeExtended) {
-  APValue &Result = Temporaries[Key];
-  assert(Result.isUninit() && "temporary created multiple times");
+  auto LB = Temporaries.lower_bound(Key);
+
+  // If an element with key Key is found, reset the value and return it. This
+  // can happen if Key is part of a default argument expression.
+  if (LB != Temporaries.end() && LB->first == Key)
+    return LB->second = APValue();
+
+  APValue &Result =
+      Temporaries.insert(LB, std::make_pair(Key, APValue()))->second;
   Info.CleanupStack.push_back(Cleanup(&Result, IsLifetimeExtended));
   return Result;
 }


Index: test/SemaCXX/constexpr-default-arg.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/constexpr-default-arg.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++1y -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+constexpr bool equals(const float& arg = 1.0f) {
+  return arg == 1.0f;
+}
+
+constexpr bool test_default_arg() {
+  return equals() && equals();
+}
Index: lib/AST/ExprConstant.cpp
===================================================================
--- lib/AST/ExprConstant.cpp
+++ lib/AST/ExprConstant.cpp
@@ -1162,8 +1162,15 @@
 
 APValue &CallStackFrame::createTemporary(const void *Key,
                                          bool IsLifetimeExtended) {
-  APValue &Result = Temporaries[Key];
-  assert(Result.isUninit() && "temporary created multiple times");
+  auto LB = Temporaries.lower_bound(Key);
+
+  // If an element with key Key is found, reset the value and return it. This
+  // can happen if Key is part of a default argument expression.
+  if (LB != Temporaries.end() && LB->first == Key)
+    return LB->second = APValue();
+
+  APValue &Result =
+      Temporaries.insert(LB, std::make_pair(Key, APValue()))->second;
   Info.CleanupStack.push_back(Cleanup(&Result, IsLifetimeExtended));
   return Result;
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to