Index: clang-tools-extra/clang-tidy/tool/tidy-llvm.py =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/tool/tidy-llvm.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python +# +#===- tidy-llvm --------------------------------------------*- python -*--===# +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +#===-----------------------------------------------------------------------===# + + +""" +tidy-llvm +========================== + +Runs clang-tidy over named .cpp and .h files. +tidy-llvm must be run from inside an LLVM source tree. +But otherwise, it finds the root of the llvm tree, verifies build requirements, +constructs include paths, concatenates an argument list and then runs clang-tidy. +clang-tidy will in turn examine any .clang-tidy in the path +""" + +import os +import sys + +def fail(s): + print(s) + sys.exit(1) + +def file_exists(name): + if not os.path.isfile(name): + fail('Cannot find ' + name) + return name + +def dir_exists(name): + if not os.path.isdir(name): + fail('Cannot find ' + name) + return name + +def find_llvm_root(): + result = './' + while not os.path.isdir(os.path.join(result, "build")): + if os.path.realpath(result) == '/': + fail('Error: could not find llvm root.') + result += '../' + return os.path.realpath(result) + +# LLVM_ENABLE_PROJECTS:STRING=clang;clang-tools-extra +# CMAKE_EXPORT_COMPILE_COMMANDS:UNINITIALIZED=On + +def verify_config(root): + found = 0 + for line in open(file_exists(os.path.join(root, "build/CMakeCache.txt")), 'r'): + if "LLVM_ENABLE_PROJECTS:STRING=" in line: + if "clang;clang-tools-extra" not in line: + fail('Build needs clang;clang-tools-extra') + found = 1 + break + if not found: + fail('Build needs clang;clang-tools-extra') + for line in open(file_exists(os.path.join(root, "build/CMakeCache.txt")), 'r'): + if "CMAKE_EXPORT_COMPILE_COMMANDS:UNINITIALIZED=On" in line: + return + fail('Build needs CMAKE_EXPORT_COMPILE_COMMANDS:UNINITIALIZED=On') + +def construct_llvm_include_paths(root): + libcxx = dir_exists(os.path.join(root, "libcxx/include")) + build_include = dir_exists(os.path.join(root, "build/include")) + llvm_include = dir_exists(os.path.join(root, "llvm/include")) + return "-I" + libcxx + " -I" + build_include + " -I" + llvm_include + +def construct_current_include_paths(): + paths = "" + for e in os.listdir(): + if os.path.isdir(e): + paths += " -I" + e + return paths + +def process_args(root, cmd_args): + for name in cmd_args: + if not name.endswith(".h") and not name.endswith(".cpp"): + fail(name + " does not end with .h or .cpp") + file_exists(name) + return " ".join(str(f) for f in cmd_args) + +def main(): + root = find_llvm_root() + verify_config(root) + llvm_include_paths = construct_llvm_include_paths(root) + current_include_paths = construct_current_include_paths() + if len(sys.argv) == 1: + fail("tidy-llvm: [.cpp and .h files]") + args = process_args(root, sys.argv[1:]) + cmd = ("clang-tidy -checks=\"llvm-*\" -p " + + os.path.join(root, "build") + " " + args + " -- -I." + + current_include_paths + " " + llvm_include_paths) + os.system(cmd) + +if __name__ == '__main__': + main()