xref: /llvm-project/lldb/test/API/lang/c/strings/TestCStrings.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Tests that C strings work as expected in expressions
3"""
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class CStringsTestCase(TestBase):
11    def test_with_run_command(self):
12        """Tests that C strings work as expected in expressions"""
13        self.build()
14        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
15
16        line = line_number("main.c", "// breakpoint 1")
17        lldbutil.run_break_set_by_file_and_line(
18            self, "main.c", line, num_expected_locations=1, loc_exact=True
19        )
20
21        self.runCmd("process launch", RUN_SUCCEEDED)
22
23        self.expect("expression -- a[2]", patterns=["\((const )?char\) \$0 = 'c'"])
24
25        self.expect("expression -- z[2]", startstr="(const char) $1 = 'x'")
26
27        # On Linux, the expression below will test GNU indirect function calls.
28        self.expect('expression -- (int)strlen("hello")', startstr="(int) $2 = 5")
29
30        self.expect('expression -- "world"[2]', startstr="(const char) $3 = 'r'")
31
32        self.expect('expression -- ""[0]', startstr="(const char) $4 = '\\0'")
33
34        self.expect('expr --raw -- "hello"', substrs=["[0] = 'h'", "[5] = '\\0'"])
35
36        self.expect('expression "hello"', substrs=["[6]) $", "hello"])
37
38        self.expect(
39            'expression (char*)"hello"', substrs=["(char *) $", " = 0x", "hello"]
40        )
41
42        self.expect('expression (int)strlen("")', substrs=["(int) $", " = 0"])
43
44        self.expect("expression !z", substrs=["false"])
45