Index: llvm/lib/DWARFLinker/DWARFLinker.cpp =================================================================== --- llvm/lib/DWARFLinker/DWARFLinker.cpp +++ llvm/lib/DWARFLinker/DWARFLinker.cpp @@ -120,6 +120,39 @@ DwarfEmitter::~DwarfEmitter() {} +namespace { +Optional StripTemplateParameters(StringRef Name) { + // We are looking for template parameters to strip from Name. e.g. + // + // operator< + // + // We look for > at the end but if it does not contain any < then we + // have something like operator>>. We check for the operator<=> case. + if (!Name.endswith(">") || Name.count("<") == 0 || Name.endswith("<=>")) + return {}; + + // How many < until we have the start of the template parameters. + size_t NumLeftAnglesToCount = 1; + + // If we have operator<=> then we need to skip its < as well. + NumLeftAnglesToCount += Name.count("<=>"); + + size_t RightAngleCount = Name.count('>'); + size_t LeftAngleCount = Name.count('<'); + + // If we have more < than > we have operator< or operator<< + // we to account for their < as well. + if (LeftAngleCount > RightAngleCount) + NumLeftAnglesToCount += LeftAngleCount - RightAngleCount; + + size_t StartOfTemplate = 0; + while (NumLeftAnglesToCount--) + StartOfTemplate = Name.find('<', StartOfTemplate) + 1; + + return Name.substr(0,StartOfTemplate-1); +} +} + bool DWARFLinker::DIECloner::getDIENames(const DWARFDie &Die, AttributesInfo &Info, OffsetsStringPool &StringPool, @@ -141,10 +174,11 @@ Info.Name = StringPool.getEntry(Name); if (StripTemplate && Info.Name && Info.MangledName != Info.Name) { - // FIXME: dsymutil compatibility. This is wrong for operator< - auto Split = Info.Name.getString().split('<'); - if (!Split.second.empty()) - Info.NameWithoutTemplate = StringPool.getEntry(Split.first); + StringRef Name = Info.Name.getString(); + Optional StrippedName = StripTemplateParameters(Name); + + if (StrippedName) + Info.NameWithoutTemplate = StringPool.getEntry(*StrippedName); } return Info.Name || Info.MangledName; Index: llvm/test/tools/dsymutil/X86/template_operators.test =================================================================== --- /dev/null +++ llvm/test/tools/dsymutil/X86/template_operators.test @@ -0,0 +1,62 @@ +RUN: dsymutil -oso-prepend-path %p/../Inputs %p/../Inputs/private/tmp/templated_operators/template_operators -o %t.apple.dSYM + +RUN: llvm-dwarfdump -apple-names %t.apple.dSYM | FileCheck %s -check-prefix=NAMES + +The test was compiled from a single source: +$ cat template_operators.cpp +template +bool operator<(const T&, const T&){ + return true; +} + +template +bool operator>(const T&, const T&){ + return true; +} + +template +bool operator<<(const T&, const T&){ + return true; +} + +template +bool operator>>(const T&, const T&){ + return true; +} + +template +bool operator==(const T&, const T&){ + return true; +} + +struct B{}; + +struct D{}; + +template +bool operator<=>(const T&,const T&){ return true;} + +int main() { + B b1; + B b2; + D d1; + D d2; + + return b1 < b2 && b1 > b2 && b1 << b2 && b1 >> b2 && b1 == b2 && d1 <=> d2; +} +$ clang++ -std=c++2a -g template_operators.cpp -c -o template_operators.o +$ clang template_operators.o -o template_operators + + +NAMES-DAG: "operator<" +NAMES-DAG: "operator<" +NAMES-DAG: "operator>" +NAMES-DAG: "operator>" +NAMES-DAG: "operator<<" +NAMES-DAG: "operator<<" +NAMES-DAG: "operator>>" +NAMES-DAG: "operator>>" +NAMES-DAG: "operator<=>" +NAMES-DAG: "operator<=>" +NAMES-DAG: "operator==" +NAMES-DAG: "operator=="