Issue 164730
Summary [clang] Preferred name not generated to declare variables in DWARF when used as template argument in code *only*
Labels clang
Assignees
Reporter royitaqi
    # Problem

Compiling the following code generates an object file whose DWARF doesn't contain the preferred name. This is true when compiling with `-ggdb` and with `-glldb`.

Inspecting the DWARF shows that the variable is declared as `Foo<Foo<int> >`. The preferred name, i.e. `BarInt` (case sensitive), is no where to be found in the DWARF.

Code:
```
template <typename T> struct Foo;

typedef Foo<int> BarInt;

template <typename T>
struct [[clang::preferred_name(BarInt)]] Foo{};

int main() {
  // BarInt barInt;
  Foo<BarInt> fooBarInt;
  return 0;
}
```

DWARF:
```
0x00000033:     DW_TAG_variable
 DW_AT_location	(DW_OP_fbreg +11)
 DW_AT_name	("fooBarInt")
 DW_AT_decl_file	("/Users/royshi/demo/preferred_name/two.cpp")
 DW_AT_decl_line	(10)
                  DW_AT_type	(0x00000043 "Foo<Foo<int> >")
```

So lldb cannot print the preferred name:
```
(lldb) p fooBarInt
(Foo<Foo<int> >)  {}
```


# Repro

Use the above source code (call it `two.cpp`), then:
```
~/public_llvm/build/bin/clang -O0 -glldb -c two.cpp   # or -ggdb

dwarfdump two.o

# Note: For me, the above clang cannot link, so I used xcrun clang++. It shouldn't matter which one you use.
xcrun clang++ two.o

lldb a.out -o "b two.cpp:10" -o "r" -o "p fooBarInt"
```

See compilation, dwarfdump, and lldb output:
- `-ggdb`: https://gist.github.com/royitaqi/cbf7a4d038f5b73dc60e5932bb2b593d
- `-glldb`: https://gist.github.com/royitaqi/46af47f0472976eb94b9725c073668df

I tested these on macOS. Haven't tested on Linux.


# Analysis

If we uncomment the line `// BarInt barInt;`, the compiler can now generate typedef(s) for the preferred name.

For example, with `-ggdb`, we now see this typedef.
```
0x0000004d:   DW_TAG_typedef
 DW_AT_type	(0x00000055 "Foo<int>")
 DW_AT_name	("BarInt")
 DW_AT_decl_file	("/Users/royshi/demo/preferred_name/two.cpp")
 DW_AT_decl_line	(3)
```
_Note: FWIW, `-glldb` generates one more typedef, I was told that this is by design._

However, this typedef is only used by the variable `barInt`, _not_ by `fooBarInt`.
```
0x00000032: DW_TAG_variable
                  DW_AT_location	(DW_OP_fbreg +11)
 DW_AT_name	("barInt")
 DW_AT_decl_file	("/Users/royshi/demo/preferred_name/two.cpp")
 DW_AT_decl_line	(9)
                  DW_AT_type	(0x0000004d "BarInt")

0x0000003d:     DW_TAG_variable
 DW_AT_location	(DW_OP_fbreg +10)
 DW_AT_name	("fooBarInt")
 DW_AT_decl_file	("/Users/royshi/demo/preferred_name/two.cpp")
 DW_AT_decl_line	(10)
                  DW_AT_type	(0x00000062 "Foo<Foo<int> >")
```

See full output:
- `-ggdb`: https://gist.github.com/royitaqi/d2b6c7c9ca56f886f051479bb7edde90
- `-glldb`: https://gist.github.com/royitaqi/030ac6963d7696f4cc34bb826803e098

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to