Michael137 created this revision.
Michael137 added reviewers: aprantl, dblaikie.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Michael137 requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

**Summary**

Starting with DWARFv5, DW_AT_default_value can be used to indicate
that a template argument has a default value. With this patch LLVM
will emit the this attribute earlier versions of DWARF, unless
compiling with -gstrict-dwarf.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139953

Files:
  clang/test/CodeGenCXX/debug-info-template-parameter.cpp
  llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
  llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h


Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
+++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
@@ -350,6 +350,10 @@
 
   virtual bool isDwoUnit() const = 0;
   const MCSymbol *getCrossSectionRelativeBaseAddress() const override;
+
+  /// Returns 'true' if the current DwarfVersion is compatible
+  /// with the specified \p Version.
+  bool isCompatibleWithVersion(uint16_t Version) const;
 };
 
 class DwarfTypeUnit final : public DwarfUnit {
Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -1060,7 +1060,7 @@
     addType(ParamDIE, TP->getType());
   if (!TP->getName().empty())
     addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
-  if (TP->isDefault() && (DD->getDwarfVersion() >= 5))
+  if (TP->isDefault() && isCompatibleWithVersion(5))
     addFlag(ParamDIE, dwarf::DW_AT_default_value);
 }
 
@@ -1074,7 +1074,7 @@
     addType(ParamDIE, VP->getType());
   if (!VP->getName().empty())
     addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
-  if (VP->isDefault() && (DD->getDwarfVersion() >= 5))
+  if (VP->isDefault() && isCompatibleWithVersion(5))
     addFlag(ParamDIE, dwarf::DW_AT_default_value);
   if (Metadata *Val = VP->getValue()) {
     if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
@@ -1852,3 +1852,7 @@
 void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
   DD->getAddressPool().resetUsedFlag(true);
 }
+
+bool DwarfUnit::isCompatibleWithVersion(uint16_t Version) const {
+  return !Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= Version;
+}
Index: clang/test/CodeGenCXX/debug-info-template-parameter.cpp
===================================================================
--- clang/test/CodeGenCXX/debug-info-template-parameter.cpp
+++ clang/test/CodeGenCXX/debug-info-template-parameter.cpp
@@ -4,6 +4,7 @@
 // RUN: %clang_cc1 -emit-llvm %std_cxx11-14 -dwarf-version=5 -triple x86_64 %s 
-O0 -disable-llvm-passes -debug-info-kind=standalone -o - | FileCheck %s 
--check-prefixes=CHECK,PRE17
 // RUN: %clang_cc1 -emit-llvm %std_cxx17- -dwarf-version=5 -triple x86_64 %s 
-O0 -disable-llvm-passes -debug-info-kind=standalone -o - | FileCheck %s 
--check-prefixes=CHECK,CXX17
 // RUN: %clang_cc1 -emit-llvm %std_cxx17- -dwarf-version=4 -triple x86_64 %s 
-O0 -disable-llvm-passes -debug-info-kind=standalone -o - | FileCheck %s 
--check-prefixes=CHECK,CXX17
+// RUN: %clang_cc1 %std_cxx17- -dwarf-version=4 -gstrict-dwarf -triple x86_64 
%s -O0 -disable-llvm-passes -debug-info-kind=standalone -emit-obj -o - %s | 
llvm-dwarfdump --debug-info - | FileCheck %s --check-prefixes=DWARF-DUMP,STRICT
 
 // CHECK: DILocalVariable(name: "f1", {{.*}}, type: ![[TEMPLATE_TYPE:[0-9]+]]
 // CHECK: [[TEMPLATE_TYPE]] = {{.*}}!DICompositeType({{.*}}, templateParams: 
![[F1_TYPE:[0-9]+]]
@@ -21,6 +22,24 @@
 // PRE17: [[THIRD]] = !DITemplateValueParameter(name: "b", type: !{{[0-9]*}}, 
defaulted: true, value: i8 1)
 // CXX17: [[THIRD]] = !DITemplateValueParameter(name: "b", type: !{{[0-9]*}}, 
defaulted: true, value: i1 true)
 
+// DWARF-DUMP:     DW_TAG_class_type
+// DWARF-DUMP:       DW_AT_name      ("foo<char, 3, true, 1>")
+// DWARF-DUMP:       DW_TAG_template_type_parameter
+// DWARF-DUMP-NEXT:    DW_AT_type    ({{.*}} "char")
+// DWARF-DUMP-NEXT:    DW_AT_name    ("T")
+// DWARFv4-NEXT:       DW_AT_default_value   (true)
+// STRICT-NOT:         DW_AT_default_value   (true)
+// DWARF-DUMP:       DW_TAG_template_value_parameter
+// DWARF-DUMP-NEXT:    DW_AT_type    ({{.*}} "int")
+// DWARF-DUMP-NEXT:    DW_AT_name    ("i")
+// DWARFv4-NEXT:       DW_AT_default_value   (true)
+// STRICT-NOT:         DW_AT_default_value   (true)
+// DWARF-DUMP:       DW_TAG_template_value_parameter
+// DWARF-DUMP-NEXT:    DW_AT_type    ({{.*}} "bool")
+// DWARF-DUMP-NEXT:    DW_AT_name    ("b")
+// DWARFv4-NEXT:       DW_AT_default_value   (true)
+// STRICT-NOT:         DW_AT_default_value   (true)
+
 template <typename T = char, int i = 3, bool b = true, int x = sizeof(T)>
 class foo {
 };


Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
+++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
@@ -350,6 +350,10 @@
 
   virtual bool isDwoUnit() const = 0;
   const MCSymbol *getCrossSectionRelativeBaseAddress() const override;
+
+  /// Returns 'true' if the current DwarfVersion is compatible
+  /// with the specified \p Version.
+  bool isCompatibleWithVersion(uint16_t Version) const;
 };
 
 class DwarfTypeUnit final : public DwarfUnit {
Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -1060,7 +1060,7 @@
     addType(ParamDIE, TP->getType());
   if (!TP->getName().empty())
     addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
-  if (TP->isDefault() && (DD->getDwarfVersion() >= 5))
+  if (TP->isDefault() && isCompatibleWithVersion(5))
     addFlag(ParamDIE, dwarf::DW_AT_default_value);
 }
 
@@ -1074,7 +1074,7 @@
     addType(ParamDIE, VP->getType());
   if (!VP->getName().empty())
     addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
-  if (VP->isDefault() && (DD->getDwarfVersion() >= 5))
+  if (VP->isDefault() && isCompatibleWithVersion(5))
     addFlag(ParamDIE, dwarf::DW_AT_default_value);
   if (Metadata *Val = VP->getValue()) {
     if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
@@ -1852,3 +1852,7 @@
 void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
   DD->getAddressPool().resetUsedFlag(true);
 }
+
+bool DwarfUnit::isCompatibleWithVersion(uint16_t Version) const {
+  return !Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= Version;
+}
Index: clang/test/CodeGenCXX/debug-info-template-parameter.cpp
===================================================================
--- clang/test/CodeGenCXX/debug-info-template-parameter.cpp
+++ clang/test/CodeGenCXX/debug-info-template-parameter.cpp
@@ -4,6 +4,7 @@
 // RUN: %clang_cc1 -emit-llvm %std_cxx11-14 -dwarf-version=5 -triple x86_64 %s -O0 -disable-llvm-passes -debug-info-kind=standalone -o - | FileCheck %s --check-prefixes=CHECK,PRE17
 // RUN: %clang_cc1 -emit-llvm %std_cxx17- -dwarf-version=5 -triple x86_64 %s -O0 -disable-llvm-passes -debug-info-kind=standalone -o - | FileCheck %s --check-prefixes=CHECK,CXX17
 // RUN: %clang_cc1 -emit-llvm %std_cxx17- -dwarf-version=4 -triple x86_64 %s -O0 -disable-llvm-passes -debug-info-kind=standalone -o - | FileCheck %s --check-prefixes=CHECK,CXX17
+// RUN: %clang_cc1 %std_cxx17- -dwarf-version=4 -gstrict-dwarf -triple x86_64 %s -O0 -disable-llvm-passes -debug-info-kind=standalone -emit-obj -o - %s | llvm-dwarfdump --debug-info - | FileCheck %s --check-prefixes=DWARF-DUMP,STRICT
 
 // CHECK: DILocalVariable(name: "f1", {{.*}}, type: ![[TEMPLATE_TYPE:[0-9]+]]
 // CHECK: [[TEMPLATE_TYPE]] = {{.*}}!DICompositeType({{.*}}, templateParams: ![[F1_TYPE:[0-9]+]]
@@ -21,6 +22,24 @@
 // PRE17: [[THIRD]] = !DITemplateValueParameter(name: "b", type: !{{[0-9]*}}, defaulted: true, value: i8 1)
 // CXX17: [[THIRD]] = !DITemplateValueParameter(name: "b", type: !{{[0-9]*}}, defaulted: true, value: i1 true)
 
+// DWARF-DUMP:     DW_TAG_class_type
+// DWARF-DUMP:       DW_AT_name      ("foo<char, 3, true, 1>")
+// DWARF-DUMP:       DW_TAG_template_type_parameter
+// DWARF-DUMP-NEXT:    DW_AT_type    ({{.*}} "char")
+// DWARF-DUMP-NEXT:    DW_AT_name    ("T")
+// DWARFv4-NEXT:       DW_AT_default_value   (true)
+// STRICT-NOT:         DW_AT_default_value   (true)
+// DWARF-DUMP:       DW_TAG_template_value_parameter
+// DWARF-DUMP-NEXT:    DW_AT_type    ({{.*}} "int")
+// DWARF-DUMP-NEXT:    DW_AT_name    ("i")
+// DWARFv4-NEXT:       DW_AT_default_value   (true)
+// STRICT-NOT:         DW_AT_default_value   (true)
+// DWARF-DUMP:       DW_TAG_template_value_parameter
+// DWARF-DUMP-NEXT:    DW_AT_type    ({{.*}} "bool")
+// DWARF-DUMP-NEXT:    DW_AT_name    ("b")
+// DWARFv4-NEXT:       DW_AT_default_value   (true)
+// STRICT-NOT:         DW_AT_default_value   (true)
+
 template <typename T = char, int i = 3, bool b = true, int x = sizeof(T)>
 class foo {
 };
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to