From bb3980ae9fa7e19540080285f2bf2d960ea802fc Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Tue, 5 Apr 2022 17:16:42 +0000 Subject: [PATCH] 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)) --- clang/include/clang/AST/PrettyPrinter.h | 7 ++++++- clang/lib/AST/TemplateBase.cpp | 20 ++++++++++--------- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + .../debug-info-simple-template-names.cpp | 2 +- llvm/lib/DebugInfo/DWARF/DWARFDie.cpp | 19 +----------------- .../llvm-dwarfdump/X86/prettyprint_types.s | 4 ++-- .../X86/simplified-template-names.s | 6 +++--- 7 files changed, 25 insertions(+), 34 deletions(-) diff --git a/clang/include/clang/AST/PrettyPrinter.h b/clang/include/clang/AST/PrettyPrinter.h index 54cb57227f7a..cb25b2750dd4 100644 --- a/clang/include/clang/AST/PrettyPrinter.h +++ b/clang/include/clang/AST/PrettyPrinter.h @@ -74,7 +74,8 @@ struct PrintingPolicy { SuppressImplicitBase(false), FullyQualifiedName(false), PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true), UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false), - CleanUglifiedParameters(false), EntireContentsOfLargeArray(true) {} + CleanUglifiedParameters(false), EntireContentsOfLargeArray(true), + UseEnumerators(true) {} /// Adjust this printing policy for cases where it's known that we're /// printing C++ code (for instance, if AST dumping reaches a C++-only @@ -290,6 +291,10 @@ struct PrintingPolicy { /// template parameters, no matter how many elements there are. unsigned EntireContentsOfLargeArray : 1; + /// Whether to print enumerator non-type template parameters with a matching + /// enumerator name or via cast of an integer. + unsigned UseEnumerators : 1; + /// Callbacks to use to allow the behavior of printing to be customized. const PrintingCallbacks *Callbacks = nullptr; }; diff --git a/clang/lib/AST/TemplateBase.cpp b/clang/lib/AST/TemplateBase.cpp index 3418401517c8..229a8db21ab6 100644 --- a/clang/lib/AST/TemplateBase.cpp +++ b/clang/lib/AST/TemplateBase.cpp @@ -59,15 +59,17 @@ static void printIntegral(const TemplateArgument &TemplArg, raw_ostream &Out, const Type *T = TemplArg.getIntegralType().getTypePtr(); const llvm::APSInt &Val = TemplArg.getAsIntegral(); - if (const EnumType *ET = T->getAs()) { - for (const EnumConstantDecl* ECD : ET->getDecl()->enumerators()) { - // In Sema::CheckTemplateArugment, enum template arguments value are - // extended to the size of the integer underlying the enum type. This - // may create a size difference between the enum value and template - // argument value, requiring isSameValue here instead of operator==. - if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) { - ECD->printQualifiedName(Out, Policy); - return; + if (Policy.UseEnumerators) { + if (const EnumType *ET = T->getAs()) { + for (const EnumConstantDecl *ECD : ET->getDecl()->enumerators()) { + // In Sema::CheckTemplateArugment, enum template arguments value are + // extended to the size of the integer underlying the enum type. This + // may create a size difference between the enum value and template + // argument value, requiring isSameValue here instead of operator==. + if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) { + ECD->printQualifiedName(Out, Policy); + return; + } } } } diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index beb640375dfb..a37dc81c4bb8 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -248,6 +248,7 @@ PrintingPolicy CGDebugInfo::getPrintingPolicy() const { PP.PrintCanonicalTypes = true; PP.UsePreferredNames = false; PP.AlwaysIncludeTypeForTemplateArgument = true; + PP.UseEnumerators = false; // Apply -fdebug-prefix-map. PP.Callbacks = &PrintCB; diff --git a/clang/test/CodeGenCXX/debug-info-simple-template-names.cpp b/clang/test/CodeGenCXX/debug-info-simple-template-names.cpp index 00c4361e11ef..5bf1b54b9770 100644 --- a/clang/test/CodeGenCXX/debug-info-simple-template-names.cpp +++ b/clang/test/CodeGenCXX/debug-info-simple-template-names.cpp @@ -31,7 +31,7 @@ struct t4 { }; t4 v1; -// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "t3<(anonymous namespace)::LocalEnum, (anonymous namespace)::LocalEnum1>" +// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "t3<(anonymous namespace)::LocalEnum, ((anonymous namespace)::LocalEnum)0>" void f() { // Basic examples of simplifiable/rebuildable names f1<>(); diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp index 86bf36cc457d..1dbe22dcd1c5 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp @@ -397,27 +397,10 @@ struct DWARFTypePrinter { DWARFDie T = resolveReferencedType(C); Sep(); if (T.getTag() == DW_TAG_enumeration_type) { - auto V = C.find(DW_AT_const_value); - bool FoundEnumerator = false; - for (const DWARFDie &Enumerator : T) { - auto EV = Enumerator.find(DW_AT_const_value); - if (V && EV && - V->getAsSignedConstant() == EV->getAsSignedConstant()) { - if (T.find(DW_AT_enum_class)) { - appendQualifiedName(T); - OS << "::"; - } else - appendScopes(T.getParent()); - OS << Enumerator.getShortName(); - FoundEnumerator = true; - break; - } - } - if (FoundEnumerator) - continue; OS << '('; appendQualifiedName(T); OS << ')'; + auto V = C.find(DW_AT_const_value); OS << to_string(*V->getAsSignedConstant()); continue; } diff --git a/llvm/test/tools/llvm-dwarfdump/X86/prettyprint_types.s b/llvm/test/tools/llvm-dwarfdump/X86/prettyprint_types.s index f04e245a420b..92010dcc14c3 100644 --- a/llvm/test/tools/llvm-dwarfdump/X86/prettyprint_types.s +++ b/llvm/test/tools/llvm-dwarfdump/X86/prettyprint_types.s @@ -140,9 +140,9 @@ # CHECK: DW_AT_type{{.*}}"t2 >" # enum literals -# CHECK: DW_AT_type{{.*}}"tv") +# CHECK: DW_AT_type{{.*}}"tv") # CHECK: DW_AT_type{{.*}}"tv") -# CHECK: DW_AT_type{{.*}}"tv") +# CHECK: DW_AT_type{{.*}}"tv") # char literals # CHECK: DW_AT_type{{.*}}"tv") diff --git a/llvm/test/tools/llvm-dwarfdump/X86/simplified-template-names.s b/llvm/test/tools/llvm-dwarfdump/X86/simplified-template-names.s index 1f522343e010..6bca48e15a15 100644 --- a/llvm/test/tools/llvm-dwarfdump/X86/simplified-template-names.s +++ b/llvm/test/tools/llvm-dwarfdump/X86/simplified-template-names.s @@ -12600,15 +12600,15 @@ i: .Linfo_string195: .asciz "_Z2f3IN2ns11EnumerationEJLS1_1ELS1_2EEEvv" # string offset=2877 .Linfo_string196: - .asciz "_STNf3|" # string offset=2919 + .asciz "_STNf3|" # string offset=2919 .Linfo_string197: .asciz "_Z2f3IN2ns16EnumerationClassEJLS1_1ELS1_2EEEvv" # string offset=2981 .Linfo_string198: - .asciz "_STNf3|" # string offset=3028 + .asciz "_STNf3|" # string offset=3028 .Linfo_string199: .asciz "_Z2f3IN2ns16EnumerationSmallEJLS1_255EEEvv" # string offset=3118 .Linfo_string200: - .asciz "_STNf3|" # string offset=3161 + .asciz "_STNf3|" # string offset=3161 .Linfo_string201: .asciz "_Z2f3IN2ns3$_0EJLS1_1ELS1_2EEEvv" # string offset=3201 .Linfo_string202: