Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apple Clang: std::source_location returns a wrong results if called in a function parameter. #68108

Closed
H-G-Hristov opened this issue Oct 3, 2023 · 4 comments
Labels
clang Clang issues not falling into any other category

Comments

@H-G-Hristov
Copy link
Contributor

H-G-Hristov commented Oct 3, 2023

This is more of a question. The examples in this article as seen on Compiler Explorer (trunk) work as expected but in Xcode 15 they return the location of the std::source_location::current() call. Could that be a bug in Apple Clang 15.0?

https://www.cppstories.com/2021/non-terminal-variadic-args/

template <typename... Ts>
struct Logger
{
    Logger(Ts&&... ts, const std::source_location& loc = std::source_location::current())
    {
        std::cout << loc.function_name() << " line " << loc.line() << ": ";
        ((std::cout << std::forward<Ts>(ts) << " "), ...);
        std::cout << '\n';
    }
};

template <typename... Ts>
Logger(Ts&&...) -> Logger<Ts...>;

Apple Clang 15 output (wrong):

> % ./nagent
Logger<const char (&)[26], int, int, int, int>::Logger(Ts &&..., const std::source_location &) [Ts = <const char (&)[26], int, int, int, int>] line 281: Alabalanica turska panica 1 2 3 5 

GCC 13.2 output (correct):

>  # ./nagent
int main(int, char**) line 295: Alabalanica turska panica 1 2 3 5 

Apple Clang details:

> % clang --version
Apple clang version 15.0.0 (clang-1500.0.40.1)
Target: arm64-apple-darwin23.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Oct 3, 2023
@DimitryAndric
Copy link
Collaborator

If it is only reproducible with Apple clang, please file a bug report with Apple. Do you have a complete example that you can post in this bug, instead of referring to godbolt or some other site?

@H-G-Hristov
Copy link
Contributor Author

If it is only reproducible with Apple clang, please file a bug report with Apple. Do you have a complete example that you can post in this bug, instead of referring to godbolt or some other site?

This should do:

#include <iostream>
#include <string_view>
#include <source_location>
#include <fmt/core.h>

struct Logger {
    Logger(std::source_location l = std::source_location::current()) : loc(std::move(l)) { }
    
    template <typename ...Args>
    void debug(std::string_view format, Args&& ...args) {
	    std::cout << fmt::format("{}({}) ", loc.file_name(), loc.line())
                  << fmt::format(format, std::forward<Args>(args)...) << '\n';
    }
    
private:
    std::source_location loc;    
};
 
int main() {
    std::cout << sizeof(std::source_location) << '\n';
    Logger().debug("{}, {}", "hello", "world");
    Logger().debug("{}, {}", 10, 42);
}

I hope this is Apple's Clang repository: apple#7562

@tbaederr
Copy link
Contributor

tbaederr commented Oct 3, 2023

IIRC @cor3ntin fixed that for clang 17(?)

@cor3ntin
Copy link
Contributor

cor3ntin commented Oct 3, 2023

This has been working since clang 16
https://compiler-explorer.com/z/o7T8jbh3a

Sadly it's difficult to map a version of clang to a version of AppleClang, and I don't know how fast Apple has been at adopting Clang 16. Because this is a question for the Apple-shipped version of clang, I think we can close this issue.

(apple#7562 does seem to be the appropriate place for this!)

@cor3ntin cor3ntin closed this as completed Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

No branches or pull requests

4 participants