Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions libcxxabi/src/demangle/ItaniumDemangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ class TemplateTemplateParamDecl final : public Node {
template <typename Fn> void match(Fn F) const { F(Name, Params, Requires); }

void printLeft(OutputBuffer &OB) const override {
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
ScopedOverride<bool> LT(OB.TemplateTracker.InsideTemplate, true);
OB += "template<";
Params.printWithComma(OB);
OB += "> typename ";
Expand Down Expand Up @@ -1550,7 +1550,7 @@ class TemplateArgs final : public Node {
NodeArray getParams() { return Params; }

void printLeft(OutputBuffer &OB) const override {
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
ScopedOverride<bool> LT(OB.TemplateTracker.InsideTemplate, true);
OB += "<";
Params.printWithComma(OB);
OB += ">";
Expand Down Expand Up @@ -1824,7 +1824,7 @@ class ClosureTypeName : public Node {

void printDeclarator(OutputBuffer &OB) const {
if (!TemplateParams.empty()) {
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
ScopedOverride<bool> LT(OB.TemplateTracker.InsideTemplate, true);
OB += "<";
TemplateParams.printWithComma(OB);
OB += ">";
Expand Down Expand Up @@ -1885,7 +1885,9 @@ class BinaryExpr : public Node {
}

void printLeft(OutputBuffer &OB) const override {
bool ParenAll = OB.isGtInsideTemplateArgs() &&
// If we're printing a '<' inside of a template argument, and we haven't
// yet parenthesized the expression, do so now.
bool ParenAll = !OB.isInParensInTemplateArgs() &&
(InfixOperator == ">" || InfixOperator == ">>");
if (ParenAll)
OB.printOpen();
Expand Down Expand Up @@ -2061,7 +2063,7 @@ class CastExpr : public Node {
void printLeft(OutputBuffer &OB) const override {
OB += CastKind;
{
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
ScopedOverride<bool> LT(OB.TemplateTracker.InsideTemplate, true);
OB += "<";
OB.printLeft(*To);
OB += ">";
Expand Down
26 changes: 20 additions & 6 deletions libcxxabi/src/demangle/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,32 @@ class OutputBuffer {
unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
unsigned CurrentPackMax = std::numeric_limits<unsigned>::max();

/// When zero, we're printing template args and '>' needs to be parenthesized.
/// Use a counter so we can simply increment inside parentheses.
unsigned GtIsGt = 1;
struct {
/// The depth of '(' and ')' inside the currently printed template
/// arguments.
unsigned ParenDepth = 0;

bool isGtInsideTemplateArgs() const { return GtIsGt == 0; }
/// True if we're currently printing a template argument.
bool InsideTemplate = false;
} TemplateTracker;

/// Returns true if we're currently between a '(' and ')' when printing
/// template args.
bool isInParensInTemplateArgs() const {
return TemplateTracker.ParenDepth > 0;
}

/// Returns true if we're printing template args.
bool isInsideTemplateArgs() const { return TemplateTracker.InsideTemplate; }

void printOpen(char Open = '(') {
GtIsGt++;
if (isInsideTemplateArgs())
TemplateTracker.ParenDepth++;
*this += Open;
}
void printClose(char Close = ')') {
GtIsGt--;
if (isInsideTemplateArgs())
TemplateTracker.ParenDepth--;
*this += Close;
}

Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Core/DemangledNameInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bool TrackingOutputBuffer::shouldTrack() const {
if (!isPrintingTopLevelFunctionType())
return false;

if (isGtInsideTemplateArgs())
if (isInsideTemplateArgs())
return false;

if (NameInfo.ArgumentsRange.first > 0)
Expand All @@ -29,7 +29,7 @@ bool TrackingOutputBuffer::canFinalize() const {
if (!isPrintingTopLevelFunctionType())
return false;

if (isGtInsideTemplateArgs())
if (isInsideTemplateArgs())
return false;

if (NameInfo.ArgumentsRange.first == 0)
Expand Down
10 changes: 10 additions & 0 deletions lldb/unittests/Core/MangledTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,16 @@ DemanglingPartsTestCase g_demangling_parts_test_cases[] = {
/*.basename=*/"operator()",
/*.scope=*/"dyld4::Loader::runInitializersBottomUpPlusUpwardLinks(dyld4::RuntimeState&) const::$_0::",
/*.qualifiers=*/" const",
},
{"_Z4funcILN3foo4EnumE1EEvv",
{
/*.BasenameRange=*/{5, 9}, /*.TemplateArgumentsRange=*/{9, 23}, /*.ScopeRange=*/{5, 5},
/*.ArgumentsRange=*/{23, 25}, /*.QualifiersRange=*/{25, 25}, /*.NameQualifiersRange=*/{0, 0},
/*.PrefixRange=*/{0, 0}, /*.SuffixRange=*/{0, 0}
},
/*.basename=*/"func",
/*.scope=*/"",
/*.qualifiers=*/"",
}
// clang-format on
};
Expand Down
12 changes: 7 additions & 5 deletions llvm/include/llvm/Demangle/ItaniumDemangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ class TemplateTemplateParamDecl final : public Node {
template <typename Fn> void match(Fn F) const { F(Name, Params, Requires); }

void printLeft(OutputBuffer &OB) const override {
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
ScopedOverride<bool> LT(OB.TemplateTracker.InsideTemplate, true);
OB += "template<";
Params.printWithComma(OB);
OB += "> typename ";
Expand Down Expand Up @@ -1550,7 +1550,7 @@ class TemplateArgs final : public Node {
NodeArray getParams() { return Params; }

void printLeft(OutputBuffer &OB) const override {
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
ScopedOverride<bool> LT(OB.TemplateTracker.InsideTemplate, true);
OB += "<";
Params.printWithComma(OB);
OB += ">";
Expand Down Expand Up @@ -1824,7 +1824,7 @@ class ClosureTypeName : public Node {

void printDeclarator(OutputBuffer &OB) const {
if (!TemplateParams.empty()) {
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
ScopedOverride<bool> LT(OB.TemplateTracker.InsideTemplate, true);
OB += "<";
TemplateParams.printWithComma(OB);
OB += ">";
Expand Down Expand Up @@ -1885,7 +1885,9 @@ class BinaryExpr : public Node {
}

void printLeft(OutputBuffer &OB) const override {
bool ParenAll = OB.isGtInsideTemplateArgs() &&
// If we're printing a '<' inside of a template argument, and we haven't
// yet parenthesized the expression, do so now.
bool ParenAll = !OB.isInParensInTemplateArgs() &&
(InfixOperator == ">" || InfixOperator == ">>");
if (ParenAll)
OB.printOpen();
Expand Down Expand Up @@ -2061,7 +2063,7 @@ class CastExpr : public Node {
void printLeft(OutputBuffer &OB) const override {
OB += CastKind;
{
ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
ScopedOverride<bool> LT(OB.TemplateTracker.InsideTemplate, true);
OB += "<";
OB.printLeft(*To);
OB += ">";
Expand Down
26 changes: 20 additions & 6 deletions llvm/include/llvm/Demangle/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,32 @@ class OutputBuffer {
unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
unsigned CurrentPackMax = std::numeric_limits<unsigned>::max();

/// When zero, we're printing template args and '>' needs to be parenthesized.
/// Use a counter so we can simply increment inside parentheses.
unsigned GtIsGt = 1;
struct {
/// The depth of '(' and ')' inside the currently printed template
/// arguments.
unsigned ParenDepth = 0;

bool isGtInsideTemplateArgs() const { return GtIsGt == 0; }
/// True if we're currently printing a template argument.
bool InsideTemplate = false;
} TemplateTracker;

/// Returns true if we're currently between a '(' and ')' when printing
/// template args.
bool isInParensInTemplateArgs() const {
return TemplateTracker.ParenDepth > 0;
}

/// Returns true if we're printing template args.
bool isInsideTemplateArgs() const { return TemplateTracker.InsideTemplate; }

void printOpen(char Open = '(') {
GtIsGt++;
if (isInsideTemplateArgs())
TemplateTracker.ParenDepth++;
*this += Open;
}
void printClose(char Close = ')') {
GtIsGt--;
if (isInsideTemplateArgs())
TemplateTracker.ParenDepth--;
*this += Close;
}

Expand Down