1"""
2Test that the language option for breakpoints works correctly
3parser.
4"""
5
6
7
8import lldb
9from lldbsuite.test.lldbtest import *
10import lldbsuite.test.lldbutil as lldbutil
11
12
13class TestBreakpointLanguage(TestBase):
14
15    def check_location_file(self, bp, loc, test_name):
16        bp_loc = bp.GetLocationAtIndex(loc)
17        addr = bp_loc.GetAddress()
18        comp_unit = addr.GetCompileUnit()
19        comp_name = comp_unit.GetFileSpec().GetFilename()
20        return comp_name == test_name
21
22    def test_regex_breakpoint_language(self):
23        """Test that the name regex breakpoint commands obey the language filter."""
24
25        self.build()
26        # Create a target by the debugger.
27        exe = self.getBuildArtifact("a.out")
28        error = lldb.SBError()
29        # Don't read in dependencies so we don't come across false matches that
30        # add unwanted breakpoint hits.
31        self.target = self.dbg.CreateTarget(exe, None, None, False, error)
32        self.assertTrue(self.target, VALID_TARGET)
33
34        cpp_bp = self.target.BreakpointCreateByRegex(
35            "func_from",
36            lldb.eLanguageTypeC_plus_plus,
37            lldb.SBFileSpecList(),
38            lldb.SBFileSpecList())
39        self.assertEqual(
40            cpp_bp.GetNumLocations(), 1,
41            "Only one C++ symbol matches")
42        self.assertTrue(self.check_location_file(cpp_bp, 0, "b.cpp"))
43
44        c_bp = self.target.BreakpointCreateByRegex(
45            "func_from",
46            lldb.eLanguageTypeC,
47            lldb.SBFileSpecList(),
48            lldb.SBFileSpecList())
49        self.assertEqual(
50            c_bp.GetNumLocations(), 1,
51            "Only one C symbol matches")
52        self.assertTrue(self.check_location_file(c_bp, 0, "a.c"))
53
54        objc_bp = self.target.BreakpointCreateByRegex(
55            "func_from",
56            lldb.eLanguageTypeObjC,
57            lldb.SBFileSpecList(),
58            lldb.SBFileSpecList())
59        self.assertEqual(
60            objc_bp.GetNumLocations(), 0,
61            "No ObjC symbol matches")
62
63    def test_by_name_breakpoint_language(self):
64        """Test that the name regex breakpoint commands obey the language filter."""
65
66        self.build()
67        # Create a target by the debugger.
68        exe = self.getBuildArtifact("a.out")
69        error = lldb.SBError()
70        # Don't read in dependencies so we don't come across false matches that
71        # add unwanted breakpoint hits.
72        self.target = self.dbg.CreateTarget(exe, None, None, False, error)
73        self.assertTrue(self.target, VALID_TARGET)
74
75        cpp_bp = self.target.BreakpointCreateByName(
76            "func_from_cpp",
77            lldb.eFunctionNameTypeAuto,
78            lldb.eLanguageTypeC_plus_plus,
79            lldb.SBFileSpecList(),
80            lldb.SBFileSpecList())
81        self.assertEqual(
82            cpp_bp.GetNumLocations(), 1,
83            "Only one C++ symbol matches")
84        self.assertTrue(self.check_location_file(cpp_bp, 0, "b.cpp"))
85
86        no_cpp_bp = self.target.BreakpointCreateByName(
87            "func_from_c",
88            lldb.eFunctionNameTypeAuto,
89            lldb.eLanguageTypeC_plus_plus,
90            lldb.SBFileSpecList(),
91            lldb.SBFileSpecList())
92        self.assertEqual(
93            no_cpp_bp.GetNumLocations(), 0,
94            "And the C one doesn't match")
95
96        c_bp = self.target.BreakpointCreateByName(
97            "func_from_c",
98            lldb.eFunctionNameTypeAuto,
99            lldb.eLanguageTypeC,
100            lldb.SBFileSpecList(),
101            lldb.SBFileSpecList())
102        self.assertEqual(
103            c_bp.GetNumLocations(), 1,
104            "Only one C symbol matches")
105        self.assertTrue(self.check_location_file(c_bp, 0, "a.c"))
106
107        no_c_bp = self.target.BreakpointCreateByName(
108            "func_from_cpp",
109            lldb.eFunctionNameTypeAuto,
110            lldb.eLanguageTypeC,
111            lldb.SBFileSpecList(),
112            lldb.SBFileSpecList())
113        self.assertEqual(
114            no_c_bp.GetNumLocations(), 0,
115            "And the C++ one doesn't match")
116
117        objc_bp = self.target.BreakpointCreateByName(
118            "func_from_cpp",
119            lldb.eFunctionNameTypeAuto,
120            lldb.eLanguageTypeObjC,
121            lldb.SBFileSpecList(),
122            lldb.SBFileSpecList())
123        self.assertEqual(
124            objc_bp.GetNumLocations(), 0,
125            "No ObjC symbol matches")
126