Skip to content

Custom implementation gives incorrect location when used in a default argument (at least in Mac+clang) #7

@jmmut

Description

@jmmut

Hi, thanks for the code, it's great to have this working cross-compiler cross-platform.

However, I noticed that the custom implementation you added doesn't give the expected call site location when used like this:
(example code simplified from https://en.cppreference.com/w/cpp/utility/source_location/current)

#include <iostream>
#include "source_location/source_location.hpp"

source_location src_clone(source_location a = source_location::current())
{
    return a;
}

int main()
{
    auto s2 = src_clone();          // location should point here 
    std::cout
        << s2.line() << ' ' << s2.function_name() << '\n';
}

I tried like this in Mac with Clang12:

$ clang++ -std=c++11 test.cpp 
test.cpp:5:64: warning: predefined identifier is only valid inside function [-Wpredefined-identifier-outside-function]
source_location src_clone(source_location a = source_location::current())
                                                               ^
./source_location/source_location.hpp:64:60: note: expanded from macro 'current'
#define current(args...) current( __LINE__ , 0, __FILE__ , __PRETTY_FUNCTION__ )
                                                           ^
1 warning generated.


$ ./a.out 
4 top level

Instead of "4 top level" I would expect "11 main".

I understand it might not be possible to give the call site location without compiler support (I can't think of a way to make it work) but I was wondering if you are aware of this and if your tool is working as you expected, as there is no mention to this in the readme and I imagine this is the main use case for source_location to avoid littering call site user code.

Your tool is still useful despite this. Thanks again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions