195860828SRaphael Isemannfrom lldbsuite.test.decorators import *
295860828SRaphael Isemannfrom lldbsuite.test.lldbtest import *
395860828SRaphael Isemannfrom lldbsuite.test import lldbutil
495860828SRaphael Isemann
595860828SRaphael Isemann
695860828SRaphael Isemannclass TestCase(TestBase):
795860828SRaphael Isemann    @add_test_categories(["libc++"])
895860828SRaphael Isemann    @skipIf(compiler=no_match("clang"))
995860828SRaphael Isemann    def test(self):
1095860828SRaphael Isemann        self.build()
1195860828SRaphael Isemann
12*2238dcc3SJonas Devlieghere        lldbutil.run_to_source_breakpoint(
13*2238dcc3SJonas Devlieghere            self, "// Set break point at this line.", lldb.SBFileSpec("main.cpp")
14*2238dcc3SJonas Devlieghere        )
1595860828SRaphael Isemann
16*2238dcc3SJonas Devlieghere        if self.expectedCompiler(["clang"]) and self.expectedCompilerVersion(
17*2238dcc3SJonas Devlieghere            [">", "16.0"]
18*2238dcc3SJonas Devlieghere        ):
193c667298SMichael Buch            vec_type = "std::vector<int>"
203c667298SMichael Buch        else:
213c667298SMichael Buch            vec_type = "std::vector<int, std::allocator<int> >"
223c667298SMichael Buch
2395860828SRaphael Isemann        # Test printing the vector before enabling any C++ module setting.
243c667298SMichael Buch        self.expect_expr("a", result_type=vec_type)
2595860828SRaphael Isemann
2695860828SRaphael Isemann        # Set loading the import-std-module to 'fallback' which loads the module
2795860828SRaphael Isemann        # and retries when an expression fails to parse.
2895860828SRaphael Isemann        self.runCmd("settings set target.import-std-module fallback")
2995860828SRaphael Isemann
3095860828SRaphael Isemann        # Printing the vector still works. This should return the same type
31ce6a56e6SMichael Buch        # as before as this shouldn't use a C++ module type.
323c667298SMichael Buch        self.expect_expr("a", result_type=vec_type)
3395860828SRaphael Isemann
3495860828SRaphael Isemann        # This expression can only parse with a C++ module. LLDB should
3595860828SRaphael Isemann        # automatically fall back to import the C++ module to get this working.
3695860828SRaphael Isemann        self.expect_expr("std::max<std::size_t>(0U, a.size())", result_value="3")
3795860828SRaphael Isemann
3895860828SRaphael Isemann        # The 'a' and 'local' part can be parsed without loading a C++ module and will
3995860828SRaphael Isemann        # load type/runtime information. The 'std::max...' part will fail to
4095860828SRaphael Isemann        # parse without a C++ module. Make sure we reset all the relevant parts of
4195860828SRaphael Isemann        # the C++ parser so that we don't end up with for example a second
4295860828SRaphael Isemann        # definition of 'local' when retrying.
43*2238dcc3SJonas Devlieghere        self.expect_expr(
44*2238dcc3SJonas Devlieghere            "a; local; std::max<std::size_t>(0U, a.size())", result_value="3"
45*2238dcc3SJonas Devlieghere        )
4695860828SRaphael Isemann
4795860828SRaphael Isemann        # Try to declare top-level declarations that require a C++ module to parse.
4895860828SRaphael Isemann        # Top-level expressions don't support importing the C++ module (yet), so
4995860828SRaphael Isemann        # this should still fail as before.
50*2238dcc3SJonas Devlieghere        self.expect(
51*2238dcc3SJonas Devlieghere            "expr --top-level -- int i = std::max(1, 2);",
52*2238dcc3SJonas Devlieghere            error=True,
53*2238dcc3SJonas Devlieghere            substrs=["no member named 'max' in namespace 'std'"],
54*2238dcc3SJonas Devlieghere        )
5595860828SRaphael Isemann
5695860828SRaphael Isemann        # The proper diagnostic however should be shown on the retry.
57*2238dcc3SJonas Devlieghere        self.expect(
58*2238dcc3SJonas Devlieghere            "expr std::max(1, 2); unknown_identifier",
59*2238dcc3SJonas Devlieghere            error=True,
60*2238dcc3SJonas Devlieghere            substrs=["use of undeclared identifier 'unknown_identifier'"],
61*2238dcc3SJonas Devlieghere        )
6295860828SRaphael Isemann
6395860828SRaphael Isemann        # Turn on the 'import-std-module' setting and make sure we import the
6495860828SRaphael Isemann        # C++ module.
6595860828SRaphael Isemann        self.runCmd("settings set target.import-std-module true")
6695860828SRaphael Isemann        # This is still expected to work.
6795860828SRaphael Isemann        self.expect_expr("std::max<std::size_t>(0U, a.size())", result_value="3")
6895860828SRaphael Isemann
6995860828SRaphael Isemann        # Turn of the 'import-std-module' setting and make sure we don't load
7095860828SRaphael Isemann        # the module (which should prevent parsing the expression involving
7195860828SRaphael Isemann        # 'std::max').
7295860828SRaphael Isemann        self.runCmd("settings set target.import-std-module false")
73*2238dcc3SJonas Devlieghere        self.expect(
74*2238dcc3SJonas Devlieghere            "expr std::max(1, 2);",
75*2238dcc3SJonas Devlieghere            error=True,
76*2238dcc3SJonas Devlieghere            substrs=["no member named 'max' in namespace 'std'"],
77*2238dcc3SJonas Devlieghere        )
78