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::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::abs(-42)", error=True)
54        self.expect("expr -l C99 -- std::abs(-42)", error=True)
55        self.expect("expr -l C11 -- std::abs(-42)", error=True)
56        self.expect("expr -l ObjC -- std::abs(-42)", error=True)
57