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