This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Transformation of contrib/check_GNU_style.sh to a python script
- From: Martin Liška <mliska at suse dot cz>
- To: GCC Development <gcc at gcc dot gnu dot org>
- Cc: Tom_deVries at mentor dot com, Martin Sebor <msebor at gmail dot com>
- Date: Mon, 15 May 2017 16:00:31 +0200
- Subject: Re: Transformation of contrib/check_GNU_style.sh to a python script
- Authentication-results: sourceware.org; auth=none
- References: <e871583b-30a6-9c01-52fa-504d8ead0fdc@suse.cz>
Sorry, wrong file.
Martin
#!/usr/bin/env python3
import sys
import re
from unidiff import PatchSet
def report_error(filename, line_no, error):
print('%s:%d:%s' % (filename, line_no, error))
def validate(filename, line_no, line):
# 1: validate line length
line_expanded = line.replace('\t', ' ' * 8)
if len(line_expanded) > 80:
report_error(filename, line_no, 'line should not exceed 80 characters')
# 2) 8 spaces check
if ' ' * 8 in line:
report_error(filename, line_no, '8 spaces should be replace with a tab')
# 3) trailing white space
if line.endswith(' '):
report_error(filename, line_no, 'trailing whitespace')
# 4) Dot, space, space, new sentence.
if re.search('\w+\.(\s|\s{3,})\w', line):
report_error(filename, line_no, 'dot, space, space, new sentence')
# 5) Dot, space, space, end of comment.
if re.search('\w+\.(\s{0,1}|\s{3,})\*/', line):
report_error(filename, line_no, 'dot, space, space, end of comment')
with open(sys.argv[1], 'rb') as diff_file:
patch = PatchSet(diff_file, encoding = 'utf-8')
for pfile in patch.modified_files:
for hunk in pfile:
delta = 0
for line in hunk:
if line.is_added and line.target_line_no != None:
validate(pfile.target_file.lstrip('b/'), line.target_line_no, line.value)