diff --git a/clang/test/CodeGenCXX/debug-info-template-parameter.cpp b/clang/test/CodeGenCXX/debug-info-template-parameter.cpp --- a/clang/test/CodeGenCXX/debug-info-template-parameter.cpp +++ b/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") +// 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 class foo { }; diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h --- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h +++ b/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 { diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ b/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(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; +}