Author: David Blaikie Date: 2021-07-17T23:58:15-07:00 New Revision: dac582ad3a78b18bdd2e6615f1ec105ee05adfe1
URL: https://github.com/llvm/llvm-project/commit/dac582ad3a78b18bdd2e6615f1ec105ee05adfe1 DIFF: https://github.com/llvm/llvm-project/commit/dac582ad3a78b18bdd2e6615f1ec105ee05adfe1.diff LOG: DebugInfo: Name class templates with default arguments consistently (both direct naming, and as a template argument for a function template) It's noteworthy that GCC has the same bug here, which is a bit surprising. Both Clang and GCC's bug is only for function template arguments that are themselves templates with default template arguments (f1<t1<int[, missing_default_here]>>). Probably because function name matching isn't generally necessary - whereas type matching is necessary for DWARF consumers to associate declarations and definitions across translation units, so the bug's been addressed there already - but continued to exist for function templates since it's fairly benign there. I came across this while working on a change that could reconstitute these pretty printed names based on the rest of the DWARF, reducing the size of the DWARF by not having to encode all the template parameters in the name string. That reconstitution code can't tell the difference between a defaulted argument or not, so couldn't create the current buggy-ish output. Making the names more consistent between direct and indirect references, and between function and class templates seems all to the good. (I fixed the function template version of this a few years back in 9fdd09a4ccd01feb8e00be22b17e944e46807746 - clearly I should've looked more closely and generalized the code better so it only had to be fixed once - well, doing that here now) Added: Modified: clang/lib/CodeGen/CGDebugInfo.cpp clang/lib/CodeGen/CGDebugInfo.h clang/test/CodeGenCXX/debug-info-template.cpp Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index e4c3af07e664..432e2400a440 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -249,26 +249,7 @@ PrintingPolicy CGDebugInfo::getPrintingPolicy() const { } StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { - assert(FD && "Invalid FunctionDecl!"); - IdentifierInfo *FII = FD->getIdentifier(); - FunctionTemplateSpecializationInfo *Info = - FD->getTemplateSpecializationInfo(); - - if (!Info && FII) - return FII->getName(); - - SmallString<128> NS; - llvm::raw_svector_ostream OS(NS); - FD->printName(OS); - - // Add any template specialization args. - if (Info) { - const TemplateArgumentList *TArgs = Info->TemplateArguments; - printTemplateArgumentList(OS, TArgs->asArray(), getPrintingPolicy()); - } - - // Copy this name on the side and use its reference. - return internString(OS.str()); + return internString(GetName(FD)); } StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { @@ -301,16 +282,8 @@ StringRef CGDebugInfo::getSelectorName(Selector S) { StringRef CGDebugInfo::getClassName(const RecordDecl *RD) { if (isa<ClassTemplateSpecializationDecl>(RD)) { - SmallString<128> Name; - llvm::raw_svector_ostream OS(Name); - PrintingPolicy PP = getPrintingPolicy(); - PP.PrintCanonicalTypes = true; - PP.SuppressInlineNamespace = false; - RD->getNameForDiagnostic(OS, PP, - /*Qualified*/ false); - // Copy this name on the side and use its reference. - return internString(Name); + return internString(GetName(RD)); } // quick optimization to avoid having to intern strings that are already @@ -4003,12 +3976,7 @@ void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, return; llvm::TimeTraceScope TimeScope("DebugFunction", [&]() { - std::string Name; - llvm::raw_string_ostream OS(Name); - if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) - ND->getNameForDiagnostic(OS, getPrintingPolicy(), - /*Qualified=*/true); - return Name; + return GetName(D, true); }); llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; @@ -4776,6 +4744,18 @@ llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls( return GVE; } +std::string CGDebugInfo::GetName(const Decl *D, bool Qualified) const { + std::string Name; + llvm::raw_string_ostream OS(Name); + if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) { + PrintingPolicy PP = getPrintingPolicy(); + PP.PrintCanonicalTypes = true; + PP.SuppressInlineNamespace = false; + ND->getNameForDiagnostic(OS, PP, Qualified); + } + return Name; +} + void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, const VarDecl *D) { assert(CGM.getCodeGenOpts().hasReducedDebugInfo()); @@ -4783,11 +4763,7 @@ void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, return; llvm::TimeTraceScope TimeScope("DebugGlobalVariable", [&]() { - std::string Name; - llvm::raw_string_ostream OS(Name); - D->getNameForDiagnostic(OS, getPrintingPolicy(), - /*Qualified=*/true); - return Name; + return GetName(D, true); }); // If we already created a DIGlobalVariable for this declaration, just attach @@ -4851,11 +4827,7 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) { if (VD->hasAttr<NoDebugAttr>()) return; llvm::TimeTraceScope TimeScope("DebugConstGlobalVariable", [&]() { - std::string Name; - llvm::raw_string_ostream OS(Name); - VD->getNameForDiagnostic(OS, getPrintingPolicy(), - /*Qualified=*/true); - return Name; + return GetName(VD, true); }); auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h index 9c52bc75475f..b01165f85a6c 100644 --- a/clang/lib/CodeGen/CGDebugInfo.h +++ b/clang/lib/CodeGen/CGDebugInfo.h @@ -571,6 +571,8 @@ class CGDebugInfo { llvm::DIType *WrappedType; }; + std::string GetName(const Decl*, bool Qualified = false) const; + /// Build up structure info for the byref. See \a BuildByRefType. BlockByRefType EmitTypeForVarWithBlocksAttr(const VarDecl *VD, uint64_t *OffSet); diff --git a/clang/test/CodeGenCXX/debug-info-template.cpp b/clang/test/CodeGenCXX/debug-info-template.cpp index 4342b3e27a0b..0255ec9df00f 100644 --- a/clang/test/CodeGenCXX/debug-info-template.cpp +++ b/clang/test/CodeGenCXX/debug-info-template.cpp @@ -187,3 +187,13 @@ template<typename T> struct ClassTemplateInlineNamespaceArg { }; ClassTemplateInlineNamespaceArg<inl::t1> ClassTemplateInlineNamespaceArgObj; // CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "ClassTemplateInlineNamespaceArg<inl::t1>", + +namespace IndirectDefaultArgument { +template<typename T1, typename T2 = int> +struct t1 { }; +template<typename T> +void f1() { +} +template void f1<t1<int>>(); +// CHECK: !DISubprogram(name: "f1<IndirectDefaultArgument::t1<int, int> >", +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits