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
17    # We only emulate a fake libc++ in this test and don't use the real libc++,
18    # but we still add the libc++ category so that this test is only run in
19    # test configurations where libc++ is actually supposed to be tested.
20    @add_test_categories(["libc++"])
21    @skipIfRemote
22    @skipIf(compiler=no_match("clang"))
23    def test(self):
24        self.build()
25
26        sysroot = os.path.join(os.getcwd(), "root")
27
28        # Set the sysroot this test is using to provide a custom libc++.
29        self.runCmd("platform select --sysroot '" + sysroot + "' host",
30                    CURRENT_EXECUTABLE_SET)
31
32        lldbutil.run_to_source_breakpoint(self,
33                                          "// Set break point at this line.",
34                                          lldb.SBFileSpec("main.cpp"))
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("expr (size_t)v.size()",
44                    substrs=[module_build_error_msg],
45                    error=True)
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(fallback_expr,
53                    substrs=["use of undeclared identifier 'error_to_trigger_fallback_mode'"],
54                    error=True)
55        # Test that the module build error is not displayed.
56        self.expect(fallback_expr,
57                    substrs=[module_build_error_msg],
58                    matching=False,
59                    error=True)
60