def test_03(self): test_srcdir = self.srcdir('test_03') result = self.complement([ CompileCommand(directory=test_srcdir, arguments=['clang++', '-DAB=1'], file='a_b.cpp'), CompileCommand(directory=test_srcdir, arguments=['clang++', '-DA=1'], file='a.cpp'), CompileCommand(directory=test_srcdir, arguments=['clang++', '-DB=1'], file='b.cpp'), ]) self.assertEqual(4, len(result)) self.assertEqual('a.hpp', result[0].file) self.assertEqual(['clang++', '-DA=1', '-c', 'a.hpp'], result[0].arguments) self.assertEqual('a_private.hpp', result[1].file) self.assertEqual(['clang++', '-DA=1', '-c', 'a_private.hpp'], result[1].arguments) self.assertEqual('b.hpp', result[2].file) self.assertEqual(['clang++', '-DB=1', '-c', 'b.hpp'], result[2].arguments) self.assertEqual('b_private.hpp', result[3].file) self.assertEqual(['clang++', '-DB=1', '-c', 'b_private.hpp'], result[3].arguments)
def test_comparable(self): a1 = CompileCommand("/", "a.c", ["cc"]) a2 = CompileCommand("/", "a.c", ["cc"]) b = CompileCommand("/", "b.c", ["cc"]) self.assertTrue(a1 == a2) self.assertFalse(a1 == b) self.assertTrue(a1 != b) self.assertFalse(a1 != a2) self.assertEqual(a1, a2)
def test_01(self): test_srcdir = self.srcdir('test_01') result = self.complement([ CompileCommand(directory=test_srcdir, arguments=['clang++', '-DA=1'], file='a.cpp'), CompileCommand(directory=test_srcdir, arguments=['clang++', '-DB=1'], file='b.cpp'), ]) self.assertEqual(1, len(result)) self.assertEqual('a.hpp', result[0].file) self.assertEqual(['clang++', '-DA=1'], result[0].arguments)
def test_05(self): test_srcdir = self.srcdir('test_05') result = self.complement([ CompileCommand(directory=test_srcdir, arguments=['clang++', '-DLATIN=1'], file='latin-1-á.cpp'), CompileCommand(directory=test_srcdir, arguments=['clang++', '-DUTF=8'], file='utf-8-á.cpp'), ]) self.assertEqual(2, len(result)) self.assertEqual('latin-1-á.hpp', result[0].file) self.assertEqual(['clang++', '-DLATIN=1'], result[0].arguments) self.assertEqual('utf-8-á.hpp', result[1].file) self.assertEqual(['clang++', '-DUTF=8'], result[1].arguments)
def test_quoted(self): d_commands = list(self.db.get_compile_commands("/tmp/d.cpp")) self.assertEqual(len(d_commands), 1) self.assertEqual( d_commands[0], CompileCommand("/tmp/", "/tmp/d.cpp", ["clang", "-I", "a b c", "-I", "d e h"]))
def derive_compile_command(header_file, reference): header_file_relative = mimic_path_relativity(header_file, reference.file, reference.directory) args = sanitize_compile_options(reference) args.extend(["-c", header_file_relative]) return CompileCommand(directory=reference.directory, file=header_file_relative, arguments=args)
def test_02(self): test_srcdir = self.srcdir('test_02') result = self.complement([ CompileCommand(directory=test_srcdir, arguments=['clang++', '-Iinclude', '-DA=1'], file='src/a.cpp'), CompileCommand(directory=test_srcdir, arguments=['clang++', '-Iinclude', '-DB=1'], file='src/b.cpp'), ]) self.assertEqual(2, len(result)) self.assertEqual('include/a/a.hpp', result[0].file) self.assertEqual(['clang++', '-Iinclude', '-DA=1'], result[0].arguments) self.assertEqual('include/b/b.hpp', result[1].file) self.assertEqual(['clang++', '-Iinclude', '-DB=1'], result[1].arguments)
def _dict_to_compile_command(d): if 'arguments' in d: arguments = d['arguments'] else: # PERFORMANCE: I think shlex is inherently slow, # something performing better may be necessary arguments = shlex.split(d['command']) return CompileCommand(d['directory'], d['file'], arguments, d.get('output'))
def test_get_compile_commands(self): a_commands = list(self.db.get_compile_commands("/tmp/a.cpp")) self.assertEqual(len(a_commands), 1) self.assertEqual(a_commands[0], CompileCommand("/tmp/", "/tmp/a.cpp", ["clang", "-DA=1"])) b_commands = list(self.db.get_compile_commands("/tmp/b.cpp")) self.assertEqual(len(b_commands), 2) self.assertEqual(b_commands[0], CompileCommand("/tmp/", "/tmp/b.cpp", ["clang", "-DB=1"])) self.assertEqual(b_commands[1], CompileCommand("/tmp/", "/tmp/b.cpp", ["clang", "-DB=2"])) c_commands = list(self.db.get_compile_commands("/tmp/c.cpp")) self.assertEqual(len(c_commands), 1) self.assertEqual(c_commands[0], CompileCommand("/tmp/", "/tmp/c.cpp", ["clang", "-DC=1"], "c.o"))
def test_06(self): test_srcdir = self.srcdir('test_06') result = self.complement([ CompileCommand(directory=test_srcdir, arguments=['clang++', '-Iinclude', '-Iinclude/a'], file='a.cpp'), ]) self.assertEqual(1, len(result)) self.assertEqual('include/a/a', result[0].file) self.assertEqual(['clang++', '-Iinclude', '-Iinclude/a'], result[0].arguments)
def _dict_to_compile_command(d): if 'arguments' in d: arguments = d['arguments'] else: # PERFORMANCE: I think shlex is inherently slow, # something performing better may be necessary arguments = shlex.split( d['command'], # XXX: os.name is "posix" on mysys2/cygwin, # is that correct? posix=os.name == "posix") return CompileCommand(d['directory'], d['file'], arguments, d.get('output'))
# -- http://clang.llvm.org/docs/JSONCompilationDatabase.html ARGUMENTS_TO_JSON_DATA = [ (['clang++'], r'"clang++"'), (['clang++', '-std=c++11'], r'"clang++ -std=c++11"'), (['clang++', '-DFOO=a b'], r'"clang++ \"-DFOO=a b\""'), (['clang++', '-DFOO="str"'], r'"clang++ -DFOO=\\\"str\\\""'), (['clang++', '-DFOO="string with spaces"'], r'"clang++ \"-DFOO=\\\"string with spaces\\\"\""'), (['clang++', '-DFOO="string with spaces and \\-slash"'], r'"clang++ \"-DFOO=\\\"string with spaces and \\\\-slash\\\"\""'), (['clang++', "-DBAR='c'"], '"clang++ \\"-DBAR=\'c\'\\""'), ] COMPILE_COMMANDS_TO_JSON_DATA = ([ CompileCommand("/tmp", "foo.cpp", ["clang++"]), CompileCommand("/tmp/bar", "bar.cpp", ["clang++", "-std=c++11"]), CompileCommand("/tmp/foo", "foo.cpp", ["clang++", "-std=c++11"], "foo.o"), ], r"""[ { "directory": "/tmp", "command": "clang++", "file": "foo.cpp" }, { "directory": "/tmp/bar", "command": "clang++ -std=c++11", "file": "bar.cpp" },
def derive_compile_command(header_file, reference): return CompileCommand(directory=reference.directory, file=mimic_path_relativity(header_file, reference.file, reference.directory), arguments=sanitize_compile_options(reference))