1"""
2Check that missing module source files are correctly handled by LLDB.
3"""
4
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8import os
9import shutil
10
11
12class TestCase(TestBase):
13    # We only emulate a fake libc++ in this test and don't use the real libc++,
14    # but we still add the libc++ category so that this test is only run in
15    # test configurations where libc++ is actually supposed to be tested.
16    @add_test_categories(["libc++"])
17    @skipIf(compiler=no_match("clang"))
18    @skipIfRemote
19    def test(self):
20        # The path to our temporary target root that contains the temporary
21        # module sources.
22        target_sysroot = self.getBuildArtifact("root")
23
24        # Copy the sources to the root.
25        shutil.copytree(self.getSourcePath("root"), target_sysroot)
26        # Build the binary with the copied sources.
27        self.build()
28        # Delete the copied sources so that they are now unavailable.
29        shutil.rmtree(target_sysroot)
30
31        # Set the sysroot where our dummy libc++ used to exist. Just to make
32        # sure we don't find some existing headers on the system that could
33        # XPASS this test.
34        self.runCmd("platform select --sysroot '" + target_sysroot + "' host")
35
36        lldbutil.run_to_source_breakpoint(
37            self, "// Set break point at this line.", lldb.SBFileSpec("main.cpp")
38        )
39
40        # Import the std C++ module and run an expression.
41        # As we deleted the sources, LLDB should refuse the load the module
42        # and just print the normal error we get from the expression.
43        self.runCmd("settings set target.import-std-module true")
44        self.expect(
45            "expr v.unknown_identifier",
46            error=True,
47            substrs=["no member named 'unknown_identifier'"],
48        )
49        # Check that there is no confusing error about failing to build the
50        # module.
51        self.expect(
52            "expr v.unknown_identifier",
53            error=True,
54            matching=False,
55            substrs=["could not build module 'std'"],
56        )
57
58        # Test the fallback mode. It should also just print the normal
59        # error but not mention a failed module build.
60        self.runCmd("settings set target.import-std-module fallback")
61
62        self.expect(
63            "expr v.unknown_identifier",
64            error=True,
65            substrs=["no member named 'unknown_identifier'"],
66        )
67        self.expect(
68            "expr v.unknown_identifier",
69            error=True,
70            matching=False,
71            substrs=["could not build module 'std'"],
72        )
73