DebugInfo: Don't use enumerators in template names for debug info as they are not...
authorDavid Blaikie <dblaikie@gmail.com>
Tue, 5 Apr 2022 17:16:42 +0000 (17:16 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Tue, 5 Apr 2022 17:16:42 +0000 (17:16 +0000)
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
clang/lib/AST/TemplateBase.cpp
clang/lib/CodeGen/CGDebugInfo.cpp
clang/test/CodeGenCXX/debug-info-simple-template-names.cpp
llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
llvm/test/tools/llvm-dwarfdump/X86/prettyprint_types.s
llvm/test/tools/llvm-dwarfdump/X86/simplified-template-names.s

index 54cb572..cb25b27 100644 (file)
@@ -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;
 };
index 3418401..229a8db 100644 (file)
@@ -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<EnumType>()) {
-    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<EnumType>()) {
+      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;
+        }
       }
     }
   }
index beb6403..a37dc81 100644 (file)
@@ -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;
index 00c4361..5bf1b54 100644 (file)
@@ -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<>();
index 86bf36c..1dbe22d 100644 (file)
@@ -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;
         }
index f04e245..92010dc 100644 (file)
 # CHECK:   DW_AT_type{{.*}}"t2<t2<int> >"
 
 # enum literals
-# CHECK:   DW_AT_type{{.*}}"tv<e1, E1>")
+# CHECK:   DW_AT_type{{.*}}"tv<e1, (e1)0>")
 # CHECK:   DW_AT_type{{.*}}"tv<e1, (e1)1>")
-# CHECK:   DW_AT_type{{.*}}"tv<e2, e2::E2>")
+# CHECK:   DW_AT_type{{.*}}"tv<e2, (e2)0>")
 
 # char literals
 # CHECK:   DW_AT_type{{.*}}"tv<unsigned char, (unsigned char)'x'>")
index 1f52234..6bca48e 100644 (file)
 .Linfo_string195:
        .asciz  "_Z2f3IN2ns11EnumerationEJLS1_1ELS1_2EEEvv" # string offset=2877
 .Linfo_string196:
-       .asciz  "_STNf3|<ns::Enumeration, ns::Enumerator2, (ns::Enumeration)2>" # string offset=2919
+       .asciz  "_STNf3|<ns::Enumeration, (ns::Enumeration)1, (ns::Enumeration)2>" # string offset=2919
 .Linfo_string197:
        .asciz  "_Z2f3IN2ns16EnumerationClassEJLS1_1ELS1_2EEEvv" # string offset=2981
 .Linfo_string198:
-       .asciz  "_STNf3|<ns::EnumerationClass, ns::EnumerationClass::Enumerator2, (ns::EnumerationClass)2>" # string offset=3028
+       .asciz  "_STNf3|<ns::EnumerationClass, (ns::EnumerationClass)1, (ns::EnumerationClass)2>" # string offset=3028
 .Linfo_string199:
        .asciz  "_Z2f3IN2ns16EnumerationSmallEJLS1_255EEEvv" # string offset=3118
 .Linfo_string200:
-       .asciz  "_STNf3|<ns::EnumerationSmall, ns::kNeg>" # string offset=3161
+       .asciz  "_STNf3|<ns::EnumerationSmall, (ns::EnumerationSmall)255>" # string offset=3161
 .Linfo_string201:
        .asciz  "_Z2f3IN2ns3$_0EJLS1_1ELS1_2EEEvv" # string offset=3201
 .Linfo_string202: