brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 07b8fc5 Raw
106 lines · python
1#!/usr/bin/env python2#3# Common lint functions applicable to multiple types of files.4 5from __future__ import print_function6import re7 8 9def VerifyLineLength(filename, lines, max_length):10    """Checks to make sure the file has no lines with lines exceeding the length11    limit.12 13    Args:14      filename: the file under consideration as string15      lines: contents of the file as string array16      max_length: maximum acceptable line length as number17 18    Returns:19      A list of tuples with format [(filename, line number, msg), ...] with any20      violations found.21    """22    lint = []23    line_num = 124    for line in lines:25        length = len(line.rstrip("\n"))26        if length > max_length:27            lint.append(28                (29                    filename,30                    line_num,31                    "Line exceeds %d chars (%d)" % (max_length, length),32                )33            )34        line_num += 135    return lint36 37 38def VerifyTabs(filename, lines):39    """Checks to make sure the file has no tab characters.40 41    Args:42      filename: the file under consideration as string43      lines: contents of the file as string array44 45    Returns:46      A list of tuples with format [(line_number, msg), ...] with any violations47      found.48    """49    lint = []50    tab_re = re.compile(r"\t")51    line_num = 152    for line in lines:53        if tab_re.match(line.rstrip("\n")):54            lint.append((filename, line_num, "Tab found instead of whitespace"))55        line_num += 156    return lint57 58 59def VerifyTrailingWhitespace(filename, lines):60    """Checks to make sure the file has no lines with trailing whitespace.61 62    Args:63      filename: the file under consideration as string64      lines: contents of the file as string array65 66    Returns:67      A list of tuples with format [(filename, line number, msg), ...] with any68      violations found.69    """70    lint = []71    trailing_whitespace_re = re.compile(r"\s+$")72    line_num = 173    for line in lines:74        if trailing_whitespace_re.match(line.rstrip("\n")):75            lint.append((filename, line_num, "Trailing whitespace"))76        line_num += 177    return lint78 79 80class BaseLint:81    def RunOnFile(filename, lines):82        raise Exception("RunOnFile() unimplemented")83 84 85def RunLintOverAllFiles(linter, filenames):86    """Runs linter over the contents of all files.87 88    Args:89      lint: subclass of BaseLint, implementing RunOnFile()90      filenames: list of all files whose contents will be linted91 92    Returns:93      A list of tuples with format [(filename, line number, msg), ...] with any94      violations found.95    """96    lint = []97    for filename in filenames:98        file = open(filename, "r")99        if not file:100            print("Could not open %s" % filename)101            continue102        lines = file.readlines()103        lint.extend(linter.RunOnFile(filename, lines))104 105    return lint106