Bug 875605 - Add HG + update style rules for moz-check-style. r=ms2ger

This commit is contained in:
Benoit Girard 2013-05-25 19:33:36 -04:00
parent 718761d900
commit e359f339b0
3 changed files with 58 additions and 25 deletions

View File

@ -88,13 +88,14 @@ Syntax: %(program_name)s [--verbose=#] [--git-commit=<COMMITISH>] [--output=vs7]
""" % {'program_name': sys.argv[0]}
def process_patch(patch_string):
def process_patch(patch_string, cwd, scm):
"""Does lint on a single patch.
Args:
patch_string: A string of a patch.
"""
patch = DiffParser(patch_string.splitlines())
for filename, diff in patch.files.iteritems():
file_extension = os.path.splitext(filename)[1]
@ -117,11 +118,11 @@ def process_patch(patch_string):
if line_number in line_numbers:
cpplint.error(filename, line_number, category, confidence, message)
cpplint.process_file(filename, error=error_for_patch)
cpplint.process_file(os.path.join(scm.find_checkout_root(cwd), filename), error=error_for_patch)
def main():
cpplint.use_webkit_styles()
cpplint.use_mozilla_styles()
(args, flags) = cpplint.parse_arguments(sys.argv[1:], ["git-commit="])
if args:
@ -132,9 +133,9 @@ def main():
scm = detect_scm_system(cwd)
if "--git-commit" in flags:
process_patch(scm.create_patch_from_local_commit(flags["--git-commit"]))
process_patch(scm.create_patch_from_local_commit(flags["--git-commit"]), cwd, scm)
else:
process_patch(scm.create_patch())
process_patch(scm.create_patch(), cwd, scm)
sys.stderr.write('Total errors found: %d\n' % cpplint.error_count())
sys.exit(cpplint.error_count() > 0)

View File

@ -151,6 +151,7 @@ _ERROR_CATEGORIES = '''\
whitespace/braces
whitespace/comma
whitespace/comments
whitespace/comments-doublespace
whitespace/end_of_line
whitespace/ending_newline
whitespace/indent
@ -843,7 +844,7 @@ def check_for_copyright(filename, lines, error):
# We'll say it should occur by line 10. Don't forget there's a
# dummy line at the front.
for line in xrange(1, min(len(lines), 11)):
if re.search(r'Copyright', lines[line], re.I):
if re.search(r'Copyright|License', lines[line], re.I):
break
else: # means no copyright line was found
error(filename, 0, 'legal/copyright', 5,
@ -1509,7 +1510,7 @@ def check_spacing(filename, clean_lines, line_number, error):
and line[comment_position-1] not in string.whitespace)
or (comment_position >= 2
and line[comment_position-2] not in string.whitespace))):
error(filename, line_number, 'whitespace/comments', 2,
error(filename, line_number, 'whitespace/comments-doublespace', 2,
'At least two spaces is best between code and comments')
# There should always be a space between the // and the comment
commentend = comment_position + 2
@ -1809,6 +1810,14 @@ def check_braces(filename, clean_lines, line_number, error):
line = clean_lines.elided[line_number] # Get rid of comments and strings.
"""
These don't match our style guideline:
https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style#Control_Structures
TODO: Spin this off in a different rule and disable that rule for mozilla
rather then commenting this out
if match(r'\s*{\s*$', line):
# We allow an open brace to start a line in the case where someone
# is using braces for function definition or in a block to
@ -1838,6 +1847,7 @@ def check_braces(filename, clean_lines, line_number, error):
and search(r'\b(if|for|foreach|while|else)\b', previous_line)):
error(filename, line_number, 'whitespace/braces', 4,
'One line control clauses should not use braces.')
"""
# An else clause should be on the same line as the preceding closing brace.
if match(r'\s*else\s*', line):
@ -2121,11 +2131,11 @@ def check_style(filename, clean_lines, line_number, file_extension, error):
error(filename, line_number, 'whitespace/end_of_line', 4,
'Line ends in whitespace. Consider deleting these extra spaces.')
# There are certain situations we allow one space, notably for labels
elif ((initial_spaces >= 1 and initial_spaces <= 3)
elif ((initial_spaces == 1 or initial_spaces == 3)
and not match(r'\s*\w+\s*:\s*$', cleansed_line)):
error(filename, line_number, 'whitespace/indent', 3,
'Weird number of spaces at line-start. '
'Are you using a 4-space indent?')
'Are you using at least 2-space indent?')
# Labels should always be indented at least one space.
elif not initial_spaces and line[:2] != '//':
label_match = match(r'(?P<label>[^:]+):\s*$', line)
@ -2419,12 +2429,14 @@ def check_language(filename, clean_lines, line_number, file_extension, include_s
# In addition, we look for people taking the address of a cast. This
# is dangerous -- casts can assign to temporaries, so the pointer doesn't
# point where you think.
"""
if search(
r'(&\([^)]+\)[\w(])|(&(static|dynamic|reinterpret)_cast\b)', line):
error(filename, line_number, 'runtime/casting', 4,
('Are you taking an address of a cast? '
'This is dangerous: could be a temp var. '
'Take the address before doing the cast, rather than after'))
"""
# Check for people declaring static/global STL strings at the top level.
# This is dangerous because the C++ language does not guarantee that
@ -3062,23 +3074,16 @@ def parse_arguments(args, additional_flags=[]):
return (filenames, additional_flag_values)
def use_webkit_styles():
def use_mozilla_styles():
"""Disables some features which are not suitable for WebKit."""
# FIXME: For filters we will never want to have, remove them.
# For filters we want to have similar functionalities,
# modify the implementation and enable them.
global _DEFAULT_FILTERS
_DEFAULT_FILTERS = [
'-whitespace/comments',
'-whitespace/comments-doublespace',
'-whitespace/blank_line',
'-runtime/explicit', # explicit
'-runtime/virtual', # virtual dtor
'-runtime/printf',
'-runtime/threadsafe_fn',
'-runtime/rtti',
'-build/include_what_you_use', # <string> for std::string
'-legal/copyright',
'-readability/multiline_comment',
'-readability/braces', # int foo() {};
'-readability/fn_size',
'-build/storage_class', # const static
@ -3086,10 +3091,7 @@ def use_webkit_styles():
'-whitespace/labels',
'-runtime/arrays', # variable length array
'-build/header_guard',
'-readability/casting',
'-readability/function',
'-runtime/casting',
'-runtime/sizeof',
]

View File

@ -32,18 +32,22 @@
import os
import re
import subprocess
import sys
# Import WebKit-specific modules.
from modules.logging import error, log
def detect_scm_system(path):
if HG.in_working_directory(path):
return HG(cwd=path)
if SVN.in_working_directory(path):
return SVN(cwd=path)
if Git.in_working_directory(path):
return Git(cwd=path)
return None
raise ScriptError("working directory is not a HG/SVN/Git repo")
def first_non_empty_line_after_index(lines, index=0):
first_non_empty_line = index
@ -293,7 +297,7 @@ class Git(SCM):
@classmethod
def in_working_directory(cls, path):
return cls.run_command(['git', 'rev-parse', '--is-inside-work-tree'], cwd=path) == "true"
return cls.run_command(['git', 'rev-parse', '--is-inside-work-tree'], raise_on_failure=False, cwd=path) == "true"
@classmethod
def find_checkout_root(cls, path):
@ -388,3 +392,29 @@ class Git(SCM):
def files_changed_summary_for_commit(self, commit_id):
return self.run_command(['git', 'diff-tree', '--shortstat', '--no-commit-id', commit_id])
# All hg-specific logic should go here.
class HG(SCM):
def __init__(self, cwd, dryrun=False):
SCM.__init__(self, cwd, dryrun)
@classmethod
def in_working_directory(cls, path):
return cls.run_command(['hg', 'status'], cwd=path, return_exit_code=True) == 0
@classmethod
def find_checkout_root(cls, path):
checkout_root = cls.run_command(['hg', 'root'], cwd=path)
return checkout_root
def status_command(self):
return ['hg', 'status']
def display_name(self):
return "hg"
def create_patch(self):
if self.run_command(['hg', 'diff']) != "":
sys.stderr.write("Warning: outstanding changes not include in style check.\n")
return self.run_command(['hg', 'export', 'tip'])