Skip to content

Commit d92a58f

Browse files
charles-zablitaugusto2112
authored andcommitted
add NameFormatPreference enum
1 parent e204eeb commit d92a58f

File tree

3 files changed

+55
-42
lines changed

3 files changed

+55
-42
lines changed

lldb/include/lldb/Core/Mangled.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class Mangled {
3939
ePreferDemangledWithoutArguments
4040
};
4141

42+
enum NameFormatPreference {
43+
eCompactName,
44+
eFullName,
45+
};
46+
4247
enum ManglingScheme {
4348
eManglingSchemeNone = 0,
4449
eManglingSchemeMSVC,
@@ -125,7 +130,9 @@ class Mangled {
125130
///
126131
/// \return
127132
/// A const reference to the demangled name string object.
128-
ConstString GetDemangledName(const SymbolContext *sc = nullptr) const;
133+
ConstString
134+
GetDemangledName(const SymbolContext *sc = nullptr,
135+
NameFormatPreference preference = eFullName) const;
129136

130137
/// Display demangled name get accessor.
131138
///
@@ -291,7 +298,9 @@ class Mangled {
291298
/// demangled name (if any). If \c force is \c true (or the mangled name
292299
/// on this object was not previously demangled), demangle and cache the
293300
/// name.
294-
ConstString GetDemangledNameImpl(bool force, const SymbolContext *sc = nullptr) const;
301+
ConstString
302+
GetDemangledNameImpl(bool force, const SymbolContext *sc = nullptr,
303+
NameFormatPreference preference = eFullName) const;
295304

296305
/// The mangled version of the name.
297306
ConstString m_mangled;

lldb/source/Core/Mangled.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,32 @@ void Mangled::SetValue(ConstString name) {
152152
#ifdef LLDB_ENABLE_SWIFT
153153
std::pair<ConstString, DemangledNameInfo>
154154
GetSwiftDemangledStr(ConstString m_mangled, const SymbolContext *sc,
155-
ConstString &m_demangled) {
155+
ConstString &m_demangled,
156+
Mangled::NameFormatPreference preference) {
156157
const char *mangled_name = m_mangled.AsCString("");
157158
Log *log = GetLog(LLDBLog::Demangle);
158159
LLDB_LOGF(log, "demangle swift: %s", mangled_name);
160+
SwiftLanguageRuntime::DemangleMode demangle_mode;
161+
switch (preference) {
162+
case Mangled::eFullName:
163+
demangle_mode = SwiftLanguageRuntime::DemangleMode::eTypeName;
164+
break;
165+
case Mangled::eCompactName:
166+
demangle_mode = SwiftLanguageRuntime::DemangleMode::eSimplified;
167+
break;
168+
}
159169
auto [demangled, info] = SwiftLanguageRuntime::TrackedDemangleSymbolAsString(
160-
mangled_name, SwiftLanguageRuntime::eTypeName, sc);
170+
mangled_name, demangle_mode, sc);
161171
info.PrefixRange.second =
162172
std::min(info.BasenameRange.first, info.ArgumentsRange.first);
163173
info.SuffixRange.first =
164174
std::max(info.BasenameRange.second, info.ArgumentsRange.second);
165175
info.SuffixRange.second = demangled.length();
166176

167-
// Don't cache the demangled name the function isn't available yet.
168-
if (!sc || !sc->function) {
177+
// Don't cache the demangled name if the function isn't available yet.
178+
// Only cache eFullName demangled functions to keep the cache consistent.
179+
if (!sc || !sc->function ||
180+
preference == Mangled::NameFormatPreference::eCompactName) {
169181
LLDB_LOGF(log, "demangle swift: %s -> \"%s\" (not cached)", mangled_name,
170182
demangled.c_str());
171183
return std::make_pair(ConstString(demangled), info);
@@ -327,12 +339,11 @@ bool Mangled::GetRichManglingInfo(RichManglingContext &context,
327339
llvm_unreachable("Fully covered switch above!");
328340
}
329341

330-
ConstString Mangled::GetDemangledName(
331-
// BEGIN SWIFT
332-
const SymbolContext *sc
342+
ConstString Mangled::GetDemangledName( // BEGIN SWIFT
343+
const SymbolContext *sc, NameFormatPreference preference
333344
// END SWIFT
334345
) const {
335-
return GetDemangledNameImpl(/*force=*/false, sc);
346+
return GetDemangledNameImpl(/*force=*/false, sc, preference);
336347
}
337348

338349
std::optional<DemangledNameInfo> const &Mangled::GetDemangledInfo() const {
@@ -346,11 +357,10 @@ std::optional<DemangledNameInfo> const &Mangled::GetDemangledInfo() const {
346357
// class will need to use this accessor if it wishes to decode the demangled
347358
// name. The result is cached and will be kept until a new string value is
348359
// supplied to this object, or until the end of the object's lifetime.
349-
ConstString Mangled::GetDemangledNameImpl(
350-
bool force
351-
// BEGIN SWIFT
352-
, const SymbolContext *sc
353-
// END SWIFT
360+
ConstString Mangled::GetDemangledNameImpl(bool force, // BEGIN SWIFT
361+
const SymbolContext *sc,
362+
NameFormatPreference preference
363+
// END SWIFT
354364
) const {
355365
if (!m_mangled)
356366
return m_demangled;
@@ -388,7 +398,8 @@ ConstString Mangled::GetDemangledNameImpl(
388398
// explicitly unsupported on llvm.org.
389399
#ifdef LLDB_ENABLE_SWIFT
390400
{
391-
auto demangled = GetSwiftDemangledStr(m_mangled, sc, m_demangled);
401+
auto demangled =
402+
GetSwiftDemangledStr(m_mangled, sc, m_demangled, preference);
392403
m_demangled_info.emplace(std::move(demangled.second));
393404
return demangled.first;
394405
}

lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,50 +1911,46 @@ SwiftLanguage::GetDemangledFunctionNameWithoutArguments(Mangled mangled) const {
19111911
return mangled_name;
19121912
}
19131913

1914-
static llvm::Expected<std::pair<std::string, DemangledNameInfo>>
1914+
static llvm::Expected<std::pair<llvm::StringRef, DemangledNameInfo>>
19151915
GetAndValidateInfo(const SymbolContext &sc) {
19161916
Mangled mangled = sc.GetPossiblyInlinedFunctionName();
19171917
if (!mangled)
19181918
return llvm::createStringError("Function does not have a mangled name.");
19191919

1920-
const char *mangled_name = mangled.GetMangledName().AsCString("");
1921-
auto [demangled_name, info] =
1922-
SwiftLanguageRuntime::TrackedDemangleSymbolAsString(
1923-
mangled_name, SwiftLanguageRuntime::eSimplified, &sc);
1924-
info.PrefixRange.second =
1925-
std::min(info.BasenameRange.first, info.ArgumentsRange.first);
1926-
info.SuffixRange.first =
1927-
std::max(info.BasenameRange.second, info.ArgumentsRange.second);
1928-
info.SuffixRange.second = demangled_name.length();
1929-
1920+
auto demangled_name =
1921+
mangled.GetDemangledName(nullptr, Mangled::eCompactName).GetStringRef();
19301922
if (demangled_name.empty())
19311923
return llvm::createStringError(
19321924
"Function '%s' does not have a demangled name.",
19331925
mangled.GetMangledName().AsCString(""));
19341926

1927+
const std::optional<DemangledNameInfo> &info = mangled.GetDemangledInfo();
1928+
if (!info)
1929+
return llvm::createStringError(
1930+
"Function '%s' does not have demangled info.", demangled_name.data());
1931+
19351932
// Function without a basename is nonsense.
1936-
if (!info.hasBasename())
1933+
if (!info->hasBasename())
19371934
return llvm::createStringError(
19381935
"DemangledInfo for '%s does not have basename range.",
19391936
demangled_name.data());
19401937

1941-
return std::make_pair(demangled_name, info);
1938+
return std::make_pair(demangled_name, *info);
19421939
}
19431940

1944-
static llvm::Expected<std::string>
1941+
static llvm::Expected<llvm::StringRef>
19451942
GetDemangledBasename(const SymbolContext &sc) {
19461943
auto info_or_err = GetAndValidateInfo(sc);
19471944
if (!info_or_err)
19481945
return info_or_err.takeError();
19491946

19501947
auto [demangled_name, info] = *info_or_err;
19511948

1952-
return demangled_name.substr(info.BasenameRange.first,
1953-
info.BasenameRange.second -
1954-
info.BasenameRange.first);
1949+
return demangled_name.slice(info.BasenameRange.first,
1950+
info.BasenameRange.second);
19551951
}
19561952

1957-
static llvm::Expected<std::string>
1953+
static llvm::Expected<llvm::StringRef>
19581954
GetDemangledFunctionPrefix(const SymbolContext &sc) {
19591955
auto info_or_err = GetAndValidateInfo(sc);
19601956
if (!info_or_err)
@@ -1967,11 +1963,10 @@ GetDemangledFunctionPrefix(const SymbolContext &sc) {
19671963
"DemangledInfo for '%s does not have suffix range.",
19681964
demangled_name.data());
19691965

1970-
return demangled_name.substr(
1971-
info.PrefixRange.first, info.PrefixRange.second - info.PrefixRange.first);
1966+
return demangled_name.slice(info.PrefixRange.first, info.PrefixRange.second);
19721967
}
19731968

1974-
static llvm::Expected<std::string>
1969+
static llvm::Expected<llvm::StringRef>
19751970
GetDemangledFunctionSuffix(const SymbolContext &sc) {
19761971
auto info_or_err = GetAndValidateInfo(sc);
19771972
if (!info_or_err)
@@ -1984,8 +1979,7 @@ GetDemangledFunctionSuffix(const SymbolContext &sc) {
19841979
"DemangledInfo for '%s does not have suffix range.",
19851980
demangled_name.data());
19861981

1987-
return demangled_name.substr(
1988-
info.SuffixRange.first, info.SuffixRange.second - info.SuffixRange.first);
1982+
return demangled_name.slice(info.SuffixRange.first, info.SuffixRange.second);
19891983
}
19901984

19911985
static bool PrintDemangledArgumentList(Stream &s, const SymbolContext &sc) {
@@ -2003,9 +1997,8 @@ static bool PrintDemangledArgumentList(Stream &s, const SymbolContext &sc) {
20031997
if (!info.hasArguments())
20041998
return false;
20051999

2006-
s << demangled_name.substr(info.ArgumentsRange.first,
2007-
info.ArgumentsRange.second -
2008-
info.ArgumentsRange.first);
2000+
s << demangled_name.slice(info.ArgumentsRange.first,
2001+
info.ArgumentsRange.second);
20092002

20102003
return true;
20112004
}

0 commit comments

Comments
 (0)