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        # Check that diagnostics from the first parse attempt don't show up
52        # in the C++ module parse attempt. In the expression below, we first
53        # fail to parse 'std::max'. Then we retry with a loaded C++ module
54        # and succeed to parse the 'std::max' part. However, the
55        # trailing 'unknown_identifier' will fail to parse even with the
56        # loaded module. The 'std::max' diagnostic from the first attempt
57        # however should not be shown to the user.
58        self.expect("expr std::max(1, 2); unknown_identifier", error=True,
59                    matching=False,
60                    substrs=["no member named 'max'"])
61        # The proper diagnostic however should be shown on the retry.
62        self.expect("expr std::max(1, 2); unknown_identifier", error=True,
63                    substrs=["use of undeclared identifier 'unknown_identifier'"])
64
65        # Turn on the 'import-std-module' setting and make sure we import the
66        # C++ module.
67        self.runCmd("settings set target.import-std-module true")
68        # This is still expected to work.
69        self.expect_expr("std::max<std::size_t>(0U, a.size())", result_value="3")
70
71        # Turn of the 'import-std-module' setting and make sure we don't load
72        # the module (which should prevent parsing the expression involving
73        # 'std::max').
74        self.runCmd("settings set target.import-std-module false")
75        self.expect("expr std::max(1, 2);", error=True,
76                    substrs=["no member named 'max' in namespace 'std'"])
77