Skip to content

Commit af97edf

Browse files
authored
[lldb] Refactor GetFormatFromCString to always check for partial matches (NFC) (llvm#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.
1 parent f219cda commit af97edf

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

lldb/include/lldb/DataFormatters/FormatManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class FormatManager : public IFormatChangeListener {
138138
}
139139

140140
static bool GetFormatFromCString(const char *format_cstr,
141-
bool partial_match_ok, lldb::Format &format);
141+
lldb::Format &format);
142142

143143
static char GetFormatAsFormatChar(lldb::Format format);
144144

lldb/source/Core/FormatEntity.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,11 +2151,7 @@ static Status ParseInternal(llvm::StringRef &format, Entry &parent_entry,
21512151
if (entry.printf_format.find('%') == std::string::npos) {
21522152
bool clear_printf = false;
21532153

2154-
if (FormatManager::GetFormatFromCString(
2155-
entry.printf_format.c_str(), false, entry.fmt)) {
2156-
// We have an LLDB format, so clear the printf format
2157-
clear_printf = true;
2158-
} else if (entry.printf_format.size() == 1) {
2154+
if (entry.printf_format.size() == 1) {
21592155
switch (entry.printf_format[0]) {
21602156
case '@': // if this is an @ sign, print ObjC description
21612157
entry.number = ValueObject::
@@ -2198,20 +2194,20 @@ static Status ParseInternal(llvm::StringRef &format, Entry &parent_entry,
21982194
eValueObjectRepresentationStyleExpressionPath;
21992195
clear_printf = true;
22002196
break;
2201-
default:
2197+
}
2198+
}
2199+
2200+
if (entry.number == 0) {
2201+
if (FormatManager::GetFormatFromCString(
2202+
entry.printf_format.c_str(), entry.fmt)) {
2203+
clear_printf = true;
2204+
} else if (entry.printf_format == "tid") {
2205+
verify_is_thread_id = true;
2206+
} else {
22022207
error.SetErrorStringWithFormat("invalid format: '%s'",
22032208
entry.printf_format.c_str());
22042209
return error;
22052210
}
2206-
} else if (FormatManager::GetFormatFromCString(
2207-
entry.printf_format.c_str(), true, entry.fmt)) {
2208-
clear_printf = true;
2209-
} else if (entry.printf_format == "tid") {
2210-
verify_is_thread_id = true;
2211-
} else {
2212-
error.SetErrorStringWithFormat("invalid format: '%s'",
2213-
entry.printf_format.c_str());
2214-
return error;
22152211
}
22162212

22172213
// Our format string turned out to not be a printf style format

lldb/source/DataFormatters/FormatManager.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static bool GetFormatFromFormatChar(char format_char, Format &format) {
9191
}
9292

9393
static bool GetFormatFromFormatName(llvm::StringRef format_name,
94-
bool partial_match_ok, Format &format) {
94+
Format &format) {
9595
uint32_t i;
9696
for (i = 0; i < g_num_format_infos; ++i) {
9797
if (format_name.equals_insensitive(g_format_infos[i].format_name)) {
@@ -100,13 +100,11 @@ static bool GetFormatFromFormatName(llvm::StringRef format_name,
100100
}
101101
}
102102

103-
if (partial_match_ok) {
104-
for (i = 0; i < g_num_format_infos; ++i) {
105-
if (llvm::StringRef(g_format_infos[i].format_name)
106-
.starts_with_insensitive(format_name)) {
107-
format = g_format_infos[i].format;
108-
return true;
109-
}
103+
for (i = 0; i < g_num_format_infos; ++i) {
104+
if (llvm::StringRef(g_format_infos[i].format_name)
105+
.starts_with_insensitive(format_name)) {
106+
format = g_format_infos[i].format;
107+
return true;
110108
}
111109
}
112110
format = eFormatInvalid;
@@ -124,7 +122,6 @@ void FormatManager::Changed() {
124122
}
125123

126124
bool FormatManager::GetFormatFromCString(const char *format_cstr,
127-
bool partial_match_ok,
128125
lldb::Format &format) {
129126
bool success = false;
130127
if (format_cstr && format_cstr[0]) {
@@ -134,7 +131,7 @@ bool FormatManager::GetFormatFromCString(const char *format_cstr,
134131
return true;
135132
}
136133

137-
success = GetFormatFromFormatName(format_cstr, partial_match_ok, format);
134+
success = GetFormatFromFormatName(format_cstr, format);
138135
}
139136
if (!success)
140137
format = eFormatInvalid;

lldb/source/Interpreter/OptionArgParser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ Status OptionArgParser::ToFormat(const char *s, lldb::Format &format,
9393
*byte_size_ptr = 0;
9494
}
9595

96-
const bool partial_match_ok = true;
97-
if (!FormatManager::GetFormatFromCString(s, partial_match_ok, format)) {
96+
if (!FormatManager::GetFormatFromCString(s, format)) {
9897
StreamString error_strm;
9998
error_strm.Printf(
10099
"Invalid format character or name '%s'. Valid values are:\n", s);

0 commit comments

Comments
 (0)