Laszlo Nagy | bc68758 | 2016-01-12 22:38:41 +0000 | [diff] [blame] | 1 | # -*- 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 | |
Laszlo Nagy | 258ff25 | 2017-02-14 10:43:38 +0000 | [diff] [blame] | 7 | import unittest |
Laszlo Nagy | 6d9a7e8 | 2017-04-07 11:04:49 +0000 | [diff] [blame] | 8 | import re |
| 9 | import os |
| 10 | import os.path |
Gabor Horvath | eb0584b | 2018-02-28 13:23:10 +0000 | [diff] [blame] | 11 | import libear |
| 12 | import libscanbuild.analyze as sut |
Laszlo Nagy | 6d9a7e8 | 2017-04-07 11:04:49 +0000 | [diff] [blame] | 13 | |
Laszlo Nagy | 258ff25 | 2017-02-14 10:43:38 +0000 | [diff] [blame] | 14 | |
| 15 | class ReportDirectoryTest(unittest.TestCase): |
| 16 | |
| 17 | # Test that successive report directory names ascend in lexicographic |
| 18 | # order. This is required so that report directories from two runs of |
| 19 | # scan-build can be easily matched up to compare results. |
| 20 | def test_directory_name_comparison(self): |
| 21 | with libear.TemporaryDirectory() as tmpdir, \ |
| 22 | sut.report_directory(tmpdir, False) as report_dir1, \ |
| 23 | sut.report_directory(tmpdir, False) as report_dir2, \ |
| 24 | sut.report_directory(tmpdir, False) as report_dir3: |
| 25 | self.assertLess(report_dir1, report_dir2) |
| 26 | self.assertLess(report_dir2, report_dir3) |
Laszlo Nagy | 6d9a7e8 | 2017-04-07 11:04:49 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class FilteringFlagsTest(unittest.TestCase): |
| 30 | |
| 31 | def test_language_captured(self): |
| 32 | def test(flags): |
| 33 | cmd = ['clang', '-c', 'source.c'] + flags |
| 34 | opts = sut.classify_parameters(cmd) |
| 35 | return opts['language'] |
| 36 | |
| 37 | self.assertEqual(None, test([])) |
| 38 | self.assertEqual('c', test(['-x', 'c'])) |
| 39 | self.assertEqual('cpp', test(['-x', 'cpp'])) |
| 40 | |
| 41 | def test_arch(self): |
| 42 | def test(flags): |
| 43 | cmd = ['clang', '-c', 'source.c'] + flags |
| 44 | opts = sut.classify_parameters(cmd) |
| 45 | return opts['arch_list'] |
| 46 | |
| 47 | self.assertEqual([], test([])) |
| 48 | self.assertEqual(['mips'], test(['-arch', 'mips'])) |
| 49 | self.assertEqual(['mips', 'i386'], |
| 50 | test(['-arch', 'mips', '-arch', 'i386'])) |
| 51 | |
| 52 | def assertFlagsChanged(self, expected, flags): |
| 53 | cmd = ['clang', '-c', 'source.c'] + flags |
| 54 | opts = sut.classify_parameters(cmd) |
| 55 | self.assertEqual(expected, opts['flags']) |
| 56 | |
| 57 | def assertFlagsUnchanged(self, flags): |
| 58 | self.assertFlagsChanged(flags, flags) |
| 59 | |
| 60 | def assertFlagsFiltered(self, flags): |
| 61 | self.assertFlagsChanged([], flags) |
| 62 | |
| 63 | def test_optimalizations_pass(self): |
| 64 | self.assertFlagsUnchanged(['-O']) |
| 65 | self.assertFlagsUnchanged(['-O1']) |
| 66 | self.assertFlagsUnchanged(['-Os']) |
| 67 | self.assertFlagsUnchanged(['-O2']) |
| 68 | self.assertFlagsUnchanged(['-O3']) |
| 69 | |
| 70 | def test_include_pass(self): |
| 71 | self.assertFlagsUnchanged([]) |
| 72 | self.assertFlagsUnchanged(['-include', '/usr/local/include']) |
| 73 | self.assertFlagsUnchanged(['-I.']) |
| 74 | self.assertFlagsUnchanged(['-I', '.']) |
| 75 | self.assertFlagsUnchanged(['-I/usr/local/include']) |
| 76 | self.assertFlagsUnchanged(['-I', '/usr/local/include']) |
| 77 | self.assertFlagsUnchanged(['-I/opt', '-I', '/opt/otp/include']) |
| 78 | self.assertFlagsUnchanged(['-isystem', '/path']) |
| 79 | self.assertFlagsUnchanged(['-isystem=/path']) |
| 80 | |
| 81 | def test_define_pass(self): |
| 82 | self.assertFlagsUnchanged(['-DNDEBUG']) |
| 83 | self.assertFlagsUnchanged(['-UNDEBUG']) |
| 84 | self.assertFlagsUnchanged(['-Dvar1=val1', '-Dvar2=val2']) |
| 85 | self.assertFlagsUnchanged(['-Dvar="val ues"']) |
| 86 | |
| 87 | def test_output_filtered(self): |
| 88 | self.assertFlagsFiltered(['-o', 'source.o']) |
| 89 | |
| 90 | def test_some_warning_filtered(self): |
| 91 | self.assertFlagsFiltered(['-Wall']) |
| 92 | self.assertFlagsFiltered(['-Wnoexcept']) |
| 93 | self.assertFlagsFiltered(['-Wreorder', '-Wunused', '-Wundef']) |
| 94 | self.assertFlagsUnchanged(['-Wno-reorder', '-Wno-unused']) |
| 95 | |
| 96 | def test_compile_only_flags_pass(self): |
| 97 | self.assertFlagsUnchanged(['-std=C99']) |
| 98 | self.assertFlagsUnchanged(['-nostdinc']) |
| 99 | self.assertFlagsUnchanged(['-isystem', '/image/debian']) |
| 100 | self.assertFlagsUnchanged(['-iprefix', '/usr/local']) |
| 101 | self.assertFlagsUnchanged(['-iquote=me']) |
| 102 | self.assertFlagsUnchanged(['-iquote', 'me']) |
| 103 | |
| 104 | def test_compile_and_link_flags_pass(self): |
| 105 | self.assertFlagsUnchanged(['-fsinged-char']) |
| 106 | self.assertFlagsUnchanged(['-fPIC']) |
| 107 | self.assertFlagsUnchanged(['-stdlib=libc++']) |
| 108 | self.assertFlagsUnchanged(['--sysroot', '/']) |
| 109 | self.assertFlagsUnchanged(['-isysroot', '/']) |
| 110 | |
| 111 | def test_some_flags_filtered(self): |
| 112 | self.assertFlagsFiltered(['-g']) |
| 113 | self.assertFlagsFiltered(['-fsyntax-only']) |
| 114 | self.assertFlagsFiltered(['-save-temps']) |
| 115 | self.assertFlagsFiltered(['-init', 'my_init']) |
| 116 | self.assertFlagsFiltered(['-sectorder', 'a', 'b', 'c']) |
| 117 | |
| 118 | |
| 119 | class Spy(object): |
| 120 | def __init__(self): |
| 121 | self.arg = None |
| 122 | self.success = 0 |
| 123 | |
| 124 | def call(self, params): |
| 125 | self.arg = params |
| 126 | return self.success |
| 127 | |
| 128 | |
| 129 | class RunAnalyzerTest(unittest.TestCase): |
| 130 | |
| 131 | @staticmethod |
| 132 | def run_analyzer(content, failures_report): |
| 133 | with libear.TemporaryDirectory() as tmpdir: |
| 134 | filename = os.path.join(tmpdir, 'test.cpp') |
| 135 | with open(filename, 'w') as handle: |
| 136 | handle.write(content) |
| 137 | |
| 138 | opts = { |
| 139 | 'clang': 'clang', |
| 140 | 'directory': os.getcwd(), |
| 141 | 'flags': [], |
| 142 | 'direct_args': [], |
| 143 | 'file': filename, |
| 144 | 'output_dir': tmpdir, |
| 145 | 'output_format': 'plist', |
| 146 | 'output_failures': failures_report |
| 147 | } |
| 148 | spy = Spy() |
| 149 | result = sut.run_analyzer(opts, spy.call) |
| 150 | return (result, spy.arg) |
| 151 | |
| 152 | def test_run_analyzer(self): |
| 153 | content = "int div(int n, int d) { return n / d; }" |
| 154 | (result, fwds) = RunAnalyzerTest.run_analyzer(content, False) |
| 155 | self.assertEqual(None, fwds) |
| 156 | self.assertEqual(0, result['exit_code']) |
| 157 | |
| 158 | def test_run_analyzer_crash(self): |
| 159 | content = "int div(int n, int d) { return n / d }" |
| 160 | (result, fwds) = RunAnalyzerTest.run_analyzer(content, False) |
| 161 | self.assertEqual(None, fwds) |
| 162 | self.assertEqual(1, result['exit_code']) |
| 163 | |
| 164 | def test_run_analyzer_crash_and_forwarded(self): |
| 165 | content = "int div(int n, int d) { return n / d }" |
| 166 | (_, fwds) = RunAnalyzerTest.run_analyzer(content, True) |
| 167 | self.assertEqual(1, fwds['exit_code']) |
| 168 | self.assertTrue(len(fwds['error_output']) > 0) |
| 169 | |
| 170 | |
| 171 | class ReportFailureTest(unittest.TestCase): |
| 172 | |
| 173 | def assertUnderFailures(self, path): |
| 174 | self.assertEqual('failures', os.path.basename(os.path.dirname(path))) |
| 175 | |
| 176 | def test_report_failure_create_files(self): |
| 177 | with libear.TemporaryDirectory() as tmpdir: |
| 178 | # create input file |
| 179 | filename = os.path.join(tmpdir, 'test.c') |
| 180 | with open(filename, 'w') as handle: |
| 181 | handle.write('int main() { return 0') |
| 182 | uname_msg = ' '.join(os.uname()) + os.linesep |
| 183 | error_msg = 'this is my error output' |
| 184 | # execute test |
| 185 | opts = { |
| 186 | 'clang': 'clang', |
| 187 | 'directory': os.getcwd(), |
| 188 | 'flags': [], |
| 189 | 'file': filename, |
| 190 | 'output_dir': tmpdir, |
| 191 | 'language': 'c', |
| 192 | 'error_type': 'other_error', |
| 193 | 'error_output': error_msg, |
| 194 | 'exit_code': 13 |
| 195 | } |
| 196 | sut.report_failure(opts) |
| 197 | # verify the result |
| 198 | result = dict() |
| 199 | pp_file = None |
| 200 | for root, _, files in os.walk(tmpdir): |
| 201 | keys = [os.path.join(root, name) for name in files] |
| 202 | for key in keys: |
| 203 | with open(key, 'r') as handle: |
| 204 | result[key] = handle.readlines() |
| 205 | if re.match(r'^(.*/)+clang(.*)\.i$', key): |
| 206 | pp_file = key |
| 207 | |
| 208 | # prepocessor file generated |
| 209 | self.assertUnderFailures(pp_file) |
| 210 | # info file generated and content dumped |
| 211 | info_file = pp_file + '.info.txt' |
| 212 | self.assertTrue(info_file in result) |
| 213 | self.assertEqual('Other Error\n', result[info_file][1]) |
| 214 | self.assertEqual(uname_msg, result[info_file][3]) |
| 215 | # error file generated and content dumped |
| 216 | error_file = pp_file + '.stderr.txt' |
| 217 | self.assertTrue(error_file in result) |
| 218 | self.assertEqual([error_msg], result[error_file]) |
| 219 | |
| 220 | |
| 221 | class AnalyzerTest(unittest.TestCase): |
| 222 | |
| 223 | def test_nodebug_macros_appended(self): |
| 224 | def test(flags): |
| 225 | spy = Spy() |
| 226 | opts = {'flags': flags, 'force_debug': True} |
| 227 | self.assertEqual(spy.success, |
| 228 | sut.filter_debug_flags(opts, spy.call)) |
| 229 | return spy.arg['flags'] |
| 230 | |
| 231 | self.assertEqual(['-UNDEBUG'], test([])) |
| 232 | self.assertEqual(['-DNDEBUG', '-UNDEBUG'], test(['-DNDEBUG'])) |
| 233 | self.assertEqual(['-DSomething', '-UNDEBUG'], test(['-DSomething'])) |
| 234 | |
| 235 | def test_set_language_fall_through(self): |
| 236 | def language(expected, input): |
| 237 | spy = Spy() |
| 238 | input.update({'compiler': 'c', 'file': 'test.c'}) |
| 239 | self.assertEqual(spy.success, sut.language_check(input, spy.call)) |
| 240 | self.assertEqual(expected, spy.arg['language']) |
| 241 | |
| 242 | language('c', {'language': 'c', 'flags': []}) |
| 243 | language('c++', {'language': 'c++', 'flags': []}) |
| 244 | |
| 245 | def test_set_language_stops_on_not_supported(self): |
| 246 | spy = Spy() |
| 247 | input = { |
| 248 | 'compiler': 'c', |
| 249 | 'flags': [], |
| 250 | 'file': 'test.java', |
| 251 | 'language': 'java' |
| 252 | } |
| 253 | self.assertIsNone(sut.language_check(input, spy.call)) |
| 254 | self.assertIsNone(spy.arg) |
| 255 | |
| 256 | def test_set_language_sets_flags(self): |
| 257 | def flags(expected, input): |
| 258 | spy = Spy() |
| 259 | input.update({'compiler': 'c', 'file': 'test.c'}) |
| 260 | self.assertEqual(spy.success, sut.language_check(input, spy.call)) |
| 261 | self.assertEqual(expected, spy.arg['flags']) |
| 262 | |
| 263 | flags(['-x', 'c'], {'language': 'c', 'flags': []}) |
| 264 | flags(['-x', 'c++'], {'language': 'c++', 'flags': []}) |
| 265 | |
| 266 | def test_set_language_from_filename(self): |
| 267 | def language(expected, input): |
| 268 | spy = Spy() |
| 269 | input.update({'language': None, 'flags': []}) |
| 270 | self.assertEqual(spy.success, sut.language_check(input, spy.call)) |
| 271 | self.assertEqual(expected, spy.arg['language']) |
| 272 | |
| 273 | language('c', {'file': 'file.c', 'compiler': 'c'}) |
| 274 | language('c++', {'file': 'file.c', 'compiler': 'c++'}) |
| 275 | language('c++', {'file': 'file.cxx', 'compiler': 'c'}) |
| 276 | language('c++', {'file': 'file.cxx', 'compiler': 'c++'}) |
| 277 | language('c++', {'file': 'file.cpp', 'compiler': 'c++'}) |
| 278 | language('c-cpp-output', {'file': 'file.i', 'compiler': 'c'}) |
| 279 | language('c++-cpp-output', {'file': 'file.i', 'compiler': 'c++'}) |
| 280 | |
| 281 | def test_arch_loop_sets_flags(self): |
| 282 | def flags(archs): |
| 283 | spy = Spy() |
| 284 | input = {'flags': [], 'arch_list': archs} |
| 285 | sut.arch_check(input, spy.call) |
| 286 | return spy.arg['flags'] |
| 287 | |
| 288 | self.assertEqual([], flags([])) |
| 289 | self.assertEqual(['-arch', 'i386'], flags(['i386'])) |
| 290 | self.assertEqual(['-arch', 'i386'], flags(['i386', 'ppc'])) |
| 291 | self.assertEqual(['-arch', 'sparc'], flags(['i386', 'sparc'])) |
| 292 | |
| 293 | def test_arch_loop_stops_on_not_supported(self): |
| 294 | def stop(archs): |
| 295 | spy = Spy() |
| 296 | input = {'flags': [], 'arch_list': archs} |
| 297 | self.assertIsNone(sut.arch_check(input, spy.call)) |
| 298 | self.assertIsNone(spy.arg) |
| 299 | |
| 300 | stop(['ppc']) |
| 301 | stop(['ppc64']) |
| 302 | |
| 303 | |
| 304 | @sut.require([]) |
| 305 | def method_without_expecteds(opts): |
| 306 | return 0 |
| 307 | |
| 308 | |
| 309 | @sut.require(['this', 'that']) |
| 310 | def method_with_expecteds(opts): |
| 311 | return 0 |
| 312 | |
| 313 | |
| 314 | @sut.require([]) |
| 315 | def method_exception_from_inside(opts): |
| 316 | raise Exception('here is one') |
| 317 | |
| 318 | |
| 319 | class RequireDecoratorTest(unittest.TestCase): |
| 320 | |
| 321 | def test_method_without_expecteds(self): |
| 322 | self.assertEqual(method_without_expecteds(dict()), 0) |
| 323 | self.assertEqual(method_without_expecteds({}), 0) |
| 324 | self.assertEqual(method_without_expecteds({'this': 2}), 0) |
| 325 | self.assertEqual(method_without_expecteds({'that': 3}), 0) |
| 326 | |
| 327 | def test_method_with_expecteds(self): |
| 328 | self.assertRaises(KeyError, method_with_expecteds, dict()) |
| 329 | self.assertRaises(KeyError, method_with_expecteds, {}) |
| 330 | self.assertRaises(KeyError, method_with_expecteds, {'this': 2}) |
| 331 | self.assertRaises(KeyError, method_with_expecteds, {'that': 3}) |
| 332 | self.assertEqual(method_with_expecteds({'this': 0, 'that': 3}), 0) |
| 333 | |
| 334 | def test_method_exception_not_caught(self): |
| 335 | self.assertRaises(Exception, method_exception_from_inside, dict()) |
Gabor Horvath | eb0584b | 2018-02-28 13:23:10 +0000 | [diff] [blame] | 336 | |
| 337 | |
| 338 | class PrefixWithTest(unittest.TestCase): |
| 339 | |
| 340 | def test_gives_empty_on_empty(self): |
| 341 | res = sut.prefix_with(0, []) |
| 342 | self.assertFalse(res) |
| 343 | |
| 344 | def test_interleaves_prefix(self): |
| 345 | res = sut.prefix_with(0, [1, 2, 3]) |
| 346 | self.assertListEqual([0, 1, 0, 2, 0, 3], res) |
| 347 | |
| 348 | |
| 349 | class MergeCtuMapTest(unittest.TestCase): |
| 350 | |
| 351 | def test_no_map_gives_empty(self): |
| 352 | pairs = sut.create_global_ctu_function_map([]) |
| 353 | self.assertFalse(pairs) |
| 354 | |
| 355 | def test_multiple_maps_merged(self): |
| 356 | concat_map = ['c:@F@fun1#I# ast/fun1.c.ast', |
| 357 | 'c:@F@fun2#I# ast/fun2.c.ast', |
| 358 | 'c:@F@fun3#I# ast/fun3.c.ast'] |
| 359 | pairs = sut.create_global_ctu_function_map(concat_map) |
| 360 | self.assertTrue(('c:@F@fun1#I#', 'ast/fun1.c.ast') in pairs) |
| 361 | self.assertTrue(('c:@F@fun2#I#', 'ast/fun2.c.ast') in pairs) |
| 362 | self.assertTrue(('c:@F@fun3#I#', 'ast/fun3.c.ast') in pairs) |
| 363 | self.assertEqual(3, len(pairs)) |
| 364 | |
| 365 | def test_not_unique_func_left_out(self): |
| 366 | concat_map = ['c:@F@fun1#I# ast/fun1.c.ast', |
| 367 | 'c:@F@fun2#I# ast/fun2.c.ast', |
| 368 | 'c:@F@fun1#I# ast/fun7.c.ast'] |
| 369 | pairs = sut.create_global_ctu_function_map(concat_map) |
| 370 | self.assertFalse(('c:@F@fun1#I#', 'ast/fun1.c.ast') in pairs) |
| 371 | self.assertFalse(('c:@F@fun1#I#', 'ast/fun7.c.ast') in pairs) |
| 372 | self.assertTrue(('c:@F@fun2#I#', 'ast/fun2.c.ast') in pairs) |
| 373 | self.assertEqual(1, len(pairs)) |
| 374 | |
| 375 | def test_duplicates_are_kept(self): |
| 376 | concat_map = ['c:@F@fun1#I# ast/fun1.c.ast', |
| 377 | 'c:@F@fun2#I# ast/fun2.c.ast', |
| 378 | 'c:@F@fun1#I# ast/fun1.c.ast'] |
| 379 | pairs = sut.create_global_ctu_function_map(concat_map) |
| 380 | self.assertTrue(('c:@F@fun1#I#', 'ast/fun1.c.ast') in pairs) |
| 381 | self.assertTrue(('c:@F@fun2#I#', 'ast/fun2.c.ast') in pairs) |
| 382 | self.assertEqual(2, len(pairs)) |
| 383 | |
| 384 | def test_space_handled_in_source(self): |
| 385 | concat_map = ['c:@F@fun1#I# ast/f un.c.ast'] |
| 386 | pairs = sut.create_global_ctu_function_map(concat_map) |
| 387 | self.assertTrue(('c:@F@fun1#I#', 'ast/f un.c.ast') in pairs) |
| 388 | self.assertEqual(1, len(pairs)) |
| 389 | |
| 390 | |
| 391 | class FuncMapSrcToAstTest(unittest.TestCase): |
| 392 | |
| 393 | def test_empty_gives_empty(self): |
| 394 | fun_ast_lst = sut.func_map_list_src_to_ast([]) |
| 395 | self.assertFalse(fun_ast_lst) |
| 396 | |
| 397 | def test_sources_to_asts(self): |
| 398 | fun_src_lst = ['c:@F@f1#I# ' + os.path.join(os.sep + 'path', 'f1.c'), |
| 399 | 'c:@F@f2#I# ' + os.path.join(os.sep + 'path', 'f2.c')] |
| 400 | fun_ast_lst = sut.func_map_list_src_to_ast(fun_src_lst) |
| 401 | self.assertTrue('c:@F@f1#I# ' + |
| 402 | os.path.join('ast', 'path', 'f1.c.ast') |
| 403 | in fun_ast_lst) |
| 404 | self.assertTrue('c:@F@f2#I# ' + |
| 405 | os.path.join('ast', 'path', 'f2.c.ast') |
| 406 | in fun_ast_lst) |
| 407 | self.assertEqual(2, len(fun_ast_lst)) |
| 408 | |
| 409 | def test_spaces_handled(self): |
| 410 | fun_src_lst = ['c:@F@f1#I# ' + os.path.join(os.sep + 'path', 'f 1.c')] |
| 411 | fun_ast_lst = sut.func_map_list_src_to_ast(fun_src_lst) |
| 412 | self.assertTrue('c:@F@f1#I# ' + |
| 413 | os.path.join('ast', 'path', 'f 1.c.ast') |
| 414 | in fun_ast_lst) |
| 415 | self.assertEqual(1, len(fun_ast_lst)) |