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