Skip to content

Commit bb3980a

Browse files
committed
DebugInfo: Don't use enumerators in template names for debug info as they are not canonical
Since enumerators may not be available in every translation unit they can't be reliably used to name entities. (this also makes simplified template name roundtripping infeasible - since the expected name could only be rebuilt if the enumeration definition could be found (or only if it couldn't be found, depending on the context of the original name))
1 parent 6efda5e commit bb3980a

File tree

7 files changed

+25
-34
lines changed

7 files changed

+25
-34
lines changed

clang/include/clang/AST/PrettyPrinter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ struct PrintingPolicy {
7474
SuppressImplicitBase(false), FullyQualifiedName(false),
7575
PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true),
7676
UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false),
77-
CleanUglifiedParameters(false), EntireContentsOfLargeArray(true) {}
77+
CleanUglifiedParameters(false), EntireContentsOfLargeArray(true),
78+
UseEnumerators(true) {}
7879

7980
/// Adjust this printing policy for cases where it's known that we're
8081
/// printing C++ code (for instance, if AST dumping reaches a C++-only
@@ -290,6 +291,10 @@ struct PrintingPolicy {
290291
/// template parameters, no matter how many elements there are.
291292
unsigned EntireContentsOfLargeArray : 1;
292293

294+
/// Whether to print enumerator non-type template parameters with a matching
295+
/// enumerator name or via cast of an integer.
296+
unsigned UseEnumerators : 1;
297+
293298
/// Callbacks to use to allow the behavior of printing to be customized.
294299
const PrintingCallbacks *Callbacks = nullptr;
295300
};

clang/lib/AST/TemplateBase.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ static void printIntegral(const TemplateArgument &TemplArg, raw_ostream &Out,
5959
const Type *T = TemplArg.getIntegralType().getTypePtr();
6060
const llvm::APSInt &Val = TemplArg.getAsIntegral();
6161

62-
if (const EnumType *ET = T->getAs<EnumType>()) {
63-
for (const EnumConstantDecl* ECD : ET->getDecl()->enumerators()) {
64-
// In Sema::CheckTemplateArugment, enum template arguments value are
65-
// extended to the size of the integer underlying the enum type. This
66-
// may create a size difference between the enum value and template
67-
// argument value, requiring isSameValue here instead of operator==.
68-
if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) {
69-
ECD->printQualifiedName(Out, Policy);
70-
return;
62+
if (Policy.UseEnumerators) {
63+
if (const EnumType *ET = T->getAs<EnumType>()) {
64+
for (const EnumConstantDecl *ECD : ET->getDecl()->enumerators()) {
65+
// In Sema::CheckTemplateArugment, enum template arguments value are
66+
// extended to the size of the integer underlying the enum type. This
67+
// may create a size difference between the enum value and template
68+
// argument value, requiring isSameValue here instead of operator==.
69+
if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) {
70+
ECD->printQualifiedName(Out, Policy);
71+
return;
72+
}
7173
}
7274
}
7375
}

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
248248
PP.PrintCanonicalTypes = true;
249249
PP.UsePreferredNames = false;
250250
PP.AlwaysIncludeTypeForTemplateArgument = true;
251+
PP.UseEnumerators = false;
251252

252253
// Apply -fdebug-prefix-map.
253254
PP.Callbacks = &PrintCB;

clang/test/CodeGenCXX/debug-info-simple-template-names.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct t4 {
3131
};
3232

3333
t4 v1;
34-
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "t3<(anonymous namespace)::LocalEnum, (anonymous namespace)::LocalEnum1>"
34+
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "t3<(anonymous namespace)::LocalEnum, ((anonymous namespace)::LocalEnum)0>"
3535
void f() {
3636
// Basic examples of simplifiable/rebuildable names
3737
f1<>();

llvm/lib/DebugInfo/DWARF/DWARFDie.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -397,27 +397,10 @@ struct DWARFTypePrinter {
397397
DWARFDie T = resolveReferencedType(C);
398398
Sep();
399399
if (T.getTag() == DW_TAG_enumeration_type) {
400-
auto V = C.find(DW_AT_const_value);
401-
bool FoundEnumerator = false;
402-
for (const DWARFDie &Enumerator : T) {
403-
auto EV = Enumerator.find(DW_AT_const_value);
404-
if (V && EV &&
405-
V->getAsSignedConstant() == EV->getAsSignedConstant()) {
406-
if (T.find(DW_AT_enum_class)) {
407-
appendQualifiedName(T);
408-
OS << "::";
409-
} else
410-
appendScopes(T.getParent());
411-
OS << Enumerator.getShortName();
412-
FoundEnumerator = true;
413-
break;
414-
}
415-
}
416-
if (FoundEnumerator)
417-
continue;
418400
OS << '(';
419401
appendQualifiedName(T);
420402
OS << ')';
403+
auto V = C.find(DW_AT_const_value);
421404
OS << to_string(*V->getAsSignedConstant());
422405
continue;
423406
}

llvm/test/tools/llvm-dwarfdump/X86/prettyprint_types.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@
140140
# CHECK: DW_AT_type{{.*}}"t2<t2<int> >"
141141

142142
# enum literals
143-
# CHECK: DW_AT_type{{.*}}"tv<e1, E1>")
143+
# CHECK: DW_AT_type{{.*}}"tv<e1, (e1)0>")
144144
# CHECK: DW_AT_type{{.*}}"tv<e1, (e1)1>")
145-
# CHECK: DW_AT_type{{.*}}"tv<e2, e2::E2>")
145+
# CHECK: DW_AT_type{{.*}}"tv<e2, (e2)0>")
146146

147147
# char literals
148148
# CHECK: DW_AT_type{{.*}}"tv<unsigned char, (unsigned char)'x'>")

llvm/test/tools/llvm-dwarfdump/X86/simplified-template-names.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12600,15 +12600,15 @@ i:
1260012600
.Linfo_string195:
1260112601
.asciz "_Z2f3IN2ns11EnumerationEJLS1_1ELS1_2EEEvv" # string offset=2877
1260212602
.Linfo_string196:
12603-
.asciz "_STNf3|<ns::Enumeration, ns::Enumerator2, (ns::Enumeration)2>" # string offset=2919
12603+
.asciz "_STNf3|<ns::Enumeration, (ns::Enumeration)1, (ns::Enumeration)2>" # string offset=2919
1260412604
.Linfo_string197:
1260512605
.asciz "_Z2f3IN2ns16EnumerationClassEJLS1_1ELS1_2EEEvv" # string offset=2981
1260612606
.Linfo_string198:
12607-
.asciz "_STNf3|<ns::EnumerationClass, ns::EnumerationClass::Enumerator2, (ns::EnumerationClass)2>" # string offset=3028
12607+
.asciz "_STNf3|<ns::EnumerationClass, (ns::EnumerationClass)1, (ns::EnumerationClass)2>" # string offset=3028
1260812608
.Linfo_string199:
1260912609
.asciz "_Z2f3IN2ns16EnumerationSmallEJLS1_255EEEvv" # string offset=3118
1261012610
.Linfo_string200:
12611-
.asciz "_STNf3|<ns::EnumerationSmall, ns::kNeg>" # string offset=3161
12611+
.asciz "_STNf3|<ns::EnumerationSmall, (ns::EnumerationSmall)255>" # string offset=3161
1261212612
.Linfo_string201:
1261312613
.asciz "_Z2f3IN2ns3$_0EJLS1_1ELS1_2EEEvv" # string offset=3201
1261412614
.Linfo_string202:

0 commit comments

Comments
 (0)