DebugInfo: Don't use preferred template names in debug info
authorDavid Blaikie <dblaikie@gmail.com>
Mon, 20 Sep 2021 03:34:45 +0000 (20:34 -0700)
committerDavid Blaikie <dblaikie@gmail.com>
Wed, 22 Sep 2021 03:08:16 +0000 (20:08 -0700)
Using the preferred name creates a mismatch between the textual name of
a type and the DWARF tags describing the parameters as well as possible
inconsistency between DWARF producers (like Clang and GCC, or
older/newer Clang versions, etc).

clang/include/clang/AST/PrettyPrinter.h
clang/lib/AST/TypePrinter.cpp
clang/lib/CodeGen/CGDebugInfo.cpp
clang/test/CodeGenCXX/debug-info-template.cpp

index 3baf2b2..8cab7f1 100644 (file)
@@ -74,7 +74,8 @@ struct PrintingPolicy {
         MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
         MSVCFormatting(false), ConstantsAsWritten(false),
         SuppressImplicitBase(false), FullyQualifiedName(false),
-        PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true) {}
+        PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true),
+        UsePreferredNames(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
@@ -272,6 +273,7 @@ struct PrintingPolicy {
   /// written. When a template argument is unnamed, printing it results in
   /// invalid C++ code.
   unsigned PrintInjectedClassNameWithArguments : 1;
+  unsigned UsePreferredNames : 1;
 
   /// Callbacks to use to allow the behavior of printing to be customized.
   const PrintingCallbacks *Callbacks = nullptr;
index 749a3e2..3c7a6b8 100644 (file)
@@ -1370,9 +1370,11 @@ void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
 
 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
   // Print the preferred name if we have one for this type.
-  for (const auto *PNA : T->getDecl()->specific_attrs<PreferredNameAttr>()) {
-    if (declaresSameEntity(PNA->getTypedefType()->getAsCXXRecordDecl(),
-                           T->getDecl())) {
+  if (Policy.UsePreferredNames) {
+    for (const auto *PNA : T->getDecl()->specific_attrs<PreferredNameAttr>()) {
+      if (!declaresSameEntity(PNA->getTypedefType()->getAsCXXRecordDecl(),
+                              T->getDecl()))
+        continue;
       // Find the outermost typedef or alias template.
       QualType T = PNA->getTypedefType();
       while (true) {
index 8660e23..5889647 100644 (file)
@@ -245,6 +245,7 @@ PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
 
   PP.SuppressInlineNamespace = false;
   PP.PrintCanonicalTypes = true;
+  PP.UsePreferredNames = false;
 
   // Apply -fdebug-prefix-map.
   PP.Callbacks = &PrintCB;
index 546fe01..ba25e21 100644 (file)
@@ -258,3 +258,15 @@ template void f1<t1>();
 // CHECK: ![[TEMP_TEMP_INL_ARGS]] = !{![[TEMP_TEMP_INL_ARGS_T:[0-9]*]]}
 // CHECK: ![[TEMP_TEMP_INL_ARGS_T]] = !DITemplateValueParameter(tag: DW_TAG_GNU_template_template_param, value: !"TemplateTemplateParamInlineNamespace::inl::t1")
 } // namespace TemplateTemplateParamInlineNamespace
+
+namespace NoPreferredNames {
+template <typename T> struct t1;
+using t1i = t1<int>;
+template <typename T>
+struct __attribute__((__preferred_name__(t1i))) t1 {};
+template <typename T>
+void f1() {}
+template void f1<t1<int>>();
+// CHECK: !DISubprogram(name: "f1<NoPreferredNames::t1<int> >",
+
+} // namespace NoPreferredNames