[libcxxabi] [lldb] [llvm] [lldb] Add frame-format option to highlight function names in backtraces (PR #131836)
Michael Buch via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 10 16:30:22 PDT 2025
https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/131836
>From 4e239db0006f1c3db4073ab981173762df732d60 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Tue, 11 Mar 2025 08:57:13 +0000
Subject: [PATCH 1/5] [llvm][ItaniumDemangle] Add printLeft/printRight APIs to
OutputBuffer
This patch includes the necessary changes for the LLDB feature proposed in https://discourse.llvm.org/t/rfc-lldb-highlighting-function-names-in-lldb-backtraces/85309. The TL;DR is that we want to track where certain parts of a demangled name begin/end so we can highlight them in backtraces.
We introduce a new `printLeft`/`printRight` API that a client (in our case LLDB) can implement to track state while printing the demangle tree. This requires redirecting all calls to to `printLeft`/`printRight` to the `OutputBuffer`. One quirk with the new API is that `Utility.h` would now depend on `ItaniumDemangle.h` and vice-versa. To keep these files header-only I made the definitions `inline` and implement the new APIs in `ItaniumDemangle.h` (so the definition of `Node` is available to them).
---
libcxxabi/src/demangle/ItaniumDemangle.h | 91 ++++++++++++--------
libcxxabi/src/demangle/Utility.h | 10 +++
llvm/include/llvm/Demangle/ItaniumDemangle.h | 91 ++++++++++++--------
llvm/include/llvm/Demangle/Utility.h | 10 +++
4 files changed, 128 insertions(+), 74 deletions(-)
diff --git a/libcxxabi/src/demangle/ItaniumDemangle.h b/libcxxabi/src/demangle/ItaniumDemangle.h
index 3df41b5f4d7d0..aa91b93e2a3bb 100644
--- a/libcxxabi/src/demangle/ItaniumDemangle.h
+++ b/libcxxabi/src/demangle/ItaniumDemangle.h
@@ -281,20 +281,11 @@ class Node {
}
void print(OutputBuffer &OB) const {
- printLeft(OB);
+ OB.printLeft(*this);
if (RHSComponentCache != Cache::No)
- printRight(OB);
+ OB.printRight(*this);
}
- // Print the "left" side of this Node into OutputBuffer.
- virtual void printLeft(OutputBuffer &) const = 0;
-
- // Print the "right". This distinction is necessary to represent C++ types
- // that appear on the RHS of their subtype, such as arrays or functions.
- // Since most types don't have such a component, provide a default
- // implementation.
- virtual void printRight(OutputBuffer &) const {}
-
// Print an initializer list of this type. Returns true if we printed a custom
// representation, false if nothing has been printed and the default
// representation should be used.
@@ -310,6 +301,24 @@ class Node {
#ifndef NDEBUG
DEMANGLE_DUMP_METHOD void dump() const;
#endif
+
+private:
+ friend class OutputBuffer;
+
+ // Print the "left" side of this Node into OutputBuffer.
+ //
+ // Note, should only be called from OutputBuffer implementations.
+ // Call \ref OutputBuffer::printLeft instead.
+ virtual void printLeft(OutputBuffer &) const = 0;
+
+ // Print the "right". This distinction is necessary to represent C++ types
+ // that appear on the RHS of their subtype, such as arrays or functions.
+ // Since most types don't have such a component, provide a default
+ // implementation.
+ //
+ // Note, should only be called from OutputBuffer implementations.
+ // Call \ref OutputBuffer::printRight instead.
+ virtual void printRight(OutputBuffer &) const {}
};
class NodeArray {
@@ -458,11 +467,11 @@ class QualType final : public Node {
}
void printLeft(OutputBuffer &OB) const override {
- Child->printLeft(OB);
+ OB.printLeft(*Child);
printQuals(OB);
}
- void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
+ void printRight(OutputBuffer &OB) const override { OB.printRight(*Child); }
};
class ConversionOperatorType final : public Node {
@@ -491,7 +500,7 @@ class PostfixQualifiedType final : public Node {
template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
void printLeft(OutputBuffer &OB) const override {
- Ty->printLeft(OB);
+ OB.printLeft(*Ty);
OB += Postfix;
}
};
@@ -577,7 +586,7 @@ struct AbiTagAttr : Node {
std::string_view getBaseName() const override { return Base->getBaseName(); }
void printLeft(OutputBuffer &OB) const override {
- Base->printLeft(OB);
+ OB.printLeft(*Base);
OB += "[abi:";
OB += Tag;
OB += "]";
@@ -644,7 +653,7 @@ class PointerType final : public Node {
// We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
if (Pointee->getKind() != KObjCProtoName ||
!static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
- Pointee->printLeft(OB);
+ OB.printLeft(*Pointee);
if (Pointee->hasArray(OB))
OB += " ";
if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
@@ -663,7 +672,7 @@ class PointerType final : public Node {
!static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
OB += ")";
- Pointee->printRight(OB);
+ OB.printRight(*Pointee);
}
}
};
@@ -729,7 +738,7 @@ class ReferenceType : public Node {
std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
if (!Collapsed.second)
return;
- Collapsed.second->printLeft(OB);
+ OB.printLeft(*Collapsed.second);
if (Collapsed.second->hasArray(OB))
OB += " ";
if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
@@ -746,7 +755,7 @@ class ReferenceType : public Node {
return;
if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
OB += ")";
- Collapsed.second->printRight(OB);
+ OB.printRight(*Collapsed.second);
}
};
@@ -766,7 +775,7 @@ class PointerToMemberType final : public Node {
}
void printLeft(OutputBuffer &OB) const override {
- MemberType->printLeft(OB);
+ OB.printLeft(*MemberType);
if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
OB += "(";
else
@@ -778,7 +787,7 @@ class PointerToMemberType final : public Node {
void printRight(OutputBuffer &OB) const override {
if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
OB += ")";
- MemberType->printRight(OB);
+ OB.printRight(*MemberType);
}
};
@@ -798,7 +807,7 @@ class ArrayType final : public Node {
bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
bool hasArraySlow(OutputBuffer &) const override { return true; }
- void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
+ void printLeft(OutputBuffer &OB) const override { OB.printLeft(*Base); }
void printRight(OutputBuffer &OB) const override {
if (OB.back() != ']')
@@ -807,7 +816,7 @@ class ArrayType final : public Node {
if (Dimension)
Dimension->print(OB);
OB += "]";
- Base->printRight(OB);
+ OB.printRight(*Base);
}
bool printInitListAsType(OutputBuffer &OB,
@@ -851,7 +860,7 @@ class FunctionType final : public Node {
// by printing out the return types's left, then print our parameters, then
// finally print right of the return type.
void printLeft(OutputBuffer &OB) const override {
- Ret->printLeft(OB);
+ OB.printLeft(*Ret);
OB += " ";
}
@@ -859,7 +868,7 @@ class FunctionType final : public Node {
OB.printOpen();
Params.printWithComma(OB);
OB.printClose();
- Ret->printRight(OB);
+ OB.printRight(*Ret);
if (CVQuals & QualConst)
OB += " const";
@@ -964,6 +973,8 @@ class FunctionEncoding final : public Node {
FunctionRefQual getRefQual() const { return RefQual; }
NodeArray getParams() const { return Params; }
const Node *getReturnType() const { return Ret; }
+ const Node *getAttrs() const { return Attrs; }
+ const Node *getRequires() const { return Requires; }
bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
bool hasFunctionSlow(OutputBuffer &) const override { return true; }
@@ -972,10 +983,11 @@ class FunctionEncoding final : public Node {
void printLeft(OutputBuffer &OB) const override {
if (Ret) {
- Ret->printLeft(OB);
+ OB.printLeft(*Ret);
if (!Ret->hasRHSComponent(OB))
OB += " ";
}
+
Name->print(OB);
}
@@ -983,8 +995,9 @@ class FunctionEncoding final : public Node {
OB.printOpen();
Params.printWithComma(OB);
OB.printClose();
+
if (Ret)
- Ret->printRight(OB);
+ OB.printRight(*Ret);
if (CVQuals & QualConst)
OB += " const";
@@ -1324,14 +1337,14 @@ class NonTypeTemplateParamDecl final : public Node {
template<typename Fn> void match(Fn F) const { F(Name, Type); }
void printLeft(OutputBuffer &OB) const override {
- Type->printLeft(OB);
+ OB.printLeft(*Type);
if (!Type->hasRHSComponent(OB))
OB += " ";
}
void printRight(OutputBuffer &OB) const override {
Name->print(OB);
- Type->printRight(OB);
+ OB.printRight(*Type);
}
};
@@ -1376,11 +1389,11 @@ class TemplateParamPackDecl final : public Node {
template<typename Fn> void match(Fn F) const { F(Param); }
void printLeft(OutputBuffer &OB) const override {
- Param->printLeft(OB);
+ OB.printLeft(*Param);
OB += "...";
}
- void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
+ void printRight(OutputBuffer &OB) const override { OB.printRight(*Param); }
};
/// An unexpanded parameter pack (either in the expression or type context). If
@@ -1445,13 +1458,13 @@ class ParameterPack final : public Node {
initializePackExpansion(OB);
size_t Idx = OB.CurrentPackIndex;
if (Idx < Data.size())
- Data[Idx]->printLeft(OB);
+ OB.printLeft(*Data[Idx]);
}
void printRight(OutputBuffer &OB) const override {
initializePackExpansion(OB);
size_t Idx = OB.CurrentPackIndex;
if (Idx < Data.size())
- Data[Idx]->printRight(OB);
+ OB.printRight(*Data[Idx]);
}
};
@@ -1609,13 +1622,13 @@ struct ForwardTemplateReference : Node {
if (Printing)
return;
ScopedOverride<bool> SavePrinting(Printing, true);
- Ref->printLeft(OB);
+ OB.printLeft(*Ref);
}
void printRight(OutputBuffer &OB) const override {
if (Printing)
return;
ScopedOverride<bool> SavePrinting(Printing, true);
- Ref->printRight(OB);
+ OB.printRight(*Ref);
}
};
@@ -1767,7 +1780,7 @@ class DtorName : public Node {
void printLeft(OutputBuffer &OB) const override {
OB += "~";
- Base->printLeft(OB);
+ OB.printLeft(*Base);
}
};
@@ -2047,7 +2060,7 @@ class CastExpr : public Node {
{
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "<";
- To->printLeft(OB);
+ OB.printLeft(*To);
OB += ">";
}
OB.printOpen();
@@ -6176,6 +6189,10 @@ struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
Alloc>::AbstractManglingParser;
};
+inline void OutputBuffer::printLeft(const Node &N) { N.printLeft(*this); }
+
+inline void OutputBuffer::printRight(const Node &N) { N.printRight(*this); }
+
DEMANGLE_NAMESPACE_END
#if defined(__clang__)
diff --git a/libcxxabi/src/demangle/Utility.h b/libcxxabi/src/demangle/Utility.h
index f1fad35d60d98..055f02634dabb 100644
--- a/libcxxabi/src/demangle/Utility.h
+++ b/libcxxabi/src/demangle/Utility.h
@@ -27,6 +27,8 @@
DEMANGLE_NAMESPACE_BEGIN
+class Node;
+
// Stream that AST nodes write their string representation into after the AST
// has been parsed.
class OutputBuffer {
@@ -79,10 +81,18 @@ class OutputBuffer {
OutputBuffer(const OutputBuffer &) = delete;
OutputBuffer &operator=(const OutputBuffer &) = delete;
+ virtual ~OutputBuffer() {}
+
operator std::string_view() const {
return std::string_view(Buffer, CurrentPosition);
}
+ /// Called by the demangler when printing the demangle tree. By
+ /// default calls into \c Node::print{Left|Right} but can be overriden
+ /// by clients to track additional state when printing the demangled name.
+ virtual void printLeft(const Node &N);
+ virtual void printRight(const Node &N);
+
/// If a ParameterPackExpansion (or similar type) is encountered, the offset
/// into the pack that we're currently printing.
unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
diff --git a/llvm/include/llvm/Demangle/ItaniumDemangle.h b/llvm/include/llvm/Demangle/ItaniumDemangle.h
index b0363c1a7a786..e5569e75690e1 100644
--- a/llvm/include/llvm/Demangle/ItaniumDemangle.h
+++ b/llvm/include/llvm/Demangle/ItaniumDemangle.h
@@ -281,20 +281,11 @@ class Node {
}
void print(OutputBuffer &OB) const {
- printLeft(OB);
+ OB.printLeft(*this);
if (RHSComponentCache != Cache::No)
- printRight(OB);
+ OB.printRight(*this);
}
- // Print the "left" side of this Node into OutputBuffer.
- virtual void printLeft(OutputBuffer &) const = 0;
-
- // Print the "right". This distinction is necessary to represent C++ types
- // that appear on the RHS of their subtype, such as arrays or functions.
- // Since most types don't have such a component, provide a default
- // implementation.
- virtual void printRight(OutputBuffer &) const {}
-
// Print an initializer list of this type. Returns true if we printed a custom
// representation, false if nothing has been printed and the default
// representation should be used.
@@ -310,6 +301,24 @@ class Node {
#ifndef NDEBUG
DEMANGLE_DUMP_METHOD void dump() const;
#endif
+
+private:
+ friend class OutputBuffer;
+
+ // Print the "left" side of this Node into OutputBuffer.
+ //
+ // Note, should only be called from OutputBuffer implementations.
+ // Call \ref OutputBuffer::printLeft instead.
+ virtual void printLeft(OutputBuffer &) const = 0;
+
+ // Print the "right". This distinction is necessary to represent C++ types
+ // that appear on the RHS of their subtype, such as arrays or functions.
+ // Since most types don't have such a component, provide a default
+ // implementation.
+ //
+ // Note, should only be called from OutputBuffer implementations.
+ // Call \ref OutputBuffer::printRight instead.
+ virtual void printRight(OutputBuffer &) const {}
};
class NodeArray {
@@ -458,11 +467,11 @@ class QualType final : public Node {
}
void printLeft(OutputBuffer &OB) const override {
- Child->printLeft(OB);
+ OB.printLeft(*Child);
printQuals(OB);
}
- void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
+ void printRight(OutputBuffer &OB) const override { OB.printRight(*Child); }
};
class ConversionOperatorType final : public Node {
@@ -491,7 +500,7 @@ class PostfixQualifiedType final : public Node {
template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
void printLeft(OutputBuffer &OB) const override {
- Ty->printLeft(OB);
+ OB.printLeft(*Ty);
OB += Postfix;
}
};
@@ -577,7 +586,7 @@ struct AbiTagAttr : Node {
std::string_view getBaseName() const override { return Base->getBaseName(); }
void printLeft(OutputBuffer &OB) const override {
- Base->printLeft(OB);
+ OB.printLeft(*Base);
OB += "[abi:";
OB += Tag;
OB += "]";
@@ -644,7 +653,7 @@ class PointerType final : public Node {
// We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
if (Pointee->getKind() != KObjCProtoName ||
!static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
- Pointee->printLeft(OB);
+ OB.printLeft(*Pointee);
if (Pointee->hasArray(OB))
OB += " ";
if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
@@ -663,7 +672,7 @@ class PointerType final : public Node {
!static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
OB += ")";
- Pointee->printRight(OB);
+ OB.printRight(*Pointee);
}
}
};
@@ -729,7 +738,7 @@ class ReferenceType : public Node {
std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
if (!Collapsed.second)
return;
- Collapsed.second->printLeft(OB);
+ OB.printLeft(*Collapsed.second);
if (Collapsed.second->hasArray(OB))
OB += " ";
if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
@@ -746,7 +755,7 @@ class ReferenceType : public Node {
return;
if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
OB += ")";
- Collapsed.second->printRight(OB);
+ OB.printRight(*Collapsed.second);
}
};
@@ -766,7 +775,7 @@ class PointerToMemberType final : public Node {
}
void printLeft(OutputBuffer &OB) const override {
- MemberType->printLeft(OB);
+ OB.printLeft(*MemberType);
if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
OB += "(";
else
@@ -778,7 +787,7 @@ class PointerToMemberType final : public Node {
void printRight(OutputBuffer &OB) const override {
if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
OB += ")";
- MemberType->printRight(OB);
+ OB.printRight(*MemberType);
}
};
@@ -798,7 +807,7 @@ class ArrayType final : public Node {
bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
bool hasArraySlow(OutputBuffer &) const override { return true; }
- void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
+ void printLeft(OutputBuffer &OB) const override { OB.printLeft(*Base); }
void printRight(OutputBuffer &OB) const override {
if (OB.back() != ']')
@@ -807,7 +816,7 @@ class ArrayType final : public Node {
if (Dimension)
Dimension->print(OB);
OB += "]";
- Base->printRight(OB);
+ OB.printRight(*Base);
}
bool printInitListAsType(OutputBuffer &OB,
@@ -851,7 +860,7 @@ class FunctionType final : public Node {
// by printing out the return types's left, then print our parameters, then
// finally print right of the return type.
void printLeft(OutputBuffer &OB) const override {
- Ret->printLeft(OB);
+ OB.printLeft(*Ret);
OB += " ";
}
@@ -859,7 +868,7 @@ class FunctionType final : public Node {
OB.printOpen();
Params.printWithComma(OB);
OB.printClose();
- Ret->printRight(OB);
+ OB.printRight(*Ret);
if (CVQuals & QualConst)
OB += " const";
@@ -964,6 +973,8 @@ class FunctionEncoding final : public Node {
FunctionRefQual getRefQual() const { return RefQual; }
NodeArray getParams() const { return Params; }
const Node *getReturnType() const { return Ret; }
+ const Node *getAttrs() const { return Attrs; }
+ const Node *getRequires() const { return Requires; }
bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
bool hasFunctionSlow(OutputBuffer &) const override { return true; }
@@ -972,10 +983,11 @@ class FunctionEncoding final : public Node {
void printLeft(OutputBuffer &OB) const override {
if (Ret) {
- Ret->printLeft(OB);
+ OB.printLeft(*Ret);
if (!Ret->hasRHSComponent(OB))
OB += " ";
}
+
Name->print(OB);
}
@@ -983,8 +995,9 @@ class FunctionEncoding final : public Node {
OB.printOpen();
Params.printWithComma(OB);
OB.printClose();
+
if (Ret)
- Ret->printRight(OB);
+ OB.printRight(*Ret);
if (CVQuals & QualConst)
OB += " const";
@@ -1324,14 +1337,14 @@ class NonTypeTemplateParamDecl final : public Node {
template<typename Fn> void match(Fn F) const { F(Name, Type); }
void printLeft(OutputBuffer &OB) const override {
- Type->printLeft(OB);
+ OB.printLeft(*Type);
if (!Type->hasRHSComponent(OB))
OB += " ";
}
void printRight(OutputBuffer &OB) const override {
Name->print(OB);
- Type->printRight(OB);
+ OB.printRight(*Type);
}
};
@@ -1376,11 +1389,11 @@ class TemplateParamPackDecl final : public Node {
template<typename Fn> void match(Fn F) const { F(Param); }
void printLeft(OutputBuffer &OB) const override {
- Param->printLeft(OB);
+ OB.printLeft(*Param);
OB += "...";
}
- void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
+ void printRight(OutputBuffer &OB) const override { OB.printRight(*Param); }
};
/// An unexpanded parameter pack (either in the expression or type context). If
@@ -1445,13 +1458,13 @@ class ParameterPack final : public Node {
initializePackExpansion(OB);
size_t Idx = OB.CurrentPackIndex;
if (Idx < Data.size())
- Data[Idx]->printLeft(OB);
+ OB.printLeft(*Data[Idx]);
}
void printRight(OutputBuffer &OB) const override {
initializePackExpansion(OB);
size_t Idx = OB.CurrentPackIndex;
if (Idx < Data.size())
- Data[Idx]->printRight(OB);
+ OB.printRight(*Data[Idx]);
}
};
@@ -1609,13 +1622,13 @@ struct ForwardTemplateReference : Node {
if (Printing)
return;
ScopedOverride<bool> SavePrinting(Printing, true);
- Ref->printLeft(OB);
+ OB.printLeft(*Ref);
}
void printRight(OutputBuffer &OB) const override {
if (Printing)
return;
ScopedOverride<bool> SavePrinting(Printing, true);
- Ref->printRight(OB);
+ OB.printRight(*Ref);
}
};
@@ -1767,7 +1780,7 @@ class DtorName : public Node {
void printLeft(OutputBuffer &OB) const override {
OB += "~";
- Base->printLeft(OB);
+ OB.printLeft(*Base);
}
};
@@ -2047,7 +2060,7 @@ class CastExpr : public Node {
{
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "<";
- To->printLeft(OB);
+ OB.printLeft(*To);
OB += ">";
}
OB.printOpen();
@@ -6176,6 +6189,10 @@ struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
Alloc>::AbstractManglingParser;
};
+inline void OutputBuffer::printLeft(const Node &N) { N.printLeft(*this); }
+
+inline void OutputBuffer::printRight(const Node &N) { N.printRight(*this); }
+
DEMANGLE_NAMESPACE_END
#if defined(__clang__)
diff --git a/llvm/include/llvm/Demangle/Utility.h b/llvm/include/llvm/Demangle/Utility.h
index e893cceea2cdc..63cddb3d22a0f 100644
--- a/llvm/include/llvm/Demangle/Utility.h
+++ b/llvm/include/llvm/Demangle/Utility.h
@@ -27,6 +27,8 @@
DEMANGLE_NAMESPACE_BEGIN
+class Node;
+
// Stream that AST nodes write their string representation into after the AST
// has been parsed.
class OutputBuffer {
@@ -79,10 +81,18 @@ class OutputBuffer {
OutputBuffer(const OutputBuffer &) = delete;
OutputBuffer &operator=(const OutputBuffer &) = delete;
+ virtual ~OutputBuffer() {}
+
operator std::string_view() const {
return std::string_view(Buffer, CurrentPosition);
}
+ /// Called by the demangler when printing the demangle tree. By
+ /// default calls into \c Node::print{Left|Right} but can be overriden
+ /// by clients to track additional state when printing the demangled name.
+ virtual void printLeft(const Node &N);
+ virtual void printRight(const Node &N);
+
/// If a ParameterPackExpansion (or similar type) is encountered, the offset
/// into the pack that we're currently printing.
unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
>From 0bd8a9d72f734ef9cb9b9bce6b9280a05d8851d5 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Mon, 7 Apr 2025 13:22:27 +0100
Subject: [PATCH 2/5] [lldb] Implement TrackingOutputBuffer to track demangled
name information
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This patch implements a new `TrackingOutputBuffer` which tracks where the scope/basename/arguments begin and end in the demangled string.
The idea that a function name can be decomposed into <scope, base, arguments>. The assumption is that given the ranges of those three elements and the demangled name, LLDB will be able to to reconstruct the full demangled name. The tracking of those ranges is pretty simple. We don’t ever deal with nesting, so whenever we recurse into a template argument list or another function type, we just stop tracking any positions. Once we recursed out of those, and are back to printing the top-level function name, we continue tracking the positions.
We introduce a new structure `FunctionNameInfo` that holds all this information and is stored in the new `TrackingOutputBuffer` class.
Tests are in `ItaniumDemangleTest.cpp`.
---
lldb/include/lldb/Core/DemangledNameInfo.h | 151 +++++++++++++++
lldb/source/Core/CMakeLists.txt | 1 +
lldb/source/Core/DemangledNameInfo.cpp | 213 +++++++++++++++++++++
lldb/unittests/Core/MangledTest.cpp | 143 ++++++++++++++
4 files changed, 508 insertions(+)
create mode 100644 lldb/include/lldb/Core/DemangledNameInfo.h
create mode 100644 lldb/source/Core/DemangledNameInfo.cpp
diff --git a/lldb/include/lldb/Core/DemangledNameInfo.h b/lldb/include/lldb/Core/DemangledNameInfo.h
new file mode 100644
index 0000000000000..8acb25518cdd3
--- /dev/null
+++ b/lldb/include/lldb/Core/DemangledNameInfo.h
@@ -0,0 +1,151 @@
+//===-- DemangledNameInfo.h -------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_CORE_DEMANGLEDNAMEINFO_H
+#define LLDB_CORE_DEMANGLEDNAMEINFO_H
+
+#include "llvm/Demangle/ItaniumDemangle.h"
+#include "llvm/Demangle/Utility.h"
+
+#include <cstddef>
+#include <utility>
+
+namespace lldb_private {
+
+/// Stores information about where certain portions of a demangled
+/// function name begin and end.
+struct DemangledNameInfo {
+ /// A [start, end) pair for the function basename.
+ /// The basename is the name without scope qualifiers
+ /// and without template parameters. E.g.,
+ /// \code{.cpp}
+ /// void foo::bar<int>::someFunc<float>(int) const &&
+ /// ^ ^
+ /// start end
+ /// \endcode
+ std::pair<size_t, size_t> BasenameRange;
+
+ /// A [start, end) pair for the function scope qualifiers.
+ /// E.g., for
+ /// \code{.cpp}
+ /// void foo::bar<int>::qux<float>(int) const &&
+ /// ^ ^
+ /// start end
+ /// \endcode
+ std::pair<size_t, size_t> ScopeRange;
+
+ /// Indicates the [start, end) of the function argument lits.
+ /// E.g.,
+ /// \code{.cpp}
+ /// int (*getFunc<float>(float, double))(int, int)
+ /// ^ ^
+ /// start end
+ /// \endcode
+ std::pair<size_t, size_t> ArgumentsRange;
+
+ /// Returns \c true if this object holds a valid basename range.
+ bool hasBasename() const {
+ return BasenameRange.first != BasenameRange.second &&
+ BasenameRange.second > 0;
+ }
+
+ friend bool operator==(const DemangledNameInfo &lhs,
+ const DemangledNameInfo &rhs) {
+ return std::tie(lhs.BasenameRange, lhs.ArgumentsRange, lhs.ScopeRange) ==
+ std::tie(rhs.BasenameRange, rhs.ArgumentsRange, rhs.ScopeRange);
+ }
+
+ friend bool operator!=(const DemangledNameInfo &lhs,
+ const DemangledNameInfo &rhs) {
+ return !(lhs == rhs);
+ }
+};
+
+/// An OutputBuffer which keeps a record of where certain parts of a
+/// demangled name begin/end (e.g., basename, scope, argument list, etc.).
+/// The tracking occurs during printing of the Itanium demangle tree.
+///
+/// Usage:
+/// \code{.cpp}
+///
+/// Node *N = mangling_parser.parseType();
+///
+/// TrackingOutputBuffer buffer;
+/// N->printLeft(OB);
+///
+/// assert (buffer.NameInfo.hasBasename());
+///
+/// \endcode
+struct TrackingOutputBuffer : public llvm::itanium_demangle::OutputBuffer {
+ using OutputBuffer::OutputBuffer;
+
+ /// Holds information about the demangled name that is
+ /// being printed into this buffer.
+ DemangledNameInfo NameInfo;
+
+ void printLeft(const llvm::itanium_demangle::Node &N) override;
+ void printRight(const llvm::itanium_demangle::Node &N) override;
+
+private:
+ void printLeftImpl(const llvm::itanium_demangle::FunctionType &N);
+ void printRightImpl(const llvm::itanium_demangle::FunctionType &N);
+
+ void printLeftImpl(const llvm::itanium_demangle::FunctionEncoding &N);
+ void printRightImpl(const llvm::itanium_demangle::FunctionEncoding &N);
+
+ void printLeftImpl(const llvm::itanium_demangle::NestedName &N);
+ void printLeftImpl(const llvm::itanium_demangle::NameWithTemplateArgs &N);
+
+ /// Called whenever we start printing a function type in the Itanium
+ /// mangling scheme. Examples include \ref FunctionEncoding, \ref
+ /// FunctionType, etc.
+ ///
+ /// \returns A ScopedOverride which will update the nesting depth of
+ /// currently printed function types on destruction.
+ [[nodiscard]] llvm::itanium_demangle::ScopedOverride<unsigned>
+ enterFunctionTypePrinting();
+
+ /// Returns \c true if we're not printing any nested function types,
+ /// just a \ref FunctionEncoding in the Itanium mangling scheme.
+ bool isPrintingTopLevelFunctionType() const;
+
+ /// If this object \ref shouldTrack, then update the end of
+ /// the basename range to the current \c OB position.
+ void updateBasenameEnd();
+
+ /// If this object \ref shouldTrack, then update the beginning
+ /// of the scope range to the current \c OB position.
+ void updateScopeStart();
+
+ /// If this object \ref shouldTrack, then update the end of
+ /// the scope range to the current \c OB position.
+ void updateScopeEnd();
+
+ /// Returns \c true if the members of this object can be
+ /// updated. E.g., when we're printing nested template
+ /// arguments, we don't need to be tracking basename
+ /// locations.
+ bool shouldTrack() const;
+
+ /// Helpers alled to track beginning and end of the function
+ /// arguments.
+ void finalizeArgumentEnd();
+ void finalizeStart();
+ void finalizeEnd();
+
+ /// Helper used in the finalize APIs.
+ bool canFinalize() const;
+
+ /// Incremented each time we start printing a function type node
+ /// in the Itanium mangling scheme (e.g., \ref FunctionEncoding
+ /// or \ref FunctionType).
+ unsigned FunctionPrintingDepth = 0;
+};
+} // namespace lldb_private
+
+#endif // LLDB_CORE_DEMANGLEDNAMEINFO_H
diff --git a/lldb/source/Core/CMakeLists.txt b/lldb/source/Core/CMakeLists.txt
index 0a08da0fec230..13bd765cbcafa 100644
--- a/lldb/source/Core/CMakeLists.txt
+++ b/lldb/source/Core/CMakeLists.txt
@@ -28,6 +28,7 @@ add_lldb_library(lldbCore
Debugger.cpp
DebuggerEvents.cpp
Declaration.cpp
+ DemangledNameInfo.cpp
Disassembler.cpp
DumpDataExtractor.cpp
DumpRegisterValue.cpp
diff --git a/lldb/source/Core/DemangledNameInfo.cpp b/lldb/source/Core/DemangledNameInfo.cpp
new file mode 100644
index 0000000000000..89757032409c2
--- /dev/null
+++ b/lldb/source/Core/DemangledNameInfo.cpp
@@ -0,0 +1,213 @@
+//===-- DemangledNameInfo.cpp ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Core/DemangledNameInfo.h"
+
+using namespace llvm::itanium_demangle;
+
+namespace lldb_private {
+
+bool TrackingOutputBuffer::shouldTrack() const {
+ if (!isPrintingTopLevelFunctionType())
+ return false;
+
+ if (isGtInsideTemplateArgs())
+ return false;
+
+ if (NameInfo.ArgumentsRange.first > 0)
+ return false;
+
+ return true;
+}
+
+bool TrackingOutputBuffer::canFinalize() const {
+ if (!isPrintingTopLevelFunctionType())
+ return false;
+
+ if (isGtInsideTemplateArgs())
+ return false;
+
+ if (NameInfo.ArgumentsRange.first == 0)
+ return false;
+
+ return true;
+}
+
+void TrackingOutputBuffer::updateBasenameEnd() {
+ if (!shouldTrack())
+ return;
+
+ NameInfo.BasenameRange.second = getCurrentPosition();
+}
+
+void TrackingOutputBuffer::updateScopeStart() {
+ if (!shouldTrack())
+ return;
+
+ NameInfo.ScopeRange.first = getCurrentPosition();
+}
+
+void TrackingOutputBuffer::updateScopeEnd() {
+ if (!shouldTrack())
+ return;
+
+ NameInfo.ScopeRange.second = getCurrentPosition();
+}
+
+void TrackingOutputBuffer::finalizeArgumentEnd() {
+ if (!canFinalize())
+ return;
+
+ NameInfo.ArgumentsRange.second = getCurrentPosition();
+}
+
+void TrackingOutputBuffer::finalizeStart() {
+ if (!shouldTrack())
+ return;
+
+ NameInfo.ArgumentsRange.first = getCurrentPosition();
+
+ // If nothing has set the end of the basename yet (for example when
+ // printing templates), then the beginning of the arguments is the end of
+ // the basename.
+ if (NameInfo.BasenameRange.second == 0)
+ NameInfo.BasenameRange.second = getCurrentPosition();
+
+ assert(!shouldTrack());
+ assert(canFinalize());
+}
+
+void TrackingOutputBuffer::finalizeEnd() {
+ if (!canFinalize())
+ return;
+
+ if (NameInfo.ScopeRange.first > NameInfo.ScopeRange.second)
+ NameInfo.ScopeRange.second = NameInfo.ScopeRange.first;
+ NameInfo.BasenameRange.first = NameInfo.ScopeRange.second;
+}
+
+ScopedOverride<unsigned> TrackingOutputBuffer::enterFunctionTypePrinting() {
+ return {FunctionPrintingDepth, FunctionPrintingDepth + 1};
+}
+
+bool TrackingOutputBuffer::isPrintingTopLevelFunctionType() const {
+ return FunctionPrintingDepth == 1;
+}
+
+void TrackingOutputBuffer::printLeft(const Node &N) {
+ switch (N.getKind()) {
+ case Node::KFunctionType:
+ printLeftImpl(static_cast<const FunctionType &>(N));
+ break;
+ case Node::KFunctionEncoding:
+ printLeftImpl(static_cast<const FunctionEncoding &>(N));
+ break;
+ case Node::KNestedName:
+ printLeftImpl(static_cast<const NestedName &>(N));
+ break;
+ case Node::KNameWithTemplateArgs:
+ printLeftImpl(static_cast<const NameWithTemplateArgs &>(N));
+ break;
+ default:
+ OutputBuffer::printLeft(N);
+ }
+}
+
+void TrackingOutputBuffer::printRight(const Node &N) {
+ switch (N.getKind()) {
+ case Node::KFunctionType:
+ printRightImpl(static_cast<const FunctionType &>(N));
+ break;
+ case Node::KFunctionEncoding:
+ printRightImpl(static_cast<const FunctionEncoding &>(N));
+ break;
+ default:
+ OutputBuffer::printRight(N);
+ }
+}
+
+void TrackingOutputBuffer::printLeftImpl(const FunctionType &N) {
+ auto Scoped = enterFunctionTypePrinting();
+ OutputBuffer::printLeft(N);
+}
+
+void TrackingOutputBuffer::printRightImpl(const FunctionType &N) {
+ auto Scoped = enterFunctionTypePrinting();
+ OutputBuffer::printRight(N);
+}
+
+void TrackingOutputBuffer::printLeftImpl(const FunctionEncoding &N) {
+ auto Scoped = enterFunctionTypePrinting();
+
+ const Node *Ret = N.getReturnType();
+ if (Ret) {
+ printLeft(*Ret);
+ if (!Ret->hasRHSComponent(*this))
+ *this += " ";
+ }
+
+ updateScopeStart();
+
+ N.getName()->print(*this);
+}
+
+void TrackingOutputBuffer::printRightImpl(const FunctionEncoding &N) {
+ auto Scoped = enterFunctionTypePrinting();
+ finalizeStart();
+
+ printOpen();
+ N.getParams().printWithComma(*this);
+ printClose();
+
+ finalizeArgumentEnd();
+
+ const Node *Ret = N.getReturnType();
+
+ if (Ret)
+ printRight(*Ret);
+
+ auto CVQuals = N.getCVQuals();
+ auto RefQual = N.getRefQual();
+ auto *Attrs = N.getAttrs();
+ auto *Requires = N.getRequires();
+
+ if (CVQuals & QualConst)
+ *this += " const";
+ if (CVQuals & QualVolatile)
+ *this += " volatile";
+ if (CVQuals & QualRestrict)
+ *this += " restrict";
+ if (RefQual == FrefQualLValue)
+ *this += " &";
+ else if (RefQual == FrefQualRValue)
+ *this += " &&";
+ if (Attrs != nullptr)
+ Attrs->print(*this);
+ if (Requires != nullptr) {
+ *this += " requires ";
+ Requires->print(*this);
+ }
+
+ finalizeEnd();
+}
+
+void TrackingOutputBuffer::printLeftImpl(const NestedName &N) {
+ N.Qual->print(*this);
+ *this += "::";
+ updateScopeEnd();
+ N.Name->print(*this);
+ updateBasenameEnd();
+}
+
+void TrackingOutputBuffer::printLeftImpl(const NameWithTemplateArgs &N) {
+ N.Name->print(*this);
+ updateBasenameEnd();
+ N.TemplateArgs->print(*this);
+}
+
+} // namespace lldb_private
diff --git a/lldb/unittests/Core/MangledTest.cpp b/lldb/unittests/Core/MangledTest.cpp
index a3760ba43b3c9..b039299d032dd 100644
--- a/lldb/unittests/Core/MangledTest.cpp
+++ b/lldb/unittests/Core/MangledTest.cpp
@@ -11,6 +11,7 @@
#include "TestingSupport/SubsystemRAII.h"
#include "TestingSupport/TestUtilities.h"
+#include "lldb/Core/DemangledNameInfo.h"
#include "lldb/Core/Mangled.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
@@ -319,3 +320,145 @@ TEST(MangledTest, NameIndexes_FindFunctionSymbols) {
EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeBase));
EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeMethod));
}
+
+struct DemanglingPartsTestCase {
+ const char *mangled;
+ DemangledNameInfo expected_info;
+ std::string_view basename;
+ std::string_view scope;
+ bool valid_basename = true;
+};
+
+DemanglingPartsTestCase g_demangling_parts_test_cases[] = {
+ // clang-format off
+ { "_ZNVKO3BarIN2ns3QuxIiEEE1CIPFi3FooIS_IiES6_EEE6methodIS6_EENS5_IT_SC_E5InnerIiEESD_SD_",
+ { .BasenameRange = {92, 98}, .ScopeRange = {36, 92}, .ArgumentsRange = { 108, 158 } },
+ .basename = "method",
+ .scope = "Bar<ns::Qux<int>>::C<int (*)(Foo<Bar<int>, Bar<int>>)>::"
+ },
+ { "_Z7getFuncIfEPFiiiET_",
+ { .BasenameRange = {6, 13}, .ScopeRange = {6, 6}, .ArgumentsRange = { 20, 27 } },
+ .basename = "getFunc",
+ .scope = ""
+ },
+ { "_ZN1f1b1c1gEv",
+ { .BasenameRange = {9, 10}, .ScopeRange = {0, 9}, .ArgumentsRange = { 10, 12 } },
+ .basename = "g",
+ .scope = "f::b::c::"
+ },
+ { "_ZN5test73fD1IiEEDTcmtlNS_1DEL_ZNS_1bEEEcvT__EES2_",
+ { .BasenameRange = {45, 48}, .ScopeRange = {38, 45}, .ArgumentsRange = { 53, 58 } },
+ .basename = "fD1",
+ .scope = "test7::"
+ },
+ { "_ZN5test73fD1IiEEDTcmtlNS_1DEL_ZNS_1bINDT1cE1dEEEEEcvT__EES2_",
+ { .BasenameRange = {61, 64}, .ScopeRange = {54, 61}, .ArgumentsRange = { 69, 79 } },
+ .basename = "fD1",
+ .scope = "test7::"
+ },
+ { "_ZN5test7INDT1cE1dINDT1cE1dEEEE3fD1INDT1cE1dINDT1cE1dEEEEEDTcmtlNS_1DEL_ZNS_1bINDT1cE1dEEEEEcvT__EES2_",
+ { .BasenameRange = {120, 123}, .ScopeRange = {81, 120}, .ArgumentsRange = { 155, 168 } },
+ .basename = "fD1",
+ .scope = "test7<decltype(c)::d<decltype(c)::d>>::"
+ },
+ { "_ZN8nlohmann16json_abi_v3_11_310basic_jsonINSt3__13mapENS2_6vectorENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEbxydS8_NS0_14adl_serializerENS4_IhNS8_IhEEEEvE5parseIRA29_KcEESE_OT_NS2_8functionIFbiNS0_6detail13parse_event_tERSE_EEEbb",
+ { .BasenameRange = {687, 692}, .ScopeRange = {343, 687}, .ArgumentsRange = { 713, 1174 } },
+ .basename = "parse",
+ .scope = "nlohmann::json_abi_v3_11_3::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::json_abi_v3_11_3::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char>>, void>::"
+ },
+ { "_ZN8nlohmann16json_abi_v3_11_310basic_jsonINSt3__13mapENS2_6vectorENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEbxydS8_NS0_14adl_serializerENS4_IhNS8_IhEEEEvEC1EDn",
+ { .BasenameRange = {344, 354}, .ScopeRange = {0, 344}, .ArgumentsRange = { 354, 370 } },
+ .basename = "basic_json",
+ .scope = "nlohmann::json_abi_v3_11_3::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::json_abi_v3_11_3::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char>>, void>::"
+ },
+ { "_Z3fppIiEPFPFvvEiEf",
+ { .BasenameRange = {10, 13}, .ScopeRange = {10, 10}, .ArgumentsRange = { 18, 25 } },
+ .basename = "fpp",
+ .scope = ""
+ },
+ { "_Z3fppIiEPFPFvvEN2ns3FooIiEEEf",
+ { .BasenameRange = {10, 13}, .ScopeRange = {10, 10}, .ArgumentsRange = { 18, 25 } },
+ .basename = "fpp",
+ .scope = ""
+ },
+ { "_Z3fppIiEPFPFvPFN2ns3FooIiEENS2_3BarIfE3QuxEEEPFS2_S2_EEf",
+ { .BasenameRange = {10, 13}, .ScopeRange = {10, 10}, .ArgumentsRange = { 18, 25 } },
+ .basename = "fpp",
+ .scope = ""
+ },
+ { "_ZN2ns8HasFuncsINS_3FooINS1_IiE3BarIfE3QuxEEEE3fppIiEEPFPFvvEiEf",
+ { .BasenameRange = {64, 67}, .ScopeRange = {10, 64}, .ArgumentsRange = { 72, 79 } },
+ .basename = "fpp",
+ .scope = "ns::HasFuncs<ns::Foo<ns::Foo<int>::Bar<float>::Qux>>::"
+ },
+ { "_ZN2ns8HasFuncsINS_3FooINS1_IiE3BarIfE3QuxEEEE3fppIiEEPFPFvvES2_Ef",
+ { .BasenameRange = {64, 67}, .ScopeRange = {10, 64}, .ArgumentsRange = { 72, 79 } },
+ .basename = "fpp",
+ .scope = "ns::HasFuncs<ns::Foo<ns::Foo<int>::Bar<float>::Qux>>::"
+ },
+ { "_ZN2ns8HasFuncsINS_3FooINS1_IiE3BarIfE3QuxEEEE3fppIiEEPFPFvPFS2_S5_EEPFS2_S2_EEf",
+ { .BasenameRange = {64, 67}, .ScopeRange = {10, 64}, .ArgumentsRange = { 72, 79 } },
+ .basename = "fpp",
+ .scope = "ns::HasFuncs<ns::Foo<ns::Foo<int>::Bar<float>::Qux>>::"
+ },
+ { "_ZTV11ImageLoader",
+ { .BasenameRange = {0, 0}, .ScopeRange = {0, 0}, .ArgumentsRange = { 0, 0 } },
+ .basename = "",
+ .scope = "",
+ .valid_basename = false
+ }
+ // clang-format on
+};
+
+struct DemanglingPartsTestFixture
+ : public ::testing::TestWithParam<DemanglingPartsTestCase> {};
+
+namespace {
+class TestAllocator {
+ llvm::BumpPtrAllocator Alloc;
+
+public:
+ void reset() { Alloc.Reset(); }
+
+ template <typename T, typename... Args> T *makeNode(Args &&...args) {
+ return new (Alloc.Allocate(sizeof(T), alignof(T)))
+ T(std::forward<Args>(args)...);
+ }
+
+ void *allocateNodeArray(size_t sz) {
+ return Alloc.Allocate(sizeof(llvm::itanium_demangle::Node *) * sz,
+ alignof(llvm::itanium_demangle::Node *));
+ }
+};
+} // namespace
+
+TEST_P(DemanglingPartsTestFixture, DemanglingParts) {
+ const auto &[mangled, info, basename, scope, valid_basename] = GetParam();
+
+ llvm::itanium_demangle::ManglingParser<TestAllocator> Parser(
+ mangled, mangled + ::strlen(mangled));
+
+ const auto *Root = Parser.parse();
+
+ ASSERT_NE(nullptr, Root);
+
+ TrackingOutputBuffer OB;
+ Root->print(OB);
+ auto demangled = std::string_view(OB);
+
+ ASSERT_EQ(OB.NameInfo.hasBasename(), valid_basename);
+
+ EXPECT_EQ(OB.NameInfo.BasenameRange, info.BasenameRange);
+ EXPECT_EQ(OB.NameInfo.ScopeRange, info.ScopeRange);
+ EXPECT_EQ(OB.NameInfo.ArgumentsRange, info.ArgumentsRange);
+
+ auto get_part = [&](const std::pair<size_t, size_t> &loc) {
+ return demangled.substr(loc.first, loc.second - loc.first);
+ };
+
+ EXPECT_EQ(get_part(OB.NameInfo.BasenameRange), basename);
+ EXPECT_EQ(get_part(OB.NameInfo.ScopeRange), scope);
+}
+
+INSTANTIATE_TEST_SUITE_P(DemanglingPartsTests, DemanglingPartsTestFixture,
+ ::testing::ValuesIn(g_demangling_parts_test_cases));
>From a139c23f1e46094f95fffe06b3aa6633988f4286 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Mon, 10 Mar 2025 15:39:10 +0000
Subject: [PATCH 3/5] [lldb][Mangled] Add API to force re-demangling a Mangled
object
Add version of GetDemangledName that will force re-demangling. This is required because LLDB will SetDemangledName without going through the demangler. So we need a way to force demangling to set the m_demangled_info member when we need it.
---
lldb/include/lldb/Core/Mangled.h | 6 ++++++
lldb/source/Core/Mangled.cpp | 11 ++++++++---
2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/lldb/include/lldb/Core/Mangled.h b/lldb/include/lldb/Core/Mangled.h
index 5988d919a89b8..43707f2b9d26a 100644
--- a/lldb/include/lldb/Core/Mangled.h
+++ b/lldb/include/lldb/Core/Mangled.h
@@ -276,6 +276,12 @@ class Mangled {
void Encode(DataEncoder &encoder, ConstStringTable &strtab) const;
private:
+ /// If \c force is \c false, this function will re-use the previously
+ /// demangled name (if any). If \c force is \c true (or the mangled name
+ /// on this object was not previously demangled), demangle and cache the
+ /// name.
+ ConstString GetDemangledNameImpl(bool force) const;
+
/// The mangled version of the name.
ConstString m_mangled;
diff --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp
index ddaaedea04183..db5aff40d34d3 100644
--- a/lldb/source/Core/Mangled.cpp
+++ b/lldb/source/Core/Mangled.cpp
@@ -265,19 +265,24 @@ bool Mangled::GetRichManglingInfo(RichManglingContext &context,
llvm_unreachable("Fully covered switch above!");
}
+ConstString Mangled::GetDemangledName() const {
+ return GetDemangledNameImpl(/*force=*/false);
+}
+
// Generate the demangled name on demand using this accessor. Code in this
// class will need to use this accessor if it wishes to decode the demangled
// name. The result is cached and will be kept until a new string value is
// supplied to this object, or until the end of the object's lifetime.
-ConstString Mangled::GetDemangledName() const {
+ConstString Mangled::GetDemangledNameImpl(bool force) const {
if (!m_mangled)
return m_demangled;
// Re-use previously demangled names.
- if (!m_demangled.IsNull())
+ if (!force && !m_demangled.IsNull())
return m_demangled;
- if (m_mangled.GetMangledCounterpart(m_demangled) && !m_demangled.IsNull())
+ if (!force && m_mangled.GetMangledCounterpart(m_demangled) &&
+ !m_demangled.IsNull())
return m_demangled;
// We didn't already mangle this name, demangle it and if all goes well
>From 834f7762e76e710b6d5c8eb3f1f3aa8c4967eed7 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Mon, 7 Apr 2025 13:54:27 +0100
Subject: [PATCH 4/5] [lldb][Mangled] Retrieve and cache demangled name info
Uses the `TrackingOutputBuffer` to populate the new member `Mangled::m_demangled_info`.
`m_demangled_info` is lazily popluated by `GetDemangledInfo`. To ensure `m_demangled` and `m_demangled_info` are in-sync we clear `m_demangled_info` anytime `m_demangled` is set/cleared.
TODO: test
---
lldb/include/lldb/Core/DemangledNameInfo.h | 15 +-
lldb/include/lldb/Core/Mangled.h | 20 ++-
lldb/source/Core/DemangledNameInfo.cpp | 17 +++
lldb/source/Core/Mangled.cpp | 41 ++++-
lldb/unittests/Core/MangledTest.cpp | 170 +++++++++++++++++----
llvm/include/llvm/Demangle/Demangle.h | 7 +
llvm/lib/Demangle/ItaniumDemangle.cpp | 17 ++-
7 files changed, 243 insertions(+), 44 deletions(-)
diff --git a/lldb/include/lldb/Core/DemangledNameInfo.h b/lldb/include/lldb/Core/DemangledNameInfo.h
index 8acb25518cdd3..173c73ec32531 100644
--- a/lldb/include/lldb/Core/DemangledNameInfo.h
+++ b/lldb/include/lldb/Core/DemangledNameInfo.h
@@ -48,9 +48,20 @@ struct DemangledNameInfo {
/// \endcode
std::pair<size_t, size_t> ArgumentsRange;
+ /// Indicates the [start, end) of the function qualifiers
+ /// (e.g., CV-qualifiers, reference qualifiers, requires clauses).
+ ///
+ /// E.g.,
+ /// \code{.cpp}
+ /// void foo::bar<int>::qux<float>(int) const &&
+ /// ^ ^
+ /// start end
+ /// \endcode
+ std::pair<size_t, size_t> QualifiersRange;
+
/// Returns \c true if this object holds a valid basename range.
bool hasBasename() const {
- return BasenameRange.first != BasenameRange.second &&
+ return BasenameRange.second > BasenameRange.first &&
BasenameRange.second > 0;
}
@@ -137,6 +148,8 @@ struct TrackingOutputBuffer : public llvm::itanium_demangle::OutputBuffer {
void finalizeArgumentEnd();
void finalizeStart();
void finalizeEnd();
+ void finalizeQualifiersStart();
+ void finalizeQualifiersEnd();
/// Helper used in the finalize APIs.
bool canFinalize() const;
diff --git a/lldb/include/lldb/Core/Mangled.h b/lldb/include/lldb/Core/Mangled.h
index 43707f2b9d26a..6f4ed741e8c5e 100644
--- a/lldb/include/lldb/Core/Mangled.h
+++ b/lldb/include/lldb/Core/Mangled.h
@@ -9,10 +9,11 @@
#ifndef LLDB_CORE_MANGLED_H
#define LLDB_CORE_MANGLED_H
+#include "lldb/Core/DemangledNameInfo.h"
+#include "lldb/Utility/ConstString.h"
#include "lldb/lldb-enumerations.h"
#include "lldb/lldb-forward.h"
#include "lldb/lldb-types.h"
-#include "lldb/Utility/ConstString.h"
#include "llvm/ADT/StringRef.h"
#include <cstddef>
@@ -134,9 +135,15 @@ class Mangled {
/// A const reference to the display demangled name string object.
ConstString GetDisplayDemangledName() const;
- void SetDemangledName(ConstString name) { m_demangled = name; }
+ void SetDemangledName(ConstString name) {
+ m_demangled = name;
+ m_demangled_info.reset();
+ }
- void SetMangledName(ConstString name) { m_mangled = name; }
+ void SetMangledName(ConstString name) {
+ m_mangled = name;
+ m_demangled_info.reset();
+ }
/// Mangled name get accessor.
///
@@ -275,6 +282,9 @@ class Mangled {
/// table offsets in the cache data.
void Encode(DataEncoder &encoder, ConstStringTable &strtab) const;
+ /// Retrieve \c DemangledNameInfo of the demangled name held by this object.
+ const std::optional<DemangledNameInfo> &GetDemangledInfo() const;
+
private:
/// If \c force is \c false, this function will re-use the previously
/// demangled name (if any). If \c force is \c true (or the mangled name
@@ -288,6 +298,10 @@ class Mangled {
/// Mutable so we can get it on demand with
/// a const version of this object.
mutable ConstString m_demangled;
+
+ /// If available, holds information about where in \c m_demangled certain
+ /// parts of the name (e.g., basename, arguments, etc.) begin and end.
+ mutable std::optional<DemangledNameInfo> m_demangled_info = std::nullopt;
};
Stream &operator<<(Stream &s, const Mangled &obj);
diff --git a/lldb/source/Core/DemangledNameInfo.cpp b/lldb/source/Core/DemangledNameInfo.cpp
index 89757032409c2..54a06edc5ec1d 100644
--- a/lldb/source/Core/DemangledNameInfo.cpp
+++ b/lldb/source/Core/DemangledNameInfo.cpp
@@ -66,6 +66,20 @@ void TrackingOutputBuffer::finalizeArgumentEnd() {
NameInfo.ArgumentsRange.second = getCurrentPosition();
}
+void TrackingOutputBuffer::finalizeQualifiersStart() {
+ if (!canFinalize())
+ return;
+
+ NameInfo.QualifiersRange.first = getCurrentPosition();
+}
+
+void TrackingOutputBuffer::finalizeQualifiersEnd() {
+ if (!canFinalize())
+ return;
+
+ NameInfo.QualifiersRange.second = getCurrentPosition();
+}
+
void TrackingOutputBuffer::finalizeStart() {
if (!shouldTrack())
return;
@@ -171,6 +185,8 @@ void TrackingOutputBuffer::printRightImpl(const FunctionEncoding &N) {
if (Ret)
printRight(*Ret);
+ finalizeQualifiersStart();
+
auto CVQuals = N.getCVQuals();
auto RefQual = N.getRefQual();
auto *Attrs = N.getAttrs();
@@ -193,6 +209,7 @@ void TrackingOutputBuffer::printRightImpl(const FunctionEncoding &N) {
Requires->print(*this);
}
+ finalizeQualifiersEnd();
finalizeEnd();
}
diff --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp
index db5aff40d34d3..f6766950f777d 100644
--- a/lldb/source/Core/Mangled.cpp
+++ b/lldb/source/Core/Mangled.cpp
@@ -9,6 +9,7 @@
#include "lldb/Core/Mangled.h"
#include "lldb/Core/DataFileCache.h"
+#include "lldb/Core/DemangledNameInfo.h"
#include "lldb/Core/RichManglingContext.h"
#include "lldb/Target/Language.h"
#include "lldb/Utility/ConstString.h"
@@ -111,6 +112,7 @@ Mangled::operator bool() const { return m_mangled || m_demangled; }
void Mangled::Clear() {
m_mangled.Clear();
m_demangled.Clear();
+ m_demangled_info.reset();
}
// Compare the string values.
@@ -124,13 +126,16 @@ void Mangled::SetValue(ConstString name) {
if (cstring_is_mangled(name.GetStringRef())) {
m_demangled.Clear();
m_mangled = name;
+ m_demangled_info.reset();
} else {
m_demangled = name;
m_mangled.Clear();
+ m_demangled_info.reset();
}
} else {
m_demangled.Clear();
m_mangled.Clear();
+ m_demangled_info.reset();
}
}
@@ -152,20 +157,26 @@ static char *GetMSVCDemangledStr(llvm::StringRef M) {
return demangled_cstr;
}
-static char *GetItaniumDemangledStr(const char *M) {
+static std::pair<char *, DemangledNameInfo>
+GetItaniumDemangledStr(const char *M) {
char *demangled_cstr = nullptr;
+ DemangledNameInfo info;
llvm::ItaniumPartialDemangler ipd;
bool err = ipd.partialDemangle(M);
if (!err) {
- // Default buffer and size (will realloc in case it's too small).
+ // Default buffer and size (OutputBuffer will realloc in case it's too
+ // small).
size_t demangled_size = 80;
- demangled_cstr = static_cast<char *>(std::malloc(demangled_size));
- demangled_cstr = ipd.finishDemangle(demangled_cstr, &demangled_size);
+ demangled_cstr = static_cast<char *>(std::malloc(80));
+
+ TrackingOutputBuffer OB(demangled_cstr, demangled_size);
+ demangled_cstr = ipd.finishDemangle(&OB);
+ info = std::move(OB.NameInfo);
assert(demangled_cstr &&
"finishDemangle must always succeed if partialDemangle did");
- assert(demangled_cstr[demangled_size - 1] == '\0' &&
+ assert(demangled_cstr[OB.getCurrentPosition() - 1] == '\0' &&
"Expected demangled_size to return length including trailing null");
}
@@ -174,9 +185,14 @@ static char *GetItaniumDemangledStr(const char *M) {
LLDB_LOGF(log, "demangled itanium: %s -> \"%s\"", M, demangled_cstr);
else
LLDB_LOGF(log, "demangled itanium: %s -> error: failed to demangle", M);
+
+ if (!info.hasBasename())
+ LLDB_LOGF(log,
+ "demangled itanium: %s -> error: failed to retrieve name info",
+ M);
}
- return demangled_cstr;
+ return {demangled_cstr, std::move(info)};
}
static char *GetRustV0DemangledStr(llvm::StringRef M) {
@@ -269,6 +285,13 @@ ConstString Mangled::GetDemangledName() const {
return GetDemangledNameImpl(/*force=*/false);
}
+std::optional<DemangledNameInfo> const &Mangled::GetDemangledInfo() const {
+ if (!m_demangled_info)
+ GetDemangledNameImpl(/*force=*/true);
+
+ return m_demangled_info;
+}
+
// Generate the demangled name on demand using this accessor. Code in this
// class will need to use this accessor if it wishes to decode the demangled
// name. The result is cached and will be kept until a new string value is
@@ -293,7 +316,10 @@ ConstString Mangled::GetDemangledNameImpl(bool force) const {
demangled_name = GetMSVCDemangledStr(m_mangled);
break;
case eManglingSchemeItanium: {
- demangled_name = GetItaniumDemangledStr(m_mangled.GetCString());
+ std::pair<char *, DemangledNameInfo> demangled =
+ GetItaniumDemangledStr(m_mangled.GetCString());
+ demangled_name = demangled.first;
+ m_demangled_info.emplace(std::move(demangled.second));
break;
}
case eManglingSchemeRustV0:
@@ -452,6 +478,7 @@ bool Mangled::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
const StringTableReader &strtab) {
m_mangled.Clear();
m_demangled.Clear();
+ m_demangled_info.reset();
MangledEncoding encoding = (MangledEncoding)data.GetU8(offset_ptr);
switch (encoding) {
case Empty:
diff --git a/lldb/unittests/Core/MangledTest.cpp b/lldb/unittests/Core/MangledTest.cpp
index b039299d032dd..e903cadd410f3 100644
--- a/lldb/unittests/Core/MangledTest.cpp
+++ b/lldb/unittests/Core/MangledTest.cpp
@@ -321,90 +321,197 @@ TEST(MangledTest, NameIndexes_FindFunctionSymbols) {
EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeMethod));
}
+TEST(MangledTest, DemangledNameInfo_SetMangledResets) {
+ Mangled mangled;
+ EXPECT_EQ(mangled.GetDemangledInfo(), std::nullopt);
+
+ mangled.SetMangledName(ConstString("_Z3foov"));
+ ASSERT_TRUE(mangled);
+
+ auto info1 = mangled.GetDemangledInfo();
+ EXPECT_NE(info1, std::nullopt);
+ EXPECT_TRUE(info1->hasBasename());
+
+ mangled.SetMangledName(ConstString("_Z4funcv"));
+
+ // Should have re-calculated demangled-info since mangled name changed.
+ auto info2 = mangled.GetDemangledInfo();
+ ASSERT_NE(info2, std::nullopt);
+ EXPECT_TRUE(info2->hasBasename());
+
+ EXPECT_NE(info1.value(), info2.value());
+ EXPECT_EQ(mangled.GetDemangledName(), "func()");
+}
+
+TEST(MangledTest, DemangledNameInfo_SetDemangledResets) {
+ Mangled mangled("_Z3foov");
+ ASSERT_TRUE(mangled);
+
+ mangled.SetDemangledName(ConstString(""));
+
+ // Mangled name hasn't changed, so GetDemangledInfo causes re-demangling
+ // of previously set mangled name.
+ EXPECT_NE(mangled.GetDemangledInfo(), std::nullopt);
+ EXPECT_EQ(mangled.GetDemangledName(), "foo()");
+}
+
+TEST(MangledTest, DemangledNameInfo_Clear) {
+ Mangled mangled("_Z3foov");
+ ASSERT_TRUE(mangled);
+ EXPECT_NE(mangled.GetDemangledInfo(), std::nullopt);
+
+ mangled.Clear();
+
+ EXPECT_EQ(mangled.GetDemangledInfo(), std::nullopt);
+}
+
+TEST(MangledTest, DemangledNameInfo_SetValue) {
+ Mangled mangled("_Z4funcv");
+ ASSERT_TRUE(mangled);
+
+ auto demangled_func = mangled.GetDemangledInfo();
+
+ // SetValue(mangled) resets demangled-info
+ mangled.SetValue(ConstString("_Z3foov"));
+ auto demangled_foo = mangled.GetDemangledInfo();
+ EXPECT_NE(demangled_foo, std::nullopt);
+ EXPECT_NE(demangled_foo, demangled_func);
+
+ // SetValue(demangled) resets demangled-info
+ mangled.SetValue(ConstString("_Z4funcv"));
+ EXPECT_EQ(mangled.GetDemangledInfo(), demangled_func);
+
+ // SetValue(empty) resets demangled-info
+ mangled.SetValue(ConstString());
+ EXPECT_EQ(mangled.GetDemangledInfo(), std::nullopt);
+
+ // Demangling invalid mangled name will set demangled-info
+ // (without a valid basename).
+ mangled.SetValue(ConstString("_Zinvalid"));
+ ASSERT_NE(mangled.GetDemangledInfo(), std::nullopt);
+ EXPECT_FALSE(mangled.GetDemangledInfo()->hasBasename());
+}
+
struct DemanglingPartsTestCase {
const char *mangled;
DemangledNameInfo expected_info;
std::string_view basename;
std::string_view scope;
+ std::string_view qualifiers;
bool valid_basename = true;
};
DemanglingPartsTestCase g_demangling_parts_test_cases[] = {
// clang-format off
{ "_ZNVKO3BarIN2ns3QuxIiEEE1CIPFi3FooIS_IiES6_EEE6methodIS6_EENS5_IT_SC_E5InnerIiEESD_SD_",
- { .BasenameRange = {92, 98}, .ScopeRange = {36, 92}, .ArgumentsRange = { 108, 158 } },
+ { .BasenameRange = {92, 98}, .ScopeRange = {36, 92}, .ArgumentsRange = { 108, 158 },
+ .QualifiersRange = {158, 176} },
.basename = "method",
- .scope = "Bar<ns::Qux<int>>::C<int (*)(Foo<Bar<int>, Bar<int>>)>::"
+ .scope = "Bar<ns::Qux<int>>::C<int (*)(Foo<Bar<int>, Bar<int>>)>::",
+ .qualifiers = " const volatile &&"
},
{ "_Z7getFuncIfEPFiiiET_",
- { .BasenameRange = {6, 13}, .ScopeRange = {6, 6}, .ArgumentsRange = { 20, 27 } },
+ { .BasenameRange = {6, 13}, .ScopeRange = {6, 6}, .ArgumentsRange = { 20, 27 }, .QualifiersRange = {38, 38} },
.basename = "getFunc",
- .scope = ""
+ .scope = "",
+ .qualifiers = ""
},
{ "_ZN1f1b1c1gEv",
- { .BasenameRange = {9, 10}, .ScopeRange = {0, 9}, .ArgumentsRange = { 10, 12 } },
+ { .BasenameRange = {9, 10}, .ScopeRange = {0, 9}, .ArgumentsRange = { 10, 12 },
+ .QualifiersRange = {12, 12} },
.basename = "g",
- .scope = "f::b::c::"
+ .scope = "f::b::c::",
+ .qualifiers = ""
},
{ "_ZN5test73fD1IiEEDTcmtlNS_1DEL_ZNS_1bEEEcvT__EES2_",
- { .BasenameRange = {45, 48}, .ScopeRange = {38, 45}, .ArgumentsRange = { 53, 58 } },
+ { .BasenameRange = {45, 48}, .ScopeRange = {38, 45}, .ArgumentsRange = { 53, 58 },
+ .QualifiersRange = {58, 58} },
.basename = "fD1",
- .scope = "test7::"
+ .scope = "test7::",
+ .qualifiers = ""
},
{ "_ZN5test73fD1IiEEDTcmtlNS_1DEL_ZNS_1bINDT1cE1dEEEEEcvT__EES2_",
- { .BasenameRange = {61, 64}, .ScopeRange = {54, 61}, .ArgumentsRange = { 69, 79 } },
+ { .BasenameRange = {61, 64}, .ScopeRange = {54, 61}, .ArgumentsRange = { 69, 79 },
+ .QualifiersRange = {79, 79} },
.basename = "fD1",
- .scope = "test7::"
+ .scope = "test7::",
+ .qualifiers = ""
},
{ "_ZN5test7INDT1cE1dINDT1cE1dEEEE3fD1INDT1cE1dINDT1cE1dEEEEEDTcmtlNS_1DEL_ZNS_1bINDT1cE1dEEEEEcvT__EES2_",
- { .BasenameRange = {120, 123}, .ScopeRange = {81, 120}, .ArgumentsRange = { 155, 168 } },
+ { .BasenameRange = {120, 123}, .ScopeRange = {81, 120}, .ArgumentsRange = { 155, 168 },
+ .QualifiersRange = {168, 168} },
.basename = "fD1",
- .scope = "test7<decltype(c)::d<decltype(c)::d>>::"
+ .scope = "test7<decltype(c)::d<decltype(c)::d>>::",
+ .qualifiers = ""
},
{ "_ZN8nlohmann16json_abi_v3_11_310basic_jsonINSt3__13mapENS2_6vectorENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEbxydS8_NS0_14adl_serializerENS4_IhNS8_IhEEEEvE5parseIRA29_KcEESE_OT_NS2_8functionIFbiNS0_6detail13parse_event_tERSE_EEEbb",
- { .BasenameRange = {687, 692}, .ScopeRange = {343, 687}, .ArgumentsRange = { 713, 1174 } },
+ { .BasenameRange = {687, 692}, .ScopeRange = {343, 687}, .ArgumentsRange = { 713, 1174 },
+ .QualifiersRange = {1174, 1174} },
.basename = "parse",
- .scope = "nlohmann::json_abi_v3_11_3::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::json_abi_v3_11_3::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char>>, void>::"
+ .scope = "nlohmann::json_abi_v3_11_3::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::json_abi_v3_11_3::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char>>, void>::",
+ .qualifiers = ""
},
{ "_ZN8nlohmann16json_abi_v3_11_310basic_jsonINSt3__13mapENS2_6vectorENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEbxydS8_NS0_14adl_serializerENS4_IhNS8_IhEEEEvEC1EDn",
- { .BasenameRange = {344, 354}, .ScopeRange = {0, 344}, .ArgumentsRange = { 354, 370 } },
+ { .BasenameRange = {344, 354}, .ScopeRange = {0, 344}, .ArgumentsRange = { 354, 370 },
+ .QualifiersRange = {370, 370} },
.basename = "basic_json",
- .scope = "nlohmann::json_abi_v3_11_3::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::json_abi_v3_11_3::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char>>, void>::"
+ .scope = "nlohmann::json_abi_v3_11_3::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::json_abi_v3_11_3::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char>>, void>::",
+ .qualifiers = ""
},
{ "_Z3fppIiEPFPFvvEiEf",
- { .BasenameRange = {10, 13}, .ScopeRange = {10, 10}, .ArgumentsRange = { 18, 25 } },
+ { .BasenameRange = {10, 13}, .ScopeRange = {10, 10}, .ArgumentsRange = { 18, 25 }, .QualifiersRange = {34,34} },
.basename = "fpp",
- .scope = ""
+ .scope = "",
+ .qualifiers = ""
},
{ "_Z3fppIiEPFPFvvEN2ns3FooIiEEEf",
- { .BasenameRange = {10, 13}, .ScopeRange = {10, 10}, .ArgumentsRange = { 18, 25 } },
+ { .BasenameRange = {10, 13}, .ScopeRange = {10, 10}, .ArgumentsRange = { 18, 25 },
+ .QualifiersRange = {43, 43} },
.basename = "fpp",
- .scope = ""
+ .scope = "",
+ .qualifiers = ""
},
{ "_Z3fppIiEPFPFvPFN2ns3FooIiEENS2_3BarIfE3QuxEEEPFS2_S2_EEf",
- { .BasenameRange = {10, 13}, .ScopeRange = {10, 10}, .ArgumentsRange = { 18, 25 } },
+ { .BasenameRange = {10, 13}, .ScopeRange = {10, 10}, .ArgumentsRange = { 18, 25 },
+ .QualifiersRange = {108, 108} },
.basename = "fpp",
- .scope = ""
+ .scope = "",
+ .qualifiers = ""
},
{ "_ZN2ns8HasFuncsINS_3FooINS1_IiE3BarIfE3QuxEEEE3fppIiEEPFPFvvEiEf",
- { .BasenameRange = {64, 67}, .ScopeRange = {10, 64}, .ArgumentsRange = { 72, 79 } },
+ { .BasenameRange = {64, 67}, .ScopeRange = {10, 64}, .ArgumentsRange = { 72, 79 },
+ .QualifiersRange = {88, 88} },
.basename = "fpp",
- .scope = "ns::HasFuncs<ns::Foo<ns::Foo<int>::Bar<float>::Qux>>::"
+ .scope = "ns::HasFuncs<ns::Foo<ns::Foo<int>::Bar<float>::Qux>>::",
+ .qualifiers = ""
},
{ "_ZN2ns8HasFuncsINS_3FooINS1_IiE3BarIfE3QuxEEEE3fppIiEEPFPFvvES2_Ef",
- { .BasenameRange = {64, 67}, .ScopeRange = {10, 64}, .ArgumentsRange = { 72, 79 } },
+ { .BasenameRange = {64, 67}, .ScopeRange = {10, 64}, .ArgumentsRange = { 72, 79 },
+ .QualifiersRange = {97, 97} },
.basename = "fpp",
- .scope = "ns::HasFuncs<ns::Foo<ns::Foo<int>::Bar<float>::Qux>>::"
+ .scope = "ns::HasFuncs<ns::Foo<ns::Foo<int>::Bar<float>::Qux>>::",
+ .qualifiers = "",
},
{ "_ZN2ns8HasFuncsINS_3FooINS1_IiE3BarIfE3QuxEEEE3fppIiEEPFPFvPFS2_S5_EEPFS2_S2_EEf",
- { .BasenameRange = {64, 67}, .ScopeRange = {10, 64}, .ArgumentsRange = { 72, 79 } },
+ { .BasenameRange = {64, 67}, .ScopeRange = {10, 64}, .ArgumentsRange = { 72, 79 },
+ .QualifiersRange = {162, 162} },
.basename = "fpp",
- .scope = "ns::HasFuncs<ns::Foo<ns::Foo<int>::Bar<float>::Qux>>::"
+ .scope = "ns::HasFuncs<ns::Foo<ns::Foo<int>::Bar<float>::Qux>>::",
+ .qualifiers = "",
+ },
+ { "_ZNKO2ns3ns23Bar3fooIiEEPFPFNS0_3FooIiEEiENS3_IfEEEi",
+ { .BasenameRange = {37, 40}, .ScopeRange = {23, 37}, .ArgumentsRange = { 45, 50 },
+ .QualifiersRange = {78, 87} },
+ .basename = "foo",
+ .scope = "ns::ns2::Bar::",
+ .qualifiers = " const &&",
},
{ "_ZTV11ImageLoader",
- { .BasenameRange = {0, 0}, .ScopeRange = {0, 0}, .ArgumentsRange = { 0, 0 } },
+ { .BasenameRange = {0, 0}, .ScopeRange = {0, 0}, .ArgumentsRange = { 0, 0 },
+ .QualifiersRange = {0, 0} },
.basename = "",
.scope = "",
+ .qualifiers = "",
.valid_basename = false
}
// clang-format on
@@ -433,7 +540,8 @@ class TestAllocator {
} // namespace
TEST_P(DemanglingPartsTestFixture, DemanglingParts) {
- const auto &[mangled, info, basename, scope, valid_basename] = GetParam();
+ const auto &[mangled, info, basename, scope, qualifiers, valid_basename] =
+ GetParam();
llvm::itanium_demangle::ManglingParser<TestAllocator> Parser(
mangled, mangled + ::strlen(mangled));
@@ -451,6 +559,7 @@ TEST_P(DemanglingPartsTestFixture, DemanglingParts) {
EXPECT_EQ(OB.NameInfo.BasenameRange, info.BasenameRange);
EXPECT_EQ(OB.NameInfo.ScopeRange, info.ScopeRange);
EXPECT_EQ(OB.NameInfo.ArgumentsRange, info.ArgumentsRange);
+ EXPECT_EQ(OB.NameInfo.QualifiersRange, info.QualifiersRange);
auto get_part = [&](const std::pair<size_t, size_t> &loc) {
return demangled.substr(loc.first, loc.second - loc.first);
@@ -458,6 +567,7 @@ TEST_P(DemanglingPartsTestFixture, DemanglingParts) {
EXPECT_EQ(get_part(OB.NameInfo.BasenameRange), basename);
EXPECT_EQ(get_part(OB.NameInfo.ScopeRange), scope);
+ EXPECT_EQ(get_part(OB.NameInfo.QualifiersRange), qualifiers);
}
INSTANTIATE_TEST_SUITE_P(DemanglingPartsTests, DemanglingPartsTestFixture,
diff --git a/llvm/include/llvm/Demangle/Demangle.h b/llvm/include/llvm/Demangle/Demangle.h
index 132e5088b5514..21e7457b6336f 100644
--- a/llvm/include/llvm/Demangle/Demangle.h
+++ b/llvm/include/llvm/Demangle/Demangle.h
@@ -93,6 +93,13 @@ struct ItaniumPartialDemangler {
/// second and third parameters to __cxa_demangle.
char *finishDemangle(char *Buf, size_t *N) const;
+ /// See \ref finishDemangle
+ ///
+ /// \param[in] OB A llvm::itanium_demangle::OutputBuffer that the demangled
+ /// name will be printed into.
+ ///
+ char *finishDemangle(void *OB) const;
+
/// Get the base name of a function. This doesn't include trailing template
/// arguments, ie for "a::b<int>" this function returns "b".
char *getFunctionBaseName(char *Buf, size_t *N) const;
diff --git a/llvm/lib/Demangle/ItaniumDemangle.cpp b/llvm/lib/Demangle/ItaniumDemangle.cpp
index 5c21b06a1d095..1009cc91ca12a 100644
--- a/llvm/lib/Demangle/ItaniumDemangle.cpp
+++ b/llvm/lib/Demangle/ItaniumDemangle.cpp
@@ -411,9 +411,7 @@ bool ItaniumPartialDemangler::partialDemangle(const char *MangledName) {
RootNode = Parser->parse();
return RootNode == nullptr;
}
-
-static char *printNode(const Node *RootNode, char *Buf, size_t *N) {
- OutputBuffer OB(Buf, N);
+static char *printNode(const Node *RootNode, OutputBuffer &OB, size_t *N) {
RootNode->print(OB);
OB += '\0';
if (N != nullptr)
@@ -421,6 +419,11 @@ static char *printNode(const Node *RootNode, char *Buf, size_t *N) {
return OB.getBuffer();
}
+static char *printNode(const Node *RootNode, char *Buf, size_t *N) {
+ OutputBuffer OB(Buf, N);
+ return printNode(RootNode, OB, N);
+}
+
char *ItaniumPartialDemangler::getFunctionBaseName(char *Buf, size_t *N) const {
if (!isFunction())
return nullptr;
@@ -540,6 +543,14 @@ char *ItaniumPartialDemangler::finishDemangle(char *Buf, size_t *N) const {
return printNode(static_cast<Node *>(RootNode), Buf, N);
}
+char *ItaniumPartialDemangler::finishDemangle(void *OB) const {
+ assert(RootNode != nullptr && "must call partialDemangle()");
+ assert(OB != nullptr && "valid OutputBuffer argument required");
+ return printNode(static_cast<Node *>(RootNode),
+ *static_cast<OutputBuffer *>(OB),
+ /*N=*/nullptr);
+}
+
bool ItaniumPartialDemangler::hasFunctionQualifiers() const {
assert(RootNode != nullptr && "must call partialDemangle()");
if (!isFunction())
>From 140956cc24f35c6479c2a4e3737a3a73d3cd82ed Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 9 Apr 2025 15:06:36 +0100
Subject: [PATCH 5/5] [lldb] Introduce new frame-format variables for function
parts
---
lldb/include/lldb/Core/FormatEntity.h | 7 +
lldb/include/lldb/Core/PluginManager.h | 14 +-
lldb/include/lldb/Target/Language.h | 10 +
lldb/source/Core/FormatEntity.cpp | 218 +++++++++-----
lldb/source/Core/PluginManager.cpp | 27 +-
.../Plugins/Language/CPlusPlus/CMakeLists.txt | 12 +
.../Language/CPlusPlus/CPlusPlusLanguage.cpp | 279 +++++++++++++++++-
.../Language/CPlusPlus/CPlusPlusLanguage.h | 10 +
.../CPlusPlus/LanguageCPlusPlusProperties.td | 8 +
.../Shell/Settings/TestCxxFrameFormat.test | 51 ++++
.../TestCxxFrameFormatMixedLanguages.test | 51 ++++
.../TestFrameFormatFunctionArguments.test | 60 ++++
.../TestFrameFormatFunctionBasename.test | 64 ++++
.../TestFrameFormatFunctionQualifiers.test | 42 +++
.../TestFrameFormatFunctionReturn.test | 76 +++++
.../TestFrameFormatFunctionScope.test | 58 ++++
...tFrameFormatFunctionTemplateArguments.test | 54 ++++
.../Shell/Settings/TestFrameFormatName.test | 4 +-
18 files changed, 969 insertions(+), 76 deletions(-)
create mode 100644 lldb/source/Plugins/Language/CPlusPlus/LanguageCPlusPlusProperties.td
create mode 100644 lldb/test/Shell/Settings/TestCxxFrameFormat.test
create mode 100644 lldb/test/Shell/Settings/TestCxxFrameFormatMixedLanguages.test
create mode 100644 lldb/test/Shell/Settings/TestFrameFormatFunctionArguments.test
create mode 100644 lldb/test/Shell/Settings/TestFrameFormatFunctionBasename.test
create mode 100644 lldb/test/Shell/Settings/TestFrameFormatFunctionQualifiers.test
create mode 100644 lldb/test/Shell/Settings/TestFrameFormatFunctionReturn.test
create mode 100644 lldb/test/Shell/Settings/TestFrameFormatFunctionScope.test
create mode 100644 lldb/test/Shell/Settings/TestFrameFormatFunctionTemplateArguments.test
diff --git a/lldb/include/lldb/Core/FormatEntity.h b/lldb/include/lldb/Core/FormatEntity.h
index 51e9ce37e54e7..1a03cd6ba8a5b 100644
--- a/lldb/include/lldb/Core/FormatEntity.h
+++ b/lldb/include/lldb/Core/FormatEntity.h
@@ -88,6 +88,13 @@ struct Entry {
FunctionNameWithArgs,
FunctionNameNoArgs,
FunctionMangledName,
+ FunctionScope,
+ FunctionBasename,
+ FunctionTemplateArguments,
+ FunctionArguments,
+ FunctionReturnLeft,
+ FunctionReturnRight,
+ FunctionQualifiers,
FunctionAddrOffset,
FunctionAddrOffsetConcrete,
FunctionLineOffset,
diff --git a/lldb/include/lldb/Core/PluginManager.h b/lldb/include/lldb/Core/PluginManager.h
index a6dab045adf27..d73dd71d833f3 100644
--- a/lldb/include/lldb/Core/PluginManager.h
+++ b/lldb/include/lldb/Core/PluginManager.h
@@ -141,8 +141,10 @@ class PluginManager {
GetOperatingSystemCreateCallbackForPluginName(llvm::StringRef name);
// Language
- static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description,
- LanguageCreateInstance create_callback);
+ static bool
+ RegisterPlugin(llvm::StringRef name, llvm::StringRef description,
+ LanguageCreateInstance create_callback,
+ DebuggerInitializeCallback debugger_init_callback = nullptr);
static bool UnregisterPlugin(LanguageCreateInstance create_callback);
@@ -613,6 +615,14 @@ class PluginManager {
static bool CreateSettingForStructuredDataPlugin(
Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
llvm::StringRef description, bool is_global_property);
+
+ static lldb::OptionValuePropertiesSP
+ GetSettingForCPlusPlusLanguagePlugin(Debugger &debugger,
+ llvm::StringRef setting_name);
+
+ static bool CreateSettingForCPlusPlusLanguagePlugin(
+ Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
+ llvm::StringRef description, bool is_global_property);
};
} // namespace lldb_private
diff --git a/lldb/include/lldb/Target/Language.h b/lldb/include/lldb/Target/Language.h
index b699a90aff8e4..318702a46c2ce 100644
--- a/lldb/include/lldb/Target/Language.h
+++ b/lldb/include/lldb/Target/Language.h
@@ -15,6 +15,7 @@
#include <set>
#include <vector>
+#include "lldb/Core/FormatEntity.h"
#include "lldb/Core/Highlighter.h"
#include "lldb/Core/PluginInterface.h"
#include "lldb/DataFormatters/DumpValueObjectOptions.h"
@@ -273,6 +274,13 @@ class Language : public PluginInterface {
FunctionNameRepresentation representation,
Stream &s);
+ virtual bool HandleFrameFormatVariable(const SymbolContext &sc,
+ const ExecutionContext *exe_ctx,
+ FormatEntity::Entry::Type type,
+ Stream &s) {
+ return false;
+ }
+
virtual ConstString
GetDemangledFunctionNameWithoutArguments(Mangled mangled) const {
if (ConstString demangled = mangled.GetDemangledName())
@@ -389,6 +397,8 @@ class Language : public PluginInterface {
/// Python uses \b except. Defaults to \b catch.
virtual llvm::StringRef GetCatchKeyword() const { return "catch"; }
+ virtual const FormatEntity::Entry *GetFrameFormat() const { return nullptr; }
+
protected:
// Classes that inherit from Language can see and modify these
diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp
index 04dea7efde54d..564b85a2d0ed2 100644
--- a/lldb/source/Core/FormatEntity.cpp
+++ b/lldb/source/Core/FormatEntity.cpp
@@ -122,7 +122,14 @@ constexpr Definition g_function_child_entries[] = {
Definition("pc-offset", EntryType::FunctionPCOffset),
Definition("initial-function", EntryType::FunctionInitial),
Definition("changed", EntryType::FunctionChanged),
- Definition("is-optimized", EntryType::FunctionIsOptimized)};
+ Definition("is-optimized", EntryType::FunctionIsOptimized),
+ Definition("scope", EntryType::FunctionScope),
+ Definition("basename", EntryType::FunctionBasename),
+ Definition("template-arguments", EntryType::FunctionTemplateArguments),
+ Definition("arguments", EntryType::FunctionArguments),
+ Definition("return-left", EntryType::FunctionReturnLeft),
+ Definition("return-right", EntryType::FunctionReturnRight),
+ Definition("qualifiers", EntryType::FunctionQualifiers)};
constexpr Definition g_line_child_entries[] = {
Entry::DefinitionWithChildren("file", EntryType::LineEntryFile,
@@ -352,6 +359,13 @@ const char *FormatEntity::Entry::TypeToCString(Type t) {
ENUM_TO_CSTR(FunctionNameWithArgs);
ENUM_TO_CSTR(FunctionNameNoArgs);
ENUM_TO_CSTR(FunctionMangledName);
+ ENUM_TO_CSTR(FunctionScope);
+ ENUM_TO_CSTR(FunctionBasename);
+ ENUM_TO_CSTR(FunctionTemplateArguments);
+ ENUM_TO_CSTR(FunctionArguments);
+ ENUM_TO_CSTR(FunctionReturnLeft);
+ ENUM_TO_CSTR(FunctionReturnRight);
+ ENUM_TO_CSTR(FunctionQualifiers);
ENUM_TO_CSTR(FunctionAddrOffset);
ENUM_TO_CSTR(FunctionAddrOffsetConcrete);
ENUM_TO_CSTR(FunctionLineOffset);
@@ -1160,6 +1174,123 @@ static void FormatInlinedBlock(Stream &out_stream, Block *block) {
}
}
+static VariableListSP GetFunctionVariableList(const SymbolContext &sc) {
+ assert(sc.function);
+
+ if (sc.block)
+ if (Block *inline_block = sc.block->GetContainingInlinedBlock())
+ return inline_block->GetBlockVariableList(true);
+
+ return sc.function->GetBlock(true).GetBlockVariableList(true);
+}
+
+static char const *GetInlinedFunctionName(const SymbolContext &sc) {
+ if (!sc.block)
+ return nullptr;
+
+ const Block *inline_block = sc.block->GetContainingInlinedBlock();
+ if (!inline_block)
+ return nullptr;
+
+ const InlineFunctionInfo *inline_info =
+ inline_block->GetInlinedFunctionInfo();
+ if (!inline_info)
+ return nullptr;
+
+ return inline_info->GetName().AsCString(nullptr);
+}
+
+static bool PrintFunctionNameWithArgs(Stream &s,
+ const ExecutionContext *exe_ctx,
+ const SymbolContext &sc) {
+ assert(sc.function);
+
+ ExecutionContextScope *exe_scope =
+ exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
+
+ const char *cstr = sc.function->GetName().AsCString(nullptr);
+ if (!cstr)
+ return false;
+
+ if (const char *inlined_name = GetInlinedFunctionName(sc)) {
+ s.PutCString(cstr);
+ s.PutCString(" [inlined] ");
+ cstr = inlined_name;
+ }
+
+ VariableList args;
+ if (auto variable_list_sp = GetFunctionVariableList(sc))
+ variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
+ args);
+
+ if (args.GetSize() > 0) {
+ PrettyPrintFunctionNameWithArgs(s, cstr, exe_scope, args);
+ } else {
+ s.PutCString(cstr);
+ }
+
+ return true;
+}
+
+static bool FormatFunctionNameWithArgs(Stream &s, const SymbolContext *sc,
+ const ExecutionContext *exe_ctx) {
+ if (!sc)
+ return false;
+
+ Language *language_plugin = nullptr;
+ bool language_plugin_handled = false;
+ StreamString ss;
+ if (sc->function)
+ language_plugin = Language::FindPlugin(sc->function->GetLanguage());
+ else if (sc->symbol)
+ language_plugin = Language::FindPlugin(sc->symbol->GetLanguage());
+
+ if (language_plugin)
+ language_plugin_handled = language_plugin->GetFunctionDisplayName(
+ sc, exe_ctx, Language::FunctionNameRepresentation::eNameWithArgs, ss);
+
+ if (language_plugin_handled) {
+ s << ss.GetString();
+ return true;
+ }
+
+ if (sc->function)
+ return PrintFunctionNameWithArgs(s, exe_ctx, *sc);
+
+ if (!sc->symbol)
+ return false;
+
+ const char *cstr = sc->symbol->GetName().AsCString(nullptr);
+ if (!cstr)
+ return false;
+
+ s.PutCString(cstr);
+ return true;
+}
+
+static bool FormatFunctionNameForLanguage(Stream &s, const SymbolContext *sc,
+ const ExecutionContext *exe_ctx) {
+ if (!sc)
+ return false;
+
+ Language *language_plugin = nullptr;
+ if (sc->function)
+ language_plugin = Language::FindPlugin(sc->function->GetLanguage());
+ else if (sc->symbol)
+ language_plugin = Language::FindPlugin(sc->symbol->GetLanguage());
+
+ if (!language_plugin)
+ return false;
+
+ const auto *format = language_plugin->GetFrameFormat();
+ if (!format)
+ return false;
+
+ return FormatEntity::Format(*format, s, sc, exe_ctx, /*addr=*/nullptr,
+ /*valobj=*/nullptr, /*function_changed=*/false,
+ /*initial_function=*/false);
+}
+
bool FormatEntity::FormatStringRef(const llvm::StringRef &format_str, Stream &s,
const SymbolContext *sc,
const ExecutionContext *exe_ctx,
@@ -1717,79 +1848,34 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
}
return false;
- case Entry::Type::FunctionNameWithArgs: {
+ case Entry::Type::FunctionScope:
+ case Entry::Type::FunctionBasename:
+ case Entry::Type::FunctionTemplateArguments:
+ case Entry::Type::FunctionArguments:
+ case Entry::Type::FunctionReturnRight:
+ case Entry::Type::FunctionReturnLeft:
+ case Entry::Type::FunctionQualifiers: {
if (!sc)
return false;
- Language *language_plugin = nullptr;
- bool language_plugin_handled = false;
- StreamString ss;
- if (sc->function)
- language_plugin = Language::FindPlugin(sc->function->GetLanguage());
- else if (sc->symbol)
- language_plugin = Language::FindPlugin(sc->symbol->GetLanguage());
-
- if (language_plugin)
- language_plugin_handled = language_plugin->GetFunctionDisplayName(
- sc, exe_ctx, Language::FunctionNameRepresentation::eNameWithArgs, ss);
+ if (!sc->function)
+ return false;
- if (language_plugin_handled) {
- s << ss.GetString();
- return true;
- } else {
- // Print the function name with arguments in it
- if (sc->function) {
- ExecutionContextScope *exe_scope =
- exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
- const char *cstr = sc->function->GetName().AsCString(nullptr);
- if (cstr) {
- const InlineFunctionInfo *inline_info = nullptr;
- VariableListSP variable_list_sp;
- bool get_function_vars = true;
- if (sc->block) {
- Block *inline_block = sc->block->GetContainingInlinedBlock();
-
- if (inline_block) {
- get_function_vars = false;
- inline_info = inline_block->GetInlinedFunctionInfo();
- if (inline_info)
- variable_list_sp = inline_block->GetBlockVariableList(true);
- }
- }
+ Language *language_plugin =
+ Language::FindPlugin(sc->function->GetLanguage());
+ if (!language_plugin)
+ return false;
- if (get_function_vars) {
- variable_list_sp =
- sc->function->GetBlock(true).GetBlockVariableList(true);
- }
+ return language_plugin->HandleFrameFormatVariable(*sc, exe_ctx, entry.type,
+ s);
+ }
- if (inline_info) {
- s.PutCString(cstr);
- s.PutCString(" [inlined] ");
- cstr = inline_info->GetName().GetCString();
- }
+ case Entry::Type::FunctionNameWithArgs: {
+ if (FormatFunctionNameForLanguage(s, sc, exe_ctx))
+ return true;
- VariableList args;
- if (variable_list_sp)
- variable_list_sp->AppendVariablesWithScope(
- eValueTypeVariableArgument, args);
- if (args.GetSize() > 0) {
- PrettyPrintFunctionNameWithArgs(s, cstr, exe_scope, args);
- } else {
- s.PutCString(cstr);
- }
- return true;
- }
- } else if (sc->symbol) {
- const char *cstr = sc->symbol->GetName().AsCString(nullptr);
- if (cstr) {
- s.PutCString(cstr);
- return true;
- }
- }
- }
+ return FormatFunctionNameWithArgs(s, sc, exe_ctx);
}
- return false;
-
case Entry::Type::FunctionMangledName: {
if (!sc)
return false;
diff --git a/lldb/source/Core/PluginManager.cpp b/lldb/source/Core/PluginManager.cpp
index e6cb248ef31ce..73c018330a24e 100644
--- a/lldb/source/Core/PluginManager.cpp
+++ b/lldb/source/Core/PluginManager.cpp
@@ -564,11 +564,12 @@ static LanguageInstances &GetLanguageInstances() {
return g_instances;
}
-bool PluginManager::RegisterPlugin(llvm::StringRef name,
- llvm::StringRef description,
- LanguageCreateInstance create_callback) {
- return GetLanguageInstances().RegisterPlugin(name, description,
- create_callback);
+bool PluginManager::RegisterPlugin(
+ llvm::StringRef name, llvm::StringRef description,
+ LanguageCreateInstance create_callback,
+ DebuggerInitializeCallback debugger_init_callback) {
+ return GetLanguageInstances().RegisterPlugin(
+ name, description, create_callback, debugger_init_callback);
}
bool PluginManager::UnregisterPlugin(LanguageCreateInstance create_callback) {
@@ -1682,6 +1683,7 @@ void PluginManager::DebuggerInitialize(Debugger &debugger) {
GetStructuredDataPluginInstances().PerformDebuggerCallback(debugger);
GetTracePluginInstances().PerformDebuggerCallback(debugger);
GetScriptedInterfaceInstances().PerformDebuggerCallback(debugger);
+ GetLanguageInstances().PerformDebuggerCallback(debugger);
}
// This is the preferred new way to register plugin specific settings. e.g.
@@ -1810,6 +1812,7 @@ static constexpr llvm::StringLiteral kSymbolLocatorPluginName("symbol-locator");
static constexpr llvm::StringLiteral kJITLoaderPluginName("jit-loader");
static constexpr llvm::StringLiteral
kStructuredDataPluginName("structured-data");
+static constexpr llvm::StringLiteral kCPlusPlusLanguagePlugin("cplusplus");
lldb::OptionValuePropertiesSP
PluginManager::GetSettingForDynamicLoaderPlugin(Debugger &debugger,
@@ -1967,3 +1970,17 @@ bool PluginManager::CreateSettingForStructuredDataPlugin(
"Settings for structured data plug-ins",
properties_sp, description, is_global_property);
}
+
+lldb::OptionValuePropertiesSP
+PluginManager::GetSettingForCPlusPlusLanguagePlugin(
+ Debugger &debugger, llvm::StringRef setting_name) {
+ return GetSettingForPlugin(debugger, setting_name, kCPlusPlusLanguagePlugin);
+}
+
+bool PluginManager::CreateSettingForCPlusPlusLanguagePlugin(
+ Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
+ llvm::StringRef description, bool is_global_property) {
+ return CreateSettingForPlugin(debugger, kCPlusPlusLanguagePlugin,
+ "Settings for CPlusPlus language plug-ins",
+ properties_sp, description, is_global_property);
+}
diff --git a/lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt b/lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
index ccdc4d0ae99b3..9bb10c2a792a9 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
+++ b/lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
@@ -1,3 +1,11 @@
+lldb_tablegen(LanguageCPlusPlusProperties.inc -gen-lldb-property-defs
+ SOURCE LanguageCPlusPlusProperties.td
+ TARGET LLDBPluginLanguageCPlusPlusPropertiesGen)
+
+lldb_tablegen(LanguageCPlusPlusPropertiesEnum.inc -gen-lldb-property-enum-defs
+ SOURCE LanguageCPlusPlusProperties.td
+ TARGET LLDBPluginLanguageCPlusPlusPropertiesEnumGen)
+
add_lldb_library(lldbPluginCPlusPlusLanguage PLUGIN
BlockPointer.cpp
Coroutines.cpp
@@ -41,3 +49,7 @@ add_lldb_library(lldbPluginCPlusPlusLanguage PLUGIN
LINK_COMPONENTS
Support
)
+
+add_dependencies(lldbPluginCPlusPlusLanguage
+ LLDBPluginLanguageCPlusPlusPropertiesGen
+ LLDBPluginLanguageCPlusPlusPropertiesEnumGen)
diff --git a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
index 4b045d12ad494..e981b2f4b4e76 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
@@ -27,6 +27,7 @@
#include "lldb/DataFormatters/DataVisualization.h"
#include "lldb/DataFormatters/FormattersHelpers.h"
#include "lldb/DataFormatters/VectorType.h"
+#include "lldb/Interpreter/OptionValueProperties.h"
#include "lldb/Symbol/SymbolFile.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Utility/ConstString.h"
@@ -55,7 +56,7 @@ LLDB_PLUGIN_DEFINE(CPlusPlusLanguage)
void CPlusPlusLanguage::Initialize() {
PluginManager::RegisterPlugin(GetPluginNameStatic(), "C++ Language",
- CreateInstance);
+ CreateInstance, &DebuggerInitialize);
}
void CPlusPlusLanguage::Terminate() {
@@ -208,6 +209,152 @@ static bool PrettyPrintFunctionNameWithArgs(Stream &out_stream,
return true;
}
+static std::optional<llvm::StringRef> GetDemangledBasename(Function &function) {
+ auto demangled_name = function.GetName().GetStringRef();
+ if (demangled_name.empty())
+ return std::nullopt;
+
+ const std::optional<DemangledNameInfo> &info =
+ function.GetMangled().GetDemangledInfo();
+ if (!info)
+ return std::nullopt;
+
+ // Function without a basename is nonsense.
+ if (!info->hasBasename())
+ return std::nullopt;
+
+ assert(info->BasenameRange.first < demangled_name.size());
+ assert(info->BasenameRange.second < demangled_name.size());
+
+ return demangled_name.substr(info->BasenameRange.first,
+ info->BasenameRange.second -
+ info->BasenameRange.first);
+}
+
+static std::optional<llvm::StringRef>
+GetDemangledTemplateArguments(Function &function) {
+ auto demangled_name = function.GetName().GetStringRef();
+ if (demangled_name.empty())
+ return std::nullopt;
+
+ const std::optional<DemangledNameInfo> &info =
+ function.GetMangled().GetDemangledInfo();
+ if (!info)
+ return std::nullopt;
+
+ // Function without a basename is nonsense.
+ if (!info->hasBasename())
+ return std::nullopt;
+
+ assert(info->BasenameRange.second < demangled_name.size());
+ assert(info->ArgumentsRange.first < demangled_name.size());
+ assert(info->ArgumentsRange.first >= info->BasenameRange.second);
+
+ return demangled_name.substr(info->BasenameRange.second,
+ info->ArgumentsRange.first -
+ info->BasenameRange.second);
+}
+
+static std::optional<llvm::StringRef>
+GetDemangledReturnTypeLHS(Function &function) {
+ auto demangled_name = function.GetName().GetStringRef();
+ if (demangled_name.empty())
+ return std::nullopt;
+
+ const std::optional<DemangledNameInfo> &info =
+ function.GetMangled().GetDemangledInfo();
+ if (!info)
+ return std::nullopt;
+
+ // Function without a basename is nonsense.
+ if (!info->hasBasename())
+ return std::nullopt;
+
+ assert(info->ScopeRange.first < demangled_name.size());
+
+ return demangled_name.substr(0, info->ScopeRange.first);
+}
+
+static std::optional<llvm::StringRef>
+GetDemangledFunctionQualifiers(Function &function) {
+ auto demangled_name = function.GetName().GetStringRef();
+ if (demangled_name.empty())
+ return std::nullopt;
+
+ const std::optional<DemangledNameInfo> &info =
+ function.GetMangled().GetDemangledInfo();
+ if (!info)
+ return std::nullopt;
+
+ // Function without a basename is nonsense.
+ if (!info->hasBasename())
+ return std::nullopt;
+
+ assert(info->QualifiersRange.first <= demangled_name.size());
+ assert(info->QualifiersRange.second <= demangled_name.size());
+ assert(info->QualifiersRange.second >= info->QualifiersRange.first);
+
+ return demangled_name.substr(info->QualifiersRange.first,
+ info->QualifiersRange.second -
+ info->QualifiersRange.first);
+}
+
+static std::optional<llvm::StringRef>
+GetDemangledReturnTypeRHS(Function &function) {
+ auto demangled_name = function.GetName().GetStringRef();
+ if (demangled_name.empty())
+ return std::nullopt;
+
+ const std::optional<DemangledNameInfo> &info =
+ function.GetMangled().GetDemangledInfo();
+ if (!info)
+ return std::nullopt;
+
+ // Function without a basename is nonsense.
+ if (!info->hasBasename())
+ return std::nullopt;
+
+ assert(info->QualifiersRange.first <= demangled_name.size());
+ assert(info->ArgumentsRange.second <= demangled_name.size());
+ assert(info->QualifiersRange.first >= info->ArgumentsRange.second);
+
+ return demangled_name.substr(info->ArgumentsRange.second,
+ info->QualifiersRange.first -
+ info->ArgumentsRange.second);
+}
+
+static std::optional<llvm::StringRef> GetDemangledScope(Function &function) {
+ auto demangled_name = function.GetName().GetStringRef();
+ if (demangled_name.empty())
+ return std::nullopt;
+
+ const std::optional<DemangledNameInfo> &info =
+ function.GetMangled().GetDemangledInfo();
+ if (!info)
+ return std::nullopt;
+
+ // Function without a basename is nonsense.
+ if (!info->hasBasename())
+ return std::nullopt;
+
+ assert(info->ScopeRange.first < demangled_name.size());
+ assert(info->ScopeRange.second < demangled_name.size());
+ assert(info->ScopeRange.second >= info->ScopeRange.first);
+
+ return demangled_name.substr(
+ info->ScopeRange.first, info->ScopeRange.second - info->ScopeRange.first);
+}
+
+static VariableListSP GetFunctionVariableList(const SymbolContext &sc) {
+ assert(sc.function);
+
+ if (sc.block)
+ if (Block *inline_block = sc.block->GetContainingInlinedBlock())
+ return inline_block->GetBlockVariableList(true);
+
+ return sc.function->GetBlock(true).GetBlockVariableList(true);
+}
+
bool CPlusPlusLanguage::MethodName::TrySimplifiedParse() {
// This method tries to parse simple method definitions which are presumably
// most comman in user programs. Definitions that can be parsed by this
@@ -1759,3 +1906,133 @@ bool CPlusPlusLanguage::GetFunctionDisplayName(
return false;
}
+bool CPlusPlusLanguage::HandleFrameFormatVariable(
+ const SymbolContext &sc, const ExecutionContext *exe_ctx,
+ FormatEntity::Entry::Type type, Stream &s) {
+ assert(sc.function);
+
+ switch (type) {
+ case FormatEntity::Entry::Type::FunctionScope: {
+ std::optional<llvm::StringRef> scope = GetDemangledScope(*sc.function);
+ if (!scope)
+ return false;
+
+ s << *scope;
+
+ return true;
+ }
+
+ case FormatEntity::Entry::Type::FunctionBasename: {
+ std::optional<llvm::StringRef> name = GetDemangledBasename(*sc.function);
+ if (!name)
+ return false;
+
+ s << *name;
+
+ return true;
+ }
+
+ case FormatEntity::Entry::Type::FunctionTemplateArguments: {
+ std::optional<llvm::StringRef> template_args =
+ GetDemangledTemplateArguments(*sc.function);
+ if (!template_args)
+ return false;
+
+ s << *template_args;
+
+ return true;
+ }
+
+ case FormatEntity::Entry::Type::FunctionArguments: {
+ VariableList args;
+ if (auto variable_list_sp = GetFunctionVariableList(sc))
+ variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
+ args);
+
+ ExecutionContextScope *exe_scope =
+ exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
+
+ s << '(';
+ FormatEntity::PrettyPrintFunctionArguments(s, args, exe_scope);
+ s << ')';
+
+ return true;
+ }
+ case FormatEntity::Entry::Type::FunctionReturnRight: {
+ std::optional<llvm::StringRef> return_rhs =
+ GetDemangledReturnTypeRHS(*sc.function);
+ if (!return_rhs)
+ return false;
+
+ s << *return_rhs;
+
+ return true;
+ }
+ case FormatEntity::Entry::Type::FunctionReturnLeft: {
+ std::optional<llvm::StringRef> return_lhs =
+ GetDemangledReturnTypeLHS(*sc.function);
+ if (!return_lhs)
+ return false;
+
+ s << *return_lhs;
+
+ return true;
+ }
+ case FormatEntity::Entry::Type::FunctionQualifiers: {
+ std::optional<llvm::StringRef> quals =
+ GetDemangledFunctionQualifiers(*sc.function);
+ if (!quals)
+ return false;
+
+ s << *quals;
+
+ return true;
+ }
+ default:
+ return false;
+ }
+}
+
+#define LLDB_PROPERTIES_language_cplusplus
+#include "LanguageCPlusPlusProperties.inc"
+
+enum {
+#define LLDB_PROPERTIES_language_cplusplus
+#include "LanguageCPlusPlusPropertiesEnum.inc"
+};
+
+namespace {
+class PluginProperties : public Properties {
+public:
+ static llvm::StringRef GetSettingName() { return "language"; }
+
+ PluginProperties() {
+ m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
+ m_collection_sp->Initialize(g_language_cplusplus_properties);
+ }
+
+ const FormatEntity::Entry *GetFrameFormat() const {
+ return GetPropertyAtIndexAs<const FormatEntity::Entry *>(
+ ePropertyFrameFormat);
+ }
+};
+} // namespace
+
+static PluginProperties &GetGlobalPluginProperties() {
+ static PluginProperties g_settings;
+ return g_settings;
+}
+
+const FormatEntity::Entry *CPlusPlusLanguage::GetFrameFormat() const {
+ return GetGlobalPluginProperties().GetFrameFormat();
+}
+
+void CPlusPlusLanguage::DebuggerInitialize(Debugger &debugger) {
+ if (!PluginManager::GetSettingForCPlusPlusLanguagePlugin(
+ debugger, PluginProperties::GetSettingName())) {
+ PluginManager::CreateSettingForCPlusPlusLanguagePlugin(
+ debugger, GetGlobalPluginProperties().GetValueProperties(),
+ "Properties for the CPlusPlus language plug-in.",
+ /*is_global_property=*/true);
+ }
+}
diff --git a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h
index 623d481bf117f..3e5c863a6b8b0 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h
+++ b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h
@@ -143,6 +143,11 @@ class CPlusPlusLanguage : public Language {
FunctionNameRepresentation representation,
Stream &s) override;
+ bool HandleFrameFormatVariable(const SymbolContext &sc,
+ const ExecutionContext *exe_ctx,
+ FormatEntity::Entry::Type type,
+ Stream &s) override;
+
static bool IsCPPMangledName(llvm::StringRef name);
// Extract C++ context and identifier from a string using heuristic matching
@@ -169,8 +174,13 @@ class CPlusPlusLanguage : public Language {
llvm::StringRef GetInstanceVariableName() override { return "this"; }
+ const FormatEntity::Entry *GetFrameFormat() const override;
+
// PluginInterface protocol
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
+
+private:
+ static void DebuggerInitialize(Debugger &);
};
} // namespace lldb_private
diff --git a/lldb/source/Plugins/Language/CPlusPlus/LanguageCPlusPlusProperties.td b/lldb/source/Plugins/Language/CPlusPlus/LanguageCPlusPlusProperties.td
new file mode 100644
index 0000000000000..1a53b6f2bf0eb
--- /dev/null
+++ b/lldb/source/Plugins/Language/CPlusPlus/LanguageCPlusPlusProperties.td
@@ -0,0 +1,8 @@
+include "../../../../include/lldb/Core/PropertiesBase.td"
+
+let Definition = "language_cplusplus" in {
+ def FrameFormat: Property<"frame-format", "FormatEntity">,
+ Global,
+ DefaultStringValue<"${function.return-left}${function.scope}${ansi.fg.cyan}${function.basename}${ansi.normal}${function.template-arguments}${function.arguments}${function.return-right}${function.qualifiers}">,
+ Desc<"C++ specific frame format string to use when displaying stack frame information for threads.">;
+}
diff --git a/lldb/test/Shell/Settings/TestCxxFrameFormat.test b/lldb/test/Shell/Settings/TestCxxFrameFormat.test
new file mode 100644
index 0000000000000..a7a3c0c90e700
--- /dev/null
+++ b/lldb/test/Shell/Settings/TestCxxFrameFormat.test
@@ -0,0 +1,51 @@
+# Test the ${plugin.cplusplus.language.frame-format} setting.
+
+# RUN: split-file %s %t
+# RUN: %build %t/main.cpp -o %t.out
+# RUN: %lldb -o "settings set interpreter.stop-command-source-on-error false" \
+# RUN: -x -b -s %t/commands.input %t.out -o exit 2>&1 \
+# RUN: | FileCheck %s
+#
+# RUN: %build %t/main.m -o %t.objc.out
+# RUN: %lldb -x -b -s %t/commands.input %t.objc.out -o exit 2>&1 \
+# RUN: | FileCheck %s --check-prefix=CHECK-OBJC
+
+#--- main.cpp
+namespace ns::ns2 {
+void custom(int x) asm("_Zinvalid_mangling");
+void custom(int x) {}
+
+void bar() { custom(5); }
+void foo() { bar(); }
+}
+
+int main(int argc, char const *argv[]) {
+ ns::ns2::foo();
+ return 0;
+}
+
+#--- main.m
+
+int func(int x) {}
+int bar(int y) { func(y); }
+
+int main() { return bar(10); }
+
+#--- commands.input
+settings set plugin.cplusplus.language.frame-format "${function.scope}${function.basename}"
+settings set -f frame-format "custom-frame '${function.name-with-args}'\n"
+break set -l 3
+
+run
+bt
+
+# CHECK: custom-frame '_Zinvalid_mangling(x=5)'
+# CHECK: custom-frame 'ns::ns2::bar'
+# CHECK: custom-frame 'ns::ns2::foo'
+
+settings set plugin.cplusplus.language.frame-format "this affects C++ only"
+bt
+
+# CHECK-OBJC: bt
+# CHECK-OBJC: bt
+# CHECK-OBJC: custom-frame 'bar(y=10)'
diff --git a/lldb/test/Shell/Settings/TestCxxFrameFormatMixedLanguages.test b/lldb/test/Shell/Settings/TestCxxFrameFormatMixedLanguages.test
new file mode 100644
index 0000000000000..36684bfb5b420
--- /dev/null
+++ b/lldb/test/Shell/Settings/TestCxxFrameFormatMixedLanguages.test
@@ -0,0 +1,51 @@
+# Test the ${plugin.cplusplus.language.frame-format} setting
+# when interoperating multiple languages.
+
+# RUN: split-file %s %t
+# RUN: %clangxx_host -x c -c -g %t/lib.c -o %t.clib.o
+# RUN: %clangxx_host -c -g %t/lib.cpp -o %t.cxxlib.o
+# RUN: %clangxx_host %t/main.m %t.cxxlib.o %t.clib.o -o %t.out
+# RUN: %lldb -x -b -s %t/commands.input %t.out -o exit 2>&1 | FileCheck %s
+
+#--- lib.c
+
+void foo();
+
+void func() {
+ foo();
+}
+
+#--- lib.cpp
+
+namespace ns {
+struct Foo {
+ void method() {}
+};
+}
+
+extern "C" {
+void foo() {
+ ns::Foo{}.method();
+}
+}
+
+#--- main.m
+
+void func();
+
+int main() {
+ func();
+}
+
+#--- commands.input
+settings set plugin.cplusplus.language.frame-format "this affects C++ only"
+settings set -f frame-format "custom-frame '${function.name-with-args}'\n"
+break set -n method
+
+run
+bt
+
+# CHECK: custom-frame 'this affects C++ only'
+# CHECK: custom-frame 'this affects C++ only'
+# CHECK: custom-frame 'func'
+# CHECK: custom-frame 'main'
diff --git a/lldb/test/Shell/Settings/TestFrameFormatFunctionArguments.test b/lldb/test/Shell/Settings/TestFrameFormatFunctionArguments.test
new file mode 100644
index 0000000000000..84a3877b08a79
--- /dev/null
+++ b/lldb/test/Shell/Settings/TestFrameFormatFunctionArguments.test
@@ -0,0 +1,60 @@
+# Test the ${function.arguments} frame-format variable.
+
+# RUN: split-file %s %t
+# RUN: %build %t/main.cpp -o %t.out
+# RUN: %lldb -o "settings set interpreter.stop-command-source-on-error false" \
+# RUN: -x -b -s %t/commands.input %t.out -o exit 2>&1 \
+# RUN: | FileCheck %s
+#
+# Check that we have an appropriate fallback for languages that
+# don't implement this frame format variable (in this case Objective-C).
+#
+# RUN: %build %t/main.m -o %t.objc.out
+# RUN: %lldb -x -b -s %t/commands.input %t.objc.out -o exit 2>&1 \
+# RUN: | FileCheck %s --check-prefix=CHECK-OBJC
+
+#--- main.cpp
+struct Foo {
+ void func() {}
+};
+
+void bar() {
+ Foo{}.func();
+}
+
+void foo(int, int x) {
+ bar();
+}
+
+void myFunc(char const * str,
+ void (*fptr)(int, int)) {
+ fptr(5, 10);
+}
+
+int main(int argc, char const *argv[]) {
+ myFunc("hello", &foo);
+ return 0;
+}
+
+#--- main.m
+
+int func() {}
+int bar() { func(); }
+
+int main() { return bar(); }
+
+#--- commands.input
+settings set -f frame-format "custom-frame '${function.arguments}'\n"
+break set -n func
+
+run
+bt
+
+# CHECK: custom-frame '(this={{.*}})'
+# CHECK: custom-frame '()'
+# CHECK: custom-frame '((null)=5, x=10)'
+# CHECK: custom-frame '(str="hello", fptr=({{.*}}.out`foo(int, int) at main.cpp:{{[0-9]+}}))'
+# CHECK: custom-frame '(argc=1, argv={{.*}})'
+
+# CHECK-OBJC: bt
+# CHECK-OBJC-NOT: custom-frame
diff --git a/lldb/test/Shell/Settings/TestFrameFormatFunctionBasename.test b/lldb/test/Shell/Settings/TestFrameFormatFunctionBasename.test
new file mode 100644
index 0000000000000..5acb4ca6587b1
--- /dev/null
+++ b/lldb/test/Shell/Settings/TestFrameFormatFunctionBasename.test
@@ -0,0 +1,64 @@
+# Test the ${function.basename} frame-format variable.
+
+# RUN: split-file %s %t
+# RUN: %build %t/main.cpp -o %t.out
+# RUN: %lldb -o "settings set interpreter.stop-command-source-on-error false" \
+# RUN: -x -b -s %t/commands.input %t.out -o exit 2>&1 \
+# RUN: | FileCheck %s
+#
+# Check that we have an appropriate fallback for languages that
+# don't implement this frame format variable (in this case Objective-C).
+#
+# RUN: %build %t/main.m -o %t.objc.out
+# RUN: %lldb -x -b -s %t/commands.input %t.objc.out -o exit 2>&1 \
+# RUN: | FileCheck %s --check-prefix=CHECK-OBJC
+
+#--- main.cpp
+namespace ns {
+template<typename T>
+struct Bar {
+ template<typename K>
+ T bar(K k) const & { return 1.0f; }
+};
+
+template<typename T>
+struct Foo {
+ template<typename K>
+ [[gnu::abi_tag("Test")]] void foo() const volatile && {
+ Bar<float> b;
+ b.bar(b);
+ }
+};
+
+template<typename T>
+T func() {
+ ns::Foo<int>{}.foo<int>();
+ return T{};
+}
+} // namespace ns
+
+int main() {
+ ns::func<ns::Foo<int>>();
+ return 0;
+}
+
+#--- main.m
+
+int func() {}
+int bar() { func(); }
+
+int main() { return bar(); }
+
+#--- commands.input
+settings set -f frame-format "custom-frame '${function.basename}'\n"
+break set -n bar
+
+run
+bt
+
+# CHECK: custom-frame 'bar'
+# CHECK: custom-frame 'foo[abi:Test]'
+# CHECK: custom-frame 'func'
+
+# CHECK-OBJC: bt
+# CHECK-OBJC-NOT: custom-frame
diff --git a/lldb/test/Shell/Settings/TestFrameFormatFunctionQualifiers.test b/lldb/test/Shell/Settings/TestFrameFormatFunctionQualifiers.test
new file mode 100644
index 0000000000000..288f1cee2b596
--- /dev/null
+++ b/lldb/test/Shell/Settings/TestFrameFormatFunctionQualifiers.test
@@ -0,0 +1,42 @@
+# Test the ${function.qualifiers} frame-format variable.
+
+# RUN: split-file %s %t
+# RUN: %build %t/main.cpp -o %t.out
+# RUN: %lldb -o "settings set interpreter.stop-command-source-on-error false" \
+# RUN: -x -b -s %t/commands.input %t.out -o exit 2>&1 \
+# RUN: | FileCheck %s
+#
+# Check that we have an appropriate fallback for languages that
+# don't implement this frame format variable (in this case Objective-C).
+#
+# RUN: %build %t/main.m -o %t.objc.out
+# RUN: %lldb -x -b -s %t/commands.input %t.objc.out -o exit 2>&1 \
+# RUN: | FileCheck %s --check-prefix=CHECK-OBJC
+
+#--- main.cpp
+struct Foo {
+ void foo() const volatile && {}
+ void bar() { Foo{}.foo(); }
+};
+
+int main() { Foo{}.bar(); }
+
+#--- main.m
+
+int foo() {}
+int bar() { foo(); }
+
+int main() { return bar(); }
+
+#--- commands.input
+settings set -f frame-format "custom-frame '${function.qualifiers}'\n"
+break set -n foo
+
+run
+bt
+
+# CHECK: custom-frame ' const volatile &&'
+# CHECK: custom-frame ''
+
+# CHECK-OBJC: bt
+# CHECK-OBJC-NOT: custom-frame
diff --git a/lldb/test/Shell/Settings/TestFrameFormatFunctionReturn.test b/lldb/test/Shell/Settings/TestFrameFormatFunctionReturn.test
new file mode 100644
index 0000000000000..321af8d53858f
--- /dev/null
+++ b/lldb/test/Shell/Settings/TestFrameFormatFunctionReturn.test
@@ -0,0 +1,76 @@
+# Test the ${function.return-left} and ${function.return-right}
+# frame-format variables.
+
+# RUN: split-file %s %t
+# RUN: %build %t/main.cpp -o %t.out
+# RUN: %lldb -o "settings set interpreter.stop-command-source-on-error false" \
+# RUN: -x -b -s %t/commands.input %t.out -o exit 2>&1 \
+# RUN: | FileCheck %s
+#
+# Check that we have an appropriate fallback for languages that
+# don't implement this frame format variable (in this case Objective-C).
+#
+# RUN: %build %t/main.m -o %t.objc.out
+# RUN: %lldb -x -b -s %t/commands.input %t.objc.out -o exit 2>&1 \
+# RUN: | FileCheck %s --check-prefix=CHECK-OBJC
+
+#--- main.cpp
+namespace ns::ns2 {
+template<typename T>
+struct Foo {};
+
+template<typename T>
+Foo<int> qux(int) {
+ return {};
+}
+
+template<typename T>
+Foo<int> (*bar(Foo<float>))(int) {
+ qux<T>(5);
+ return qux<T>;
+}
+
+struct Bar {
+ template<typename T>
+ Foo<int> (* (*foo(int) const &&)(Foo<float>))(int) {
+ bar<T>(Foo<float>{});
+ return bar<T>;
+ }
+};
+}
+
+int main(int argc, char const *argv[]) {
+ ns::ns2::Bar{}.foo<int>(5);
+ return 0;
+}
+
+#--- main.m
+
+int qux() {}
+int bar() { qux(); }
+
+int main() { return bar(); }
+
+#--- commands.input
+settings set -f frame-format "custom-frame '${function.return-left}'\n"
+break set -n qux
+
+run
+bt
+
+# CHECK: custom-frame 'ns::ns2::Foo<int> '
+# CHECK: custom-frame 'ns::ns2::Foo<int> (*'
+# CHECK: custom-frame 'ns::ns2::Foo<int> (* (*'
+
+# CHECK-OBJC: bt
+# CHECK-OBJC-NOT: custom-frame
+
+settings set -f frame-format "other-frame '${function.return-right}'\n"
+bt
+
+# CHECK: other-frame ''
+# CHECK: other-frame ')(int)'
+# CHECK: other-frame ')(ns::ns2::Foo<float>))(int)'
+
+# CHECK-OBJC: bt
+# CHECK-OBJC-NOT: other-frame
diff --git a/lldb/test/Shell/Settings/TestFrameFormatFunctionScope.test b/lldb/test/Shell/Settings/TestFrameFormatFunctionScope.test
new file mode 100644
index 0000000000000..9c459bd622051
--- /dev/null
+++ b/lldb/test/Shell/Settings/TestFrameFormatFunctionScope.test
@@ -0,0 +1,58 @@
+# Test the ${function.scope} frame-format variable.
+
+# RUN: split-file %s %t
+# RUN: %build %t/main.cpp -o %t.out
+# RUN: %lldb -o "settings set interpreter.stop-command-source-on-error false" \
+# RUN: -x -b -s %t/commands.input %t.out -o exit 2>&1 \
+# RUN: | FileCheck %s
+#
+# Check that we have an appropriate fallback for languages that
+# don't implement this frame format variable (in this case Objective-C).
+#
+# RUN: %build %t/main.m -o %t.objc.out
+# RUN: %lldb -x -b -s %t/commands.input %t.objc.out -o exit 2>&1 \
+# RUN: | FileCheck %s --check-prefix=CHECK-OBJC
+
+#--- main.cpp
+namespace ns::ns2 {
+inline namespace ins {
+template <typename T>
+struct Foo {
+ void func() {}
+};
+
+int foo() {
+ Foo<int>{}.func();
+ return 5;
+}
+} // namespace ins
+} // namespace ns::ns2
+
+using namespace ns::ns2;
+
+int bar() {
+ return ns::ns2::foo();
+}
+
+int main() { return bar(); }
+
+#--- main.m
+
+int func() {}
+int bar() { func(); }
+
+int main() { return bar(); }
+
+#--- commands.input
+settings set -f frame-format "custom-frame '${function.scope}'\n"
+break set -n func
+
+run
+bt
+
+# CHECK: frame 'ns::ns2::ins::Foo<int>::'
+# CHECK: frame 'ns::ns2::ins::'
+# CHECK: frame ''
+
+# CHECK-OBJC: bt
+# CHECK-OBJC-NOT: custom-frame
diff --git a/lldb/test/Shell/Settings/TestFrameFormatFunctionTemplateArguments.test b/lldb/test/Shell/Settings/TestFrameFormatFunctionTemplateArguments.test
new file mode 100644
index 0000000000000..23ba101b9b3f3
--- /dev/null
+++ b/lldb/test/Shell/Settings/TestFrameFormatFunctionTemplateArguments.test
@@ -0,0 +1,54 @@
+# Test the ${function.template-arguments} frame-format variable.
+
+# RUN: split-file %s %t
+# RUN: %build %t/main.cpp -o %t.cxx.out
+# RUN: %lldb -x -b -s %t/commands.input %t.cxx.out -o exit 2>&1 \
+# RUN: | FileCheck %s
+#
+# Check that we have an appropriate fallback for languages that
+# don't implement this frame format variable (in this case Objective-C).
+#
+# RUN: %build %t/main.m -o %t.objc.out
+# RUN: %lldb -x -b -s %t/commands.input %t.objc.out -o exit 2>&1 \
+# RUN: | FileCheck %s --check-prefix=CHECK-OBJC
+
+#--- main.cpp
+template<typename K>
+struct Foo {
+ template<typename T>
+ void func() {}
+};
+
+template<typename T, template <typename> class K,
+ typename M>
+int foo() {
+ Foo<int>{}.func<T>();
+ return 5;
+}
+
+int bar() {
+ return foo<int, Foo, Foo<float>>();
+}
+
+int main() { return bar(); }
+
+#--- main.m
+
+int func() {}
+int bar() { func(); }
+
+int main() { return bar(); }
+
+#--- commands.input
+settings set -f frame-format "custom-frame '${function.template-arguments}'\n"
+break set -n func
+
+run
+bt
+
+# CHECK: custom-frame '<int>'
+# CHECK: custom-frame '<int, Foo, Foo<float>>'
+# CHECK: custom-frame ''
+
+# CHECK-OBJC: bt
+# CHECK-OBJC-NOT: custom-frame
diff --git a/lldb/test/Shell/Settings/TestFrameFormatName.test b/lldb/test/Shell/Settings/TestFrameFormatName.test
index caa3242527c6e..24f5d29dbff3b 100644
--- a/lldb/test/Shell/Settings/TestFrameFormatName.test
+++ b/lldb/test/Shell/Settings/TestFrameFormatName.test
@@ -22,13 +22,13 @@ c
c
# NAME_WITH_ARGS: frame int ns::foo<void (Foo::*)(int (*)(int)) const noexcept>(str="method")
c
-# NAME_WITH_ARGS: frame ns::returns_func_ptr<int>((null)={{.*}})
+# NAME_WITH_ARGS: frame detail::Quux<double> (* (*ns::returns_func_ptr<int>((null)={{.*}}))(int))(float)
c
# NAME_WITH_ARGS: frame void Foo::foo<int (*)()>(this={{.*}}, arg=({{.*}}`(anonymous namespace)::anon_bar() at {{.*}}))
c
# NAME_WITH_ARGS: frame void Foo::operator<<<1>(this={{.*}}, (null)=0)
c
-# NAME_WITH_ARGS: frame Foo::returns_func_ptr<int>(this={{.*}}, (null)={{.*}})
+# NAME_WITH_ARGS: frame detail::Quux<double> (* (*Foo::returns_func_ptr<int>(this={{.*}}, (null)={{.*}}))(int))(float) const
c
# NAME_WITH_ARGS: frame main [inlined] inlined_foo(str="bar")
q
More information about the llvm-commits
mailing list