sys/clang-format-diff: other small fixes to the style script

This commit is contained in:
Riccardo Schirone 2018-11-15 12:18:12 +01:00
parent 79edcb32f5
commit bb3d054c6d

View File

@ -62,6 +62,8 @@ def main():
help='let clang-format sort include blocks')
parser.add_argument('-v', '--verbose', action='store_true',
help='be more verbose, ineffective without -i')
parser.add_argument('--debug', action='store_true',
help='debug mode')
parser.add_argument('-style',
help='formatting style to apply (LLVM, Google, Chromium, '
'Mozilla, WebKit)')
@ -69,6 +71,10 @@ def main():
help='location of binary to use for clang-format')
args = parser.parse_args()
def debug(s):
if args.debug:
sys.stderr.write(str(s) + '\n')
# Extract changed lines for each file.
filename = None
lines_by_file = {}
@ -99,7 +105,14 @@ def main():
ranges = []
range_start, range_end = None, None
range_line = -1
for i in range(1, line_count):
debug(line_count)
i = 0
while True:
# stop iterating when finding the next diff
if lineidx + i >= len(input) or input[lineidx + i].startswith('diff'):
break
debug('lineidx : ' + input[lineidx + i])
# do not count lines that are removed
if not input[lineidx + i].startswith('-'):
range_line += 1
@ -107,13 +120,18 @@ def main():
if input[lineidx + i].startswith('+'):
if range_start is None:
range_start = start_line + range_line
debug('set range_start: ' + str(start_line + range_line))
elif range_start is not None and range_end is None:
range_end = start_line + range_line
debug('set range_end: ' + str(start_line + range_line))
lines_by_file.setdefault(filename, []).append([range_start, range_end - 1])
range_start, range_end = None, None
i += 1
# Reformat files containing changes in place.
for filename, lines in lines_by_file.items():
debug('%s: %s' % (filename,lines))
command = [args.binary, filename]
if args.sort_includes:
command.append('-sort-includes')
@ -141,15 +159,13 @@ def main():
for i in range(x[0], x[1] + 1):
modified_lines[i] = True
delta = 10
# handle functions definitions/declarations: do not use space before (
old_nlines = len(open(filename).read().split('\n'))
new_nlines = len(formatted_code)
delta = abs(new_nlines - old_nlines)
for i, l in enumerate(formatted_code):
if modified_lines and not any(map(lambda x: x in modified_lines, range(i + 1 - delta, i + 1 + delta))):
continue
debug('formatted_code: ' + formatted_code[i])
if formatted_code[i].startswith('R_API ') or formatted_code[i].startswith('static ') or formatted_code[i].startswith('R_IPI '):
formatted_code[i] = formatted_code[i].replace(' (', '(')