Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / bindings / python / tests / cindex / test_cdb.py
bloba5cc22796aa2ad957e21670872768cbb18be47bb
1 import os
2 from clang.cindex import Config
4 if "CLANG_LIBRARY_PATH" in os.environ:
5 Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
7 from clang.cindex import CompilationDatabase
8 from clang.cindex import CompilationDatabaseError
9 from clang.cindex import CompileCommands
10 from clang.cindex import CompileCommand
11 import os
12 import gc
13 import unittest
14 import sys
15 from .util import skip_if_no_fspath
16 from .util import str_to_path
19 kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
22 @unittest.skipIf(sys.platform == "win32", "TODO: Fix these tests on Windows")
23 class TestCDB(unittest.TestCase):
24 def test_create_fail(self):
25 """Check we fail loading a database with an assertion"""
26 path = os.path.dirname(__file__)
28 # clang_CompilationDatabase_fromDirectory calls fprintf(stderr, ...)
29 # Suppress its output.
30 stderr = os.dup(2)
31 with open(os.devnull, "wb") as null:
32 os.dup2(null.fileno(), 2)
33 with self.assertRaises(CompilationDatabaseError) as cm:
34 cdb = CompilationDatabase.fromDirectory(path)
35 os.dup2(stderr, 2)
36 os.close(stderr)
38 e = cm.exception
39 self.assertEqual(e.cdb_error, CompilationDatabaseError.ERROR_CANNOTLOADDATABASE)
41 def test_create(self):
42 """Check we can load a compilation database"""
43 cdb = CompilationDatabase.fromDirectory(kInputsDir)
45 def test_lookup_succeed(self):
46 """Check we get some results if the file exists in the db"""
47 cdb = CompilationDatabase.fromDirectory(kInputsDir)
48 cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
49 self.assertNotEqual(len(cmds), 0)
51 @skip_if_no_fspath
52 def test_lookup_succeed_pathlike(self):
53 """Same as test_lookup_succeed, but with PathLikes"""
54 cdb = CompilationDatabase.fromDirectory(str_to_path(kInputsDir))
55 cmds = cdb.getCompileCommands(
56 str_to_path("/home/john.doe/MyProject/project.cpp")
58 self.assertNotEqual(len(cmds), 0)
60 def test_all_compilecommand(self):
61 """Check we get all results from the db"""
62 cdb = CompilationDatabase.fromDirectory(kInputsDir)
63 cmds = cdb.getAllCompileCommands()
64 self.assertEqual(len(cmds), 3)
65 expected = [
67 "wd": "/home/john.doe/MyProject",
68 "file": "/home/john.doe/MyProject/project.cpp",
69 "line": [
70 "clang++",
71 "--driver-mode=g++",
72 "-o",
73 "project.o",
74 "-c",
75 "/home/john.doe/MyProject/project.cpp",
79 "wd": "/home/john.doe/MyProjectA",
80 "file": "/home/john.doe/MyProject/project2.cpp",
81 "line": [
82 "clang++",
83 "--driver-mode=g++",
84 "-o",
85 "project2.o",
86 "-c",
87 "/home/john.doe/MyProject/project2.cpp",
91 "wd": "/home/john.doe/MyProjectB",
92 "file": "/home/john.doe/MyProject/project2.cpp",
93 "line": [
94 "clang++",
95 "--driver-mode=g++",
96 "-DFEATURE=1",
97 "-o",
98 "project2-feature.o",
99 "-c",
100 "/home/john.doe/MyProject/project2.cpp",
104 for i in range(len(cmds)):
105 self.assertEqual(cmds[i].directory, expected[i]["wd"])
106 self.assertEqual(cmds[i].filename, expected[i]["file"])
107 for arg, exp in zip(cmds[i].arguments, expected[i]["line"]):
108 self.assertEqual(arg, exp)
110 def test_1_compilecommand(self):
111 """Check file with single compile command"""
112 cdb = CompilationDatabase.fromDirectory(kInputsDir)
113 file = "/home/john.doe/MyProject/project.cpp"
114 cmds = cdb.getCompileCommands(file)
115 self.assertEqual(len(cmds), 1)
116 self.assertEqual(cmds[0].directory, os.path.dirname(file))
117 self.assertEqual(cmds[0].filename, file)
118 expected = [
119 "clang++",
120 "--driver-mode=g++",
121 "-o",
122 "project.o",
123 "-c",
124 "/home/john.doe/MyProject/project.cpp",
126 for arg, exp in zip(cmds[0].arguments, expected):
127 self.assertEqual(arg, exp)
129 def test_2_compilecommand(self):
130 """Check file with 2 compile commands"""
131 cdb = CompilationDatabase.fromDirectory(kInputsDir)
132 cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project2.cpp")
133 self.assertEqual(len(cmds), 2)
134 expected = [
136 "wd": "/home/john.doe/MyProjectA",
137 "line": [
138 "clang++",
139 "--driver-mode=g++",
140 "-o",
141 "project2.o",
142 "-c",
143 "/home/john.doe/MyProject/project2.cpp",
147 "wd": "/home/john.doe/MyProjectB",
148 "line": [
149 "clang++",
150 "--driver-mode=g++",
151 "-DFEATURE=1",
152 "-o",
153 "project2-feature.o",
154 "-c",
155 "/home/john.doe/MyProject/project2.cpp",
159 for i in range(len(cmds)):
160 self.assertEqual(cmds[i].directory, expected[i]["wd"])
161 for arg, exp in zip(cmds[i].arguments, expected[i]["line"]):
162 self.assertEqual(arg, exp)
164 def test_compilecommand_iterator_stops(self):
165 """Check that iterator stops after the correct number of elements"""
166 cdb = CompilationDatabase.fromDirectory(kInputsDir)
167 count = 0
168 for cmd in cdb.getCompileCommands("/home/john.doe/MyProject/project2.cpp"):
169 count += 1
170 self.assertLessEqual(count, 2)
172 def test_compilationDB_references(self):
173 """Ensure CompilationsCommands are independent of the database"""
174 cdb = CompilationDatabase.fromDirectory(kInputsDir)
175 cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
176 del cdb
177 gc.collect()
178 workingdir = cmds[0].directory
180 def test_compilationCommands_references(self):
181 """Ensure CompilationsCommand keeps a reference to CompilationCommands"""
182 cdb = CompilationDatabase.fromDirectory(kInputsDir)
183 cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
184 del cdb
185 cmd0 = cmds[0]
186 del cmds
187 gc.collect()
188 workingdir = cmd0.directory