xref: /llvm-project/lldb/test/API/commands/expression/import-std-module/basic/TestImportStdModule.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test importing the 'std' C++ module and evaluate expressions with it.
3"""
4
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class ImportStdModule(TestBase):
11    @add_test_categories(["libc++"])
12    @skipIf(compiler=no_match("clang"))
13    def test(self):
14        self.build()
15
16        lldbutil.run_to_source_breakpoint(
17            self, "// Set break point at this line.", lldb.SBFileSpec("main.cpp")
18        )
19
20        # Activate importing of std module.
21        self.runCmd("settings set target.import-std-module true")
22        # Calling some normal std functions that return non-template types.
23        self.expect_expr("std::abs(-42)", result_type="int", result_value="42")
24        self.expect_expr(
25            "std::minmax<int>(1, 2).first", result_type="const int", result_value="1"
26        )
27        self.expect_expr("std::div(2, 1).quot", result_type="int", result_value="2")
28        # Using types from std.
29        self.expect_expr(
30            "(std::size_t)33U", result_type="std::size_t", result_value="33"
31        )
32        # Calling templated functions that return non-template types.
33        self.expect_expr(
34            "char char_a = 'b'; char char_b = 'a'; std::swap(char_a, char_b); char_a",
35            result_type="char",
36            result_value="'a'",
37        )
38
39    @add_test_categories(["libc++"])
40    @skipIf(compiler=no_match("clang"))
41    def test_non_cpp_language(self):
42        self.build()
43
44        lldbutil.run_to_source_breakpoint(
45            self, "// Set break point at this line.", lldb.SBFileSpec("main.cpp")
46        )
47
48        # Activate importing of std module.
49        self.runCmd("settings set target.import-std-module true")
50        # These languages don't support C++ modules, so they shouldn't
51        # be able to evaluate the expression.
52        self.expect("expr -l C -- std::minmax<int>(1, 2).first", error=True)
53        self.expect("expr -l C99 -- std::minmax<int>(1, 2).first", error=True)
54        self.expect("expr -l C11 -- std::minmax<int>(1, 2).first", error=True)
55        self.expect("expr -l ObjC -- std::minmax<int>(1, 2).first", error=True)
56