[clang] [clang][modules] Emit -gmodules debug info at end of TU (PR #248705)
Raphael Isemann via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 02:18:24 PST 2026
================
@@ -11,5 +11,6 @@
#include "gmodules-preferred-name-typedef.h"
-// CHECK: ![[#]] = !DIDerivedType(tag: DW_TAG_typedef, name: "Bar", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[PREF_BASE:[0-9]+]])
----------------
Teemperor wrote:
This looks not intentional? We now have two typedefs with the same name where one just points to the other and the other is the real one:
```llvm
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!14, !15}
!llvm.ident = !{!16}
!llvm.raw.sections = !{!17}
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_11, file: !1, producer: "clang version 24.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !2, imports: !12, dwoId: 2937162294245157521, nameTableKind: Apple, sysroot: "/")
!1 = !DIFile(filename: "PreferredNameModule", directory: "")
!2 = !{!3, !6}
!3 = !DIDerivedType(tag: DW_TAG_typedef, name: "Bar", scope: !5, file: !4, line: 3, baseType: !6)
!4 = !DIFile(filename: "/Users/teemperor/upstream/llvm/3/src/clang/test/Modules/Inputs/gmodules-preferred-name-typedef.h", directory: "")
!5 = !DIModule(scope: null, name: "PreferredNameModule", includePath: "/Users/teemperor/upstream/llvm/3/src/clang/test/Modules/Inputs")
!6 = !DIDerivedType(tag: DW_TAG_typedef, name: "Bar", scope: !5, file: !4, line: 3, baseType: !7)
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Foo<char>", scope: !5, file: !4, line: 5, size: 8, flags: DIFlagTypePassByValue, elements: !8, templateParams: !9, identifier: "_ZTS3FooIcE")
!8 = !{}
!9 = !{!10}
!10 = !DITemplateTypeParameter(name: "T", type: !11)
!11 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!12 = !{!13}
!13 = !DIImportedEntity(tag: DW_TAG_imported_declaration, scope: !5, entity: !5, file: !4, line: 7)
!14 = !{i32 2, !"Debug Info Version", i32 3}
!15 = !{i32 2, !"debugger-tuning", !"lldb"}
```
LLM diagnosis:
```cpp
The input
template<typename T> struct Foo;
typedef Foo<char> Bar; // line 3
template<typename T> struct [[clang::preferred_name(Bar)]] Foo {};
template <typename T> struct Baz { Foo<char> member; }; // line 7: instantiates Foo<char>
The relevant code in CGDebugInfo.cpp
1. Typedefs (CreateType(const TypedefType*), line 1825): to build Bar, it first calls getOrCreateType(Foo<char>) to get the underlying type.
2. Records: when CreateTypeDefinition finishes a complete class that has preferred_name (and the debugger tuning is LLDB), it calls GetPreferredNameType, which is getOrCreateType(Bar) (lines 3438 and 3562). It
returns that typedef in place of the struct.
3. The cache: getOrCreateType only writes the typedef into TypeCache once CreateTypeNode has returned (line 4453). The struct itself goes into the cache earlier, as a forward declaration, so recursive types
work.
After the commit (emission at end of TU)
By the time Bar is processed, Foo<char> is complete (it was instantiated at line 7), so:
- Outer getOrCreateType(Bar): Bar isn't cached yet, so it calls CreateType(TypedefType)
- which calls getOrCreateType(Foo<char>), which builds the full definition and then hits the preferred_name check
- which calls getOrCreateType(Bar). Bar still isn't cached, because the outer call hasn't returned yet.
- which calls getOrCreateType(Foo<char>) again. This time the struct is cached, so it returns Foo<char>.
- The inner call creates Bar → Foo<char> (!6 in after.ll).
- Foo<char> resolves to its preferred name, the inner Bar (!6).
- The outer call creates Bar → inner Bar (!3).
That gives !3 Bar → !6 Bar → !7 Foo<char>. The inner Bar (!6) also appears in retainedTypes, because it now stands in for Foo<char>.
Before the commit (emission while parsing)
Bar was emitted the moment line 3 was parsed. At that point Foo<char> was only a forward declaration, so CreateTypeDefinition returned early (line 3506) and the preferred_name lookup never ran. That gave a
single Bar → Foo<char> (still a forward declaration). Later, UpdateCompletedType filled in the struct's definition in place, which doesn't go through the preferred-name path. So there was no nested call and only
one typedef.
Is this a bug?
I think so. The commit didn't add a new bug; it moved emission to the point where a nested-call bug that was already there can trigger. A typedef whose base type is a typedef with the same name and scope is
redundant. It's probably harmless for LLDB, but the test now locks in that redundancy. A likely fix is in CreateType(const TypedefType*): after computing Underlying, check whether getTypeOrNull already has an
entry for this typedef and return that if so. Another option is to not resolve to the preferred name when the request comes from that same typedef's underlying type. Either way, the test's old single-typedef
CHECK lines should pass again.
```
https://github.com/llvm/llvm-project/pull/248705
More information about the cfe-commits
mailing list