-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[LLDB] Consolidate C++ string buffer summaries #144258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Michael137
merged 1 commit into
llvm:main
from
Nerixyz:refactor/lldb-string-buffer-summary
Jun 17, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,15 +116,7 @@ bool lldb_private::formatters::WCharStringSummaryProvider( | |
return false; | ||
|
||
// Get a wchar_t basic type from the current type system | ||
CompilerType wchar_compiler_type = | ||
valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeWChar); | ||
|
||
if (!wchar_compiler_type) | ||
return false; | ||
|
||
// Safe to pass nullptr for exe_scope here. | ||
std::optional<uint64_t> size = | ||
llvm::expectedToOptional(wchar_compiler_type.GetBitSize(nullptr)); | ||
std::optional<uint64_t> size = GetWCharByteSize(valobj); | ||
if (!size) | ||
return false; | ||
const uint32_t wchar_size = *size; | ||
|
@@ -136,13 +128,13 @@ bool lldb_private::formatters::WCharStringSummaryProvider( | |
options.SetPrefixToken("L"); | ||
|
||
switch (wchar_size) { | ||
case 8: | ||
case 1: | ||
return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8>( | ||
options); | ||
case 16: | ||
case 2: | ||
return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16>( | ||
options); | ||
case 32: | ||
case 4: | ||
return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32>( | ||
options); | ||
default: | ||
|
@@ -177,15 +169,7 @@ bool lldb_private::formatters::WCharSummaryProvider( | |
return false; | ||
|
||
// Get a wchar_t basic type from the current type system | ||
CompilerType wchar_compiler_type = | ||
valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeWChar); | ||
|
||
if (!wchar_compiler_type) | ||
return false; | ||
|
||
// Safe to pass nullptr for exe_scope here. | ||
std::optional<uint64_t> size = | ||
llvm::expectedToOptional(wchar_compiler_type.GetBitSize(nullptr)); | ||
std::optional<uint64_t> size = GetWCharByteSize(valobj); | ||
if (!size) | ||
return false; | ||
const uint32_t wchar_size = *size; | ||
|
@@ -199,13 +183,13 @@ bool lldb_private::formatters::WCharSummaryProvider( | |
options.SetBinaryZeroIsTerminator(false); | ||
|
||
switch (wchar_size) { | ||
case 8: | ||
case 1: | ||
return StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF8>( | ||
options); | ||
case 16: | ||
case 2: | ||
return StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16>( | ||
options); | ||
case 32: | ||
case 4: | ||
return StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF32>( | ||
options); | ||
default: | ||
|
@@ -214,3 +198,73 @@ bool lldb_private::formatters::WCharSummaryProvider( | |
} | ||
return true; | ||
} | ||
|
||
std::optional<uint64_t> | ||
lldb_private::formatters::GetWCharByteSize(ValueObject &valobj) { | ||
return llvm::expectedToOptional( | ||
valobj.GetCompilerType() | ||
.GetBasicTypeFromAST(lldb::eBasicTypeWChar) | ||
.GetByteSize(nullptr)); | ||
} | ||
|
||
template <StringPrinter::StringElementType element_type> | ||
bool lldb_private::formatters::StringBufferSummaryProvider( | ||
Stream &stream, const TypeSummaryOptions &summary_options, | ||
lldb::ValueObjectSP location_sp, uint64_t size, std::string prefix_token) { | ||
|
||
if (size == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah i see, this is where the tests get fixed |
||
stream.PutCString(prefix_token); | ||
stream.PutCString("\"\""); | ||
return true; | ||
} | ||
|
||
if (!location_sp) | ||
return false; | ||
|
||
StringPrinter::ReadBufferAndDumpToStreamOptions options(*location_sp); | ||
|
||
if (summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryCapped) { | ||
const auto max_size = | ||
location_sp->GetTargetSP()->GetMaximumSizeOfStringSummary(); | ||
if (size > max_size) { | ||
size = max_size; | ||
options.SetIsTruncated(true); | ||
} | ||
} | ||
|
||
{ | ||
DataExtractor extractor; | ||
const size_t bytes_read = location_sp->GetPointeeData(extractor, 0, size); | ||
if (bytes_read < size) | ||
return false; | ||
|
||
options.SetData(std::move(extractor)); | ||
} | ||
options.SetStream(&stream); | ||
if (prefix_token.empty()) | ||
options.SetPrefixToken(nullptr); | ||
else | ||
options.SetPrefixToken(prefix_token); | ||
options.SetQuote('"'); | ||
options.SetSourceSize(size); | ||
options.SetBinaryZeroIsTerminator(false); | ||
return StringPrinter::ReadBufferAndDumpToStream<element_type>(options); | ||
} | ||
|
||
// explicit instantiations for all string element types | ||
template bool | ||
lldb_private::formatters::StringBufferSummaryProvider<StringElementType::ASCII>( | ||
Stream &, const TypeSummaryOptions &, lldb::ValueObjectSP, uint64_t, | ||
std::string); | ||
template bool | ||
lldb_private::formatters::StringBufferSummaryProvider<StringElementType::UTF8>( | ||
Stream &, const TypeSummaryOptions &, lldb::ValueObjectSP, uint64_t, | ||
std::string); | ||
template bool | ||
lldb_private::formatters::StringBufferSummaryProvider<StringElementType::UTF16>( | ||
Stream &, const TypeSummaryOptions &, lldb::ValueObjectSP, uint64_t, | ||
std::string); | ||
template bool | ||
lldb_private::formatters::StringBufferSummaryProvider<StringElementType::UTF32>( | ||
Stream &, const TypeSummaryOptions &, lldb::ValueObjectSP, uint64_t, | ||
std::string); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm so this switch was always wrong in the past? Is this why the test changes had to be made?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this wasn't wrong. It used the bit size before, and the libc++ summary used the byte size. I'm not sure which is better (I suppose 8 bits/byte is true for all targets supported by LLDB).
This reminds me of another difference: The
*CharSummaryProvider
s usedvalobj.GetCompilerType().GetBasicTypeFromAST()
and the libc++ formatters usedScratchTypeSystemClang::GetBasicType()
. Is there a big difference between them, and is one preferred over the other?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. Making them consistent makes sense
The
ScratchTypeSystemClang
is the AST that is shared between all expressions run in the target. Whereas theTypeSystem
underlying aCompilerType
only contains the AST nodes created when theValueObject
and its type were created. It shouldn't make a difference for primitive types. And especially shouldn't make a difference when all we need to do is get the bytesize. That being said, I'd prefer usingvalobj.GetCompilerType().GetBasicTypeFromAST()
(assuming that doesn't break anything). Reaching into the scratch typesystem feels weird hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refactored it to use
valobj.GetCompilerType().GetBasicTypeFromAST()
now.