xref: /llvm-project/lldb/test/API/commands/expression/import-std-module/basic/TestImportStdModule.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest importing the 'std' C++ module and evaluate expressions with it.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
699451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
799451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
899451b44SJordan Rupprecht
999451b44SJordan Rupprecht
1099451b44SJordan Rupprechtclass ImportStdModule(TestBase):
1199451b44SJordan Rupprecht    @add_test_categories(["libc++"])
1299451b44SJordan Rupprecht    @skipIf(compiler=no_match("clang"))
1399451b44SJordan Rupprecht    def test(self):
1499451b44SJordan Rupprecht        self.build()
1599451b44SJordan Rupprecht
16*2238dcc3SJonas Devlieghere        lldbutil.run_to_source_breakpoint(
17*2238dcc3SJonas Devlieghere            self, "// Set break point at this line.", lldb.SBFileSpec("main.cpp")
18*2238dcc3SJonas Devlieghere        )
1999451b44SJordan Rupprecht
2099451b44SJordan Rupprecht        # Activate importing of std module.
2199451b44SJordan Rupprecht        self.runCmd("settings set target.import-std-module true")
2299451b44SJordan Rupprecht        # Calling some normal std functions that return non-template types.
2399451b44SJordan Rupprecht        self.expect_expr("std::abs(-42)", result_type="int", result_value="42")
24*2238dcc3SJonas Devlieghere        self.expect_expr(
25*2238dcc3SJonas Devlieghere            "std::minmax<int>(1, 2).first", result_type="const int", result_value="1"
26*2238dcc3SJonas Devlieghere        )
27*2238dcc3SJonas Devlieghere        self.expect_expr("std::div(2, 1).quot", result_type="int", result_value="2")
2899451b44SJordan Rupprecht        # Using types from std.
29*2238dcc3SJonas Devlieghere        self.expect_expr(
30*2238dcc3SJonas Devlieghere            "(std::size_t)33U", result_type="std::size_t", result_value="33"
31*2238dcc3SJonas Devlieghere        )
3299451b44SJordan Rupprecht        # Calling templated functions that return non-template types.
33cabee89bSRaphael Isemann        self.expect_expr(
34cabee89bSRaphael Isemann            "char char_a = 'b'; char char_b = 'a'; std::swap(char_a, char_b); char_a",
35cabee89bSRaphael Isemann            result_type="char",
36*2238dcc3SJonas Devlieghere            result_value="'a'",
37*2238dcc3SJonas Devlieghere        )
3899451b44SJordan Rupprecht
3999451b44SJordan Rupprecht    @add_test_categories(["libc++"])
4099451b44SJordan Rupprecht    @skipIf(compiler=no_match("clang"))
4199451b44SJordan Rupprecht    def test_non_cpp_language(self):
4299451b44SJordan Rupprecht        self.build()
4399451b44SJordan Rupprecht
44*2238dcc3SJonas Devlieghere        lldbutil.run_to_source_breakpoint(
45*2238dcc3SJonas Devlieghere            self, "// Set break point at this line.", lldb.SBFileSpec("main.cpp")
46*2238dcc3SJonas Devlieghere        )
4799451b44SJordan Rupprecht
4899451b44SJordan Rupprecht        # Activate importing of std module.
4999451b44SJordan Rupprecht        self.runCmd("settings set target.import-std-module true")
5099451b44SJordan Rupprecht        # These languages don't support C++ modules, so they shouldn't
5199451b44SJordan Rupprecht        # be able to evaluate the expression.
52f939a32eSRaphael Isemann        self.expect("expr -l C -- std::minmax<int>(1, 2).first", error=True)
53f939a32eSRaphael Isemann        self.expect("expr -l C99 -- std::minmax<int>(1, 2).first", error=True)
54f939a32eSRaphael Isemann        self.expect("expr -l C11 -- std::minmax<int>(1, 2).first", error=True)
55f939a32eSRaphael Isemann        self.expect("expr -l ObjC -- std::minmax<int>(1, 2).first", error=True)
56