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