run_clang_tidy.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. # Copyright 2017 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import multiprocessing
  17. import os
  18. import subprocess
  19. import sys
  20. sys.path.append(
  21. os.path.join(os.path.dirname(sys.argv[0]), '..', 'run_tests',
  22. 'python_utils'))
  23. import jobset
  24. clang_tidy = os.environ.get('CLANG_TIDY', 'clang-tidy')
  25. argp = argparse.ArgumentParser(description='Run clang-tidy against core')
  26. argp.add_argument('files', nargs='+', help='Files to tidy')
  27. argp.add_argument('--fix', dest='fix', action='store_true')
  28. argp.add_argument('-j',
  29. '--jobs',
  30. type=int,
  31. default=multiprocessing.cpu_count(),
  32. help='Number of CPUs to use')
  33. argp.add_argument('--only-changed', dest='only_changed', action='store_true')
  34. argp.set_defaults(fix=False, only_changed=False)
  35. args = argp.parse_args()
  36. # Explicitly passing the .clang-tidy config by reading it.
  37. # This is required because source files in the compilation database are
  38. # in a different source tree so clang-tidy cannot find the right config file
  39. # by seeking their parent directories.
  40. with open(".clang-tidy") as f:
  41. config = f.read()
  42. cmdline = [
  43. clang_tidy,
  44. '--config=' + config,
  45. ]
  46. if args.fix:
  47. cmdline.append('--fix-errors')
  48. if args.only_changed:
  49. orig_files = set(args.files)
  50. actual_files = []
  51. output = subprocess.check_output(
  52. ['git', 'diff', 'origin/master', 'HEAD', '--name-only'])
  53. for line in output.decode('ascii').splitlines(False):
  54. if line in orig_files:
  55. print(("check: %s" % line))
  56. actual_files.append(line)
  57. else:
  58. print(("skip: %s - not in the build" % line))
  59. args.files = actual_files
  60. jobs = []
  61. for filename in args.files:
  62. jobs.append(
  63. jobset.JobSpec(
  64. cmdline + [filename],
  65. shortname=filename,
  66. timeout_seconds=15 * 60,
  67. ))
  68. num_fails, res_set = jobset.run(jobs, maxjobs=args.jobs, quiet_success=True)
  69. sys.exit(num_fails)