blob: 9a6aae65c605883165597f98a440be0a9b868543 [file] [log] [blame]
Laszlo Nagybc687582016-01-12 22:38:41 +00001# -*- coding: utf-8 -*-
2# The LLVM Compiler Infrastructure
3#
4# This file is distributed under the University of Illinois Open Source
5# License. See LICENSE.TXT for details.
6
7import libscanbuild.command as sut
8from . import fixtures
9import unittest
10
11
12class ParseTest(unittest.TestCase):
13
14 def test_action(self):
15 def test(expected, cmd):
16 opts = sut.classify_parameters(cmd)
17 self.assertEqual(expected, opts['action'])
18
19 Link = sut.Action.Link
20 test(Link, ['clang', 'source.c'])
21
22 Compile = sut.Action.Compile
23 test(Compile, ['clang', '-c', 'source.c'])
24 test(Compile, ['clang', '-c', 'source.c', '-MF', 'source.d'])
25
26 Preprocess = sut.Action.Ignored
27 test(Preprocess, ['clang', '-E', 'source.c'])
28 test(Preprocess, ['clang', '-c', '-E', 'source.c'])
29 test(Preprocess, ['clang', '-c', '-M', 'source.c'])
30 test(Preprocess, ['clang', '-c', '-MM', 'source.c'])
31
32 def test_optimalizations(self):
33 def test(cmd):
34 opts = sut.classify_parameters(cmd)
35 return opts.get('compile_options', [])
36
37 self.assertEqual(['-O'], test(['clang', '-c', 'source.c', '-O']))
38 self.assertEqual(['-O1'], test(['clang', '-c', 'source.c', '-O1']))
39 self.assertEqual(['-Os'], test(['clang', '-c', 'source.c', '-Os']))
40 self.assertEqual(['-O2'], test(['clang', '-c', 'source.c', '-O2']))
41 self.assertEqual(['-O3'], test(['clang', '-c', 'source.c', '-O3']))
42
43 def test_language(self):
44 def test(cmd):
45 opts = sut.classify_parameters(cmd)
46 return opts.get('language')
47
48 self.assertEqual(None, test(['clang', '-c', 'source.c']))
49 self.assertEqual('c', test(['clang', '-c', 'source.c', '-x', 'c']))
50 self.assertEqual('cpp', test(['clang', '-c', 'source.c', '-x', 'cpp']))
51
52 def test_output(self):
53 def test(cmd):
54 opts = sut.classify_parameters(cmd)
55 return opts.get('output')
56
57 self.assertEqual(None, test(['clang', '-c', 'source.c']))
58 self.assertEqual('source.o',
59 test(['clang', '-c', '-o', 'source.o', 'source.c']))
60
61 def test_arch(self):
62 def test(cmd):
63 opts = sut.classify_parameters(cmd)
64 return opts.get('archs_seen', [])
65
66 eq = self.assertEqual
67
68 eq([], test(['clang', '-c', 'source.c']))
69 eq(['mips'],
70 test(['clang', '-c', 'source.c', '-arch', 'mips']))
71 eq(['mips', 'i386'],
72 test(['clang', '-c', 'source.c', '-arch', 'mips', '-arch', 'i386']))
73
74 def test_input_file(self):
75 def test(cmd):
76 opts = sut.classify_parameters(cmd)
77 return opts.get('files', [])
78
79 eq = self.assertEqual
80
81 eq(['src.c'], test(['clang', 'src.c']))
82 eq(['src.c'], test(['clang', '-c', 'src.c']))
83 eq(['s1.c', 's2.c'], test(['clang', '-c', 's1.c', 's2.c']))
84
85 def test_include(self):
86 def test(cmd):
87 opts = sut.classify_parameters(cmd)
88 return opts.get('compile_options', [])
89
90 eq = self.assertEqual
91
92 eq([], test(['clang', '-c', 'src.c']))
93 eq(['-include', '/usr/local/include'],
94 test(['clang', '-c', 'src.c', '-include', '/usr/local/include']))
95 eq(['-I.'],
96 test(['clang', '-c', 'src.c', '-I.']))
97 eq(['-I', '.'],
98 test(['clang', '-c', 'src.c', '-I', '.']))
99 eq(['-I/usr/local/include'],
100 test(['clang', '-c', 'src.c', '-I/usr/local/include']))
101 eq(['-I', '/usr/local/include'],
102 test(['clang', '-c', 'src.c', '-I', '/usr/local/include']))
103 eq(['-I/opt', '-I', '/opt/otp/include'],
104 test(['clang', '-c', 'src.c', '-I/opt', '-I', '/opt/otp/include']))
105 eq(['-isystem', '/path'],
106 test(['clang', '-c', 'src.c', '-isystem', '/path']))
107 eq(['-isystem=/path'],
108 test(['clang', '-c', 'src.c', '-isystem=/path']))
109
110 def test_define(self):
111 def test(cmd):
112 opts = sut.classify_parameters(cmd)
113 return opts.get('compile_options', [])
114
115 eq = self.assertEqual
116
117 eq([], test(['clang', '-c', 'src.c']))
118 eq(['-DNDEBUG'],
119 test(['clang', '-c', 'src.c', '-DNDEBUG']))
120 eq(['-UNDEBUG'],
121 test(['clang', '-c', 'src.c', '-UNDEBUG']))
122 eq(['-Dvar1=val1', '-Dvar2=val2'],
123 test(['clang', '-c', 'src.c', '-Dvar1=val1', '-Dvar2=val2']))
124 eq(['-Dvar="val ues"'],
125 test(['clang', '-c', 'src.c', '-Dvar="val ues"']))
126
127 def test_ignored_flags(self):
128 def test(flags):
129 cmd = ['clang', 'src.o']
130 opts = sut.classify_parameters(cmd + flags)
131 self.assertEqual(['src.o'], opts.get('compile_options'))
132
133 test([])
134 test(['-lrt', '-L/opt/company/lib'])
135 test(['-static'])
136 test(['-Wnoexcept', '-Wall'])
137 test(['-mtune=i386', '-mcpu=i386'])
138
139 def test_compile_only_flags(self):
140 def test(cmd):
141 opts = sut.classify_parameters(cmd)
142 return opts.get('compile_options', [])
143
144 eq = self.assertEqual
145
146 eq(['-std=C99'],
147 test(['clang', '-c', 'src.c', '-std=C99']))
148 eq(['-nostdinc'],
149 test(['clang', '-c', 'src.c', '-nostdinc']))
150 eq(['-isystem', '/image/debian'],
151 test(['clang', '-c', 'src.c', '-isystem', '/image/debian']))
152 eq(['-iprefix', '/usr/local'],
153 test(['clang', '-c', 'src.c', '-iprefix', '/usr/local']))
154 eq(['-iquote=me'],
155 test(['clang', '-c', 'src.c', '-iquote=me']))
156 eq(['-iquote', 'me'],
157 test(['clang', '-c', 'src.c', '-iquote', 'me']))
158
159 def test_compile_and_link_flags(self):
160 def test(cmd):
161 opts = sut.classify_parameters(cmd)
162 return opts.get('compile_options', [])
163
164 eq = self.assertEqual
165
166 eq(['-fsinged-char'],
167 test(['clang', '-c', 'src.c', '-fsinged-char']))
168 eq(['-fPIC'],
169 test(['clang', '-c', 'src.c', '-fPIC']))
170 eq(['-stdlib=libc++'],
171 test(['clang', '-c', 'src.c', '-stdlib=libc++']))
172 eq(['--sysroot', '/'],
173 test(['clang', '-c', 'src.c', '--sysroot', '/']))
174 eq(['-isysroot', '/'],
175 test(['clang', '-c', 'src.c', '-isysroot', '/']))
176 eq([],
177 test(['clang', '-c', 'src.c', '-fsyntax-only']))
178 eq([],
179 test(['clang', '-c', 'src.c', '-sectorder', 'a', 'b', 'c']))
180
181 def test_detect_cxx_from_compiler_name(self):
182 def test(cmd):
183 opts = sut.classify_parameters(cmd)
184 return opts.get('c++')
185
186 eq = self.assertEqual
187
188 eq(False, test(['cc', '-c', 'src.c']))
189 eq(True, test(['c++', '-c', 'src.c']))
190 eq(False, test(['clang', '-c', 'src.c']))
191 eq(True, test(['clang++', '-c', 'src.c']))
192 eq(False, test(['gcc', '-c', 'src.c']))
193 eq(True, test(['g++', '-c', 'src.c']))