Skip to content

Commit d779685

Browse files
authored
[lldb] Nits on uses of llvm::raw_string_ostream (NFC) (#108745)
As specified in the docs, 1) raw_string_ostream is always unbuffered and 2) the underlying buffer may be used directly ( 65b1361 for further reference ) * Don't call raw_string_ostream::flush(), which is essentially a no-op. * Avoid unneeded calls to raw_string_ostream::str(), to avoid excess indirection.
1 parent 4fdccd3 commit d779685

37 files changed

+35
-69
lines changed

lldb/include/lldb/Utility/Instrumentation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ template <typename... Ts> inline std::string stringify_args(const Ts &...ts) {
7070
std::string buffer;
7171
llvm::raw_string_ostream ss(buffer);
7272
stringify_helper(ss, ts...);
73-
return ss.str();
73+
return buffer;
7474
}
7575

7676
/// RAII object for instrumenting LLDB API functions.

lldb/source/Breakpoint/Breakpoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ json::Value Breakpoint::GetStatistics() {
11271127
llvm::raw_string_ostream ss(buffer);
11281128
json::OStream json_os(ss);
11291129
bp_data_sp->Serialize(json_os);
1130-
if (auto expected_value = llvm::json::parse(ss.str())) {
1130+
if (auto expected_value = llvm::json::parse(buffer)) {
11311131
bp.try_emplace("details", std::move(*expected_value));
11321132
} else {
11331133
std::string details_error = toString(expected_value.takeError());

lldb/source/Commands/CommandObjectLog.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class CommandObjectLogEnable : public CommandObjectParsed {
204204
channel, args.GetArgumentArrayRef(), log_file, m_options.log_options,
205205
m_options.buffer_size.GetCurrentValue(), m_options.handler,
206206
error_stream);
207-
result.GetErrorStream() << error_stream.str();
207+
result.GetErrorStream() << error;
208208

209209
if (success)
210210
result.SetStatus(eReturnStatusSuccessFinishNoResult);
@@ -273,7 +273,7 @@ class CommandObjectLogDisable : public CommandObjectParsed {
273273
if (Log::DisableLogChannel(channel, args.GetArgumentArrayRef(),
274274
error_stream))
275275
result.SetStatus(eReturnStatusSuccessFinishNoResult);
276-
result.GetErrorStream() << error_stream.str();
276+
result.GetErrorStream() << error;
277277
}
278278
}
279279
};
@@ -313,7 +313,7 @@ class CommandObjectLogList : public CommandObjectParsed {
313313
if (success)
314314
result.SetStatus(eReturnStatusSuccessFinishResult);
315315
}
316-
result.GetOutputStream() << output_stream.str();
316+
result.GetOutputStream() << output;
317317
}
318318
};
319319
class CommandObjectLogDump : public CommandObjectParsed {
@@ -404,7 +404,7 @@ class CommandObjectLogDump : public CommandObjectParsed {
404404
result.SetStatus(eReturnStatusSuccessFinishNoResult);
405405
} else {
406406
result.SetStatus(eReturnStatusFailed);
407-
result.GetErrorStream() << error_stream.str();
407+
result.GetErrorStream() << error;
408408
}
409409
}
410410

lldb/source/Commands/CommandObjectRegexCommand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ llvm::Expected<std::string> CommandObjectRegexCommand::SubstituteVariables(
5151
output << part;
5252
}
5353

54-
return output.str();
54+
return buffer;
5555
}
5656

5757
void CommandObjectRegexCommand::DoExecute(llvm::StringRef command,

lldb/source/Core/Module.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ uint32_t Module::Hash() {
16261626
const auto mtime = llvm::sys::toTimeT(m_object_mod_time);
16271627
if (mtime > 0)
16281628
id_strm << mtime;
1629-
return llvm::djbHash(id_strm.str());
1629+
return llvm::djbHash(identifier);
16301630
}
16311631

16321632
std::string Module::GetCacheKey() {
@@ -1636,7 +1636,7 @@ std::string Module::GetCacheKey() {
16361636
if (m_object_name)
16371637
strm << '(' << m_object_name << ')';
16381638
strm << '-' << llvm::format_hex(Hash(), 10);
1639-
return strm.str();
1639+
return key;
16401640
}
16411641

16421642
DataFileCache *Module::GetIndexCache() {

lldb/source/Expression/IRExecutionUnit.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,6 @@ void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
261261

262262
m_module->print(oss, nullptr);
263263

264-
oss.flush();
265-
266264
LLDB_LOGF(log, "Module being sent to JIT: \n%s", s.c_str());
267265
}
268266

lldb/source/Expression/IRInterpreter.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ static std::string PrintValue(const Value *value, bool truncate = false) {
4949
std::string s;
5050
raw_string_ostream rso(s);
5151
value->print(rso);
52-
rso.flush();
5352
if (truncate)
5453
s.resize(s.length() - 1);
5554

@@ -66,7 +65,6 @@ static std::string PrintType(const Type *type, bool truncate = false) {
6665
std::string s;
6766
raw_string_ostream rso(s);
6867
type->print(rso);
69-
rso.flush();
7068
if (truncate)
7169
s.resize(s.length() - 1);
7270
return s;
@@ -698,8 +696,6 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
698696

699697
module.print(oss, nullptr);
700698

701-
oss.flush();
702-
703699
LLDB_LOGF(log, "Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
704700
s.c_str());
705701
}

lldb/source/Host/windows/PipeWindows.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ Status PipeWindows::CreateNew(bool child_process_inherit) {
7474
std::string pipe_name;
7575
llvm::raw_string_ostream pipe_name_stream(pipe_name);
7676
pipe_name_stream << "lldb.pipe." << ::GetCurrentProcessId() << "." << serial;
77-
pipe_name_stream.flush();
7877

7978
return CreateNew(pipe_name.c_str(), child_process_inherit);
8079
}

lldb/source/Interpreter/Options.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ static std::string BuildShortOptions(const Option *long_options) {
923923
}
924924
}
925925
}
926-
return std::move(sstr.str());
926+
return storage;
927927
}
928928

929929
llvm::Expected<Args> Options::ParseAlias(const Args &args,

lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,6 @@ void DisassemblerLLVMC::MCDisasmInstance::PrintMCInst(
13661366
*m_subtarget_info_up, inst_stream);
13671367
m_instr_printer_up->setCommentStream(llvm::nulls());
13681368

1369-
comments_stream.flush();
1370-
13711369
static std::string g_newlines("\r\n");
13721370

13731371
for (size_t newline_pos = 0;

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,6 @@ class CodeComplete : public CodeCompleteConsumer {
984984
ToInsert += "(";
985985
raw_string_ostream OS(Description);
986986
F->print(OS, m_desc_policy, false);
987-
OS.flush();
988987
} else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
989988
Description = V->getType().getAsString(m_desc_policy);
990989
} else if (const FieldDecl *F = dyn_cast<FieldDecl>(D)) {
@@ -1358,7 +1357,6 @@ bool ClangExpressionParser::RewriteExpression(
13581357
llvm::raw_string_ostream out_stream(fixed_expression);
13591358

13601359
main_file_buffer.write(out_stream);
1361-
out_stream.flush();
13621360
diagnostic_manager.SetFixedExpression(fixed_expression);
13631361

13641362
return true;

lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ void StoringDiagnosticConsumer::HandleDiagnostic(
147147
// Print the diagnostic to m_output.
148148
m_output.clear();
149149
m_diag_printer->HandleDiagnostic(DiagLevel, info);
150-
m_os->flush();
151150

152151
// Store the diagnostic for later.
153152
m_diagnostics.push_back(IDAndDiagnostic(DiagLevel, m_output));

lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ static std::string PrintValue(llvm::Value *V, bool truncate = false) {
9494
std::string s;
9595
raw_string_ostream rso(s);
9696
V->print(rso);
97-
rso.flush();
9897
if (truncate)
9998
s.resize(s.length() - 1);
10099
return s;
@@ -553,8 +552,6 @@ bool IRDynamicChecks::runOnModule(llvm::Module &M) {
553552

554553
M.print(oss, nullptr);
555554

556-
oss.flush();
557-
558555
LLDB_LOGF(log, "Module after dynamic checks: \n%s", s.c_str());
559556
}
560557

lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ static std::string PrintValue(const Value *value, bool truncate = false) {
8686
if (value) {
8787
raw_string_ostream rso(s);
8888
value->print(rso);
89-
rso.flush();
9089
if (truncate)
9190
s.resize(s.length() - 1);
9291
}
@@ -97,7 +96,6 @@ static std::string PrintType(const llvm::Type *type, bool truncate = false) {
9796
std::string s;
9897
raw_string_ostream rso(s);
9998
type->print(rso);
100-
rso.flush();
10199
if (truncate)
102100
s.resize(s.length() - 1);
103101
return s;
@@ -244,7 +242,6 @@ bool IRForTarget::CreateResultVariable(llvm::Function &llvm_function) {
244242
std::string decl_desc_str;
245243
raw_string_ostream decl_desc_stream(decl_desc_str);
246244
result_decl->print(decl_desc_stream);
247-
decl_desc_stream.flush();
248245

249246
LLDB_LOG(log, "Found result decl: \"{0}\"", decl_desc_str);
250247
}
@@ -1616,8 +1613,6 @@ bool IRForTarget::runOnModule(Module &llvm_module) {
16161613

16171614
m_module->print(oss, nullptr);
16181615

1619-
oss.flush();
1620-
16211616
LLDB_LOG(log, "Module as passed in to IRForTarget: \n\"{0}\"", s);
16221617
}
16231618

@@ -1663,8 +1658,6 @@ bool IRForTarget::runOnModule(Module &llvm_module) {
16631658

16641659
m_module->print(oss, nullptr);
16651660

1666-
oss.flush();
1667-
16681661
LLDB_LOG(log, "Module after creating the result variable: \n\"{0}\"", s);
16691662
}
16701663

@@ -1762,8 +1755,6 @@ bool IRForTarget::runOnModule(Module &llvm_module) {
17621755

17631756
m_module->print(oss, nullptr);
17641757

1765-
oss.flush();
1766-
17671758
LLDB_LOG(log, "Module after preparing for execution: \n\"{0}\"", s);
17681759
}
17691760

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2685,7 +2685,7 @@ void AppleObjCRuntimeV2::WarnIfNoExpandedSharedCache() {
26852685
}
26862686
os << ". This will likely reduce debugging performance.\n";
26872687

2688-
Debugger::ReportWarning(os.str(), debugger.GetID(),
2688+
Debugger::ReportWarning(buffer, debugger.GetID(),
26892689
&m_no_expanded_cache_warning);
26902690
}
26912691

lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ NativeProcessWindows::OnDebugException(bool first_chance,
548548
<< " encountered at address "
549549
<< llvm::format_hex(record.GetExceptionAddress(), 8);
550550
StopThread(record.GetThreadID(), StopReason::eStopReasonException,
551-
desc_stream.str().c_str());
551+
desc.c_str());
552552

553553
SetState(eStateStopped, true);
554554
}

lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@ void ProcessWindows::RefreshStateAfterStop() {
492492
<< llvm::format_hex(active_exception->GetExceptionAddress(), 8);
493493
DumpAdditionalExceptionInformation(desc_stream, active_exception);
494494

495-
stop_info = StopInfo::CreateStopReasonWithException(
496-
*stop_thread, desc_stream.str().c_str());
495+
stop_info =
496+
StopInfo::CreateStopReasonWithException(*stop_thread, desc.c_str());
497497
stop_thread->SetStopInfo(stop_info);
498-
LLDB_LOG(log, "{0}", desc_stream.str());
498+
LLDB_LOG(log, "{0}", desc);
499499
return;
500500
}
501501
}

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3668,7 +3668,6 @@ GDBRemoteCommunicationClient::SendTraceStop(const TraceStopRequest &request,
36683668
std::string json_string;
36693669
llvm::raw_string_ostream os(json_string);
36703670
os << toJSON(request);
3671-
os.flush();
36723671

36733672
escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size());
36743673

@@ -3738,7 +3737,6 @@ GDBRemoteCommunicationClient::SendTraceGetState(llvm::StringRef type,
37383737
std::string json_string;
37393738
llvm::raw_string_ostream os(json_string);
37403739
os << toJSON(TraceGetStateRequest{type.str()});
3741-
os.flush();
37423740

37433741
escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size());
37443742

@@ -3772,7 +3770,6 @@ GDBRemoteCommunicationClient::SendTraceGetBinaryData(
37723770
std::string json_string;
37733771
llvm::raw_string_ostream os(json_string);
37743772
os << toJSON(request);
3775-
os.flush();
37763773

37773774
escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size());
37783775

@@ -4045,7 +4042,7 @@ GDBRemoteCommunicationClient::ReadExtFeature(llvm::StringRef object,
40454042
}
40464043
}
40474044

4048-
return output_stream.str();
4045+
return output;
40494046
}
40504047

40514048
// Notify the target that gdb is prepared to serve symbol lookup requests.

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5360,7 +5360,7 @@ std::string ProcessGDBRemote::HarmonizeThreadIdsForProfileData(
53605360
output_stream << end_delimiter;
53615361
m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map;
53625362

5363-
return output_stream.str();
5363+
return output;
53645364
}
53655365

53665366
void ProcessGDBRemote::HandleStopReply() {

lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ void ProcessMinidump::RefreshStateAfterStop() {
289289
<< " encountered at address "
290290
<< llvm::format_hex(
291291
exception_stream.ExceptionRecord.ExceptionAddress, 8);
292-
stop_info = StopInfo::CreateStopReasonWithException(
293-
*stop_thread, desc_stream.str().c_str());
292+
stop_info =
293+
StopInfo::CreateStopReasonWithException(*stop_thread, desc.c_str());
294294
}
295295

296296
stop_thread->SetStopInfo(stop_info);

lldb/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,5 +501,5 @@ void DebugNamesDWARFIndex::Dump(Stream &s) {
501501
std::string data;
502502
llvm::raw_string_ostream os(data);
503503
m_debug_names_up->dump(os);
504-
s.PutCString(os.str());
504+
s.PutCString(data);
505505
}

lldb/source/Plugins/SymbolFile/DWARF/ManualDWARFIndex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ std::string ManualDWARFIndex::GetCacheKey() {
697697
ObjectFile *objfile = m_dwarf->GetObjectFile();
698698
strm << objfile->GetModule()->GetCacheKey() << "-dwarf-index-"
699699
<< llvm::format_hex(objfile->GetCacheHash(), 10);
700-
return strm.str();
700+
return key;
701701
}
702702

703703
bool ManualDWARFIndex::LoadFromCache() {

lldb/source/Plugins/Trace/intel-pt/TraceIntelPTBundleLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Error TraceIntelPTBundleLoader::CreateJSONError(json::Path::Root &root,
7272
root.printErrorContext(value, os);
7373
return createStringError(
7474
std::errc::invalid_argument, "%s\n\nContext:\n%s\n\nSchema:\n%s",
75-
toString(root.getError()).c_str(), os.str().c_str(), GetSchema().data());
75+
toString(root.getError()).c_str(), err.c_str(), GetSchema().data());
7676
}
7777

7878
ThreadPostMortemTraceSP

lldb/source/Symbol/Symtab.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ std::string Symtab::GetCacheKey() {
11791179
// another object file in a separate symbol file.
11801180
strm << m_objfile->GetModule()->GetCacheKey() << "-symtab-"
11811181
<< llvm::format_hex(m_objfile->GetCacheHash(), 10);
1182-
return strm.str();
1182+
return key;
11831183
}
11841184

11851185
void Symtab::SaveToCache() {

lldb/source/Target/Statistics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ llvm::json::Value DebuggerStats::ReportStatistics(
414414
llvm::raw_string_ostream ss(buffer);
415415
json::OStream json_os(ss);
416416
transcript.Serialize(json_os);
417-
if (auto json_transcript = llvm::json::parse(ss.str()))
417+
if (auto json_transcript = llvm::json::parse(buffer))
418418
global_stats.try_emplace("transcript",
419419
std::move(json_transcript.get()));
420420
}

lldb/source/Target/Target.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4603,7 +4603,7 @@ void TargetProperties::CheckJITObjectsDir() {
46034603
std::optional<lldb::user_id_t> debugger_id;
46044604
if (m_target)
46054605
debugger_id = m_target->GetDebugger().GetID();
4606-
Debugger::ReportError(os.str(), debugger_id);
4606+
Debugger::ReportError(buffer, debugger_id);
46074607
}
46084608

46094609
bool TargetProperties::GetEnableSyntheticValue() const {

lldb/source/Utility/LLDBAssert.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void lldb_assert(bool expression, const char *expr_text, const char *func,
5454
llvm::formatv("Assertion failed: ({0}), function {1}, file {2}, line {3}",
5555
expr_text, func, file, line)
5656
.str(),
57-
backtrace.str(),
57+
buffer,
5858
"Please file a bug report against lldb reporting this failure log, and "
5959
"as many details as possible");
6060
}

lldb/source/Utility/Log.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ void Log::Format(llvm::StringRef file, llvm::StringRef function,
374374
llvm::raw_string_ostream message(message_string);
375375
WriteHeader(message, file, function);
376376
message << payload << "\n";
377-
WriteMessage(message.str());
377+
WriteMessage(message_string);
378378
}
379379

380380
StreamLogHandler::StreamLogHandler(int fd, bool should_close,

0 commit comments

Comments
 (0)