[PATCH] D42776: [Sema] Fix an assertion failure in constant expression evaluation of calls to functions with default arguments

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 31 18:24:29 PST 2018


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;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42776.132300.patch
Type: text/x-patch
Size: 1307 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180201/f2f49ed7/attachment-0001.bin>


More information about the cfe-commits mailing list