[PATCH] D36353: Instantiate constexpr function when it is used

Serge Pavlov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 6 10:23:53 PDT 2017


sepavloff updated this revision to Diff 109928.
sepavloff added a comment.

Updated patch

I missed CWG issue 1581. It make patch description invalid, but the patch
itself can be repaired with small modification.

This fix originated from the attempt to solve the problem described in
http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20170731/199590.html.
Compiler crashed on the code:

  template <class T>
  constexpr void f(T) {
    f(0);
  }

When parsing template body clang encounters f(0) and tries to immediately
instantiate f<int>. It is not possible, because body of the function template
f<T> is not available yet. Before r305903 (Function with unparsed body is a
definition) the function template f<T> was considered as undefined and
instantiation request was silently ignored, so no error observed. With r305903
f<T> is reported as defined but instantiation pattern is not available for it,
and the compilation crashes.

Instantiation request cannot be fulfilled during parsing of f<T>, so
the instantiation of f<int> must be postponed. CWG issue 1581 deals with
function calls which can be skipped for some reason. Return value of the call
in this case is ignored, so instantiation of such constexpr function makes
sense only if the body can trigger an error at instantiation, by static_assert,
throw or thing like T:error. If the above is true, constexpr functions does not
need to be instantiated in the point of use, it is sufficient to instantiate it
somewhere later, so that the diagnostics be be present.

According to investigation in https://bugs.llvm.org/show_bug.cgi?id=33561 too
early instantiation is a reason of hang if -fdelayed-template-parsing is used.
At least the reduced test case provided in that bug compiles successfully if
this patch is applied.

The fix was changed a bit so that constexpr functions are added to
PendingInstantiations, the call to isConstexpr is restored in SemaExpr.cpp.
The test from CWG issue 1581 was also added.


https://reviews.llvm.org/D36353

Files:
  include/clang/AST/ASTContext.h
  lib/AST/ASTContext.cpp
  lib/AST/ExprConstant.cpp
  lib/Sema/Sema.cpp
  lib/Sema/SemaExpr.cpp
  test/SemaTemplate/constexpr-instantiate.cpp
  test/SemaTemplate/instantiate-constexpr-function.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36353.109928.patch
Type: text/x-patch
Size: 8812 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170806/ddfa2ad8/attachment-0001.bin>


More information about the cfe-commits mailing list