1"""Test that importing modules in C works as expected."""
2
3
4
5import os
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class CModulesTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    @skipIfFreeBSD
18    @expectedFailureAll(
19        oslist=["linux"],
20        bugnumber="http://llvm.org/pr23456 'fopen' has unknown return type")
21    @expectedFailureAll(
22        oslist=["windows"],
23        bugnumber="llvm.org/pr24489: Name lookup not working correctly on Windows")
24    @skipIf(macos_version=["<", "10.12"])
25    @expectedFailureNetBSD
26    def test_expr(self):
27        self.build()
28        exe = self.getBuildArtifact("a.out")
29        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
30
31        # Break inside the foo function which takes a bar_ptr argument.
32        lldbutil.run_break_set_by_file_and_line(
33            self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
34
35        self.runCmd("run", RUN_SUCCEEDED)
36
37        # The stop reason of the thread should be breakpoint.
38        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
39                    substrs=['stopped',
40                             'stop reason = breakpoint'])
41
42        # The breakpoint should have a hit count of 1.
43        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
44                    substrs=[' resolved, hit count = 1'])
45
46        # Enable logging of the imported AST.
47        log_file = self.getBuildArtifact("lldb-ast-log.txt")
48        if configuration.is_reproducer_replay():
49            log_file = self.getReproducerRemappedPath(log_file)
50        self.runCmd("log enable lldb ast -f '%s'" % log_file)
51
52        self.expect(
53            "expr -l objc++ -- @import Darwin; 3",
54            VARIABLES_DISPLAYED_CORRECTLY,
55            substrs=[
56                "int",
57                "3"])
58
59        # This expr command imports __sFILE with definition
60        # (FILE is a typedef to __sFILE.)
61        self.expect(
62            "expr *fopen(\"/dev/zero\", \"w\")",
63            VARIABLES_DISPLAYED_CORRECTLY,
64            substrs=[
65                "FILE",
66                "_close"])
67
68        # Check that the AST log contains exactly one definition of __sFILE.
69        f = open(log_file)
70        log_lines = f.readlines()
71        f.close()
72        os.remove(log_file)
73        self.assertEqual(" ".join(log_lines).count("struct __sFILE definition"),
74                         1)
75
76        self.expect("expr *myFile", VARIABLES_DISPLAYED_CORRECTLY,
77                    substrs=["a", "5", "b", "9"])
78
79        self.expect(
80            "expr MIN((uint64_t)2, (uint64_t)3)",
81            VARIABLES_DISPLAYED_CORRECTLY,
82            substrs=[
83                "uint64_t",
84                "2"])
85
86        self.expect("expr stdin", VARIABLES_DISPLAYED_CORRECTLY,
87                    substrs=["(FILE *)", "0x"])
88
89    def setUp(self):
90        # Call super's setUp().
91        TestBase.setUp(self)
92        # Find the line number to break inside main().
93        self.line = line_number('main.c', '// Set breakpoint 0 here.')
94