1"""
2Tests that the import-std-module=fallback setting is only showing the error
3diagnostics from the first parse attempt which isn't using a module.
4This is supposed to prevent that a broken libc++ module renders failing
5expressions useless as the original failing errors are suppressed by the
6module build errors.
7"""
8
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12import os
13
14
15class TestCase(TestBase):
16    # We only emulate a fake libc++ in this test and don't use the real libc++,
17    # but we still add the libc++ category so that this test is only run in
18    # test configurations where libc++ is actually supposed to be tested.
19    @add_test_categories(["libc++"])
20    @skipIfRemote
21    @skipIf(compiler=no_match("clang"))
22    def test(self):
23        self.build()
24
25        sysroot = os.path.join(os.getcwd(), "root")
26
27        # Set the sysroot this test is using to provide a custom libc++.
28        self.runCmd(
29            "platform select --sysroot '" + sysroot + "' host", CURRENT_EXECUTABLE_SET
30        )
31
32        lldbutil.run_to_source_breakpoint(
33            self, "// Set break point at this line.", lldb.SBFileSpec("main.cpp")
34        )
35
36        # The expected error message when the fake libc++ module in this test
37        # fails to build from within LLDB (as it contains invalid code).
38        module_build_error_msg = "unknown type name 'random_token_to_fail_the_build'"
39
40        # First force the std module to be imported. This should show the
41        # module build error to the user.
42        self.runCmd("settings set target.import-std-module true")
43        self.expect(
44            "expr (size_t)v.size()", substrs=[module_build_error_msg], error=True
45        )
46
47        # In the fallback mode the module build error should not be shown.
48        self.runCmd("settings set target.import-std-module fallback")
49        fallback_expr = "expr v ; error_to_trigger_fallback_mode"
50        # First check for the actual expression error that should be displayed
51        # and is useful for the user.
52        self.expect(
53            fallback_expr,
54            substrs=["use of undeclared identifier 'error_to_trigger_fallback_mode'"],
55            error=True,
56        )
57        # Test that the module build error is not displayed.
58        self.expect(
59            fallback_expr, substrs=[module_build_error_msg], matching=False, error=True
60        )
61