Index: clang-tidy/tool/clang-tidy-diff.py =================================================================== --- clang-tidy/tool/clang-tidy-diff.py +++ clang-tidy/tool/clang-tidy-diff.py @@ -26,11 +26,23 @@ import argparse import json +import os import re import subprocess import sys +def find_compilation_database(path): + """Adjusts the directory until a compilation database is found.""" + result = './' + while not os.path.isfile(os.path.join(result, path)): + if os.path.realpath(result) == '/': + print 'Error: could not find compilation database.' + sys.exit(1) + result += '../' + return os.path.realpath(result) + + def main(): parser = argparse.ArgumentParser(description= 'Run clang-tidy against changed files, and ' @@ -55,6 +67,8 @@ help='checks filter, when not specified, use clang-tidy ' 'default', default='') + parser.add_argument('-path', dest='build_path', + help='Path used to read a compile command database.') parser.add_argument('-extra-arg', dest='extra_arg', action='append', default=[], help='Additional argument to append to the compiler ' @@ -73,6 +87,14 @@ args = parser.parse_args(argv) + db_path = 'compile_commands.json' + + if args.build_path is not None: + build_path = args.build_path + else: + # Find our database + build_path = find_compilation_database(db_path) + # Extract changed lines for each file. filename = None lines_by_file = {} @@ -124,6 +146,7 @@ command.append('-checks=' + quote + args.checks + quote) if args.quiet: command.append('-quiet') + command.append('-p=%s' % build_path) command.extend(lines_by_file.keys()) for arg in args.extra_arg: command.append('-extra-arg=%s' % arg) Index: test/clang-tidy/clang-tidy-diff.cpp =================================================================== --- test/clang-tidy/clang-tidy-diff.cpp +++ test/clang-tidy/clang-tidy-diff.cpp @@ -2,6 +2,9 @@ // RUN: clang-tidy -checks=-*,modernize-use-override %t.cpp -- -std=c++11 | FileCheck -check-prefix=CHECK-SANITY %s // RUN: not diff -U0 %s %t.cpp | %clang_tidy_diff -checks=-*,modernize-use-override -- -std=c++11 2>&1 | FileCheck %s // RUN: not diff -U0 %s %t.cpp | %clang_tidy_diff -checks=-*,modernize-use-override -quiet -- -std=c++11 2>&1 | FileCheck -check-prefix=CHECK-QUIET %s +// RUN: mkdir -p %T/compilation-database-test/ +// RUN: echo '[{"directory": "%T", "command": "clang++ -o test.o -std=c++11 %t.cpp", "file": "%t.cpp"}]' > %T/compilation-database-test/compile_commands.json +// RUN: not diff -U0 %s %t.cpp | %clang_tidy_diff -checks=-*,modernize-use-override -path %T/compilation-database-test 2>&1 | FileCheck -check-prefix=CHECK %s struct A { virtual void f() {} virtual void g() {}