Move the enum attributes defined in Attributes.h to a table-gen file.
[oota-llvm.git] / utils / lint / common_lint.py
1 #!/usr/bin/python
2 #
3 # Common lint functions applicable to multiple types of files.
4
5 import re
6
7 def VerifyLineLength(filename, lines, max_length):
8   """Checks to make sure the file has no lines with lines exceeding the length
9   limit.
10
11   Args:
12     filename: the file under consideration as string
13     lines: contents of the file as string array
14     max_length: maximum acceptable line length as number
15
16   Returns:
17     A list of tuples with format [(filename, line number, msg), ...] with any
18     violations found.
19   """
20   lint = []
21   line_num = 1
22   for line in lines:
23     length = len(line.rstrip('\n'))
24     if length > max_length:
25       lint.append((filename, line_num,
26                    'Line exceeds %d chars (%d)' % (max_length, length)))
27     line_num += 1
28   return lint
29
30 def VerifyTabs(filename, lines):
31   """Checks to make sure the file has no tab characters.
32
33   Args:
34     filename: the file under consideration as string
35     lines: contents of the file as string array
36
37   Returns:
38     A list of tuples with format [(line_number, msg), ...] with any violations
39     found.
40   """
41   lint = []
42   tab_re = re.compile(r'\t')
43   line_num = 1
44   for line in lines:
45     if tab_re.match(line.rstrip('\n')):
46       lint.append((filename, line_num, 'Tab found instead of whitespace'))
47     line_num += 1
48   return lint
49
50
51 def VerifyTrailingWhitespace(filename, lines):
52   """Checks to make sure the file has no lines with trailing whitespace.
53
54   Args:
55     filename: the file under consideration as string
56     lines: contents of the file as string array
57
58   Returns:
59     A list of tuples with format [(filename, line number, msg), ...] with any
60     violations found.
61   """
62   lint = []
63   trailing_whitespace_re = re.compile(r'\s+$')
64   line_num = 1
65   for line in lines:
66     if trailing_whitespace_re.match(line.rstrip('\n')):
67       lint.append((filename, line_num, 'Trailing whitespace'))
68     line_num += 1
69   return lint
70
71
72 class BaseLint:
73   def RunOnFile(filename, lines):
74     raise Exception('RunOnFile() unimplemented')
75
76
77 def RunLintOverAllFiles(linter, filenames):
78   """Runs linter over the contents of all files.
79
80   Args:
81     lint: subclass of BaseLint, implementing RunOnFile()
82     filenames: list of all files whose contents will be linted
83
84   Returns:
85     A list of tuples with format [(filename, line number, msg), ...] with any
86     violations found.
87   """
88   lint = []
89   for filename in filenames:
90     file = open(filename, 'r')
91     if not file:
92       print 'Cound not open %s' % filename
93       continue
94     lines = file.readlines()
95     lint.extend(linter.RunOnFile(filename, lines))
96
97   return lint