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