xref: /llvm-project/lldb/test/API/commands/command/nested_alias/TestNestedAlias.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest that an alias can reference other aliases without crashing.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
899451b44SJordan Rupprechtimport lldbsuite.test.lldbutil as lldbutil
999451b44SJordan Rupprecht
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprechtclass NestedAliasTestCase(TestBase):
1299451b44SJordan Rupprecht    NO_DEBUG_INFO_TESTCASE = True
1399451b44SJordan Rupprecht
1499451b44SJordan Rupprecht    def setUp(self):
1599451b44SJordan Rupprecht        # Call super's setUp().
1699451b44SJordan Rupprecht        TestBase.setUp(self)
1799451b44SJordan Rupprecht        # Find the line number to break inside main().
18*2238dcc3SJonas Devlieghere        self.line = line_number("main.cpp", "// break here")
1999451b44SJordan Rupprecht
2099451b44SJordan Rupprecht    def test_nested_alias(self):
2199451b44SJordan Rupprecht        """Test that an alias can reference other aliases without crashing."""
2299451b44SJordan Rupprecht        self.build()
2399451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
2499451b44SJordan Rupprecht        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
2599451b44SJordan Rupprecht
2699451b44SJordan Rupprecht        # Break in main() after the variables are assigned values.
2799451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
28*2238dcc3SJonas Devlieghere            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True
29*2238dcc3SJonas Devlieghere        )
3099451b44SJordan Rupprecht
3199451b44SJordan Rupprecht        self.runCmd("run", RUN_SUCCEEDED)
3299451b44SJordan Rupprecht
3399451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
34*2238dcc3SJonas Devlieghere        self.expect(
35*2238dcc3SJonas Devlieghere            "thread list",
36*2238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
37*2238dcc3SJonas Devlieghere            substrs=["stopped", "stop reason = breakpoint"],
38*2238dcc3SJonas Devlieghere        )
3999451b44SJordan Rupprecht
4099451b44SJordan Rupprecht        # The breakpoint should have a hit count of 1.
419f0b5f9aSSYNOPSYS\georgiev        lldbutil.check_breakpoint(self, bpno=1, expected_hit_count=1)
4299451b44SJordan Rupprecht
4399451b44SJordan Rupprecht        # This is the function to remove the custom aliases in order to have a
4499451b44SJordan Rupprecht        # clean slate for the next test case.
4599451b44SJordan Rupprecht        def cleanup():
46*2238dcc3SJonas Devlieghere            self.runCmd("command unalias read", check=False)
47*2238dcc3SJonas Devlieghere            self.runCmd("command unalias rd", check=False)
48*2238dcc3SJonas Devlieghere            self.runCmd("command unalias fo", check=False)
49*2238dcc3SJonas Devlieghere            self.runCmd("command unalias foself", check=False)
50*2238dcc3SJonas Devlieghere            self.runCmd("command unalias add_two", check=False)
51*2238dcc3SJonas Devlieghere            self.runCmd("command unalias two", check=False)
5299451b44SJordan Rupprecht
5399451b44SJordan Rupprecht        # Execute the cleanup function during test case tear down.
5499451b44SJordan Rupprecht        self.addTearDownHook(cleanup)
5599451b44SJordan Rupprecht
56*2238dcc3SJonas Devlieghere        self.runCmd("command alias read memory read -f A")
57*2238dcc3SJonas Devlieghere        self.runCmd("command alias rd read -c 3")
5899451b44SJordan Rupprecht
5999451b44SJordan Rupprecht        self.expect(
60*2238dcc3SJonas Devlieghere            "memory read -f A -c 3 `&my_ptr[0]`",
61*2238dcc3SJonas Devlieghere            substrs=["deadbeef", "main.cpp:", "feedbeef"],
62*2238dcc3SJonas Devlieghere        )
63*2238dcc3SJonas Devlieghere        self.expect("rd `&my_ptr[0]`", substrs=["deadbeef", "main.cpp:", "feedbeef"])
6499451b44SJordan Rupprecht
6599451b44SJordan Rupprecht        self.expect(
66*2238dcc3SJonas Devlieghere            "memory read -f A -c 3 `&my_ptr[0]`", substrs=["deadfeed"], matching=False
67*2238dcc3SJonas Devlieghere        )
68*2238dcc3SJonas Devlieghere        self.expect("rd `&my_ptr[0]`", substrs=["deadfeed"], matching=False)
6999451b44SJordan Rupprecht
70*2238dcc3SJonas Devlieghere        self.runCmd("command alias fo frame variable -O --")
71*2238dcc3SJonas Devlieghere        self.runCmd("command alias foself fo self")
7299451b44SJordan Rupprecht
7399451b44SJordan Rupprecht        self.expect(
74*2238dcc3SJonas Devlieghere            "help foself",
75*2238dcc3SJonas Devlieghere            substrs=["--show-all-children", "--raw-output"],
76*2238dcc3SJonas Devlieghere            matching=False,
77*2238dcc3SJonas Devlieghere        )
7899451b44SJordan Rupprecht        self.expect(
79*2238dcc3SJonas Devlieghere            "help foself",
80*2238dcc3SJonas Devlieghere            substrs=["Show variables for the current", "stack frame."],
81*2238dcc3SJonas Devlieghere            matching=True,
82*2238dcc3SJonas Devlieghere        )
8399451b44SJordan Rupprecht
8499451b44SJordan Rupprecht        # Check that foself was resolved and is now independent of 'fo'.
85*2238dcc3SJonas Devlieghere        self.runCmd("command unalias fo")
8699451b44SJordan Rupprecht        self.expect(
87*2238dcc3SJonas Devlieghere            "help foself",
88*2238dcc3SJonas Devlieghere            substrs=["Show variables for the current", "stack frame."],
89*2238dcc3SJonas Devlieghere            matching=True,
90*2238dcc3SJonas Devlieghere        )
91b9515041SDave Lee
92b9515041SDave Lee        # Check that aliases can be created for raw input commands.
93*2238dcc3SJonas Devlieghere        self.expect("command alias two expr -- 2")
94*2238dcc3SJonas Devlieghere        self.expect("command alias add_two two +")
95*2238dcc3SJonas Devlieghere        self.expect("add_two 3", patterns=[" = 5$"])
9676523777SDave Lee        # Check that aliases to aliases to raw input commands work the second
9776523777SDave Lee        # and subsequent times.
98*2238dcc3SJonas Devlieghere        self.expect("add_two 3", patterns=[" = 5$"])
99*2238dcc3SJonas Devlieghere        self.expect("add_two 3", patterns=[" = 5$"])
100