llvm / llvm/llvm-project

[llvm] [debuginfo] `DW_TAG_typedef` generated by `clang::preferred_name` has wrong `DW_AT_decl_line` value

Open
#166,673 2 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

(This is on macOS. Not sure what it does on Linux.)

See this code:
```
template struct Foo;

typedef Foo BarInt;

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

int main() {
BarInt barInt;
return 0;
}
```

Compile it with `xcrun clang++ -g -O0 -c`.

The generated object file contains these two DIEs:
```
0x00000044: DW_TAG_typedef
DW_AT_type (0x0000004c "BarInt")
DW_AT_name ("BarInt")
DW_AT_decl_file ("/Users/royshi/demo/preferred_name/one.cpp")
DW_AT_decl_line (3)

0x0000004c: DW_TAG_typedef
DW_AT_type (0x00000054 "Foo")
DW_AT_name ("BarInt")
DW_AT_decl_file ("/Users/royshi/demo/preferred_name/one.cpp")
DW_AT_decl_line (3)
```

The first DIE is generated by `[[clang::preferred_name(BarInt)]]` on line 6. You can prove this by removing the attribute from the source code and recompiling, after which this DIE will disappear.

The problem is that the attribute is on line 6, so the `DW_AT_decl_line` here should be `6`, not `3`.

--

Fixing this will help fix the self-recursive typedef DIE issue (https://github.com/llvm/llvm-project/issues/162954), because then the two DIE will have different decl lines, which we can use in their synthetic names to differentiate them, so that they won't be merged together by the parallel linker.

cc @Michael137, @avl-llvm, @clayborg

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the supplied source and `xcrun clang++ -g -O0 -c`, then inspect the generated DWARF DIEs and the handling of `clang::preferred_name`. The fix is done when the attribute-generated `DW_TAG_typedef` reports declaration line 6 rather than 3, with a regression test covering the reproducer.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.