Skip to content

[lldb] Avoid unnecessary regex check in dwim-print #10267

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lldb/include/lldb/Interpreter/CommandReturnObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ class CommandReturnObject {
void AppendMessageWithFormat(const char *format, ...)
__attribute__((format(printf, 2, 3)));

void AppendNote(llvm::StringRef in_string);

void AppendNoteWithFormat(const char *format, ...)
__attribute__((format(printf, 2, 3)));

void AppendWarning(llvm::StringRef in_string);

void AppendWarningWithFormat(const char *format, ...)
Expand All @@ -134,6 +139,11 @@ class CommandReturnObject {
AppendMessage(llvm::formatv(format, std::forward<Args>(args)...).str());
}

template <typename... Args>
void AppendNoteWithFormatv(const char *format, Args &&...args) {
AppendNote(llvm::formatv(format, std::forward<Args>(args)...).str());
}

template <typename... Args>
void AppendWarningWithFormatv(const char *format, Args &&... args) {
AppendWarning(llvm::formatv(format, std::forward<Args>(args)...).str());
Expand Down
26 changes: 13 additions & 13 deletions lldb/source/Commands/CommandObjectDWIMPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
// Add a hint if object description was requested, but no description
// function was implemented.
auto maybe_add_hint = [&](llvm::StringRef output) {
static bool note_shown = false;
if (note_shown)
return;

// Identify the default output of object description for Swift and
// Objective-C
// "<Name: 0x...>. The regex is:
Expand All @@ -112,21 +116,18 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
// - Followed by 5 or more hex digits.
// - Followed by ">".
// - End with zero or more whitespace characters.
const std::regex swift_class_regex("^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
static const std::regex swift_class_regex(
"^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");

if (GetDebugger().GetShowDontUsePoHint() && target_ptr &&
(language == lldb::eLanguageTypeSwift ||
language == lldb::eLanguageTypeObjC) &&
std::regex_match(output.data(), swift_class_regex)) {

static bool note_shown = false;
if (note_shown)
return;

result.GetOutputStream()
<< "note: object description requested, but type doesn't implement "
"a custom object description. Consider using \"p\" instead of "
"\"po\" (this note will only be shown once per debug session).\n";
result.AppendNote(
"object description requested, but type doesn't implement "
"a custom object description. Consider using \"p\" instead of "
"\"po\" (this note will only be shown once per debug session).\n");
note_shown = true;
}
};
Expand Down Expand Up @@ -180,8 +181,8 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
StringRef flags;
if (args.HasArgs())
flags = args.GetArgString();
result.AppendMessageWithFormatv("note: ran `frame variable {0}{1}`",
flags, expr);
result.AppendNoteWithFormatv("ran `frame variable {0}{1}`", flags,
expr);
}

dump_val_object(*valobj_sp);
Expand Down Expand Up @@ -280,8 +281,7 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
StringRef flags;
if (args.HasArgs())
flags = args.GetArgStringWithDelimiter();
result.AppendMessageWithFormatv("note: ran `expression {0}{1}`", flags,
expr);
result.AppendNoteWithFormatv("ran `expression {0}{1}`", flags, expr);
}

if (valobj_sp->GetError().GetError() != UserExpression::kNoResult)
Expand Down
24 changes: 24 additions & 0 deletions lldb/source/Interpreter/CommandReturnObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ static llvm::raw_ostream &warning(Stream &strm) {
<< "warning: ";
}

static llvm::raw_ostream &note(Stream &strm) {
return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Note,
llvm::ColorMode::Enable)
<< "note: ";
}

static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s) {
bool add_newline = false;
if (!s.empty()) {
Expand Down Expand Up @@ -74,6 +80,18 @@ void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
GetOutputStream() << sstrm.GetString();
}

void CommandReturnObject::AppendNoteWithFormat(const char *format, ...) {
if (!format)
return;
va_list args;
va_start(args, format);
StreamString sstrm;
sstrm.PrintfVarArg(format, args);
va_end(args);

note(GetOutputStream()) << sstrm.GetString();
}

void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
if (!format)
return;
Expand All @@ -92,6 +110,12 @@ void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
GetOutputStream() << in_string.rtrim() << '\n';
}

void CommandReturnObject::AppendNote(llvm::StringRef in_string) {
if (in_string.empty())
return;
note(GetOutputStream()) << in_string.rtrim() << '\n';
}

void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
if (in_string.empty())
return;
Expand Down