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