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] Emit error for duplicate mangled names within a lambda #107581

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3911,9 +3911,20 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
assert(!MayBeEmittedEagerly(Global));
addDeferredDeclToEmit(GD);
} else {
// Otherwise, remember that we saw a deferred decl with this name. The
// first use of the mangled name will cause it to move into
// DeferredDeclsToEmit.
// Otherwise, remember that we saw a deferred decl with this name.
auto DDI = DeferredDecls.find(MangledName);
const auto *FD = dyn_cast<FunctionDecl>(Global);
if (FD && DDI != DeferredDecls.end()) {
bool IsDefinitionAvailableExternally =
getContext().GetGVALinkageForFunction(FD) == GVA_AvailableExternally;
if (!IsDefinitionAvailableExternally && !FD->isMultiVersion() &&
!FD->hasAttr<OMPDeclareTargetDeclAttr>())
getDiags().Report(Global->getLocation(),
diag::err_duplicate_mangled_name)
<< MangledName;
}
// The first use of the mangled name will cause it to move
// into DeferredDeclsToEmit.
DeferredDecls[MangledName] = GD;
}
}
Expand Down
8 changes: 8 additions & 0 deletions clang/test/CodeGenCXX/duplicate-mangled-name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// RUN: %clang_cc1 -triple %itanium_abi_triple-only %s -verify -DTEST2 -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm-only %s -verify -DTEST3
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm-only %s -verify -DTEST4
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm-only %s -verify -DTEST5

#ifdef TEST1

Expand Down Expand Up @@ -70,6 +71,13 @@ float foo() {
return _ZN2nm3abcE + nm::abc;
}

#elif TEST5

inline void f(int) asm("foo");
inline void f(int) {}
inline void f() asm("foo");
inline void f(){} // expected-error {{definition with same mangled name 'foo' as another definition}}

#else

#error Unknown test
Expand Down
30 changes: 30 additions & 0 deletions clang/test/CodeGenCXX/lambda-instantiation-mangling-conflicts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// REQUIRES: aarch64-registered-target

// RUN: rm -rf %t
// RUN: split-file %s %t

// RUN: %clang_cc1 -triple aarch64 -target-feature +sme -target-feature +sme2 -emit-llvm -o - %t/a.cpp -verify
// RUN: %clang_cc1 -triple aarch64 -target-feature +sme -target-feature +sme2 -emit-llvm -o - %t/b.cpp -verify

;--- a.cpp

int normal_fn(int);
int vector_pcs_fn(int) __attribute__((aarch64_vector_pcs));

// expected-error@+3 {{definition with same mangled name '_ZZ23function_pcs_attributesvENK3$_0clIFiiEEEDaRT_' as another definition}}
__attribute__((aarch64_vector_pcs))
int function_pcs_attributes() {
auto a = [](auto &fn) { return fn(42); };
return a(normal_fn) + a(vector_pcs_fn);
}

;--- b.cpp

int normal_fn(int);
int streaming_fn(int) __arm_streaming;

// expected-error@+2 {{definition with same mangled name '_ZZ32function_params_normal_streamingvENK3$_0clIFiiEEEDaRT_' as another definition}}
int function_params_normal_streaming() {
auto a = [](auto &fn) { return fn(42); };
return a(normal_fn) + a(streaming_fn);
}
9 changes: 2 additions & 7 deletions clang/test/CodeGenCXX/mangle-lambdas-gh88906.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -triple x86_64-linux-gnu %s -emit-llvm -mconstructor-aliases -o - | FileCheck %s
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fclang-abi-compat=18 %s -emit-llvm -mconstructor-aliases -o - | FileCheck --check-prefix=CLANG18 %s
// RUN: not %clang_cc1 -triple x86_64-linux-gnu -fclang-abi-compat=18 %s -emit-llvm -mconstructor-aliases 2>&1 | FileCheck --check-prefix=CLANG18 %s
// RUN: %clang_cc1 -triple i386-pc-win32 %s -emit-llvm -mconstructor-aliases -o - | FileCheck --check-prefix=MSABI %s


Expand Down Expand Up @@ -29,12 +29,7 @@ void GH88906(){
// CHECK-LABEL: define internal void @_ZN4funcC2IN7GH889064Test1bMUlvE_EEET_
// CHECK-LABEL: define internal void @_ZN4funcC2IN7GH889064Test1cMUlvE_EEET_

// CLANG18-LABEL: define internal void @_ZZ7GH88906vEN4TestC2Ev
// CLANG18: call void @_ZN4funcC2IZ7GH88906vEN4TestUlvE_EZ7GH88906vENS1_UlvE0_EEET_T0_
// CLANG18: call void @_ZN4funcC2IZ7GH88906vEN4TestUlvE_EEET_
// CLANG18: call void @_ZN4funcC2IZ7GH88906vEN4TestUlvE_EEET_


// CLANG18: error: definition with same mangled name '_ZN4funcC1IZ7GH88906vEN4TestUlvE_EEET_' as another definition

// MSABI-LABEL: define internal x86_thiscallcc noundef ptr @"??0Test@?1??GH88906@@YAXXZ@QAE@XZ"
// MSABI: call x86_thiscallcc noundef ptr @"??$?0V<lambda_1>@a@Test@?1??GH88906@@YAXXZ@V<lambda_2>@12?1??3@YAXXZ@@func@@QAE@V<lambda_1>@a@Test@?1??GH88906@@YAXXZ@V<lambda_2>@23?1??4@YAXXZ@@Z"
Expand Down
Loading