[memprof] Upgrade a unit test to MemProf Version 3 (#117063)
[llvm-project.git] / clang / utils / check_cfc / test_check_cfc.py
blobcd4441b702cde3ab8e4901d1516618d60d10be01
1 #!/usr/bin/env python
3 """Test internal functions within check_cfc.py."""
5 import check_cfc
6 import os
7 import platform
8 import unittest
11 class TestCheckCFC(unittest.TestCase):
12 def test_flip_dash_g(self):
13 self.assertIn("-g", check_cfc.flip_dash_g(["clang", "-c"]))
14 self.assertNotIn("-g", check_cfc.flip_dash_g(["clang", "-c", "-g"]))
15 self.assertNotIn("-g", check_cfc.flip_dash_g(["clang", "-g", "-c", "-g"]))
17 def test_remove_dir_from_path(self):
18 bin_path = r"/usr/bin"
19 space_path = r"/home/user/space in path"
20 superstring_path = r"/usr/bin/local"
22 # Test removing last thing in path
23 self.assertNotIn(bin_path, check_cfc.remove_dir_from_path(bin_path, bin_path))
25 # Test removing one entry and leaving others
26 # Also tests removing repeated path
27 path_var = os.pathsep.join([superstring_path, bin_path, space_path, bin_path])
28 stripped_path_var = check_cfc.remove_dir_from_path(path_var, bin_path)
29 self.assertIn(superstring_path, stripped_path_var)
30 self.assertNotIn(bin_path, stripped_path_var.split(os.pathsep))
31 self.assertIn(space_path, stripped_path_var)
33 # Test removing non-canonical path
34 self.assertNotIn(
35 r"/usr//bin", check_cfc.remove_dir_from_path(r"/usr//bin", bin_path)
38 if platform == "Windows":
39 # Windows is case insensitive so should remove a different case
40 # path
41 self.assertNotIn(
42 bin_path, check_cfc.remove_dir_from_path(path_var, r"/USR/BIN")
44 else:
45 # Case sensitive so will not remove different case path
46 self.assertIn(
47 bin_path, check_cfc.remove_dir_from_path(path_var, r"/USR/BIN")
50 def test_is_output_specified(self):
51 self.assertTrue(check_cfc.is_output_specified(["clang", "-o", "test.o"]))
52 self.assertTrue(check_cfc.is_output_specified(["clang", "-otest.o"]))
53 self.assertFalse(check_cfc.is_output_specified(["clang", "-gline-tables-only"]))
54 # Not specified for implied output file name
55 self.assertFalse(check_cfc.is_output_specified(["clang", "test.c"]))
57 def test_get_output_file(self):
58 self.assertEqual(check_cfc.get_output_file(["clang", "-o", "test.o"]), "test.o")
59 self.assertEqual(check_cfc.get_output_file(["clang", "-otest.o"]), "test.o")
60 self.assertIsNone(check_cfc.get_output_file(["clang", "-gline-tables-only"]))
61 # Can't get output file if more than one input file
62 self.assertIsNone(
63 check_cfc.get_output_file(["clang", "-c", "test.cpp", "test2.cpp"])
65 # No output file specified
66 self.assertIsNone(check_cfc.get_output_file(["clang", "-c", "test.c"]))
68 def test_derive_output_file(self):
69 # Test getting implicit output file
70 self.assertEqual(
71 check_cfc.derive_output_file(["clang", "-c", "test.c"]), "test.o"
73 self.assertEqual(
74 check_cfc.derive_output_file(["clang", "-c", "test.cpp"]), "test.o"
76 self.assertIsNone(check_cfc.derive_output_file(["clang", "--version"]))
78 def test_is_normal_compile(self):
79 self.assertTrue(
80 check_cfc.is_normal_compile(["clang", "-c", "test.cpp", "-o", "test2.o"])
82 self.assertTrue(check_cfc.is_normal_compile(["clang", "-c", "test.cpp"]))
83 # Outputting bitcode is not a normal compile
84 self.assertFalse(
85 check_cfc.is_normal_compile(["clang", "-c", "test.cpp", "-flto"])
87 self.assertFalse(
88 check_cfc.is_normal_compile(["clang", "-c", "test.cpp", "-emit-llvm"])
90 # Outputting preprocessed output or assembly is not a normal compile
91 self.assertFalse(
92 check_cfc.is_normal_compile(["clang", "-E", "test.cpp", "-o", "test.ii"])
94 self.assertFalse(
95 check_cfc.is_normal_compile(["clang", "-S", "test.cpp", "-o", "test.s"])
97 # Input of preprocessed or assembly is not a "normal compile"
98 self.assertFalse(
99 check_cfc.is_normal_compile(["clang", "-c", "test.s", "-o", "test.o"])
101 self.assertFalse(
102 check_cfc.is_normal_compile(["clang", "-c", "test.ii", "-o", "test.o"])
104 # Specifying --version and -c is not a normal compile
105 self.assertFalse(
106 check_cfc.is_normal_compile(["clang", "-c", "test.cpp", "--version"])
108 self.assertFalse(
109 check_cfc.is_normal_compile(["clang", "-c", "test.cpp", "--help"])
111 # Outputting dependency files is not a normal compile
112 self.assertFalse(check_cfc.is_normal_compile(["clang", "-c", "-M", "test.cpp"]))
113 self.assertFalse(
114 check_cfc.is_normal_compile(["clang", "-c", "-MM", "test.cpp"])
116 # Creating a dependency file as a side effect still outputs an object file
117 self.assertTrue(check_cfc.is_normal_compile(["clang", "-c", "-MD", "test.cpp"]))
118 self.assertTrue(
119 check_cfc.is_normal_compile(["clang", "-c", "-MMD", "test.cpp"])
122 def test_replace_output_file(self):
123 self.assertEqual(
124 check_cfc.replace_output_file(["clang", "-o", "test.o"], "testg.o"),
125 ["clang", "-o", "testg.o"],
127 self.assertEqual(
128 check_cfc.replace_output_file(["clang", "-otest.o"], "testg.o"),
129 ["clang", "-otestg.o"],
131 with self.assertRaises(Exception):
132 check_cfc.replace_output_file(["clang"], "testg.o")
134 def test_add_output_file(self):
135 self.assertEqual(
136 check_cfc.add_output_file(["clang"], "testg.o"), ["clang", "-o", "testg.o"]
139 def test_set_output_file(self):
140 # Test output not specified
141 self.assertEqual(
142 check_cfc.set_output_file(["clang"], "test.o"), ["clang", "-o", "test.o"]
144 # Test output is specified
145 self.assertEqual(
146 check_cfc.set_output_file(["clang", "-o", "test.o"], "testb.o"),
147 ["clang", "-o", "testb.o"],
150 def test_get_input_file(self):
151 # No input file
152 self.assertIsNone(check_cfc.get_input_file(["clang"]))
153 # Input C file
154 self.assertEqual(check_cfc.get_input_file(["clang", "test.c"]), "test.c")
155 # Input C++ file
156 self.assertEqual(check_cfc.get_input_file(["clang", "test.cpp"]), "test.cpp")
157 # Multiple input files
158 self.assertIsNone(check_cfc.get_input_file(["clang", "test.c", "test2.cpp"]))
159 self.assertIsNone(check_cfc.get_input_file(["clang", "test.c", "test2.c"]))
160 # Don't handle preprocessed files
161 self.assertIsNone(check_cfc.get_input_file(["clang", "test.i"]))
162 self.assertIsNone(check_cfc.get_input_file(["clang", "test.ii"]))
163 # Test identifying input file with quotes
164 self.assertEqual(check_cfc.get_input_file(["clang", '"test.c"']), '"test.c"')
165 self.assertEqual(check_cfc.get_input_file(["clang", "'test.c'"]), "'test.c'")
166 # Test multiple quotes
167 self.assertEqual(
168 check_cfc.get_input_file(["clang", "\"'test.c'\""]), "\"'test.c'\""
171 def test_set_input_file(self):
172 self.assertEqual(
173 check_cfc.set_input_file(["clang", "test.c"], "test.s"), ["clang", "test.s"]
177 if __name__ == "__main__":
178 unittest.main()