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