Skip to content

Commit 5d592e3

Browse files
committed
Address some review comments:
- Remove FromContextArg and GetType methods from IdentifierInfo. - Update Token::IsOneOf to take an ArrayRef rather than variadic list. - Various minor code cleanups. - Reviewer sugggested code cleanups in DILFindVariable and LookupGlobalIdentifier. - Use lldbutil.run_to_source_breakpont to greatly simplify the DIL python API tests. - Update the DIL lexer unit tests to use the new IsOneOf token parameter format.
1 parent 06ff79b commit 5d592e3

File tree

11 files changed

+40
-181
lines changed

11 files changed

+40
-181
lines changed

lldb/include/lldb/ValueObject/DILEval.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,9 @@ class IdentifierInfo {
3131
new IdentifierInfo(Kind::eValue, type, valobj.GetSP(), {}));
3232
}
3333

34-
static std::unique_ptr<IdentifierInfo> FromContextArg(CompilerType type) {
35-
lldb::ValueObjectSP empty_value;
36-
return std::unique_ptr<IdentifierInfo>(
37-
new IdentifierInfo(Kind::eContextArg, type, empty_value, {}));
38-
}
39-
4034
Kind GetKind() const { return m_kind; }
4135
lldb::ValueObjectSP GetValue() const { return m_value; }
4236

43-
CompilerType GetType() { return m_type; }
4437
bool IsValid() const { return m_type.IsValid(); }
4538

4639
IdentifierInfo(Kind kind, CompilerType type, lldb::ValueObjectSP value,

lldb/include/lldb/ValueObject/DILLexer.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,8 @@ class Token {
4242

4343
bool IsNot(Kind kind) const { return m_kind != kind; }
4444

45-
bool IsOneOf(Kind kind1, Kind kind2) const { return Is(kind1) || Is(kind2); }
46-
47-
bool IsOneOf(std::vector<Kind> kinds) const {
48-
if (kinds.empty())
49-
return false;
50-
51-
if (kinds.size() == 1)
52-
return Is(kinds[0]);
53-
54-
Kind k = kinds.back();
55-
kinds.pop_back();
56-
return Is(k) || IsOneOf(kinds);
57-
}
58-
59-
template <typename... Ts> bool IsOneOf(Kind kind, Ts... Ks) const {
60-
return Is(kind) || IsOneOf(Ks...);
45+
bool IsOneOf(llvm::ArrayRef<Kind> kinds) const {
46+
return llvm::is_contained(kinds, m_kind);
6147
}
6248

6349
uint32_t GetLocation() const { return m_start_pos; }

lldb/include/lldb/ValueObject/DILParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DILParser {
4242
bool use_synthetic, bool fragile_ivar,
4343
bool check_ptr_vs_member);
4444

45-
~DILParser() { m_ctx_scope.reset(); }
45+
~DILParser() = default;
4646

4747
bool UseSynthetic() { return m_use_synthetic; }
4848

lldb/source/Target/StackFrame.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,7 @@ ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
514514
VariableSP &var_sp, Status &error) {
515515
ExecutionContext exe_ctx;
516516
CalculateExecutionContext(exe_ctx);
517-
518517
bool use_DIL = exe_ctx.GetTargetRef().GetUseDIL(&exe_ctx);
519-
520518
if (use_DIL)
521519
return DILGetValueForVariableExpressionPath(var_expr, use_dynamic, options,
522520
var_sp, error);
@@ -542,12 +540,11 @@ ValueObjectSP StackFrame::DILGetValueForVariableExpressionPath(
542540
error = Status::FromError(lex_or_err.takeError());
543541
return ValueObjectSP();
544542
}
545-
dil::DILLexer &lexer = *lex_or_err;
546543

547544
// Parse the expression.
548545
auto tree_or_error = dil::DILParser::Parse(
549-
var_expr, lexer, shared_from_this(), use_dynamic, !no_synth_child,
550-
!no_fragile_ivar, check_ptr_vs_member);
546+
var_expr, std::move(*lex_or_err), shared_from_this(), use_dynamic,
547+
!no_synth_child, !no_fragile_ivar, check_ptr_vs_member);
551548
if (!tree_or_error) {
552549
error = Status::FromError(tree_or_error.takeError());
553550
return ValueObjectSP();

lldb/source/ValueObject/DILAST.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace lldb_private::dil {
1313

1414
llvm::Expected<lldb::ValueObjectSP> ErrorNode::Accept(Visitor *v) const {
1515
llvm_unreachable("Attempting to Visit a DIL ErrorNode.");
16-
return lldb::ValueObjectSP();
1716
}
1817

1918
llvm::Expected<lldb::ValueObjectSP> IdentifierNode::Accept(Visitor *v) const {

lldb/source/ValueObject/DILEval.cpp

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,15 @@ static lldb::VariableSP DILFindVariable(ConstString name,
5252
lldb::VariableSP exact_match;
5353
std::vector<lldb::VariableSP> possible_matches;
5454

55-
typedef std::vector<lldb::VariableSP> collection;
56-
typedef collection::iterator iterator;
57-
58-
iterator pos, end = variable_list->end();
59-
for (pos = variable_list->begin(); pos != end; ++pos) {
60-
llvm::StringRef str_ref_name = pos->get()->GetName().GetStringRef();
55+
for (lldb::VariableSP var_sp : *variable_list) {
56+
llvm::StringRef str_ref_name = var_sp->GetName().GetStringRef();
6157
// Check for global vars, which might start with '::'.
6258
str_ref_name.consume_front("::");
6359

6460
if (str_ref_name == name.GetStringRef())
65-
possible_matches.push_back(*pos);
66-
else if (pos->get()->NameMatches(name))
67-
possible_matches.push_back(*pos);
61+
possible_matches.push_back(var_sp);
62+
else if (var_sp->NameMatches(name))
63+
possible_matches.push_back(var_sp);
6864
}
6965

7066
// Look for exact matches (favors local vars over global vars)
@@ -74,23 +70,21 @@ static lldb::VariableSP DILFindVariable(ConstString name,
7470
});
7571

7672
if (exact_match_it != possible_matches.end())
77-
exact_match = *exact_match_it;
78-
79-
if (!exact_match)
80-
// Look for a global var exact match.
81-
for (auto var_sp : possible_matches) {
82-
llvm::StringRef str_ref_name = var_sp->GetName().GetStringRef();
83-
str_ref_name.consume_front("::");
84-
if (str_ref_name == name.GetStringRef()) {
85-
exact_match = var_sp;
86-
break;
87-
}
88-
}
73+
return *exact_match_it;
8974

90-
if (!exact_match && possible_matches.size() == 1)
91-
exact_match = possible_matches[0];
75+
// Look for a global var exact match.
76+
for (auto var_sp : possible_matches) {
77+
llvm::StringRef str_ref_name = var_sp->GetName().GetStringRef();
78+
str_ref_name.consume_front("::");
79+
if (str_ref_name == name.GetStringRef())
80+
return var_sp;
81+
}
82+
83+
// If there's a single non-exact match, take it.
84+
if (possible_matches.size() == 1)
85+
return possible_matches[0];
9286

93-
return exact_match;
87+
return nullptr;
9488
}
9589

9690
std::unique_ptr<IdentifierInfo> LookupGlobalIdentifier(
@@ -239,17 +233,15 @@ Interpreter::Visit(const IdentifierNode *node) {
239233
identifier = LookupGlobalIdentifier(node->GetName(), m_exe_ctx_scope,
240234
m_target, use_dynamic);
241235
if (!identifier) {
242-
std::string errMsg;
243-
std::string name = node->GetName();
244-
errMsg = llvm::formatv("use of undeclared identifier '{0}'", name);
236+
std::string errMsg =
237+
llvm::formatv("use of undeclared identifier '{0}'", node->GetName());
245238
Status error = Status(
246239
(uint32_t)ErrorCode::kUndeclaredIdentifier, lldb::eErrorTypeGeneric,
247240
FormatDiagnostics(m_expr, errMsg, node->GetLocation()));
248241
return error.ToError();
249242
}
250243
lldb::ValueObjectSP val;
251244
lldb::TargetSP target_sp;
252-
Status error;
253245

254246
assert(identifier->GetKind() == IdentifierInfo::Kind::eValue &&
255247
"Unrecognized identifier kind");
@@ -263,10 +255,6 @@ Interpreter::Visit(const IdentifierNode *node) {
263255
return error.ToError();
264256
}
265257

266-
target_sp = val->GetTargetSP();
267-
assert(target_sp && target_sp->IsValid() &&
268-
"identifier doesn't resolve to a valid value");
269-
270258
return val;
271259
}
272260

lldb/source/ValueObject/DILParser.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <limits.h>
2121
#include <memory>
2222
#include <sstream>
23-
#include <stdlib.h>
23+
#include <cstdlib>
2424
#include <string>
2525

2626
namespace lldb_private::dil {
@@ -29,7 +29,7 @@ std::string FormatDiagnostics(llvm::StringRef text, const std::string &message,
2929
uint32_t loc) {
3030
// Get the position, in the current line of text, of the diagnostics pointer.
3131
// ('loc' is the location of the start of the current token/error within the
32-
// overal text line).
32+
// overall text line).
3333
int32_t arrow = loc + 1; // Column offset starts at 1, not 0.
3434

3535
return llvm::formatv("<expr:1:{0}>: {1}\n{2}\n{3}", loc, message,
@@ -58,9 +58,7 @@ DILParser::DILParser(llvm::StringRef dil_input_expr, DILLexer lexer,
5858
m_check_ptr_vs_member(check_ptr_vs_member) {}
5959

6060
llvm::Expected<ASTNodeUP> DILParser::Run() {
61-
ASTNodeUP expr;
62-
63-
expr = ParseExpression();
61+
ASTNodeUP expr = ParseExpression();
6462

6563
Expect(Token::Kind::eof);
6664

@@ -84,7 +82,7 @@ ASTNodeUP DILParser::ParseExpression() { return ParsePrimaryExpression(); }
8482
// "(" expression ")"
8583
//
8684
ASTNodeUP DILParser::ParsePrimaryExpression() {
87-
if (CurToken().IsOneOf(Token::coloncolon, Token::identifier)) {
85+
if (CurToken().IsOneOf({Token::coloncolon, Token::identifier})) {
8886
// Save the source location for the diagnostics message.
8987
uint32_t loc = CurToken().GetLocation();
9088
auto identifier = ParseIdExpression();

lldb/test/API/commands/frame/var-dil/basics/GlobalVariableLookup/TestFrameVarDILGlobalVariableLookup.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,8 @@ class TestFrameVarDILGlobalVariableLookup(TestBase):
2020

2121
def test_frame_var(self):
2222
self.build()
23-
self.do_test()
24-
25-
def do_test(self):
26-
target = self.createTestTarget()
27-
28-
# Now create a breakpoint in main.c at the source matching
29-
# "Set a breakpoint here"
30-
breakpoint = target.BreakpointCreateBySourceRegex(
31-
"Set a breakpoint here", lldb.SBFileSpec("main.cpp")
32-
)
33-
self.assertTrue(
34-
breakpoint and breakpoint.GetNumLocations() >= 1, VALID_BREAKPOINT
35-
)
36-
37-
error = lldb.SBError()
38-
# This is the launch info. If you want to launch with arguments or
39-
# environment variables, add them using SetArguments or
40-
# SetEnvironmentEntries
41-
42-
launch_info = target.GetLaunchInfo()
43-
process = target.Launch(launch_info, error)
44-
self.assertTrue(process, PROCESS_IS_VALID)
45-
46-
# Did we hit our breakpoint?
47-
from lldbsuite.test.lldbutil import get_threads_stopped_at_breakpoint
48-
49-
threads = get_threads_stopped_at_breakpoint(process, breakpoint)
50-
self.assertEqual(
51-
len(threads), 1, "There should be a thread stopped at our breakpoint"
52-
)
53-
# The hit count for the breakpoint should be 1.
54-
self.assertEqual(breakpoint.GetHitCount(), 1)
55-
56-
frame = threads[0].GetFrameAtIndex(0)
57-
command_result = lldb.SBCommandReturnObject()
58-
interp = self.dbg.GetCommandInterpreter()
23+
lldbutil.run_to_source_breakpoint(self, "Set a breakpoint here",
24+
lldb.SBFileSpec("main.cpp"))
5925

6026
self.expect("settings set target.experimental.use-DIL true", substrs=[""])
6127
self.expect("frame variable 'globalVar'", substrs=["-559038737"]) # 0xDEADBEEF

lldb/test/API/commands/frame/var-dil/basics/InstanceVariables/TestFrameVarDILInstanceVariables.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,8 @@ class TestFrameVarDILInstanceVariables(TestBase):
2020

2121
def test_frame_var(self):
2222
self.build()
23-
self.do_test()
24-
25-
def do_test(self):
26-
target = self.createTestTarget()
27-
28-
# Now create a breakpoint in main.c at the source matching
29-
# "Set a breakpoint here"
30-
breakpoint = target.BreakpointCreateBySourceRegex(
31-
"Set a breakpoint here", lldb.SBFileSpec("main.cpp")
32-
)
33-
self.assertTrue(
34-
breakpoint and breakpoint.GetNumLocations() >= 1, VALID_BREAKPOINT
35-
)
36-
37-
error = lldb.SBError()
38-
# This is the launch info. If you want to launch with arguments or
39-
# environment variables, add them using SetArguments or
40-
# SetEnvironmentEntries
41-
42-
launch_info = target.GetLaunchInfo()
43-
process = target.Launch(launch_info, error)
44-
self.assertTrue(process, PROCESS_IS_VALID)
45-
46-
# Did we hit our breakpoint?
47-
from lldbsuite.test.lldbutil import get_threads_stopped_at_breakpoint
48-
49-
threads = get_threads_stopped_at_breakpoint(process, breakpoint)
50-
self.assertEqual(
51-
len(threads), 1, "There should be a thread stopped at our breakpoint"
52-
)
53-
# The hit count for the breakpoint should be 1.
54-
self.assertEqual(breakpoint.GetHitCount(), 1)
55-
56-
frame = threads[0].GetFrameAtIndex(0)
57-
command_result = lldb.SBCommandReturnObject()
58-
interp = self.dbg.GetCommandInterpreter()
23+
lldbutil.run_to_source_breakpoint(self, "Set a breakpoint here",
24+
lldb.SBFileSpec("main.cpp"))
5925

6026
self.expect("settings set target.experimental.use-DIL true", substrs=[""])
6127
self.expect("frame variable 'this'", patterns=["0x[0-9]+"])

lldb/test/API/commands/frame/var-dil/basics/LocalVars/TestFrameVarDILLocalVars.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,8 @@ class TestFrameVarDILLocalVars(TestBase):
2020

2121
def test_frame_var(self):
2222
self.build()
23-
self.do_test()
24-
25-
def do_test(self):
26-
target = self.createTestTarget()
27-
28-
# Now create a breakpoint in main.c at the source matching
29-
# "Set a breakpoint here"
30-
breakpoint = target.BreakpointCreateBySourceRegex(
31-
"Set a breakpoint here", lldb.SBFileSpec("main.cpp")
32-
)
33-
self.assertTrue(
34-
breakpoint and breakpoint.GetNumLocations() >= 1, VALID_BREAKPOINT
35-
)
36-
37-
error = lldb.SBError()
38-
# This is the launch info. If you want to launch with arguments or
39-
# environment variables, add them using SetArguments or
40-
# SetEnvironmentEntries
41-
42-
launch_info = target.GetLaunchInfo()
43-
process = target.Launch(launch_info, error)
44-
self.assertTrue(process, PROCESS_IS_VALID)
45-
46-
# Did we hit our breakpoint?
47-
from lldbsuite.test.lldbutil import get_threads_stopped_at_breakpoint
48-
49-
threads = get_threads_stopped_at_breakpoint(process, breakpoint)
50-
self.assertEqual(
51-
len(threads), 1, "There should be a thread stopped at our breakpoint"
52-
)
53-
# The hit count for the breakpoint should be 1.
54-
self.assertEqual(breakpoint.GetHitCount(), 1)
55-
56-
frame = threads[0].GetFrameAtIndex(0)
57-
command_result = lldb.SBCommandReturnObject()
58-
interp = self.dbg.GetCommandInterpreter()
23+
lldbutil.run_to_source_breakpoint(self, "Set a breakpoint here",
24+
lldb.SBFileSpec("main.cpp"))
5925

6026
self.expect("settings set target.experimental.use-DIL true", substrs=[""])
6127
self.expect("frame variable a", substrs=["1"])

0 commit comments

Comments
 (0)