style: catch trailing white spaces in make and dts files
Change-Id: I2a4f1893919660e51599902b972a6f3f5717e305 Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
This commit is contained in:
parent
efb7fb6f85
commit
ddfc4c4593
2 changed files with 36 additions and 20 deletions
|
@ -60,6 +60,7 @@ lang_types = {
|
||||||
'.txt' : "text",
|
'.txt' : "text",
|
||||||
'.tex' : "tex",
|
'.tex' : "tex",
|
||||||
'.mk' : "make",
|
'.mk' : "make",
|
||||||
|
'.dts' : "dts",
|
||||||
}
|
}
|
||||||
|
|
||||||
# languages based on file prefix
|
# languages based on file prefix
|
||||||
|
|
|
@ -185,22 +185,29 @@ class LineVerifier(Verifier):
|
||||||
def check(self, filename, regions=all_regions):
|
def check(self, filename, regions=all_regions):
|
||||||
f = self.open(filename, 'r')
|
f = self.open(filename, 'r')
|
||||||
|
|
||||||
|
lang = lang_type(filename)
|
||||||
|
assert lang in self.languages
|
||||||
|
|
||||||
errors = 0
|
errors = 0
|
||||||
for num,line in enumerate(f):
|
for num,line in enumerate(f):
|
||||||
if num not in regions:
|
if num not in regions:
|
||||||
continue
|
continue
|
||||||
line = line.rstrip('\n')
|
line = line.rstrip('\n')
|
||||||
if not self.check_line(line):
|
if not self.check_line(line, language=lang):
|
||||||
self.ui.write("invalid %s in %s:%d\n" % \
|
self.ui.write("invalid %s in %s:%d\n" % \
|
||||||
(self.test_name, filename, num + 1))
|
(self.test_name, filename, num + 1))
|
||||||
if self.ui.verbose:
|
if self.ui.verbose:
|
||||||
self.ui.write(">>%s<<\n" % line[:-1])
|
self.ui.write(">>%s<<\n" % line[:-1])
|
||||||
errors += 1
|
errors += 1
|
||||||
|
f.close()
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
def fix(self, filename, regions=all_regions):
|
def fix(self, filename, regions=all_regions):
|
||||||
f = self.open(filename, 'r+')
|
f = self.open(filename, 'r+')
|
||||||
|
|
||||||
|
lang = lang_type(filename)
|
||||||
|
assert lang in self.languages
|
||||||
|
|
||||||
lines = list(f)
|
lines = list(f)
|
||||||
|
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
|
@ -209,19 +216,19 @@ class LineVerifier(Verifier):
|
||||||
for i,line in enumerate(lines):
|
for i,line in enumerate(lines):
|
||||||
line = line.rstrip('\n')
|
line = line.rstrip('\n')
|
||||||
if i in regions:
|
if i in regions:
|
||||||
line = self.fix_line(line)
|
line = self.fix_line(line, language=lang)
|
||||||
|
|
||||||
f.write(line)
|
f.write(line)
|
||||||
f.write("\n")
|
f.write("\n")
|
||||||
f.close()
|
f.close()
|
||||||
|
self.current_language = None
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def check_line(self, line):
|
def check_line(self, line, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def fix_line(self, line):
|
def fix_line(self, line, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Whitespace(LineVerifier):
|
class Whitespace(LineVerifier):
|
||||||
|
@ -232,14 +239,22 @@ class Whitespace(LineVerifier):
|
||||||
- No trailing whitespace
|
- No trailing whitespace
|
||||||
"""
|
"""
|
||||||
|
|
||||||
languages = set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons'))
|
languages = set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons',
|
||||||
|
'make', 'dts'))
|
||||||
|
trail_only = set(('make', 'dts'))
|
||||||
|
|
||||||
test_name = 'whitespace'
|
test_name = 'whitespace'
|
||||||
opt_name = 'white'
|
opt_name = 'white'
|
||||||
|
|
||||||
_lead = re.compile(r'^([ \t]+)')
|
_lead = re.compile(r'^([ \t]+)')
|
||||||
_trail = re.compile(r'([ \t]+)$')
|
_trail = re.compile(r'([ \t]+)$')
|
||||||
|
|
||||||
def check_line(self, line):
|
|
||||||
|
def skip_lead(self, language):
|
||||||
|
return language in Whitespace.trail_only
|
||||||
|
|
||||||
|
def check_line(self, line, language):
|
||||||
|
if not self.skip_lead(language):
|
||||||
match = Whitespace._lead.search(line)
|
match = Whitespace._lead.search(line)
|
||||||
if match and match.group(1).find('\t') != -1:
|
if match and match.group(1).find('\t') != -1:
|
||||||
return False
|
return False
|
||||||
|
@ -250,8 +265,8 @@ class Whitespace(LineVerifier):
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def fix_line(self, line):
|
def fix_line(self, line, language):
|
||||||
if Whitespace._lead.search(line):
|
if not self.skip_lead(language) and Whitespace._lead.search(line):
|
||||||
newline = ''
|
newline = ''
|
||||||
for i,c in enumerate(line):
|
for i,c in enumerate(line):
|
||||||
if c == ' ':
|
if c == ' ':
|
||||||
|
@ -329,11 +344,11 @@ class ControlSpace(LineVerifier):
|
||||||
|
|
||||||
_any_control = re.compile(r'\b(if|while|for)([ \t]*)\(')
|
_any_control = re.compile(r'\b(if|while|for)([ \t]*)\(')
|
||||||
|
|
||||||
def check_line(self, line):
|
def check_line(self, line, **kwargs):
|
||||||
match = ControlSpace._any_control.search(line)
|
match = ControlSpace._any_control.search(line)
|
||||||
return not (match and match.group(2) != " ")
|
return not (match and match.group(2) != " ")
|
||||||
|
|
||||||
def fix_line(self, line):
|
def fix_line(self, line, **kwargs):
|
||||||
new_line = _any_control.sub(r'\1 (', line)
|
new_line = _any_control.sub(r'\1 (', line)
|
||||||
return new_line
|
return new_line
|
||||||
|
|
||||||
|
@ -343,10 +358,10 @@ class LineLength(LineVerifier):
|
||||||
test_name = 'line length'
|
test_name = 'line length'
|
||||||
opt_name = 'length'
|
opt_name = 'length'
|
||||||
|
|
||||||
def check_line(self, line):
|
def check_line(self, line, **kwargs):
|
||||||
return style.normalized_len(line) <= 79
|
return style.normalized_len(line) <= 79
|
||||||
|
|
||||||
def fix(self, filename, regions=all_regions):
|
def fix(self, filename, regions=all_regions, **kwargs):
|
||||||
self.ui.write("Warning: cannot automatically fix overly long lines.\n")
|
self.ui.write("Warning: cannot automatically fix overly long lines.\n")
|
||||||
|
|
||||||
def fix_line(self, line):
|
def fix_line(self, line):
|
||||||
|
@ -360,10 +375,10 @@ class ControlCharacters(LineVerifier):
|
||||||
valid = ('\n', '\t')
|
valid = ('\n', '\t')
|
||||||
invalid = "".join([chr(i) for i in range(0, 0x20) if chr(i) not in valid])
|
invalid = "".join([chr(i) for i in range(0, 0x20) if chr(i) not in valid])
|
||||||
|
|
||||||
def check_line(self, line):
|
def check_line(self, line, **kwargs):
|
||||||
return self.fix_line(line) == line
|
return self.fix_line(line) == line
|
||||||
|
|
||||||
def fix_line(self, line):
|
def fix_line(self, line, **kwargs):
|
||||||
return line.translate(None, ControlCharacters.invalid)
|
return line.translate(None, ControlCharacters.invalid)
|
||||||
|
|
||||||
class BoolCompare(LineVerifier):
|
class BoolCompare(LineVerifier):
|
||||||
|
@ -373,10 +388,10 @@ class BoolCompare(LineVerifier):
|
||||||
|
|
||||||
regex = re.compile(r'\s*==\s*([Tt]rue|[Ff]alse)\b')
|
regex = re.compile(r'\s*==\s*([Tt]rue|[Ff]alse)\b')
|
||||||
|
|
||||||
def check_line(self, line):
|
def check_line(self, line, **kwargs):
|
||||||
return self.regex.search(line) == None
|
return self.regex.search(line) == None
|
||||||
|
|
||||||
def fix_line(self, line):
|
def fix_line(self, line, **kwargs):
|
||||||
match = self.regex.search(line)
|
match = self.regex.search(line)
|
||||||
if match:
|
if match:
|
||||||
if match.group(1) in ('true', 'True'):
|
if match.group(1) in ('true', 'True'):
|
||||||
|
|
Loading…
Reference in a new issue