snickolls-arm | 2decd2c | 2024-01-17 11:24:49 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 2 | |
Alexandre Rames | b78f139 | 2016-07-01 14:22:22 +0100 | [diff] [blame] | 3 | # Copyright 2016, VIXL authors |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 4 | # All rights reserved. |
| 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without |
| 7 | # modification, are permitted provided that the following conditions are met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright notice, |
| 10 | # this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | # this list of conditions and the following disclaimer in the documentation |
| 13 | # and/or other materials provided with the distribution. |
| 14 | # * Neither the name of ARM Limited nor the names of its contributors may be |
| 15 | # used to endorse or promote products derived from this software without |
| 16 | # specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| 19 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 22 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | import argparse |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 30 | import multiprocessing |
| 31 | import os |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 32 | import re |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 33 | import subprocess |
| 34 | import sys |
| 35 | import tempfile |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 36 | import shutil |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 37 | |
Anthony Barbier | 89eefef | 2019-07-05 11:15:13 +0100 | [diff] [blame] | 38 | from threaded_tests import Test, TestQueue |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 39 | import printer |
| 40 | import util |
| 41 | |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 42 | CLANG_TOOL_SUPPORTED_VERSIONS = range(11, 16) |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 43 | |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 44 | DEFAULT_CLANG_FORMAT = 'clang-format' |
| 45 | |
| 46 | CLANG_TOOL_VERSION_MATCH = r"(clang-format|LLVM) version ([\d]+)\.[\d]+\.[\d]+.*$" |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 47 | |
| 48 | is_output_redirected = not sys.stdout.isatty() |
| 49 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 50 | def BuildOptions(): |
| 51 | parser = argparse.ArgumentParser( |
| 52 | description = '''This tool runs `clang-format` on C++ files. |
Jacob Bramley | f89cb48 | 2019-06-28 15:53:18 +0100 | [diff] [blame] | 53 | If no files are provided on the command-line, all C++ source files are |
| 54 | processed, except for the test traces. |
| 55 | When available, `colordiff` is automatically used to colour the output.''', |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 56 | # Print default values. |
| 57 | formatter_class = argparse.ArgumentDefaultsHelpFormatter) |
| 58 | parser.add_argument('files', nargs = '*') |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 59 | parser.add_argument('--clang-format', default=DEFAULT_CLANG_FORMAT, |
| 60 | help='Path to clang-format.') |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 61 | parser.add_argument('--in-place', '-i', |
| 62 | action = 'store_true', default = False, |
| 63 | help = 'Edit files in place.') |
| 64 | parser.add_argument('--jobs', '-j', metavar = 'N', type = int, nargs = '?', |
| 65 | default = multiprocessing.cpu_count(), |
| 66 | const = multiprocessing.cpu_count(), |
| 67 | help = '''Runs the tests using N jobs. If the option is set |
| 68 | but no value is provided, the script will use as many jobs |
| 69 | as it thinks useful.''') |
| 70 | return parser.parse_args() |
| 71 | |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 72 | def is_supported(tool): |
| 73 | if not shutil.which(tool): |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 74 | return False |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 75 | |
| 76 | cmd = '%s -version' % tool |
| 77 | |
| 78 | try: |
| 79 | rc, version = util.getstatusoutput(cmd) |
| 80 | except OSError: |
| 81 | return False |
| 82 | |
Anthony Barbier | 89eefef | 2019-07-05 11:15:13 +0100 | [diff] [blame] | 83 | if rc != 0: |
| 84 | util.abort("Failed to execute %s: %s" % (cmd, version)) |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 85 | m = re.search(CLANG_TOOL_VERSION_MATCH, version, re.MULTILINE) |
Anthony Barbier | 89eefef | 2019-07-05 11:15:13 +0100 | [diff] [blame] | 86 | if not m: |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 87 | util.abort("Failed to get clang tool version: %s" % version) |
| 88 | _, major = m.groups() |
| 89 | |
| 90 | if int(major) in CLANG_TOOL_SUPPORTED_VERSIONS: |
| 91 | return True |
| 92 | |
| 93 | return False |
| 94 | |
| 95 | def detect_clang_tool(tool): |
| 96 | supported_tools = [tool] + [tool + '-' + str(ver) for ver in CLANG_TOOL_SUPPORTED_VERSIONS] |
| 97 | for tool in supported_tools: |
| 98 | if is_supported(tool): |
| 99 | return tool |
| 100 | |
| 101 | return None |
| 102 | |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 103 | |
Anthony Barbier | 89eefef | 2019-07-05 11:15:13 +0100 | [diff] [blame] | 104 | def RunTest(test): |
| 105 | filename = test.args['filename'] |
| 106 | clang_format = test.args['clang_format'] |
| 107 | in_place = test.args['in_place'] |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 108 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 109 | rc = 0 |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 110 | |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 111 | cmd_format = [clang_format, filename] |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 112 | temp_file, temp_file_name = tempfile.mkstemp(prefix = 'clang_format_') |
| 113 | cmd_format_string = '$ ' + ' '.join(cmd_format) + ' > %s' % temp_file_name |
| 114 | p_format = subprocess.Popen(cmd_format, |
| 115 | stdout = temp_file, stderr = subprocess.STDOUT) |
| 116 | |
| 117 | rc += p_format.wait() |
| 118 | |
| 119 | cmd_diff = ['diff', '--unified', filename, temp_file_name] |
| 120 | cmd_diff_string = '$ ' + ' '.join(cmd_diff) |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 121 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 122 | p_diff = subprocess.Popen(cmd_diff, |
| 123 | stdout = subprocess.PIPE, stderr = subprocess.STDOUT) |
| 124 | |
| 125 | if util.IsCommandAvailable('colordiff') and not is_output_redirected: |
| 126 | p_colordiff = subprocess.Popen( |
| 127 | ['colordiff', '--unified'], |
| 128 | stdin = p_diff.stdout, |
| 129 | stdout = subprocess.PIPE, stderr = subprocess.STDOUT) |
| 130 | out, unused = p_colordiff.communicate() |
mmc28a | ef2f4d1 | 2024-02-09 11:34:04 +0000 | [diff] [blame] | 131 | rc += p_colordiff.returncode |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 132 | else: |
| 133 | out, unused = p_diff.communicate() |
mmc28a | ef2f4d1 | 2024-02-09 11:34:04 +0000 | [diff] [blame] | 134 | rc += p_diff.returncode |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 135 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 136 | |
| 137 | if in_place: |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 138 | cmd_format = [clang_format, '-i', filename] |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 139 | subprocess.run(cmd_format, stdout=temp_file, stderr=subprocess.STDOUT) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 140 | |
| 141 | if rc != 0: |
Anthony Barbier | 89eefef | 2019-07-05 11:15:13 +0100 | [diff] [blame] | 142 | with Test.n_tests_failed.get_lock(): Test.n_tests_failed.value += 1 |
| 143 | else: |
| 144 | with Test.n_tests_passed.get_lock(): Test.n_tests_passed.value += 1 |
| 145 | |
| 146 | printer.__print_lock__.acquire() |
| 147 | |
| 148 | printer.UpdateProgress(test.shared.start_time, |
| 149 | Test.n_tests_passed.value, |
| 150 | Test.n_tests_failed.value, |
| 151 | test.shared.n_tests, |
| 152 | Test.n_tests_skipped.value, |
| 153 | test.shared.n_known_failures, |
| 154 | test.name, |
| 155 | prevent_next_overwrite = rc != 0, |
| 156 | has_lock = True, |
| 157 | prefix = test.shared.progress_prefix) |
| 158 | |
| 159 | if rc != 0: |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 160 | printer.Print('Incorrectly formatted file: ' + filename + '\n' + \ |
| 161 | cmd_format_string + '\n' + \ |
| 162 | cmd_diff_string + '\n' + \ |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 163 | out.decode(), has_lock = True) |
| 164 | |
Anthony Barbier | 89eefef | 2019-07-05 11:15:13 +0100 | [diff] [blame] | 165 | printer.__print_lock__.release() |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 166 | |
Jacob Bramley | be4b752 | 2016-10-19 14:27:52 +0100 | [diff] [blame] | 167 | os.remove(temp_file_name) |
| 168 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 169 | # Returns the total number of files incorrectly formatted. |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 170 | def ClangFormatFiles(files, clang_format, in_place = False, jobs = 1, |
| 171 | progress_prefix = ''): |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 172 | |
| 173 | clang_format = detect_clang_tool("clang-format") |
| 174 | |
| 175 | if not clang_format: |
| 176 | error_message = "clang-format not found. Please ensure it " \ |
| 177 | "is installed, in your PATH and the correct version." |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 178 | print(printer.COLOUR_RED + error_message + printer.NO_COLOUR) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 179 | return -1 |
| 180 | |
Anthony Barbier | 89eefef | 2019-07-05 11:15:13 +0100 | [diff] [blame] | 181 | queue = TestQueue(prefix = progress_prefix) |
| 182 | for f in files: |
| 183 | queue.AddTest(f, filename = f, clang_format = clang_format, in_place = in_place) |
| 184 | |
| 185 | rc = queue.Run(jobs, True, RunTest) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 186 | |
| 187 | printer.PrintOverwritableLine( |
| 188 | progress_prefix + '%d files are incorrectly formatted.' % rc, |
| 189 | type = printer.LINE_TYPE_LINTER) |
| 190 | printer.EnsureNewLine() |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 191 | |
Anthony Barbier | 89eefef | 2019-07-05 11:15:13 +0100 | [diff] [blame] | 192 | return rc |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 193 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 194 | if __name__ == '__main__': |
| 195 | # Parse the arguments. |
| 196 | args = BuildOptions() |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 197 | files = args.files or util.get_source_files(exclude_dirs=['.*', '*/traces/*', '*/aarch32/*']) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 198 | |
Pierre Langlois | 44096c4 | 2018-05-23 23:15:25 +0100 | [diff] [blame] | 199 | rc = ClangFormatFiles(files, clang_format = args.clang_format, |
| 200 | in_place = args.in_place, jobs = args.jobs) |
| 201 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 202 | sys.exit(rc) |