1from lldbsuite.test.decorators import *
2from lldbsuite.test.lldbtest import *
3from lldbsuite.test import lldbutil
4
5
6class TestCase(TestBase):
7    @add_test_categories(["libc++"])
8    @skipIf(compiler=no_match("clang"))
9    def test(self):
10        self.build()
11
12        lldbutil.run_to_source_breakpoint(
13            self, "// Set break point at this line.", lldb.SBFileSpec("main.cpp")
14        )
15
16        if self.expectedCompiler(["clang"]) and self.expectedCompilerVersion(
17            [">", "16.0"]
18        ):
19            vec_type = "std::vector<int>"
20        else:
21            vec_type = "std::vector<int, std::allocator<int> >"
22
23        # Test printing the vector before enabling any C++ module setting.
24        self.expect_expr("a", result_type=vec_type)
25
26        # Set loading the import-std-module to 'fallback' which loads the module
27        # and retries when an expression fails to parse.
28        self.runCmd("settings set target.import-std-module fallback")
29
30        # Printing the vector still works. This should return the same type
31        # as before as this shouldn't use a C++ module type.
32        self.expect_expr("a", result_type=vec_type)
33
34        # This expression can only parse with a C++ module. LLDB should
35        # automatically fall back to import the C++ module to get this working.
36        self.expect_expr("std::max<std::size_t>(0U, a.size())", result_value="3")
37
38        # The 'a' and 'local' part can be parsed without loading a C++ module and will
39        # load type/runtime information. The 'std::max...' part will fail to
40        # parse without a C++ module. Make sure we reset all the relevant parts of
41        # the C++ parser so that we don't end up with for example a second
42        # definition of 'local' when retrying.
43        self.expect_expr(
44            "a; local; std::max<std::size_t>(0U, a.size())", result_value="3"
45        )
46
47        # Try to declare top-level declarations that require a C++ module to parse.
48        # Top-level expressions don't support importing the C++ module (yet), so
49        # this should still fail as before.
50        self.expect(
51            "expr --top-level -- int i = std::max(1, 2);",
52            error=True,
53            substrs=["no member named 'max' in namespace 'std'"],
54        )
55
56        # The proper diagnostic however should be shown on the retry.
57        self.expect(
58            "expr std::max(1, 2); unknown_identifier",
59            error=True,
60            substrs=["use of undeclared identifier 'unknown_identifier'"],
61        )
62
63        # Turn on the 'import-std-module' setting and make sure we import the
64        # C++ module.
65        self.runCmd("settings set target.import-std-module true")
66        # This is still expected to work.
67        self.expect_expr("std::max<std::size_t>(0U, a.size())", result_value="3")
68
69        # Turn of the 'import-std-module' setting and make sure we don't load
70        # the module (which should prevent parsing the expression involving
71        # 'std::max').
72        self.runCmd("settings set target.import-std-module false")
73        self.expect(
74            "expr std::max(1, 2);",
75            error=True,
76            substrs=["no member named 'max' in namespace 'std'"],
77        )
78