<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/78367>78367</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [DebugInfo] Declarations show up in debug-names when TUs are used
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          felipepiovezan
      </td>
    </tr>
</table>

<pre>
    We are adding _declarations_ of types to debug-names when type units are involved:
For example:

```
namespace MyNamespace {
struct MyStruct1 {
 char c1;
};
struct MyStruct2 {
  char c2;
};
}

MyNamespace::MyStruct1 gv1;
MyNamespace::MyStruct2 gv2;
```

Compiling with:
```
clang++ -target x86_64 -c two_tus.cpp -g2 -gpubnames -fdebug-types-section -o - | \
llvm-dwarfdump - --debug-names | \
grep MyStruct2 -A14
```

We see:

```
  Bucket 0 [
    Name 1 {
      Hash: 0xE186C022
 String: 0x00000037 "MyStruct2"
      Entry @ 0xc9 {
 Abbrev: 0x26c
        Tag: DW_TAG_structure_type
 DW_IDX_type_unit: 0x01
        DW_IDX_die_offset: 0x00000025
 }
      Entry @ 0xd0 {
        Abbrev: 0x268
        Tag: DW_TAG_structure_type
        DW_IDX_die_offset: 0x00000034
      }
 }
  ]
```

I can't explain why this second entry is added. Looking at the debug-info section, the offset `0x34` is an offset to the declaration inside the CU:

```
0x00000029:   DW_TAG_namespace
 DW_AT_name      ("MyNamespace")

0x0000002b: DW_TAG_structure_type
                  DW_AT_declaration     (true)
 DW_AT_signature       (0x8b108ad18e93cb95)

0x00000034: DW_TAG_structure_type
                  DW_AT_declaration (true)
                  DW_AT_signature (0x690ca39e0eeaec8f)

0x0000003d:     NULL
```

But declarations are not supposed to show up in debug-names...

_Originally posted by @felipepiovezan in https://github.com/llvm/llvm-project/issues/77457#issuecomment-1894610109_
 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.ll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