From c6000317f1cef3069315de81f918e1ac556e2ace Mon Sep 17 00:00:00 2001 From: Chris Mihelich Date: Fri, 7 Jun 2024 11:34:21 -0700 Subject: Demangle in types, e.g., U5AS128 for address_space(128). PiperOrigin-RevId: 641310017 Change-Id: I0f10a2a1965e842db4efda455e0cdfbeb6656fa5 --- absl/debugging/internal/demangle.cc | 24 +++++++++++++++------- absl/debugging/internal/demangle_test.cc | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/absl/debugging/internal/demangle.cc b/absl/debugging/internal/demangle.cc index 812efe13..6507afb5 100644 --- a/absl/debugging/internal/demangle.cc +++ b/absl/debugging/internal/demangle.cc @@ -598,6 +598,7 @@ static bool ParseCtorDtorName(State *state); static bool ParseDecltype(State *state); static bool ParseType(State *state); static bool ParseCVQualifiers(State *state); +static bool ParseExtendedQualifier(State *state); static bool ParseBuiltinType(State *state); static bool ParseVendorExtendedType(State *state); static bool ParseFunctionType(State *state); @@ -1322,7 +1323,6 @@ static bool ParseDecltype(State *state) { // ::= O # rvalue reference-to (C++0x) // ::= C # complex pair (C 2000) // ::= G # imaginary (C 2000) -// ::= U # vendor extended type qualifier // ::= // ::= // ::= # note: just an alias for @@ -1378,12 +1378,6 @@ static bool ParseType(State *state) { } state->parse_state = copy; - if (ParseOneCharToken(state, 'U') && ParseSourceName(state) && - ParseType(state)) { - return true; - } - state->parse_state = copy; - if (ParseBuiltinType(state) || ParseFunctionType(state) || ParseClassEnumType(state) || ParseArrayType(state) || ParsePointerToMemberType(state) || ParseDecltype(state) || @@ -1427,19 +1421,35 @@ static bool ParseType(State *state) { return ParseLongToken(state, "_SUBSTPACK_"); } +// ::= * // ::= [r] [V] [K] +// // We don't allow empty to avoid infinite loop in // ParseType(). static bool ParseCVQualifiers(State *state) { ComplexityGuard guard(state); if (guard.IsTooComplex()) return false; int num_cv_qualifiers = 0; + while (ParseExtendedQualifier(state)) ++num_cv_qualifiers; num_cv_qualifiers += ParseOneCharToken(state, 'r'); num_cv_qualifiers += ParseOneCharToken(state, 'V'); num_cv_qualifiers += ParseOneCharToken(state, 'K'); return num_cv_qualifiers > 0; } +// ::= U [] +static bool ParseExtendedQualifier(State *state) { + ComplexityGuard guard(state); + if (guard.IsTooComplex()) return false; + ParseState copy = state->parse_state; + if (ParseOneCharToken(state, 'U') && ParseSourceName(state) && + Optional(ParseTemplateArgs(state))) { + return true; + } + state->parse_state = copy; + return false; +} + // ::= v, etc. # single-character builtin types // ::= // ::= Dd, etc. # two-character builtin types diff --git a/absl/debugging/internal/demangle_test.cc b/absl/debugging/internal/demangle_test.cc index 3285f2ca..9100d5f1 100644 --- a/absl/debugging/internal/demangle_test.cc +++ b/absl/debugging/internal/demangle_test.cc @@ -760,6 +760,40 @@ TEST(Demangle, GnuVectorSizeIsADependentOperatorExpression) { EXPECT_STREQ("f<>()", tmp); } +TEST(Demangle, SimpleAddressSpace) { + char tmp[80]; + + // Source: + // + // void f(const int __attribute__((address_space(128)))*) {} + // + // LLVM demangling: + // + // f(int const AS128*) + // + // Itanium ABI 5.1.5.1, "Qualified types", notes that address_space is mangled + // nonuniformly as a legacy exception: the number is part of the source-name + // if nondependent but is an expression in template-args if dependent. Thus + // it is a convenient test case for both forms. + EXPECT_TRUE(Demangle("_Z1fPU5AS128Ki", tmp, sizeof(tmp))); + EXPECT_STREQ("f()", tmp); +} + +TEST(Demangle, DependentAddressSpace) { + char tmp[80]; + + // Source: + // + // template void f (const int __attribute__((address_space(n)))*) {} + // template void f<128>(const int __attribute__((address_space(128)))*); + // + // LLVM demangling: + // + // void f<128>(int AS<128>*) + EXPECT_TRUE(Demangle("_Z1fILi128EEvPU2ASIT_Ei", tmp, sizeof(tmp))); + EXPECT_STREQ("f<>()", tmp); +} + TEST(Demangle, TransactionSafeEntryPoint) { char tmp[80]; -- cgit v1.2.3