llvm / llvm/llvm-project

[clang] Preferred name not generated nor used to declare variables in DWARF when used as template argument in code

Open
#164,730 9 comments 0 reactions 0 assignees View on GitHub
clang:codegen debuginfo
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

# Problem

**TL;DR** 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 >`. The preferred name, i.e. `BarInt` (case sensitive), is no where to be found in the DWARF.

Code:
```
template struct Foo;

typedef Foo BarInt;

template
struct [[clang::preferred_name(BarInt)]] Foo{};

int main() {
// BarInt barInt;
Foo 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 >")
```

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

## 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.

Note: If we uncomment that line `// BarInt barInt;`, the preferred name will be generated for `barInt` (type = `BarInt`), but not for `fooBarInt` (type = `Foo >`; I'm expecting `Foo`, since the type `BarInt` now exists).

--

cc @Michael137 , @clayborg

Contributor guide

Open the contributing guide

Research direction

Start with the two.cpp reproducer and compile it using clang with -O0 -glldb and -ggdb. Inspect the generated DWARF and compare the variable's DW_AT_type with the expected preferred name, then use the shown LLDB command to verify that fooBarInt displays the preferred template spelling.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.