summaryrefslogtreecommitdiffstats
path: root/tools/scan-build-py/tests/unit/test_command.py
blob: 9a6aae65c605883165597f98a440be0a9b868543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.

import libscanbuild.command as sut
from . import fixtures
import unittest


class ParseTest(unittest.TestCase):

    def test_action(self):
        def test(expected, cmd):
            opts = sut.classify_parameters(cmd)
            self.assertEqual(expected, opts['action'])

        Link = sut.Action.Link
        test(Link, ['clang', 'source.c'])

        Compile = sut.Action.Compile
        test(Compile, ['clang', '-c', 'source.c'])
        test(Compile, ['clang', '-c', 'source.c', '-MF', 'source.d'])

        Preprocess = sut.Action.Ignored
        test(Preprocess, ['clang', '-E', 'source.c'])
        test(Preprocess, ['clang', '-c', '-E', 'source.c'])
        test(Preprocess, ['clang', '-c', '-M', 'source.c'])
        test(Preprocess, ['clang', '-c', '-MM', 'source.c'])

    def test_optimalizations(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('compile_options', [])

        self.assertEqual(['-O'],  test(['clang', '-c', 'source.c', '-O']))
        self.assertEqual(['-O1'], test(['clang', '-c', 'source.c', '-O1']))
        self.assertEqual(['-Os'], test(['clang', '-c', 'source.c', '-Os']))
        self.assertEqual(['-O2'], test(['clang', '-c', 'source.c', '-O2']))
        self.assertEqual(['-O3'], test(['clang', '-c', 'source.c', '-O3']))

    def test_language(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('language')

        self.assertEqual(None, test(['clang', '-c', 'source.c']))
        self.assertEqual('c', test(['clang', '-c', 'source.c', '-x', 'c']))
        self.assertEqual('cpp', test(['clang', '-c', 'source.c', '-x', 'cpp']))

    def test_output(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('output')

        self.assertEqual(None, test(['clang', '-c', 'source.c']))
        self.assertEqual('source.o',
                         test(['clang', '-c', '-o', 'source.o', 'source.c']))

    def test_arch(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('archs_seen', [])

        eq = self.assertEqual

        eq([], test(['clang', '-c', 'source.c']))
        eq(['mips'],
           test(['clang', '-c', 'source.c', '-arch', 'mips']))
        eq(['mips', 'i386'],
           test(['clang', '-c', 'source.c', '-arch', 'mips', '-arch', 'i386']))

    def test_input_file(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('files', [])

        eq = self.assertEqual

        eq(['src.c'], test(['clang', 'src.c']))
        eq(['src.c'], test(['clang', '-c', 'src.c']))
        eq(['s1.c', 's2.c'], test(['clang', '-c', 's1.c', 's2.c']))

    def test_include(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('compile_options', [])

        eq = self.assertEqual

        eq([], test(['clang', '-c', 'src.c']))
        eq(['-include', '/usr/local/include'],
           test(['clang', '-c', 'src.c', '-include', '/usr/local/include']))
        eq(['-I.'],
           test(['clang', '-c', 'src.c', '-I.']))
        eq(['-I', '.'],
           test(['clang', '-c', 'src.c', '-I', '.']))
        eq(['-I/usr/local/include'],
           test(['clang', '-c', 'src.c', '-I/usr/local/include']))
        eq(['-I', '/usr/local/include'],
           test(['clang', '-c', 'src.c', '-I', '/usr/local/include']))
        eq(['-I/opt', '-I', '/opt/otp/include'],
           test(['clang', '-c', 'src.c', '-I/opt', '-I', '/opt/otp/include']))
        eq(['-isystem', '/path'],
           test(['clang', '-c', 'src.c', '-isystem', '/path']))
        eq(['-isystem=/path'],
           test(['clang', '-c', 'src.c', '-isystem=/path']))

    def test_define(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('compile_options', [])

        eq = self.assertEqual

        eq([], test(['clang', '-c', 'src.c']))
        eq(['-DNDEBUG'],
           test(['clang', '-c', 'src.c', '-DNDEBUG']))
        eq(['-UNDEBUG'],
           test(['clang', '-c', 'src.c', '-UNDEBUG']))
        eq(['-Dvar1=val1', '-Dvar2=val2'],
           test(['clang', '-c', 'src.c', '-Dvar1=val1', '-Dvar2=val2']))
        eq(['-Dvar="val ues"'],
           test(['clang', '-c', 'src.c', '-Dvar="val ues"']))

    def test_ignored_flags(self):
        def test(flags):
            cmd = ['clang', 'src.o']
            opts = sut.classify_parameters(cmd + flags)
            self.assertEqual(['src.o'], opts.get('compile_options'))

        test([])
        test(['-lrt', '-L/opt/company/lib'])
        test(['-static'])
        test(['-Wnoexcept', '-Wall'])
        test(['-mtune=i386', '-mcpu=i386'])

    def test_compile_only_flags(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('compile_options', [])

        eq = self.assertEqual

        eq(['-std=C99'],
           test(['clang', '-c', 'src.c', '-std=C99']))
        eq(['-nostdinc'],
           test(['clang', '-c', 'src.c', '-nostdinc']))
        eq(['-isystem', '/image/debian'],
           test(['clang', '-c', 'src.c', '-isystem', '/image/debian']))
        eq(['-iprefix', '/usr/local'],
           test(['clang', '-c', 'src.c', '-iprefix', '/usr/local']))
        eq(['-iquote=me'],
           test(['clang', '-c', 'src.c', '-iquote=me']))
        eq(['-iquote', 'me'],
           test(['clang', '-c', 'src.c', '-iquote', 'me']))

    def test_compile_and_link_flags(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('compile_options', [])

        eq = self.assertEqual

        eq(['-fsinged-char'],
           test(['clang', '-c', 'src.c', '-fsinged-char']))
        eq(['-fPIC'],
           test(['clang', '-c', 'src.c', '-fPIC']))
        eq(['-stdlib=libc++'],
           test(['clang', '-c', 'src.c', '-stdlib=libc++']))
        eq(['--sysroot', '/'],
           test(['clang', '-c', 'src.c', '--sysroot', '/']))
        eq(['-isysroot', '/'],
           test(['clang', '-c', 'src.c', '-isysroot', '/']))
        eq([],
           test(['clang', '-c', 'src.c', '-fsyntax-only']))
        eq([],
           test(['clang', '-c', 'src.c', '-sectorder', 'a', 'b', 'c']))

    def test_detect_cxx_from_compiler_name(self):
        def test(cmd):
            opts = sut.classify_parameters(cmd)
            return opts.get('c++')

        eq = self.assertEqual

        eq(False, test(['cc', '-c', 'src.c']))
        eq(True, test(['c++', '-c', 'src.c']))
        eq(False, test(['clang', '-c', 'src.c']))
        eq(True, test(['clang++', '-c', 'src.c']))
        eq(False, test(['gcc', '-c', 'src.c']))
        eq(True, test(['g++', '-c', 'src.c']))