Skip to content

Commit d7e112e

Browse files
committed
[llvm][ItaniumDemangle] Add function name location tracking
1 parent 0a21ef9 commit d7e112e

File tree

8 files changed

+541
-56
lines changed

8 files changed

+541
-56
lines changed

libcxxabi/src/demangle/ItaniumDemangle.h

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ class Node {
281281
}
282282

283283
void print(OutputBuffer &OB) const {
284-
printLeft(OB);
284+
OB.printLeft(*this);
285285
if (RHSComponentCache != Cache::No)
286-
printRight(OB);
286+
OB.printRight(*this);
287287
}
288288

289289
// Print the "left" side of this Node into OutputBuffer.
@@ -458,11 +458,11 @@ class QualType final : public Node {
458458
}
459459

460460
void printLeft(OutputBuffer &OB) const override {
461-
Child->printLeft(OB);
461+
OB.printLeft(*Child);
462462
printQuals(OB);
463463
}
464464

465-
void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
465+
void printRight(OutputBuffer &OB) const override { OB.printRight(*Child); }
466466
};
467467

468468
class ConversionOperatorType final : public Node {
@@ -491,7 +491,7 @@ class PostfixQualifiedType final : public Node {
491491
template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
492492

493493
void printLeft(OutputBuffer &OB) const override {
494-
Ty->printLeft(OB);
494+
OB.printLeft(*Ty);
495495
OB += Postfix;
496496
}
497497
};
@@ -577,7 +577,7 @@ struct AbiTagAttr : Node {
577577
std::string_view getBaseName() const override { return Base->getBaseName(); }
578578

579579
void printLeft(OutputBuffer &OB) const override {
580-
Base->printLeft(OB);
580+
OB.printLeft(*Base);
581581
OB += "[abi:";
582582
OB += Tag;
583583
OB += "]";
@@ -644,7 +644,7 @@ class PointerType final : public Node {
644644
// We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
645645
if (Pointee->getKind() != KObjCProtoName ||
646646
!static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
647-
Pointee->printLeft(OB);
647+
OB.printLeft(*Pointee);
648648
if (Pointee->hasArray(OB))
649649
OB += " ";
650650
if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
@@ -663,7 +663,7 @@ class PointerType final : public Node {
663663
!static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
664664
if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
665665
OB += ")";
666-
Pointee->printRight(OB);
666+
OB.printRight(*Pointee);
667667
}
668668
}
669669
};
@@ -729,7 +729,7 @@ class ReferenceType : public Node {
729729
std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
730730
if (!Collapsed.second)
731731
return;
732-
Collapsed.second->printLeft(OB);
732+
OB.printLeft(*Collapsed.second);
733733
if (Collapsed.second->hasArray(OB))
734734
OB += " ";
735735
if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
@@ -746,7 +746,7 @@ class ReferenceType : public Node {
746746
return;
747747
if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
748748
OB += ")";
749-
Collapsed.second->printRight(OB);
749+
OB.printRight(*Collapsed.second);
750750
}
751751
};
752752

@@ -766,7 +766,7 @@ class PointerToMemberType final : public Node {
766766
}
767767

768768
void printLeft(OutputBuffer &OB) const override {
769-
MemberType->printLeft(OB);
769+
OB.printLeft(*MemberType);
770770
if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
771771
OB += "(";
772772
else
@@ -778,7 +778,7 @@ class PointerToMemberType final : public Node {
778778
void printRight(OutputBuffer &OB) const override {
779779
if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
780780
OB += ")";
781-
MemberType->printRight(OB);
781+
OB.printRight(*MemberType);
782782
}
783783
};
784784

@@ -798,7 +798,7 @@ class ArrayType final : public Node {
798798
bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
799799
bool hasArraySlow(OutputBuffer &) const override { return true; }
800800

801-
void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
801+
void printLeft(OutputBuffer &OB) const override { OB.printLeft(*Base); }
802802

803803
void printRight(OutputBuffer &OB) const override {
804804
if (OB.back() != ']')
@@ -807,7 +807,7 @@ class ArrayType final : public Node {
807807
if (Dimension)
808808
Dimension->print(OB);
809809
OB += "]";
810-
Base->printRight(OB);
810+
OB.printRight(*Base);
811811
}
812812

813813
bool printInitListAsType(OutputBuffer &OB,
@@ -851,15 +851,15 @@ class FunctionType final : public Node {
851851
// by printing out the return types's left, then print our parameters, then
852852
// finally print right of the return type.
853853
void printLeft(OutputBuffer &OB) const override {
854-
Ret->printLeft(OB);
854+
OB.printLeft(*Ret);
855855
OB += " ";
856856
}
857857

858858
void printRight(OutputBuffer &OB) const override {
859859
OB.printOpen();
860860
Params.printWithComma(OB);
861861
OB.printClose();
862-
Ret->printRight(OB);
862+
OB.printRight(*Ret);
863863

864864
if (CVQuals & QualConst)
865865
OB += " const";
@@ -964,6 +964,8 @@ class FunctionEncoding final : public Node {
964964
FunctionRefQual getRefQual() const { return RefQual; }
965965
NodeArray getParams() const { return Params; }
966966
const Node *getReturnType() const { return Ret; }
967+
const Node *getAttrs() const { return Attrs; }
968+
const Node *getRequires() const { return Requires; }
967969

968970
bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
969971
bool hasFunctionSlow(OutputBuffer &) const override { return true; }
@@ -972,19 +974,21 @@ class FunctionEncoding final : public Node {
972974

973975
void printLeft(OutputBuffer &OB) const override {
974976
if (Ret) {
975-
Ret->printLeft(OB);
977+
OB.printLeft(*Ret);
976978
if (!Ret->hasRHSComponent(OB))
977979
OB += " ";
978980
}
981+
979982
Name->print(OB);
980983
}
981984

982985
void printRight(OutputBuffer &OB) const override {
983986
OB.printOpen();
984987
Params.printWithComma(OB);
985988
OB.printClose();
989+
986990
if (Ret)
987-
Ret->printRight(OB);
991+
OB.printRight(*Ret);
988992

989993
if (CVQuals & QualConst)
990994
OB += " const";
@@ -1324,14 +1328,14 @@ class NonTypeTemplateParamDecl final : public Node {
13241328
template<typename Fn> void match(Fn F) const { F(Name, Type); }
13251329

13261330
void printLeft(OutputBuffer &OB) const override {
1327-
Type->printLeft(OB);
1331+
OB.printLeft(*Type);
13281332
if (!Type->hasRHSComponent(OB))
13291333
OB += " ";
13301334
}
13311335

13321336
void printRight(OutputBuffer &OB) const override {
13331337
Name->print(OB);
1334-
Type->printRight(OB);
1338+
OB.printRight(*Type);
13351339
}
13361340
};
13371341

@@ -1376,11 +1380,11 @@ class TemplateParamPackDecl final : public Node {
13761380
template<typename Fn> void match(Fn F) const { F(Param); }
13771381

13781382
void printLeft(OutputBuffer &OB) const override {
1379-
Param->printLeft(OB);
1383+
OB.printLeft(*Param);
13801384
OB += "...";
13811385
}
13821386

1383-
void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
1387+
void printRight(OutputBuffer &OB) const override { OB.printRight(*Param); }
13841388
};
13851389

13861390
/// An unexpanded parameter pack (either in the expression or type context). If
@@ -1445,13 +1449,13 @@ class ParameterPack final : public Node {
14451449
initializePackExpansion(OB);
14461450
size_t Idx = OB.CurrentPackIndex;
14471451
if (Idx < Data.size())
1448-
Data[Idx]->printLeft(OB);
1452+
OB.printLeft(*Data[Idx]);
14491453
}
14501454
void printRight(OutputBuffer &OB) const override {
14511455
initializePackExpansion(OB);
14521456
size_t Idx = OB.CurrentPackIndex;
14531457
if (Idx < Data.size())
1454-
Data[Idx]->printRight(OB);
1458+
OB.printRight(*Data[Idx]);
14551459
}
14561460
};
14571461

@@ -1609,13 +1613,13 @@ struct ForwardTemplateReference : Node {
16091613
if (Printing)
16101614
return;
16111615
ScopedOverride<bool> SavePrinting(Printing, true);
1612-
Ref->printLeft(OB);
1616+
OB.printLeft(*Ref);
16131617
}
16141618
void printRight(OutputBuffer &OB) const override {
16151619
if (Printing)
16161620
return;
16171621
ScopedOverride<bool> SavePrinting(Printing, true);
1618-
Ref->printRight(OB);
1622+
OB.printRight(*Ref);
16191623
}
16201624
};
16211625

@@ -1767,7 +1771,7 @@ class DtorName : public Node {
17671771

17681772
void printLeft(OutputBuffer &OB) const override {
17691773
OB += "~";
1770-
Base->printLeft(OB);
1774+
OB.printLeft(*Base);
17711775
}
17721776
};
17731777

@@ -2047,7 +2051,7 @@ class CastExpr : public Node {
20472051
{
20482052
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
20492053
OB += "<";
2050-
To->printLeft(OB);
2054+
OB.printLeft(*To);
20512055
OB += ">";
20522056
}
20532057
OB.printOpen();
@@ -6176,6 +6180,10 @@ struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
61766180
Alloc>::AbstractManglingParser;
61776181
};
61786182

6183+
inline void OutputBuffer::printLeft(const Node &N) { N.printLeft(*this); }
6184+
6185+
inline void OutputBuffer::printRight(const Node &N) { N.printRight(*this); }
6186+
61796187
DEMANGLE_NAMESPACE_END
61806188

61816189
#if defined(__clang__)

libcxxabi/src/demangle/Utility.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
DEMANGLE_NAMESPACE_BEGIN
2929

30+
class Node;
31+
3032
// Stream that AST nodes write their string representation into after the AST
3133
// has been parsed.
3234
class OutputBuffer {
@@ -79,10 +81,15 @@ class OutputBuffer {
7981
OutputBuffer(const OutputBuffer &) = delete;
8082
OutputBuffer &operator=(const OutputBuffer &) = delete;
8183

84+
virtual ~OutputBuffer() {}
85+
8286
operator std::string_view() const {
8387
return std::string_view(Buffer, CurrentPosition);
8488
}
8589

90+
virtual void printLeft(const Node &N);
91+
virtual void printRight(const Node &N);
92+
8693
/// If a ParameterPackExpansion (or similar type) is encountered, the offset
8794
/// into the pack that we're currently printing.
8895
unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();

lldb/include/lldb/Core/Demangle.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//===-- Demangle.h ----------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_CORE_DEMANGLE_H
10+
#define LLDB_CORE_DEMANGLE_H
11+
12+
#include "llvm/Demangle/ItaniumDemangle.h"
13+
#include "llvm/Demangle/Utility.h"
14+
15+
#include <cstddef>
16+
#include <utility>
17+
18+
namespace lldb_private {
19+
20+
struct TrackingOutputBuffer;
21+
22+
// Stores information about parts of a demangled function name.
23+
struct FunctionNameInfo {
24+
/// A [start, end) pair for the function basename.
25+
/// The basename is the name without scope qualifiers
26+
/// and without template parameters. E.g.,
27+
/// \code{.cpp}
28+
/// void foo::bar<int>::someFunc<float>(int) const &&
29+
/// ^ ^
30+
/// start end
31+
/// \endcode
32+
std::pair<size_t, size_t> BasenameLocs;
33+
34+
/// A [start, end) pair for the function scope qualifiers.
35+
/// E.g., for
36+
/// \code{.cpp}
37+
/// void foo::bar<int>::qux<float>(int) const &&
38+
/// ^ ^
39+
/// start end
40+
/// \endcode
41+
std::pair<size_t, size_t> ScopeLocs;
42+
43+
/// Indicates the [start, end) of the function argument lits.
44+
/// E.g.,
45+
/// \code{.cpp}
46+
/// int (*getFunc<float>(float, double))(int, int)
47+
/// ^ ^
48+
/// start end
49+
/// \endcode
50+
std::pair<size_t, size_t> ArgumentLocs;
51+
52+
bool startedPrintingArguments() const;
53+
bool shouldTrack(TrackingOutputBuffer &OB) const;
54+
bool canFinalize(TrackingOutputBuffer &OB) const;
55+
void updateBasenameEnd(TrackingOutputBuffer &OB);
56+
void updateScopeStart(TrackingOutputBuffer &OB);
57+
void updateScopeEnd(TrackingOutputBuffer &OB);
58+
void finalizeArgumentEnd(TrackingOutputBuffer &OB);
59+
void finalizeStart(TrackingOutputBuffer &OB);
60+
void finalizeEnd(TrackingOutputBuffer &OB);
61+
bool hasBasename() const;
62+
};
63+
64+
struct TrackingOutputBuffer : public llvm::itanium_demangle::OutputBuffer {
65+
using OutputBuffer::OutputBuffer;
66+
67+
FunctionNameInfo FunctionInfo;
68+
unsigned FunctionPrintingDepth = 0;
69+
70+
[[nodiscard]] llvm::itanium_demangle::ScopedOverride<unsigned>
71+
enterFunctionTypePrinting();
72+
73+
bool isPrintingTopLevelFunctionType() const;
74+
75+
void printLeft(const llvm::itanium_demangle::Node &N) override;
76+
void printRight(const llvm::itanium_demangle::Node &N) override;
77+
78+
private:
79+
void printLeftImpl(const llvm::itanium_demangle::FunctionType &N);
80+
void printRightImpl(const llvm::itanium_demangle::FunctionType &N);
81+
82+
void printLeftImpl(const llvm::itanium_demangle::FunctionEncoding &N);
83+
void printRightImpl(const llvm::itanium_demangle::FunctionEncoding &N);
84+
85+
void printLeftImpl(const llvm::itanium_demangle::NestedName &N);
86+
void printLeftImpl(const llvm::itanium_demangle::NameWithTemplateArgs &N);
87+
};
88+
} // namespace lldb_private
89+
90+
#endif // LLDB_CORE_DEMANGLE_H

lldb/source/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_lldb_library(lldbCore
2828
Debugger.cpp
2929
DebuggerEvents.cpp
3030
Declaration.cpp
31+
Demangle.cpp
3132
Disassembler.cpp
3233
DumpDataExtractor.cpp
3334
DumpRegisterValue.cpp

0 commit comments

Comments
 (0)