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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
r278949 added _builtin_source_location but it doesn't do the right thing for
thing for constructors:

#include <assert.h>

namespace std {
  struct source_location {
    struct __impl {
      const char *_M_file_name;
      const char *_M_function_name;
      unsigned int _M_line, _M_column;
    };
    const __impl *__ptr;
    constexpr source_location () : __ptr (nullptr) {}
    static consteval source_location
    current (const void *__p = __builtin_source_location ()) {
      source_location __ret;
      __ret.__ptr = static_cast <const __impl *> (__p);
      return __ret;
    }
    constexpr const char *file_name () const {
      return __ptr ? __ptr->_M_file_name : "";
    }
    constexpr const char *function_name () const {
      return __ptr ? __ptr->_M_function_name : "";
    }
    constexpr unsigned line () const {
      return __ptr ? __ptr->_M_line : 0;
    }
    constexpr unsigned column () const {
      return __ptr ? __ptr->_M_column : 0;
    }
  };
}

using namespace std;

struct S {
  const char* func;
  source_location loc = source_location::current();

  S(source_location loc = source_location::current())
  : func(__FUNCTION__), loc(loc) // values of loc will be from call-site
  {}

  S(int)
  : func(__FUNCTION__) // values of loc should be hereabouts
  {}
};

int main()
{
  S s0;
  assert( s0.loc.line() == 48 );
  assert( !__builtin_strcmp(s0.loc.file_name(), __FILE__) );
  assert( !__builtin_strcmp(s0.loc.function_name(), __FUNCTION__) );

  S s1(1);
  assert( s1.loc.line() == 42 );
  assert( !__builtin_strcmp(s1.loc.file_name(), __FILE__) );
  assert( !__builtin_strcmp(s1.loc.function_name(), s1.func) );
}

Reply via email to