From 95237ae4e09d8b475fc7da3a02849b15754e5ada Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Sat, 12 May 2018 22:46:47 -0400 Subject: [PATCH] Only run tidy on modified code, relocate scripts. (#490) --- .travis.yml | 9 +- README.md | 0 apply-clang-format.sh | 5 - changelog2html.py => bin/changelog2html.py | 0 bin/check-style.sh | 12 ++ check_docs.py => bin/check_docs.py | 0 bin/clang-tidy-diff.py | 146 +++++++++++++++++++++ text2c.sh => bin/text2c.sh | 0 cmake/clang-tidy.cmake | 2 + data/CMakeLists.txt | 4 +- 10 files changed, 167 insertions(+), 11 deletions(-) mode change 100755 => 100644 README.md delete mode 100755 apply-clang-format.sh rename changelog2html.py => bin/changelog2html.py (100%) create mode 100755 bin/check-style.sh rename check_docs.py => bin/check_docs.py (100%) create mode 100755 bin/clang-tidy-diff.py rename text2c.sh => bin/text2c.sh (100%) diff --git a/.travis.yml b/.travis.yml index c10efd13..69b419ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -62,10 +62,11 @@ matrix: before_install: - eval "${MATRIX_EVAL}" before_script: - - mkdir build - - cd build - - cmake -DCHECK_CODE_QUALITY=ON .. + - mkdir build && cd build + - cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. - cd .. + - cp build/compile_commands.json . + - ./bin/check-style.sh - mkdir build-no-x11 - cd build-no-x11 - cmake -DBUILD_X11=OFF .. @@ -76,7 +77,7 @@ script: - cd ../build-no-x11 - make -j4 - cd .. - - sonar-scanner + - if [ "$CC" = "gcc-8" ] ; then sonar-scanner ; fi branches: only: - master diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/apply-clang-format.sh b/apply-clang-format.sh deleted file mode 100755 index 566a20f5..00000000 --- a/apply-clang-format.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -find . -iname "*.h" -o -iname "*.cc" -o -iname "*.hh" \ - | xargs clang-format \ - -style=file -i -fallback-style=google diff --git a/changelog2html.py b/bin/changelog2html.py similarity index 100% rename from changelog2html.py rename to bin/changelog2html.py diff --git a/bin/check-style.sh b/bin/check-style.sh new file mode 100755 index 00000000..484521d5 --- /dev/null +++ b/bin/check-style.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -euxo pipefail + +DIR="$( dirname "${BASH_SOURCE[0]}" )" +# Run clang-tidy only for the lines of code which have changed. +git diff -U0 $TRAVIS_COMMIT_RANGE | \ + $DIR/clang-tidy-diff.py -p1 \ + -checks=*,-clang-analyzer-alpha.* \ + -quiet \ + -- \ + -warnings-as-errors=*,-clang-analyzer-alpha.* \ + -format-style='{BasedOnStyle: google, IndentWidth: 2}' diff --git a/check_docs.py b/bin/check_docs.py similarity index 100% rename from check_docs.py rename to bin/check_docs.py diff --git a/bin/clang-tidy-diff.py b/bin/clang-tidy-diff.py new file mode 100755 index 00000000..45c7b015 --- /dev/null +++ b/bin/clang-tidy-diff.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python +# +#===- clang-tidy-diff.py - ClangTidy Diff Checker ------------*- python -*--===# +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +#===------------------------------------------------------------------------===# + +r""" +ClangTidy Diff Checker +====================== + +This script reads input from a unified diff, runs clang-tidy on all changed +files and outputs clang-tidy warnings in changed lines only. This is useful to +detect clang-tidy regressions in the lines touched by a specific patch. +Example usage for git/svn users: + + git diff -U0 HEAD^ | clang-tidy-diff.py -p1 + svn diff --diff-cmd=diff -x-U0 | \ + clang-tidy-diff.py -fix -checks=-*,modernize-use-override + +""" + +import argparse +import json +import re +import subprocess +import sys + + +def main(): + parser = argparse.ArgumentParser(description= + 'Run clang-tidy against changed files, and ' + 'output diagnostics only for modified ' + 'lines.') + parser.add_argument('-clang-tidy-binary', metavar='PATH', + default='clang-tidy', + help='path to clang-tidy binary') + parser.add_argument('-p', metavar='NUM', default=0, + help='strip the smallest prefix containing P slashes') + parser.add_argument('-regex', metavar='PATTERN', default=None, + help='custom pattern selecting file paths to check ' + '(case sensitive, overrides -iregex)') + parser.add_argument('-iregex', metavar='PATTERN', default= + r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc)', + help='custom pattern selecting file paths to check ' + '(case insensitive, overridden by -regex)') + + parser.add_argument('-fix', action='store_true', default=False, + help='apply suggested fixes') + parser.add_argument('-checks', + help='checks filter, when not specified, use clang-tidy ' + 'default', + default='') + parser.add_argument('-format-style', + help='style format', + 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 ' + 'command line.') + parser.add_argument('-extra-arg-before', dest='extra_arg_before', + action='append', default=[], + help='Additional argument to prepend to the compiler ' + 'command line.') + parser.add_argument('-quiet', action='store_true', default=False, + help='Run clang-tidy in quiet mode') + clang_tidy_args = [] + argv = sys.argv[1:] + if '--' in argv: + clang_tidy_args.extend(argv[argv.index('--'):]) + argv = argv[:argv.index('--')] + + args = parser.parse_args(argv) + + # Extract changed lines for each file. + filename = None + lines_by_file = {} + for line in sys.stdin: + match = re.search('^\+\+\+\ \"?(.*?/){%s}([^ \t\n\"]*)' % args.p, line) + if match: + filename = match.group(2) + if filename == None: + continue + + if args.regex is not None: + if not re.match('^%s$' % args.regex, filename): + continue + else: + if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE): + continue + + match = re.search('^@@.*\+(\d+)(,(\d+))?', line) + if match: + start_line = int(match.group(1)) + line_count = 1 + if match.group(3): + line_count = int(match.group(3)) + if line_count == 0: + continue + end_line = start_line + line_count - 1; + lines_by_file.setdefault(filename, []).append([start_line, end_line]) + + if len(lines_by_file) == 0: + print("No relevant changes found.") + sys.exit(0) + + line_filter_json = json.dumps( + [{"name" : name, "lines" : lines_by_file[name]} for name in lines_by_file], + separators = (',', ':')) + + quote = ""; + if sys.platform == 'win32': + line_filter_json=re.sub(r'"', r'"""', line_filter_json) + else: + quote = "'"; + + # Run clang-tidy on files containing changes. + command = [args.clang_tidy_binary] + command.append('-line-filter=' + quote + line_filter_json + quote) + if args.fix: + command.append('-fix') + if args.checks != '': + command.append('-checks=' + quote + args.checks + quote) + if args.format_style != '': + command.append('-format-style=' + quote + args.format_style + quote) + if args.quiet: + command.append('-quiet') + if args.build_path is not None: + command.append('-p=%s' % args.build_path) + command.extend(lines_by_file.keys()) + for arg in args.extra_arg: + command.append('-extra-arg=%s' % arg) + for arg in args.extra_arg_before: + command.append('-extra-arg-before=%s' % arg) + command.extend(clang_tidy_args) + + sys.exit(subprocess.call(' '.join(command), shell=True)) + +if __name__ == '__main__': + main() diff --git a/text2c.sh b/bin/text2c.sh similarity index 100% rename from text2c.sh rename to bin/text2c.sh diff --git a/cmake/clang-tidy.cmake b/cmake/clang-tidy.cmake index 6af7f387..1f0a202a 100644 --- a/cmake/clang-tidy.cmake +++ b/cmake/clang-tidy.cmake @@ -4,6 +4,8 @@ set(DO_CLANG_TIDY "${CLANG_TIDY_BIN}" -format-style='{BasedOnStyle: google, IndentWidth: 2}' -checks=*,-clang-analyzer-alpha.* + -fix + -fix-errors ) if(CLANG_TIDY_BIN) diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt index b611ab3b..87659d9f 100644 --- a/data/CMakeLists.txt +++ b/data/CMakeLists.txt @@ -27,7 +27,7 @@ if(BUILD_BUILTIN_CONFIG) set(builtin_config conky_no_x11.conf) endif(BUILD_X11) add_custom_command(OUTPUT defconfig.h COMMAND - ${CMAKE_SOURCE_DIR}/text2c.sh ${CMAKE_CURRENT_SOURCE_DIR}/${builtin_config} + ${CMAKE_SOURCE_DIR}/bin/text2c.sh ${CMAKE_CURRENT_SOURCE_DIR}/${builtin_config} defconfig.h defconfig DEPENDS ${builtin_config} ) add_custom_target(defconfig ALL DEPENDS defconfig.h) @@ -36,7 +36,7 @@ endif(BUILD_BUILTIN_CONFIG) if(BUILD_OLD_CONFIG) add_custom_command(OUTPUT convertconf.h COMMAND - ${CMAKE_SOURCE_DIR}/text2c.sh ${CMAKE_SOURCE_DIR}/extras/convert.lua + ${CMAKE_SOURCE_DIR}/bin/text2c.sh ${CMAKE_SOURCE_DIR}/extras/convert.lua convertconf.h convertconf DEPENDS ${CMAKE_SOURCE_DIR}/extras/convert.lua ) add_custom_target(convertconf ALL DEPENDS convertconf.h)