xref: /llvm-project/lldb/test/API/commands/frame/language/TestGuessLanguage.py (revision 80fcecb13c388ff087a27a4b0e7ca3dd8c98eaa4)
1"""
2Test the SB API SBFrame::GuessLanguage.
3"""
4
5
6import lldb
7import lldbsuite.test.lldbutil as lldbutil
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10
11
12class TestFrameGuessLanguage(TestBase):
13    # If your test case doesn't stress debug info, then
14    # set this to true.  That way it won't be run once for
15    # each debug info format.
16    NO_DEBUG_INFO_TESTCASE = True
17
18    @skipIf(compiler="clang", compiler_version=["<", "10.0"])
19    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr37658")
20    def test_guess_language(self):
21        """Test GuessLanguage for C and C++."""
22        self.build()
23        self.do_test()
24
25    def check_language(self, thread, frame_no, test_lang):
26        frame = thread.frames[frame_no]
27        self.assertTrue(frame.IsValid(), "Frame %d was not valid." % (frame_no))
28        lang = frame.GuessLanguage()
29        self.assertEqual(lang, test_lang)
30
31    def do_test(self):
32        """Test GuessLanguage for C & C++."""
33        target = self.createTestTarget()
34
35        # Now create a breakpoint in main.c at the source matching
36        # "Set a breakpoint here"
37        breakpoint = target.BreakpointCreateBySourceRegex(
38            "Set breakpoint here", lldb.SBFileSpec("somefunc.c")
39        )
40        self.assertTrue(
41            breakpoint and breakpoint.GetNumLocations() >= 1, VALID_BREAKPOINT
42        )
43
44        error = lldb.SBError()
45        # This is the launch info.  If you want to launch with arguments or
46        # environment variables, add them using SetArguments or
47        # SetEnvironmentEntries
48
49        launch_info = target.GetLaunchInfo()
50        process = target.Launch(launch_info, error)
51        self.assertTrue(process, PROCESS_IS_VALID)
52
53        # Did we hit our breakpoint?
54        from lldbsuite.test.lldbutil import get_threads_stopped_at_breakpoint
55
56        threads = get_threads_stopped_at_breakpoint(process, breakpoint)
57        self.assertEqual(
58            len(threads), 1, "There should be a thread stopped at our breakpoint"
59        )
60
61        # The hit count for the breakpoint should be 1.
62        self.assertEqual(breakpoint.GetHitCount(), 1)
63
64        thread = threads[0]
65
66        c_frame_language = lldb.eLanguageTypeC99
67        cxx_frame_language = lldb.eLanguageTypeC_plus_plus_11
68        # gcc emits DW_LANG_C89 even if -std=c99 was specified
69        if "gcc" in self.getCompiler():
70            c_frame_language = lldb.eLanguageTypeC89
71            cxx_frame_language = lldb.eLanguageTypeC_plus_plus
72
73        self.check_language(thread, 0, c_frame_language)
74        self.check_language(thread, 1, cxx_frame_language)
75        self.check_language(thread, 2, lldb.eLanguageTypeC_plus_plus)
76