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

Clang failed to use std::source_location::current() in default argument #55769

Closed
ChuanqiXu9 opened this issue May 30, 2022 · 7 comments
Closed
Labels
c++20 duplicate Resolved as duplicate libstdc++ GNU libstdc++ C++ standard library

Comments

@ChuanqiXu9
Copy link
Member

ChuanqiXu9 commented May 30, 2022

See https://godbolt.org/z/r68nn8snr

Clang failed to compile the following example while gcc and MSVC accept it both:

#include<source_location>
#include<iostream>

void test_source_location(std::source_location src = std::source_location::current()) {
    std::cout << "called at " << src.line() << "\n";
}

int main() {
    test_source_location();
}

The use here of std::source_location::current() is recommended. So it should be the problem of clang.

@llvmbot
Copy link
Collaborator

llvmbot commented May 30, 2022

@llvm/issue-subscribers-c-20

@ChuanqiXu9
Copy link
Member Author

BTW, clang would accept it too if we use std::experimental::source_location::current().

@EugeneZelenko EugeneZelenko added the libstdc++ GNU libstdc++ C++ standard library label May 30, 2022
@ChuanqiXu9
Copy link
Member Author

ChuanqiXu9 commented May 30, 2022

It looks like we could workaround the problem by inserting the following code:

if (auto *CE = dyn_cast<CallExpr>(Param->getDefaultArg())) {
    ExprResult CI = CheckForImmediateInvocation(MaybeBindToTemporary(CE), CE->getDirectCallee());
    Param->setDefaultArg(CI.get());
  }

to

EnterExpressionEvaluationContext EvalContext(
*this, ExpressionEvaluationContext::PotentiallyEvaluated, Param);
MarkDeclarationsReferencedInExpr(Param->getDefaultArg(),
/*SkipLocalVariables=*/true);

but then I meet the crash:

clang-14: /home/chuanqi.xcq/workspace.xuchuanqi/llvm-project-for-work/clang/lib/CodeGen/CodeGenModule.cpp:3943: llvm::Constant* clang::CodeGen::CodeGenModule::GetAddrOfFunction(clang::GlobalDecl, llvm::Type*, bool, bool, clang::CodeGen::ForDefinition_t): Assertion `!cast<FunctionDecl>(GD.getDecl())->isConsteval() && "consteval function should never be emitted"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -main-file-name CoroSourceLocation.cpp -mrelocation-model static -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -fcoverage-compilation-dir=/home/chuanqi.xcq/workspace.xuchuanqi/llvm-project-for-work/build -resource-dir /disk2/workspace.xuchuanqi/llvm-project-for-work/build/lib/clang/15.0.0 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/9/../../../../include/c++/9 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/9/../../../../include/c++/9/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/9/../../../../include/c++/9/backward -internal-isystem /disk2/workspace.xuchuanqi/llvm-project-for-work/build/lib/clang/15.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/9/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++20 -fdeprecated-macro -fdebug-compilation-dir=/home/chuanqi.xcq/workspace.xuchuanqi/llvm-project-for-work/build -ferror-limit 19 -fgnuc-version=4.2.1 -fno-implicit-modules -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/CoroSourceLocation-85a2fd.o -x c++ CoroSourceLocation.cpp
1.	<eof> parser at end of file
2.	CoroSourceLocation.cpp:96:5: LLVM IR generation of declaration 'main'
3.	CoroSourceLocation.cpp:96:5: Generating code for declaration 'main'
 #0 0x0000000001f6e540 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x0000000001f6be4c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007ffff7fb29d0 __restore_rt sigaction.c:0:0
 #3 0x00007ffff7675f35 raise (/lib64/libc.so.6+0x3bf35)
 #4 0x00007ffff765f8d7 abort (/lib64/libc.so.6+0x258d7)
 #5 0x00007ffff765f7a7 _nl_load_domain.cold loadmsgcat.c:0:0
 #6 0x00007ffff766e536 (/lib64/libc.so.6+0x34536)
 #7 0x0000000002459d2d clang::CodeGen::CodeGenModule::GetAddrOfFunction(clang::GlobalDecl, llvm::Type*, bool, bool, clang::CodeGen::ForDefinition_t) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x2459d2d)
 #8 0x0000000002699c72 EmitFunctionDeclPointer(clang::CodeGen::CodeGenModule&, clang::GlobalDecl) CGExpr.cpp:0:0
 #9 0x00000000026a669a EmitDirectCallee(clang::CodeGen::CodeGenFunction&, clang::GlobalDecl) CGExpr.cpp:0:0
#10 0x00000000026c1041 clang::CodeGen::CodeGenFunction::EmitCallee(clang::Expr const*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x26c1041)
#11 0x00000000026c0fdf clang::CodeGen::CodeGenFunction::EmitCallee(clang::Expr const*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x26c0fdf)
#12 0x00000000026c156a clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x26c156a)
#13 0x00000000026c188c clang::CodeGen::RValue llvm::function_ref<clang::CodeGen::RValue (clang::CodeGen::ReturnValueSlot)>::callback_fn<(anonymous namespace)::AggExprEmitter::VisitCallExpr(clang::CallExpr const*)::'lambda'(clang::CodeGen::ReturnValueSlot)>(long, clang::CodeGen::ReturnValueSlot) CGExprAgg.cpp:0:0
#14 0x00000000026c60aa (anonymous namespace)::AggExprEmitter::withReturnValueSlot(clang::Expr const*, llvm::function_ref<clang::CodeGen::RValue (clang::CodeGen::ReturnValueSlot)>) CGExprAgg.cpp:0:0
#15 0x00000000026c6735 (anonymous namespace)::AggExprEmitter::VisitCallExpr(clang::CallExpr const*) CGExprAgg.cpp:0:0
#16 0x00000000026c72fa (anonymous namespace)::AggExprEmitter::Visit(clang::Expr*) CGExprAgg.cpp:0:0
#17 0x00000000026c772e (anonymous namespace)::AggExprEmitter::Visit(clang::Expr*) CGExprAgg.cpp:0:0
#18 0x00000000026c9413 clang::CodeGen::CodeGenFunction::EmitAggExpr(clang::Expr const*, clang::CodeGen::AggValueSlot) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x26c9413)
#19 0x000000000269f9dc clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*, clang::CodeGen::AggValueSlot, bool) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x269f9dc)
#20 0x000000000269ff3b clang::CodeGen::CodeGenFunction::EmitAnyExprToTemp(clang::Expr const*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x269ff3b)
#21 0x0000000002635bf2 clang::CodeGen::CodeGenFunction::EmitCallArg(clang::CodeGen::CallArgList&, clang::Expr const*, clang::QualType) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x2635bf2)
#22 0x0000000002637607 clang::CodeGen::CodeGenFunction::EmitCallArgs(clang::CodeGen::CallArgList&, clang::CodeGen::CodeGenFunction::PrototypeWrapper, llvm::iterator_range<clang::Stmt::CastIterator<clang::Expr, clang::Expr const* const, clang::Stmt const* const>>, clang::CodeGen::CodeGenFunction::AbstractCallee, unsigned int, clang::CodeGen::CodeGenFunction::EvaluationOrder) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x2637607)
#23 0x00000000026b1752 clang::CodeGen::CodeGenFunction::EmitCall(clang::QualType, clang::CodeGen::CGCallee const&, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::Value*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x26b1752)
#24 0x00000000026c15b6 clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x26c15b6)
#25 0x00000000026fa638 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) CGExprScalar.cpp:0:0
#26 0x00000000026f79d8 (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) CGExprScalar.cpp:0:0
#27 0x00000000026f9d06 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x26f9d06)
#28 0x000000000269f97e clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*, clang::CodeGen::AggValueSlot, bool) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x269f97e)
#29 0x00000000026bf539 clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x26bf539)
#30 0x000000000239835b clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x239835b)
#31 0x000000000239ed14 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x239ed14)
#32 0x00000000023f5f1d clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x23f5f1d)
#33 0x00000000024071bf clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x24071bf)
#34 0x0000000002459ec2 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x2459ec2)
#35 0x0000000002455805 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x2455805)
#36 0x0000000002455dbb clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x2455dbb)
#37 0x000000000245e2b0 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#38 0x0000000003110009 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#39 0x00000000031024b8 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x31024b8)
#40 0x0000000003ecf8e4 clang::ParseAST(clang::Sema&, bool, bool) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x3ecf8e4)
#41 0x000000000310d178 clang::CodeGenAction::ExecuteAction() (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x310d178)
#42 0x0000000002a40cc9 clang::FrontendAction::Execute() (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x2a40cc9)
#43 0x00000000029cc846 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x29cc846)
#44 0x0000000002b13aa3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x2b13aa3)
#45 0x00000000009fed9a cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x9fed9a)
#46 0x00000000009fb448 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#47 0x0000000000986219 main (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x986219)
#48 0x00007ffff7661193 __libc_start_main (/lib64/libc.so.6+0x27193)
#49 0x00000000009fafee _start (/disk2/workspace.xuchuanqi/llvm-project-for-work/build/bin/clang-14+0x9fafee)

I am not sure if the workaround isn't complete or clang doesn't support constexpr for structs.

@jyknight
Copy link
Member

This is a duplicate of #48230

@jyknight jyknight closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2022
@EugeneZelenko EugeneZelenko added the duplicate Resolved as duplicate label May 30, 2022
@IGR2014
Copy link

IGR2014 commented Jul 5, 2022

Not yet fixed: Clang reports wrong line number.
While GCC/MSVC reports lline number of function INVOCATION, Clang reports line number of function DECLARATION (see screenshot)
image

@ChuanqiXu9
Copy link
Member Author

Not yet fixed: Clang reports wrong line number.
While GCC/MSVC reports lline number of function INVOCATION, Clang reports line number of function DECLARATION (see screenshot)
image

Hi, this issue (and the other issue you mentioned) refers to the problem we couldn't use constexpr functions in the default argument. And your suggestion should be another related but not the same issue. I suggest to create a new issue for this.

(BTW, it would be better to sent the link to Godbolt instead of posting up a picture. My mail client warns for this.)

@IGR2014
Copy link

IGR2014 commented Jul 5, 2022

Yes, sorry. Here's one: #56379

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++20 duplicate Resolved as duplicate libstdc++ GNU libstdc++ C++ standard library
Projects
None yet
Development

No branches or pull requests

5 participants