xref: /llvm-project/lldb/test/API/lang/cpp/libcxx-internals-recognizer/TestLibcxxInternalsRecognizer.py (revision 2aed0d9cd3fc8f3c600d59b8b10d10a4466e50c6)
1import lldb
2from lldbsuite.test.decorators import *
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test import lldbutil
5
6import re
7
8class LibCxxInternalsRecognizerTestCase(TestBase):
9    NO_DEBUG_INFO_TESTCASE = True
10
11    @add_test_categories(["libc++"])
12    @skipIf(compiler="clang", compiler_version=["<", "16.0"])
13    def test_frame_recognizer(self):
14        """Test that implementation details of libc++ are hidden"""
15        self.build()
16        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
17            self, "break here", lldb.SBFileSpec("main.cpp")
18        )
19
20        expected_parents = {
21            "sort_less(int, int)": ["::sort", "test_algorithms"],
22            # `std::ranges::sort` is implemented as an object of types `__sort`.
23            # We never hide the frame of the entry-point into the standard library, even
24            # if the name starts with `__` which usually indicates an internal function.
25            "ranges_sort_less(int, int)": [
26                re.compile("ranges::__sort::(__fn::)?operator\(\)"),
27                "test_algorithms",
28            ],
29            # `ranges::views::transform` internally uses `std::invoke`, and that
30            # call also shows up in the stack trace
31            "view_transform(int)": [
32                "::invoke",
33                "ranges::transform_view",
34                "test_algorithms",
35            ],
36            # Various types of `invoke` calls
37            "consume_number(int)": ["::invoke", "test_invoke"],
38            "invoke_add(int, int)": ["::invoke", "test_invoke"],
39            "Callable::member_function(int) const": ["::invoke", "test_invoke"],
40            "Callable::operator()(int) const": ["::invoke", "test_invoke"],
41            # Containers
42            "MyKey::operator<(MyKey const&) const": [
43                "less",
44                "::emplace",
45                "test_containers",
46            ],
47        }
48        stop_set = set()
49        while process.GetState() != lldb.eStateExited:
50            fn = thread.GetFrameAtIndex(0).GetFunctionName()
51            stop_set.add(fn)
52            self.assertIn(fn, expected_parents.keys())
53            frame_id = 1
54            for expected_parent in expected_parents[fn]:
55                # Skip all hidden frames
56                while (
57                    frame_id < thread.GetNumFrames()
58                    and thread.GetFrameAtIndex(frame_id).IsHidden()
59                ):
60                    frame_id = frame_id + 1
61                # Expect the correct parent frame
62                func_name = thread.GetFrameAtIndex(frame_id).GetFunctionName()
63                if isinstance(expected_parent, re.Pattern):
64                    self.assertTrue(
65                        expected_parent.search(func_name) is not None,
66                        f"'{expected_parent}' not found in '{func_name}'"
67                    )
68                else:
69                    self.assertIn(expected_parent, func_name)
70                frame_id = frame_id + 1
71            process.Continue()
72
73        # Make sure that we actually verified all intended scenarios
74        self.assertEqual(len(stop_set), len(expected_parents))
75