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>")
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.
26        self.expect_expr("a", result_type="std::vector<int>")
27
28        # This expression can only parse with a C++ module. LLDB should
29        # automatically fall back to import the C++ module to get this working.
30        self.expect_expr("std::max<std::size_t>(0U, a.size())", result_value="3")
31
32
33        # The 'a' and 'local' part can be parsed without loading a C++ module and will
34        # load type/runtime information. The 'std::max...' part will fail to
35        # parse without a C++ module. Make sure we reset all the relevant parts of
36        # the C++ parser so that we don't end up with for example a second
37        # definition of 'local' when retrying.
38        self.expect_expr("a; local; std::max<std::size_t>(0U, a.size())", result_value="3")
39
40
41        # Try to declare top-level declarations that require a C++ module to parse.
42        # Top-level expressions don't support importing the C++ module (yet), so
43        # this should still fail as before.
44        self.expect("expr --top-level -- int i = std::max(1, 2);", error=True,
45                    substrs=["no member named 'max' in namespace 'std'"])
46
47        # The proper diagnostic however should be shown on the retry.
48        self.expect("expr std::max(1, 2); unknown_identifier", error=True,
49                    substrs=["use of undeclared identifier 'unknown_identifier'"])
50
51        # Turn on the 'import-std-module' setting and make sure we import the
52        # C++ module.
53        self.runCmd("settings set target.import-std-module true")
54        # This is still expected to work.
55        self.expect_expr("std::max<std::size_t>(0U, a.size())", result_value="3")
56
57        # Turn of the 'import-std-module' setting and make sure we don't load
58        # the module (which should prevent parsing the expression involving
59        # 'std::max').
60        self.runCmd("settings set target.import-std-module false")
61        self.expect("expr std::max(1, 2);", error=True,
62                    substrs=["no member named 'max' in namespace 'std'"])
63