This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Transformation of contrib/check_GNU_style.sh to a python script


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)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]