Skip to content

Commit 0642cd7

Browse files
[lldb] Turn lldb_private::Status into a value type. (llvm#106163)
This patch removes all of the Set.* methods from Status. This cleanup is part of a series of patches that make it harder use the anti-pattern of keeping a long-lives Status object around and updating it while dropping any errors it contains on the floor. This patch is largely NFC, the more interesting next steps this enables is to: 1. remove Status.Clear() 2. assert that Status::operator=() never overwrites an error 3. remove Status::operator=() Note that step (2) will bring 90% of the benefits for users, and step (3) will dramatically clean up the error handling code in various places. In the end my goal is to convert all APIs that are of the form ` ResultTy DoFoo(Status& error) ` to ` llvm::Expected<ResultTy> DoFoo() ` How to read this patch? The interesting changes are in Status.h and Status.cpp, all other changes are mostly ` perl -pi -e 's/\.SetErrorString/ = Status::FromErrorString/g' $(git grep -l SetErrorString lldb/source) ` plus the occasional manual cleanup.
1 parent acb33a0 commit 0642cd7

File tree

270 files changed

+3660
-3422
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

270 files changed

+3660
-3422
lines changed

lldb/bindings/python/python-wrapper.swig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ PythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedStopH
306306
const char *session_dictionary_name, const StructuredDataImpl &args_impl,
307307
Status &error) {
308308
if (python_class_name == NULL || python_class_name[0] == '\0') {
309-
error.SetErrorString("Empty class name.");
309+
error = Status::FromErrorString("Empty class name.");
310310
return PythonObject();
311311
}
312312
if (!session_dictionary_name) {
313-
error.SetErrorString("No session dictionary");
313+
error = Status::FromErrorString("No session dictionary");
314314
return PythonObject();
315315
}
316316

@@ -322,7 +322,7 @@ PythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedStopH
322322
python_class_name, dict);
323323

324324
if (!pfunc.IsAllocated()) {
325-
error.SetErrorStringWithFormat("Could not find class: %s.",
325+
error = Status::FromErrorStringWithFormat("Could not find class: %s.",
326326
python_class_name);
327327
return PythonObject();
328328
}
@@ -337,23 +337,25 @@ PythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedStopH
337337
if (auto args_info = callback_func.GetArgInfo()) {
338338
size_t num_args = (*args_info).max_positional_args;
339339
if (num_args != 2) {
340-
error.SetErrorStringWithFormat(
340+
error = Status::FromErrorStringWithFormat(
341341
"Wrong number of args for "
342342
"handle_stop callback, should be 2 (excluding self), got: %zu",
343343
num_args);
344344
return PythonObject();
345345
} else
346346
return result;
347347
} else {
348-
error.SetErrorString("Couldn't get num arguments for handle_stop "
349-
"callback.");
348+
error = Status::FromErrorString(
349+
"Couldn't get num arguments for handle_stop "
350+
"callback.");
350351
return PythonObject();
351352
}
352353
return result;
353354
} else {
354-
error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
355-
"handle_stop callback.",
356-
python_class_name);
355+
error = Status::FromErrorStringWithFormat(
356+
"Class \"%s\" is missing the required "
357+
"handle_stop callback.",
358+
python_class_name);
357359
}
358360
}
359361
return PythonObject();

lldb/include/lldb/Core/StructuredDataImpl.h

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,38 +50,28 @@ class StructuredDataImpl {
5050
}
5151

5252
Status GetAsJSON(Stream &stream) const {
53-
Status error;
54-
55-
if (!m_data_sp) {
56-
error.SetErrorString("No structured data.");
57-
return error;
58-
}
53+
if (!m_data_sp)
54+
return Status::FromErrorString("No structured data.");
5955

6056
llvm::json::OStream s(stream.AsRawOstream());
6157
m_data_sp->Serialize(s);
62-
return error;
58+
return Status();
6359
}
6460

6561
Status GetDescription(Stream &stream) const {
66-
Status error;
67-
68-
if (!m_data_sp) {
69-
error.SetErrorString("Cannot pretty print structured data: "
70-
"no data to print.");
71-
return error;
72-
}
62+
if (!m_data_sp)
63+
return Status::FromErrorString("Cannot pretty print structured data: "
64+
"no data to print.");
7365

7466
// Grab the plugin
7567
lldb::StructuredDataPluginSP plugin_sp = m_plugin_wp.lock();
7668

7769
// If there's no plugin, call underlying data's dump method:
7870
if (!plugin_sp) {
79-
if (!m_data_sp) {
80-
error.SetErrorString("No data to describe.");
81-
return error;
82-
}
71+
if (!m_data_sp)
72+
return Status::FromErrorString("No data to describe.");
8373
m_data_sp->GetDescription(stream);
84-
return error;
74+
return Status();
8575
}
8676
// Get the data's description.
8777
return plugin_sp->GetDescription(m_data_sp, stream);

lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ScriptedInterface {
4848
llvm::Twine(llvm::Twine(" (") + llvm::Twine(detailed_error) +
4949
llvm::Twine(")"))
5050
.str();
51-
error.SetErrorString(full_error_message);
51+
error = Status(std::move(full_error_message));
5252
return {};
5353
}
5454

lldb/include/lldb/Interpreter/Interfaces/ScriptedPlatformInterface.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@ class ScriptedPlatformInterface : virtual public ScriptedInterface {
3131
}
3232

3333
virtual Status AttachToProcess(lldb::ProcessAttachInfoSP attach_info) {
34-
return Status("ScriptedPlatformInterface cannot attach to a process");
34+
return Status::FromErrorString(
35+
"ScriptedPlatformInterface cannot attach to a process");
3536
}
3637

3738
virtual Status LaunchProcess(lldb::ProcessLaunchInfoSP launch_info) {
38-
return Status("ScriptedPlatformInterface cannot launch process");
39+
return Status::FromErrorString(
40+
"ScriptedPlatformInterface cannot launch process");
3941
}
4042

4143
virtual Status KillProcess(lldb::pid_t pid) {
42-
return Status("ScriptedPlatformInterface cannot kill process");
44+
return Status::FromErrorString(
45+
"ScriptedPlatformInterface cannot kill process");
4346
}
4447
};
4548
} // namespace lldb_private

lldb/include/lldb/Interpreter/Interfaces/ScriptedProcessInterface.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,28 @@ class ScriptedProcessInterface : virtual public ScriptedInterface {
2929
virtual StructuredData::DictionarySP GetCapabilities() { return {}; }
3030

3131
virtual Status Attach(const ProcessAttachInfo &attach_info) {
32-
return Status("ScriptedProcess did not attach");
32+
return Status::FromErrorString("ScriptedProcess did not attach");
3333
}
3434

35-
virtual Status Launch() { return Status("ScriptedProcess did not launch"); }
35+
virtual Status Launch() {
36+
return Status::FromErrorString("ScriptedProcess did not launch");
37+
}
3638

37-
virtual Status Resume() { return Status("ScriptedProcess did not resume"); }
39+
virtual Status Resume() {
40+
return Status::FromErrorString("ScriptedProcess did not resume");
41+
}
3842

3943
virtual std::optional<MemoryRegionInfo>
4044
GetMemoryRegionContainingAddress(lldb::addr_t address, Status &error) {
41-
error.SetErrorString("ScriptedProcess have no memory region.");
45+
error = Status::FromErrorString("ScriptedProcess have no memory region.");
4246
return {};
4347
}
4448

4549
virtual StructuredData::DictionarySP GetThreadsInfo() { return {}; }
4650

4751
virtual bool CreateBreakpoint(lldb::addr_t addr, Status &error) {
48-
error.SetErrorString("ScriptedProcess don't support creating breakpoints.");
52+
error = Status::FromErrorString(
53+
"ScriptedProcess don't support creating breakpoints.");
4954
return {};
5055
}
5156

lldb/include/lldb/Interpreter/OptionValue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ class OptionValue {
119119
virtual lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx,
120120
llvm::StringRef name,
121121
Status &error) const {
122-
error.SetErrorStringWithFormatv("'{0}' is not a valid subvalue", name);
122+
error = Status::FromErrorStringWithFormatv("'{0}' is not a valid subvalue",
123+
name);
123124
return lldb::OptionValueSP();
124125
}
125126

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -176,25 +176,19 @@ class ScriptInterpreter : public PluginInterface {
176176
virtual Status ExecuteMultipleLines(
177177
const char *in_string,
178178
const ExecuteScriptOptions &options = ExecuteScriptOptions()) {
179-
Status error;
180-
error.SetErrorString("not implemented");
181-
return error;
179+
return Status::FromErrorString("not implemented");
182180
}
183181

184182
virtual Status
185183
ExportFunctionDefinitionToInterpreter(StringList &function_def) {
186-
Status error;
187-
error.SetErrorString("not implemented");
188-
return error;
184+
return Status::FromErrorString("not implemented");
189185
}
190186

191187
virtual Status GenerateBreakpointCommandCallbackData(StringList &input,
192188
std::string &output,
193189
bool has_extra_args,
194190
bool is_callback) {
195-
Status error;
196-
error.SetErrorString("not implemented");
197-
return error;
191+
return Status::FromErrorString("not implemented");
198192
}
199193

200194
virtual bool GenerateWatchpointCommandCallbackData(StringList &input,
@@ -280,8 +274,9 @@ class ScriptInterpreter : public PluginInterface {
280274
virtual StructuredData::GenericSP
281275
CreateScriptedStopHook(lldb::TargetSP target_sp, const char *class_name,
282276
const StructuredDataImpl &args_data, Status &error) {
283-
error.SetErrorString("Creating scripted stop-hooks with the current "
284-
"script interpreter is not supported.");
277+
error =
278+
Status::FromErrorString("Creating scripted stop-hooks with the current "
279+
"script interpreter is not supported.");
285280
return StructuredData::GenericSP();
286281
}
287282

@@ -308,9 +303,7 @@ class ScriptInterpreter : public PluginInterface {
308303
virtual Status GenerateFunction(const char *signature,
309304
const StringList &input,
310305
bool is_callback) {
311-
Status error;
312-
error.SetErrorString("unimplemented");
313-
return error;
306+
return Status::FromErrorString("not implemented");
314307
}
315308

316309
virtual void CollectDataForBreakpointCommandCallback(
@@ -329,18 +322,14 @@ class ScriptInterpreter : public PluginInterface {
329322
virtual Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
330323
const char *callback_text,
331324
bool is_callback) {
332-
Status error;
333-
error.SetErrorString("unimplemented");
334-
return error;
325+
return Status::FromErrorString("not implemented");
335326
}
336327

337328
/// This one is for deserialization:
338329
virtual Status SetBreakpointCommandCallback(
339330
BreakpointOptions &bp_options,
340331
std::unique_ptr<BreakpointOptions::CommandData> &data_up) {
341-
Status error;
342-
error.SetErrorString("unimplemented");
343-
return error;
332+
return Status::FromErrorString("not implemented");
344333
}
345334

346335
Status SetBreakpointCommandCallbackFunction(
@@ -352,9 +341,7 @@ class ScriptInterpreter : public PluginInterface {
352341
SetBreakpointCommandCallbackFunction(BreakpointOptions &bp_options,
353342
const char *function_name,
354343
StructuredData::ObjectSP extra_args_sp) {
355-
Status error;
356-
error.SetErrorString("unimplemented");
357-
return error;
344+
return Status::FromErrorString("not implemented");
358345
}
359346

360347
/// Set a one-liner as the callback for the watchpoint.
@@ -453,33 +440,33 @@ class ScriptInterpreter : public PluginInterface {
453440
virtual bool RunScriptFormatKeyword(const char *impl_function,
454441
Process *process, std::string &output,
455442
Status &error) {
456-
error.SetErrorString("unimplemented");
443+
error = Status::FromErrorString("unimplemented");
457444
return false;
458445
}
459446

460447
virtual bool RunScriptFormatKeyword(const char *impl_function, Thread *thread,
461448
std::string &output, Status &error) {
462-
error.SetErrorString("unimplemented");
449+
error = Status::FromErrorString("unimplemented");
463450
return false;
464451
}
465452

466453
virtual bool RunScriptFormatKeyword(const char *impl_function, Target *target,
467454
std::string &output, Status &error) {
468-
error.SetErrorString("unimplemented");
455+
error = Status::FromErrorString("unimplemented");
469456
return false;
470457
}
471458

472459
virtual bool RunScriptFormatKeyword(const char *impl_function,
473460
StackFrame *frame, std::string &output,
474461
Status &error) {
475-
error.SetErrorString("unimplemented");
462+
error = Status::FromErrorString("unimplemented");
476463
return false;
477464
}
478465

479466
virtual bool RunScriptFormatKeyword(const char *impl_function,
480467
ValueObject *value, std::string &output,
481468
Status &error) {
482-
error.SetErrorString("unimplemented");
469+
error = Status::FromErrorString("unimplemented");
483470
return false;
484471
}
485472

0 commit comments

Comments
 (0)