xref: /llvm-project/lldb/test/API/commands/command/nested_alias/TestNestedAlias.py (revision 99451b4453688a94c6014cac233d371ab4cc342d)
1"""
2Test that an alias can reference other aliases without crashing.
3"""
4
5
6
7import lldb
8from lldbsuite.test.lldbtest import *
9import lldbsuite.test.lldbutil as lldbutil
10
11
12class NestedAliasTestCase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line number to break inside main().
21        self.line = line_number('main.cpp', '// break here')
22
23    def test_nested_alias(self):
24        """Test that an alias can reference other aliases without crashing."""
25        self.build()
26        exe = self.getBuildArtifact("a.out")
27        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
28
29        # Break in main() after the variables are assigned values.
30        lldbutil.run_break_set_by_file_and_line(
31            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
32
33        self.runCmd("run", RUN_SUCCEEDED)
34
35        # The stop reason of the thread should be breakpoint.
36        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
37                    substrs=['stopped', 'stop reason = breakpoint'])
38
39        # The breakpoint should have a hit count of 1.
40        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
41                    substrs=[' resolved, hit count = 1'])
42
43        # This is the function to remove the custom aliases in order to have a
44        # clean slate for the next test case.
45        def cleanup():
46            self.runCmd('command unalias read', check=False)
47            self.runCmd('command unalias rd', check=False)
48            self.runCmd('command unalias fo', check=False)
49            self.runCmd('command unalias foself', check=False)
50
51        # Execute the cleanup function during test case tear down.
52        self.addTearDownHook(cleanup)
53
54        self.runCmd('command alias read memory read -f A')
55        self.runCmd('command alias rd read -c 3')
56
57        self.expect(
58            'memory read -f A -c 3 `&my_ptr[0]`',
59            substrs=[
60                'deadbeef',
61                'main.cpp:',
62                'feedbeef'])
63        self.expect(
64            'rd `&my_ptr[0]`',
65            substrs=[
66                'deadbeef',
67                'main.cpp:',
68                'feedbeef'])
69
70        self.expect(
71            'memory read -f A -c 3 `&my_ptr[0]`',
72            substrs=['deadfeed'],
73            matching=False)
74        self.expect('rd `&my_ptr[0]`', substrs=['deadfeed'], matching=False)
75
76        self.runCmd('command alias fo frame variable -O --')
77        self.runCmd('command alias foself fo self')
78
79        self.expect(
80            'help foself',
81            substrs=[
82                '--show-all-children',
83                '--raw-output'],
84            matching=False)
85        self.expect(
86            'help foself',
87            substrs=[
88                'Show variables for the current',
89                'stack frame.'],
90            matching=True)
91
92        # Check that foself was resolved and is now independent of 'fo'.
93        self.runCmd('command unalias fo')
94        self.expect(
95            'help foself',
96            substrs=[
97                'Show variables for the current',
98                'stack frame.'],
99            matching=True)
100