[lldb] Refactor GetFormatFromCString to always check for partial matches (NFC) (#81018)

Refactors logic in `ParseInternal` that was previously calling
`GetFormatFromCString` twice, once with `partial_match_ok` set to false,
and the second time set to true.

With this change, lldb formats (ie `%@`, `%S`, etc) are checked first.
If a format is not one of those, then `GetFormatFromCString` is called
once, and now always checks for partial matches.
This commit is contained in:
Dave Lee 2024-02-08 09:32:12 -08:00 committed by GitHub
parent f219cda7bd
commit af97edff70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 28 deletions

View File

@ -138,7 +138,7 @@ public:
} }
static bool GetFormatFromCString(const char *format_cstr, static bool GetFormatFromCString(const char *format_cstr,
bool partial_match_ok, lldb::Format &format); lldb::Format &format);
static char GetFormatAsFormatChar(lldb::Format format); static char GetFormatAsFormatChar(lldb::Format format);

View File

@ -2151,11 +2151,7 @@ static Status ParseInternal(llvm::StringRef &format, Entry &parent_entry,
if (entry.printf_format.find('%') == std::string::npos) { if (entry.printf_format.find('%') == std::string::npos) {
bool clear_printf = false; bool clear_printf = false;
if (FormatManager::GetFormatFromCString( if (entry.printf_format.size() == 1) {
entry.printf_format.c_str(), false, entry.fmt)) {
// We have an LLDB format, so clear the printf format
clear_printf = true;
} else if (entry.printf_format.size() == 1) {
switch (entry.printf_format[0]) { switch (entry.printf_format[0]) {
case '@': // if this is an @ sign, print ObjC description case '@': // if this is an @ sign, print ObjC description
entry.number = ValueObject:: entry.number = ValueObject::
@ -2198,20 +2194,20 @@ static Status ParseInternal(llvm::StringRef &format, Entry &parent_entry,
eValueObjectRepresentationStyleExpressionPath; eValueObjectRepresentationStyleExpressionPath;
clear_printf = true; clear_printf = true;
break; break;
default: }
}
if (entry.number == 0) {
if (FormatManager::GetFormatFromCString(
entry.printf_format.c_str(), entry.fmt)) {
clear_printf = true;
} else if (entry.printf_format == "tid") {
verify_is_thread_id = true;
} else {
error.SetErrorStringWithFormat("invalid format: '%s'", error.SetErrorStringWithFormat("invalid format: '%s'",
entry.printf_format.c_str()); entry.printf_format.c_str());
return error; return error;
} }
} else if (FormatManager::GetFormatFromCString(
entry.printf_format.c_str(), true, entry.fmt)) {
clear_printf = true;
} else if (entry.printf_format == "tid") {
verify_is_thread_id = true;
} else {
error.SetErrorStringWithFormat("invalid format: '%s'",
entry.printf_format.c_str());
return error;
} }
// Our format string turned out to not be a printf style format // Our format string turned out to not be a printf style format

View File

@ -91,7 +91,7 @@ static bool GetFormatFromFormatChar(char format_char, Format &format) {
} }
static bool GetFormatFromFormatName(llvm::StringRef format_name, static bool GetFormatFromFormatName(llvm::StringRef format_name,
bool partial_match_ok, Format &format) { Format &format) {
uint32_t i; uint32_t i;
for (i = 0; i < g_num_format_infos; ++i) { for (i = 0; i < g_num_format_infos; ++i) {
if (format_name.equals_insensitive(g_format_infos[i].format_name)) { if (format_name.equals_insensitive(g_format_infos[i].format_name)) {
@ -100,13 +100,11 @@ static bool GetFormatFromFormatName(llvm::StringRef format_name,
} }
} }
if (partial_match_ok) { for (i = 0; i < g_num_format_infos; ++i) {
for (i = 0; i < g_num_format_infos; ++i) { if (llvm::StringRef(g_format_infos[i].format_name)
if (llvm::StringRef(g_format_infos[i].format_name) .starts_with_insensitive(format_name)) {
.starts_with_insensitive(format_name)) { format = g_format_infos[i].format;
format = g_format_infos[i].format; return true;
return true;
}
} }
} }
format = eFormatInvalid; format = eFormatInvalid;
@ -124,7 +122,6 @@ void FormatManager::Changed() {
} }
bool FormatManager::GetFormatFromCString(const char *format_cstr, bool FormatManager::GetFormatFromCString(const char *format_cstr,
bool partial_match_ok,
lldb::Format &format) { lldb::Format &format) {
bool success = false; bool success = false;
if (format_cstr && format_cstr[0]) { if (format_cstr && format_cstr[0]) {
@ -134,7 +131,7 @@ bool FormatManager::GetFormatFromCString(const char *format_cstr,
return true; return true;
} }
success = GetFormatFromFormatName(format_cstr, partial_match_ok, format); success = GetFormatFromFormatName(format_cstr, format);
} }
if (!success) if (!success)
format = eFormatInvalid; format = eFormatInvalid;

View File

@ -93,8 +93,7 @@ Status OptionArgParser::ToFormat(const char *s, lldb::Format &format,
*byte_size_ptr = 0; *byte_size_ptr = 0;
} }
const bool partial_match_ok = true; if (!FormatManager::GetFormatFromCString(s, format)) {
if (!FormatManager::GetFormatFromCString(s, partial_match_ok, format)) {
StreamString error_strm; StreamString error_strm;
error_strm.Printf( error_strm.Printf(
"Invalid format character or name '%s'. Valid values are:\n", s); "Invalid format character or name '%s'. Valid values are:\n", s);