[PATCH] D128119: [clang] enforce instantiation of constexpr template functions

Shafik Yaghmour via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 2 13:59:14 PDT 2022


shafik added a comment.

During the parsing of the assignment in `main` for the `constexpr` case we end up in `Sema::MarkFunctionReferenced` in particular this code:

  else if (Func->isConstexpr())
      // Do not defer instantiations of constexpr functions, to avoid the
      // expression evaluator needing to call back into Sema if it sees a
      // call to such a function.
      InstantiateFunctionDefinition(PointOfInstantiation, Func);

And so I experimented with modifying `Sema::InstantiateFunctionDefinition` in particular:

  else if (TSK == TSK_ExplicitInstantiationDefinition) {

And modified in this way:

  else if (TSK == TSK_ExplicitInstantiationDefinition || (Function->isConstexpr() && !Recursive)) {

This seems more targeted, reflects what I think is the intent and passes `check-clang` as well.

I also looked at the non-constexpr case:

  template <typename T>
  T foo(T a);
  
  int main() {
    int k = foo<int>(5);
  }
  
  template <typename T>
  T foo(T a) {
    return a;
  }

And in this case we hit `Sema::InstantiateFunctionDefinition` from `Sema::PerformPendingInstantiations` which is called by `Sema::ActOnEndOfTranslationUnitFragment` and so by this time we have seen the definition already.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128119/new/

https://reviews.llvm.org/D128119



More information about the cfe-commits mailing list