Skip to content

Commit 37f3918

Browse files
authored
Merge pull request #10267 from swiftlang/dl/lldb-Avoid-unnecessary-regex-check-in-dwim-print-114608
2 parents 95d0da6 + 3f25aa8 commit 37f3918

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

lldb/include/lldb/Interpreter/CommandReturnObject.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ class CommandReturnObject {
117117
void AppendMessageWithFormat(const char *format, ...)
118118
__attribute__((format(printf, 2, 3)));
119119

120+
void AppendNote(llvm::StringRef in_string);
121+
122+
void AppendNoteWithFormat(const char *format, ...)
123+
__attribute__((format(printf, 2, 3)));
124+
120125
void AppendWarning(llvm::StringRef in_string);
121126

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

142+
template <typename... Args>
143+
void AppendNoteWithFormatv(const char *format, Args &&...args) {
144+
AppendNote(llvm::formatv(format, std::forward<Args>(args)...).str());
145+
}
146+
137147
template <typename... Args>
138148
void AppendWarningWithFormatv(const char *format, Args &&... args) {
139149
AppendWarning(llvm::formatv(format, std::forward<Args>(args)...).str());

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
104104
// Add a hint if object description was requested, but no description
105105
// function was implemented.
106106
auto maybe_add_hint = [&](llvm::StringRef output) {
107+
static bool note_shown = false;
108+
if (note_shown)
109+
return;
110+
107111
// Identify the default output of object description for Swift and
108112
// Objective-C
109113
// "<Name: 0x...>. The regex is:
@@ -113,21 +117,18 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
113117
// - Followed by 5 or more hex digits.
114118
// - Followed by ">".
115119
// - End with zero or more whitespace characters.
116-
const std::regex swift_class_regex("^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
120+
static const std::regex swift_class_regex(
121+
"^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
117122

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

123-
static bool note_shown = false;
124-
if (note_shown)
125-
return;
126-
127-
result.GetOutputStream()
128-
<< "note: object description requested, but type doesn't implement "
129-
"a custom object description. Consider using \"p\" instead of "
130-
"\"po\" (this note will only be shown once per debug session).\n";
128+
result.AppendNote(
129+
"object description requested, but type doesn't implement "
130+
"a custom object description. Consider using \"p\" instead of "
131+
"\"po\" (this note will only be shown once per debug session).\n");
131132
note_shown = true;
132133
}
133134
};
@@ -181,8 +182,8 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
181182
StringRef flags;
182183
if (args.HasArgs())
183184
flags = args.GetArgString();
184-
result.AppendMessageWithFormatv("note: ran `frame variable {0}{1}`",
185-
flags, expr);
185+
result.AppendNoteWithFormatv("ran `frame variable {0}{1}`", flags,
186+
expr);
186187
}
187188

188189
dump_val_object(*valobj_sp);
@@ -281,8 +282,7 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
281282
StringRef flags;
282283
if (args.HasArgs())
283284
flags = args.GetArgStringWithDelimiter();
284-
result.AppendMessageWithFormatv("note: ran `expression {0}{1}`", flags,
285-
expr);
285+
result.AppendNoteWithFormatv("ran `expression {0}{1}`", flags, expr);
286286
}
287287

288288
if (valobj_sp->GetError().GetError() != UserExpression::kNoResult)

lldb/source/Interpreter/CommandReturnObject.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ static llvm::raw_ostream &warning(Stream &strm) {
2727
<< "warning: ";
2828
}
2929

30+
static llvm::raw_ostream &note(Stream &strm) {
31+
return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Note,
32+
llvm::ColorMode::Enable)
33+
<< "note: ";
34+
}
35+
3036
static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s) {
3137
bool add_newline = false;
3238
if (!s.empty()) {
@@ -74,6 +80,18 @@ void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
7480
GetOutputStream() << sstrm.GetString();
7581
}
7682

83+
void CommandReturnObject::AppendNoteWithFormat(const char *format, ...) {
84+
if (!format)
85+
return;
86+
va_list args;
87+
va_start(args, format);
88+
StreamString sstrm;
89+
sstrm.PrintfVarArg(format, args);
90+
va_end(args);
91+
92+
note(GetOutputStream()) << sstrm.GetString();
93+
}
94+
7795
void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
7896
if (!format)
7997
return;
@@ -92,6 +110,12 @@ void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
92110
GetOutputStream() << in_string.rtrim() << '\n';
93111
}
94112

113+
void CommandReturnObject::AppendNote(llvm::StringRef in_string) {
114+
if (in_string.empty())
115+
return;
116+
note(GetOutputStream()) << in_string.rtrim() << '\n';
117+
}
118+
95119
void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
96120
if (in_string.empty())
97121
return;

0 commit comments

Comments
 (0)