summaryrefslogtreecommitdiffstats
path: root/clang/bindings/python/tests
diff options
context:
space:
mode:
authorArnaud A. de Grandmaison <arnaud.adegm@gmail.com>2012-06-30 11:28:04 +0000
committerArnaud A. de Grandmaison <arnaud.adegm@gmail.com>2012-06-30 11:28:04 +0000
commitc0560064dd6b8a72dc85527d0b074352a2e3cb3f (patch)
tree0b37a8389aed94877afbd4c3a9ff51d9a630a18f /clang/bindings/python/tests
parent0fe28a1a84c193c3db58495d9f11766ef99d61c7 (diff)
downloadbcm5719-llvm-c0560064dd6b8a72dc85527d0b074352a2e3cb3f.tar.gz
bcm5719-llvm-c0560064dd6b8a72dc85527d0b074352a2e3cb3f.zip
[cindex.py] add CompilationDatabase support
llvm-svn: 159485
Diffstat (limited to 'clang/bindings/python/tests')
-rw-r--r--clang/bindings/python/tests/cindex/INPUTS/compile_commands.json17
-rw-r--r--clang/bindings/python/tests/cindex/test_cdb.py81
2 files changed, 98 insertions, 0 deletions
diff --git a/clang/bindings/python/tests/cindex/INPUTS/compile_commands.json b/clang/bindings/python/tests/cindex/INPUTS/compile_commands.json
new file mode 100644
index 00000000000..944150bf7ba
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/INPUTS/compile_commands.json
@@ -0,0 +1,17 @@
+[
+{
+ "directory": "/home/john.doe/MyProject",
+ "command": "clang++ -o project.o -c /home/john.doe/MyProject/project.cpp",
+ "file": "/home/john.doe/MyProject/project.cpp"
+},
+{
+ "directory": "/home/john.doe/MyProjectA",
+ "command": "clang++ -o project2.o -c /home/john.doe/MyProject/project2.cpp",
+ "file": "/home/john.doe/MyProject/project2.cpp"
+},
+{
+ "directory": "/home/john.doe/MyProjectB",
+ "command": "clang++ -DFEATURE=1 -o project2-feature.o -c /home/john.doe/MyProject/project2.cpp",
+ "file": "/home/john.doe/MyProject/project2.cpp"
+}
+]
diff --git a/clang/bindings/python/tests/cindex/test_cdb.py b/clang/bindings/python/tests/cindex/test_cdb.py
new file mode 100644
index 00000000000..84ac1f87d56
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/test_cdb.py
@@ -0,0 +1,81 @@
+from clang.cindex import CompilationDatabase
+from clang.cindex import CompilationDatabaseError
+from clang.cindex import CompileCommands
+from clang.cindex import CompileCommand
+import os
+import gc
+
+kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS')
+
+def test_create_fail():
+ """Check we fail loading a database with an assertion"""
+ path = os.path.dirname(__file__)
+ try:
+ cdb = CompilationDatabase.fromDirectory(path)
+ except CompilationDatabaseError as e:
+ assert e.cdb_error == CompilationDatabaseError.ERROR_CANNOTLOADDATABASE
+ else:
+ assert False
+
+def test_create():
+ """Check we can load a compilation database"""
+ cdb = CompilationDatabase.fromDirectory(kInputsDir)
+
+def test_lookup_fail():
+ """Check an assertion is raised when file lookup failed"""
+ cdb = CompilationDatabase.fromDirectory(kInputsDir)
+ assert cdb.getCompileCommands('file_do_not_exist.cpp') == None
+
+def test_lookup_succeed():
+ """Check we get some results if the file exists in the db"""
+ cdb = CompilationDatabase.fromDirectory(kInputsDir)
+ cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
+ assert len(cmds) != 0
+
+def test_1_compilecommand():
+ """Check file with single compile command"""
+ cdb = CompilationDatabase.fromDirectory(kInputsDir)
+ cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
+ assert len(cmds) == 1
+ assert cmds[0].directory == '/home/john.doe/MyProject'
+ expected = [ 'clang++', '-o', 'project.o', '-c',
+ '/home/john.doe/MyProject/project.cpp']
+ for arg, exp in zip(cmds[0].arguments, expected):
+ assert arg.spelling == exp
+
+def test_2_compilecommand():
+ """Check file with 2 compile commands"""
+ cdb = CompilationDatabase.fromDirectory(kInputsDir)
+ cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp')
+ assert len(cmds) == 2
+ expected = [
+ { 'wd': '/home/john.doe/MyProjectA',
+ 'line': ['clang++', '-o', 'project2.o', '-c',
+ '/home/john.doe/MyProject/project2.cpp']},
+ { 'wd': '/home/john.doe/MyProjectB',
+ 'line': ['clang++', '-DFEATURE=1', '-o', 'project2-feature.o', '-c',
+ '/home/john.doe/MyProject/project2.cpp']}
+ ]
+ for i in range(len(cmds)):
+ assert cmds[i].directory == expected[i]['wd']
+ for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
+ assert arg.spelling == exp
+
+def test_compilationDB_references():
+ """Ensure CompilationsCommands are independent of the database"""
+ cdb = CompilationDatabase.fromDirectory(kInputsDir)
+ cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
+ del cdb
+ gc.collect()
+ workingdir = cmds[0].directory
+
+def test_compilationCommands_references():
+ """Ensure CompilationsCommand keeps a reference to CompilationCommands"""
+ cdb = CompilationDatabase.fromDirectory(kInputsDir)
+ cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
+ del cdb
+ cmd0 = cmds[0]
+ del cmds
+ gc.collect()
+ workingdir = cmd0.directory
+
OpenPOWER on IntegriCloud