Laszlo Nagy | 8bd63e5 | 2016-04-19 12:03:03 +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 | |
| 7 | import libscanbuild.compilation as sut |
| 8 | import unittest |
| 9 | |
| 10 | |
| 11 | class CompilerTest(unittest.TestCase): |
| 12 | |
| 13 | def test_is_compiler_call(self): |
| 14 | self.assertIsNotNone(sut.compiler_language(['clang'])) |
| 15 | self.assertIsNotNone(sut.compiler_language(['clang-3.6'])) |
| 16 | self.assertIsNotNone(sut.compiler_language(['clang++'])) |
| 17 | self.assertIsNotNone(sut.compiler_language(['clang++-3.5.1'])) |
| 18 | self.assertIsNotNone(sut.compiler_language(['cc'])) |
| 19 | self.assertIsNotNone(sut.compiler_language(['c++'])) |
| 20 | self.assertIsNotNone(sut.compiler_language(['gcc'])) |
| 21 | self.assertIsNotNone(sut.compiler_language(['g++'])) |
| 22 | self.assertIsNotNone(sut.compiler_language(['/usr/local/bin/gcc'])) |
| 23 | self.assertIsNotNone(sut.compiler_language(['/usr/local/bin/g++'])) |
| 24 | self.assertIsNotNone(sut.compiler_language(['/usr/local/bin/clang'])) |
| 25 | self.assertIsNotNone( |
| 26 | sut.compiler_language(['armv7_neno-linux-gnueabi-g++'])) |
| 27 | |
| 28 | self.assertIsNone(sut.compiler_language([])) |
| 29 | self.assertIsNone(sut.compiler_language([''])) |
| 30 | self.assertIsNone(sut.compiler_language(['ld'])) |
| 31 | self.assertIsNone(sut.compiler_language(['as'])) |
| 32 | self.assertIsNone(sut.compiler_language(['/usr/local/bin/compiler'])) |
| 33 | |
| 34 | |
| 35 | class SplitTest(unittest.TestCase): |
| 36 | |
| 37 | def test_detect_cxx_from_compiler_name(self): |
| 38 | def test(cmd): |
| 39 | result = sut.split_command([cmd, '-c', 'src.c']) |
| 40 | self.assertIsNotNone(result, "wrong input for test") |
| 41 | return result.compiler == 'c++' |
| 42 | |
| 43 | self.assertFalse(test('cc')) |
| 44 | self.assertFalse(test('gcc')) |
| 45 | self.assertFalse(test('clang')) |
| 46 | |
| 47 | self.assertTrue(test('c++')) |
| 48 | self.assertTrue(test('g++')) |
| 49 | self.assertTrue(test('g++-5.3.1')) |
| 50 | self.assertTrue(test('clang++')) |
| 51 | self.assertTrue(test('clang++-3.7.1')) |
| 52 | self.assertTrue(test('armv7_neno-linux-gnueabi-g++')) |
| 53 | |
| 54 | def test_action(self): |
| 55 | self.assertIsNotNone(sut.split_command(['clang', 'source.c'])) |
| 56 | self.assertIsNotNone(sut.split_command(['clang', '-c', 'source.c'])) |
| 57 | self.assertIsNotNone(sut.split_command(['clang', '-c', 'source.c', |
| 58 | '-MF', 'a.d'])) |
| 59 | |
| 60 | self.assertIsNone(sut.split_command(['clang', '-E', 'source.c'])) |
| 61 | self.assertIsNone(sut.split_command(['clang', '-c', '-E', 'source.c'])) |
| 62 | self.assertIsNone(sut.split_command(['clang', '-c', '-M', 'source.c'])) |
| 63 | self.assertIsNone( |
| 64 | sut.split_command(['clang', '-c', '-MM', 'source.c'])) |
| 65 | |
| 66 | def test_source_file(self): |
| 67 | def test(expected, cmd): |
| 68 | self.assertEqual(expected, sut.split_command(cmd).files) |
| 69 | |
| 70 | test(['src.c'], ['clang', 'src.c']) |
| 71 | test(['src.c'], ['clang', '-c', 'src.c']) |
| 72 | test(['src.C'], ['clang', '-x', 'c', 'src.C']) |
| 73 | test(['src.cpp'], ['clang++', '-c', 'src.cpp']) |
| 74 | test(['s1.c', 's2.c'], ['clang', '-c', 's1.c', 's2.c']) |
| 75 | test(['s1.c', 's2.c'], ['cc', 's1.c', 's2.c', '-ldep', '-o', 'a.out']) |
| 76 | test(['src.c'], ['clang', '-c', '-I', './include', 'src.c']) |
| 77 | test(['src.c'], ['clang', '-c', '-I', '/opt/me/include', 'src.c']) |
| 78 | test(['src.c'], ['clang', '-c', '-D', 'config=file.c', 'src.c']) |
| 79 | |
| 80 | self.assertIsNone( |
| 81 | sut.split_command(['cc', 'this.o', 'that.o', '-o', 'a.out'])) |
| 82 | self.assertIsNone( |
| 83 | sut.split_command(['cc', 'this.o', '-lthat', '-o', 'a.out'])) |
| 84 | |
| 85 | def test_filter_flags(self): |
| 86 | def test(expected, flags): |
| 87 | command = ['clang', '-c', 'src.c'] + flags |
| 88 | self.assertEqual(expected, sut.split_command(command).flags) |
| 89 | |
| 90 | def same(expected): |
| 91 | test(expected, expected) |
| 92 | |
| 93 | def filtered(flags): |
| 94 | test([], flags) |
| 95 | |
| 96 | same([]) |
| 97 | same(['-I', '/opt/me/include', '-DNDEBUG', '-ULIMITS']) |
| 98 | same(['-O', '-O2']) |
| 99 | same(['-m32', '-mmms']) |
| 100 | same(['-Wall', '-Wno-unused', '-g', '-funroll-loops']) |
| 101 | |
| 102 | filtered([]) |
| 103 | filtered(['-lclien', '-L/opt/me/lib', '-L', '/opt/you/lib']) |
| 104 | filtered(['-static']) |
| 105 | filtered(['-MD', '-MT', 'something']) |
| 106 | filtered(['-MMD', '-MF', 'something']) |
| 107 | |
| 108 | |
| 109 | class SourceClassifierTest(unittest.TestCase): |
| 110 | |
| 111 | def test_sources(self): |
| 112 | self.assertIsNone(sut.classify_source('file.o')) |
| 113 | self.assertIsNone(sut.classify_source('file.exe')) |
| 114 | self.assertIsNone(sut.classify_source('/path/file.o')) |
| 115 | self.assertIsNone(sut.classify_source('clang')) |
| 116 | |
| 117 | self.assertEqual('c', sut.classify_source('file.c')) |
| 118 | self.assertEqual('c', sut.classify_source('./file.c')) |
| 119 | self.assertEqual('c', sut.classify_source('/path/file.c')) |
| 120 | self.assertEqual('c++', sut.classify_source('file.c', False)) |
| 121 | self.assertEqual('c++', sut.classify_source('./file.c', False)) |
| 122 | self.assertEqual('c++', sut.classify_source('/path/file.c', False)) |