Skip to content

Commit 250b9a1

Browse files
committed
fixup! simplify label parsing
1 parent fdd4c39 commit 250b9a1

File tree

2 files changed

+43
-56
lines changed

2 files changed

+43
-56
lines changed

lldb/source/Expression/Expression.cpp

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "lldb/Target/Target.h"
1212

1313
#include "llvm/ADT/SmallVector.h"
14+
#include "llvm/ADT/StringExtras.h"
1415
#include "llvm/ADT/StringRef.h"
1516
#include "llvm/Support/Error.h"
1617

@@ -31,62 +32,35 @@ Expression::Expression(ExecutionContextScope &exe_scope)
3132
assert(m_target_wp.lock());
3233
}
3334

34-
/// Returns the components of the specified function call label.
35-
///
36-
/// The format of \c label is described in \c FunctionCallLabel.
37-
/// The label prefix is not one of the components.
38-
static llvm::Expected<llvm::SmallVector<llvm::StringRef, 3>>
39-
splitFunctionCallLabel(llvm::StringRef label) {
40-
if (!label.consume_front(FunctionCallLabelPrefix))
41-
return llvm::createStringError(
42-
"expected function call label prefix not found.");
43-
if (!label.consume_front(":"))
44-
return llvm::createStringError(
45-
"expected ':' as the first character after prefix.");
46-
47-
auto sep1 = label.find_first_of(":");
48-
if (sep1 == llvm::StringRef::npos)
49-
return llvm::createStringError("no ':' separator found.");
50-
51-
auto sep2 = label.find_first_of(":", sep1 + 1);
52-
if (sep2 == llvm::StringRef::npos)
53-
return llvm::createStringError("only single ':' separator found.");
54-
55-
llvm::SmallVector<llvm::StringRef, 3> components;
56-
components.push_back(label.slice(0, sep1));
57-
components.push_back(label.slice(sep1 + 1, sep2));
58-
components.push_back(label.slice(sep2 + 1, llvm::StringRef::npos));
59-
60-
return components;
61-
}
62-
6335
llvm::Expected<FunctionCallLabel>
6436
lldb_private::FunctionCallLabel::fromString(llvm::StringRef label) {
65-
auto components_or_err = splitFunctionCallLabel(label);
66-
if (!components_or_err)
67-
return llvm::joinErrors(
68-
llvm::createStringError("failed to split function call label '%s'",
69-
label.data()),
70-
components_or_err.takeError());
37+
llvm::SmallVector<llvm::StringRef, 4> components;
38+
label.split(components, ":", /*MaxSplit=*/3);
39+
40+
if (components.size() != 4)
41+
return llvm::createStringError("malformed function call label.");
7142

72-
const auto &components = *components_or_err;
43+
if (components[0] != FunctionCallLabelPrefix)
44+
return llvm::createStringError(llvm::formatv(
45+
"expected function call label prefix '{0}' but found '{1}' instead.",
46+
FunctionCallLabelPrefix, components[0]));
7347

74-
llvm::StringRef module_label = components[0];
75-
llvm::StringRef die_label = components[1];
48+
llvm::StringRef module_label = components[1];
49+
llvm::StringRef die_label = components[2];
7650

7751
lldb::user_id_t module_id = 0;
78-
if (module_label.consumeInteger(0, module_id))
52+
if (!llvm::to_integer(module_label, module_id))
7953
return llvm::createStringError(
80-
llvm::formatv("failed to parse module ID from '{0}'.", components[0]));
54+
llvm::formatv("failed to parse module ID from '{0}'.", module_label));
8155

8256
lldb::user_id_t die_id;
83-
if (die_label.consumeInteger(/*Radix=*/0, die_id))
57+
if (!llvm::to_integer(die_label, die_id))
8458
return llvm::createStringError(
85-
llvm::formatv("failed to parse symbol ID from '{0}'.", components[1]));
59+
llvm::formatv("failed to parse symbol ID from '{0}'.", die_label));
8660

8761
return FunctionCallLabel{/*.module_id=*/module_id,
8862
/*.symbol_id=*/die_id,
89-
/*.lookup_name=*/components[2]};
63+
/*.lookup_name=*/components[3]};
9064
}
9165

9266
std::string lldb_private::FunctionCallLabel::toString() const {

lldb/unittests/Expression/ExpressionTest.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,47 @@ struct LabelTestCase {
2323

2424
static LabelTestCase g_label_test_cases[] = {
2525
// Failure modes
26-
{"0x0:0x0:_Z3foov",
26+
{"bar:0x0:0x0:_Z3foov",
2727
{},
28-
{"failed to split function call label '0x0:0x0:_Z3foov'",
29-
"expected function call label prefix not found."}},
30-
{"$__lldb_func0x0:0x0:_Z3foov",
28+
{"expected function call label prefix '$__lldb_func' but found 'bar' "
29+
"instead."}},
30+
{"$__lldb_func :0x0:0x0:_Z3foov",
3131
{},
32-
{"failed to split function call label '$__lldb_func0x0:0x0:_Z3foov'",
33-
"expected ':' as the first character after prefix."}},
34-
{"$__lldb_func:",
32+
{"expected function call label prefix '$__lldb_func' but found "
33+
"'$__lldb_func ' instead."}},
34+
{"$__lldb_funcc:0x0:0x0:_Z3foov",
3535
{},
36-
{"failed to split function call label '$__lldb_func:'",
37-
"no ':' separator found."}},
38-
{"$__lldb_func:0x0:0x0",
39-
{},
40-
{"failed to split function call label '$__lldb_func:0x0:0x0'",
41-
"only single ':' separator found."}},
36+
{"expected function call label prefix '$__lldb_func' but found "
37+
"'$__lldb_funcc' instead."}},
38+
{"", {}, {"malformed function call label."}},
39+
{"foo", {}, {"malformed function call label."}},
40+
{"$__lldb_func", {}, {"malformed function call label."}},
41+
{"$__lldb_func:", {}, {"malformed function call label."}},
42+
{"$__lldb_func:0x0:0x0", {}, {"malformed function call label."}},
4243
{"$__lldb_func:abc:0x0:_Z3foov",
4344
{},
4445
{"failed to parse module ID from 'abc'."}},
4546
{"$__lldb_func:-1:0x0:_Z3foov",
4647
{},
4748
{"failed to parse module ID from '-1'."}},
49+
{"$__lldb_func:0x0invalid:0x0:_Z3foov",
50+
{},
51+
{"failed to parse module ID from '0x0invalid'."}},
52+
{"$__lldb_func:0x0 :0x0:_Z3foov",
53+
{},
54+
{"failed to parse module ID from '0x0 '."}},
4855
{"$__lldb_func:0x0:abc:_Z3foov",
4956
{},
5057
{"failed to parse symbol ID from 'abc'."}},
5158
{"$__lldb_func:0x5:-1:_Z3foov",
5259
{},
5360
{"failed to parse symbol ID from '-1'."}},
61+
{"$__lldb_func:0x5:0x0invalid:_Z3foov",
62+
{},
63+
{"failed to parse symbol ID from '0x0invalid'."}},
64+
{"$__lldb_func:0x5:0x0 :_Z3foov",
65+
{},
66+
{"failed to parse symbol ID from '0x0 '."}},
5467
{"$__lldb_func:0x0:0x0:_Z3foov",
5568
{
5669
/*.module_id=*/0x0,

0 commit comments

Comments
 (0)