xref: /llvm-project/lldb/test/API/commands/command/nested_alias/TestNestedAlias.py (revision b95150418fb6e2d22a0bd84abcdc1f3cc7e5a0bf)
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        lldbutil.check_breakpoint(self, bpno = 1, expected_hit_count = 1)
41
42        # This is the function to remove the custom aliases in order to have a
43        # clean slate for the next test case.
44        def cleanup():
45            self.runCmd('command unalias read', check=False)
46            self.runCmd('command unalias rd', check=False)
47            self.runCmd('command unalias fo', check=False)
48            self.runCmd('command unalias foself', check=False)
49            self.runCmd('command unalias add_two', check=False)
50            self.runCmd('command unalias two', check=False)
51
52        # Execute the cleanup function during test case tear down.
53        self.addTearDownHook(cleanup)
54
55        self.runCmd('command alias read memory read -f A')
56        self.runCmd('command alias rd read -c 3')
57
58        self.expect(
59            'memory read -f A -c 3 `&my_ptr[0]`',
60            substrs=[
61                'deadbeef',
62                'main.cpp:',
63                'feedbeef'])
64        self.expect(
65            'rd `&my_ptr[0]`',
66            substrs=[
67                'deadbeef',
68                'main.cpp:',
69                'feedbeef'])
70
71        self.expect(
72            'memory read -f A -c 3 `&my_ptr[0]`',
73            substrs=['deadfeed'],
74            matching=False)
75        self.expect('rd `&my_ptr[0]`', substrs=['deadfeed'], matching=False)
76
77        self.runCmd('command alias fo frame variable -O --')
78        self.runCmd('command alias foself fo self')
79
80        self.expect(
81            'help foself',
82            substrs=[
83                '--show-all-children',
84                '--raw-output'],
85            matching=False)
86        self.expect(
87            'help foself',
88            substrs=[
89                'Show variables for the current',
90                'stack frame.'],
91            matching=True)
92
93        # Check that foself was resolved and is now independent of 'fo'.
94        self.runCmd('command unalias fo')
95        self.expect(
96            'help foself',
97            substrs=[
98                'Show variables for the current',
99                'stack frame.'],
100            matching=True)
101
102        # Check that aliases can be created for raw input commands.
103        self.expect('command alias two expr -- 2')
104        self.expect('command alias add_two two +')
105        self.expect('add_two 3', patterns=[' = 5$'])
106