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