Brady Hahn d340a68ee0
Produce back-references for anonymous namespaces (#188843)
The Microsoft mangle implementation does not produce back-references for
anonymous namespaces, which results in nonsensical output from both
`undname` and `llvm-undname`. Consider the following example:

```
namespace {
    struct X {};
    X foo(X, X);
}

int main() {
    foo({}, {});
}
```

Clang 22.1.0
```
?foo@?A0xC9C482F4@@YA?AUX@?A0xC9C482F4@@U1?A0xC9C482F4@@0@Z

undname:
struct `anonymous namespace'::X __cdecl `anonymous namespace'::foo(struct `anonymous namespace'::A0xC9C482F4,struct `anonymous namespace'::A0xC9C482F4)

llvm-undname:
struct `anonymous namespace'::X __cdecl `anonymous namespace'::foo(struct `anonymous namespace'::0xC9C482F4, struct `anonymous namespace'::0xC9C482F4)
```

MSVC 19.50
```
?foo@?A0xa6a4f20e@@YA?AUX@1@U21@0@Z

undname:
struct A0xa6a4f20e::X __cdecl `anonymous namespace'::foo(struct A0xa6a4f20e::X,struct A0xa6a4f20e::X)

llvm-undname:
struct 0xa6a4f20e::X __cdecl `anonymous namespace'::foo(struct 0xa6a4f20e::X, struct 0xa6a4f20e::X)
```

As seen in the undname output for Clang's mangling, not recording a name
back-reference for the anonymous namespace when mangling results in
references to the anonymous namespace's hash in-place of the struct's
name.

When recompiling the example with the changes, Clang yields
`?foo@?A0x69FA3AC2@@YA?AUX@1@U21@0@Z`. Aside from the meaningless
discrepancy with the hash, this is identical to what is produced by
MSVC.

Resolves #37999
2026-03-27 09:58:59 -07:00

30 lines
1011 B
C++

// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=cfi-icall -fsanitize-trap=cfi-icall -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=ITANIUM %s
// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fsanitize=cfi-icall -fsanitize-trap=cfi-icall -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=MS %s
// Tests that we assign unnamed metadata nodes to functions whose types have
// internal linkage.
namespace {
struct S {};
void f(S s) {
}
}
void g() {
struct S s;
void (*fp)(S) = f;
// CHECK: call i1 @llvm.type.test(ptr {{.*}}, metadata [[VOIDS1:![0-9]+]])
fp(s);
}
// ITANIUM: define internal void @_ZN12_GLOBAL__N_11fENS_1SE({{.*}} !type [[TS1:![0-9]+]] !type [[TS2:![0-9]+]]
// MS: define internal void @"?f@?A0x{{[^@]*}}@@YAXUS@1@@Z"({{.*}} !type [[TS1:![0-9]+]] !type [[TS2:![0-9]+]]
// CHECK: [[VOIDS1]] = distinct !{}
// CHECK: [[TS1]] = !{i64 0, [[VOIDS1]]}
// CHECK: [[TS2]] = !{i64 0, [[VOIDS2:![0-9]+]]}
// CHECK: [[VOIDS2]] = distinct !{}