https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126233

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org,
                   |                            |jason at gcc dot gnu.org

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
#include <source_location>

consteval bool
foo (std::source_location loc)
{
  if (loc.line () != __LINE__ + 7)
    return false;
  if (loc.function_name ()[0])
    return false;
  return true;
}

template <bool T = foo (std::source_location::current ())>
bool
bar ()
{
  return T;
}

int
main ()
{
  if (!bar ())
    __builtin_abort ();
}

The problem is that in GCC location_t contains just file/line/column
information (and optionally later on in the middle-end BLOCK).  So, what ends
up in function_name_ is
coming from a different source, current_function_decl rather than EXPR_LOCATION
of the call to __builtin_source_location ().  And constant expression
evaluation makes sure to temporarily override current_function_decl when we
evaluate the body (but not e.g. arguments including default function arguments)
of some function.
Now, in most cases that is the right thing.

Dunno what to do in this case, shall we
  /* Handle template arguments as outside of any function for
     __builtin_source_location () purposes.  */
  temp_override<tree> ovr (current_function_decl, NULL_TREE);
in convert_nontype_argument?

Reply via email to