xref: /llvm-project/lldb/test/API/lang/objc/modules-update/TestClangModulesUpdate.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprechtimport os
299451b44SJordan Rupprechtimport shutil
399451b44SJordan Rupprecht
499451b44SJordan Rupprechtimport lldb
599451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
699451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
799451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
899451b44SJordan Rupprecht
999451b44SJordan Rupprecht
1099451b44SJordan Rupprechtclass TestClangModuleUpdate(TestBase):
111c6826e8SMichael Buch    @add_test_categories(["gmodules"])
12accb0955SJonas Devlieghere    @skipIfDarwin  # rdar://76540904
1399451b44SJordan Rupprecht    def test_expr(self):
1499451b44SJordan Rupprecht        with open(self.getBuildArtifact("module.modulemap"), "w") as f:
15*2238dcc3SJonas Devlieghere            f.write(
16*2238dcc3SJonas Devlieghere                """
1799451b44SJordan Rupprecht                    module Foo { header "f.h" }
18*2238dcc3SJonas Devlieghere                    """
19*2238dcc3SJonas Devlieghere            )
2099451b44SJordan Rupprecht        with open(self.getBuildArtifact("f.h"), "w") as f:
21*2238dcc3SJonas Devlieghere            f.write(
22*2238dcc3SJonas Devlieghere                """
2399451b44SJordan Rupprecht                    struct Q { int i; };
2499451b44SJordan Rupprecht                    void f() {}
25*2238dcc3SJonas Devlieghere                    """
26*2238dcc3SJonas Devlieghere            )
2799451b44SJordan Rupprecht
2899451b44SJordan Rupprecht        mod_cache = self.getBuildArtifact("private-module-cache")
2999451b44SJordan Rupprecht        if os.path.isdir(mod_cache):
3099451b44SJordan Rupprecht            shutil.rmtree(mod_cache)
31*2238dcc3SJonas Devlieghere        d = {"OBJC_SOURCES": "first.m"}
3299451b44SJordan Rupprecht        self.build(dictionary=d)
3399451b44SJordan Rupprecht        self.assertTrue(os.path.isdir(mod_cache), "module cache exists")
3499451b44SJordan Rupprecht
3599451b44SJordan Rupprecht        logfile = self.getBuildArtifact("modules.log")
3699451b44SJordan Rupprecht        self.runCmd("log enable -f %s lldb module" % logfile)
3799451b44SJordan Rupprecht        target, process, _, bkpt = lldbutil.run_to_name_breakpoint(self, "main")
38*2238dcc3SJonas Devlieghere        self.assertIn("int i", str(target.FindTypes("Q").GetTypeAtIndex(0)))
39*2238dcc3SJonas Devlieghere        self.expect("image list -g", patterns=[r"first\.o", r"Foo.*\.pcm"])
4099451b44SJordan Rupprecht
4199451b44SJordan Rupprecht        # Update the module.
4299451b44SJordan Rupprecht        with open(self.getBuildArtifact("f.h"), "w") as f:
43*2238dcc3SJonas Devlieghere            f.write(
44*2238dcc3SJonas Devlieghere                """
4599451b44SJordan Rupprecht                    struct S { int i; };
4699451b44SJordan Rupprecht                    struct S getS() { struct S r = {1}; return r; }
4799451b44SJordan Rupprecht                    void f() {}
48*2238dcc3SJonas Devlieghere                    """
49*2238dcc3SJonas Devlieghere            )
5099451b44SJordan Rupprecht
5199451b44SJordan Rupprecht        # Rebuild.
52*2238dcc3SJonas Devlieghere        d = {"OBJC_SOURCES": "second.m"}
5399451b44SJordan Rupprecht        self.build(dictionary=d)
5499451b44SJordan Rupprecht
5599451b44SJordan Rupprecht        # Reattach.
5699451b44SJordan Rupprecht        process.Kill()
5799451b44SJordan Rupprecht        target, process, _, _ = lldbutil.run_to_breakpoint_do_run(self, target, bkpt)
58*2238dcc3SJonas Devlieghere        self.assertIn("int i", str(target.FindTypes("S").GetTypeAtIndex(0)))
59*2238dcc3SJonas Devlieghere        self.expect("image list -g", patterns=[r"second\.o", r"Foo.*\.pcm"])
6099451b44SJordan Rupprecht
6199451b44SJordan Rupprecht        # Check log file.
6299451b44SJordan Rupprecht        found = False
63*2238dcc3SJonas Devlieghere        with open(logfile, "r") as f:
6499451b44SJordan Rupprecht            for line in f:
6599451b44SJordan Rupprecht                if "module changed" in line and "Foo" in line:
6699451b44SJordan Rupprecht                    found = True
6799451b44SJordan Rupprecht        self.assertTrue(found)
68