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