def __init__(self): self.project_name = None self.cpp_header_map = {} self.cpp_src_map = {} self.python_map = {} pylint_disable = [ 'superfluous-parens', 'too-many-instance-attributes', 'too-few-public-methods' ] # setup pylint self.pylint_opts = [ '--extension-pkg-whitelist=numpy', '--disable=' + ','.join(pylint_disable) ] self.pylint_cats = set(['error', 'warning', 'convention', 'refactor']) # setup cpp lint cpplint_args = ['.', '--extensions=' + (','.join(CXX_SUFFIX))] _ = cpplint.ParseArguments(cpplint_args) cpplint._SetFilters(','.join([ '-build/c++11', '-build/namespaces', '-build/include,', '+build/include_what_you_use', '+build/include_order' ])) cpplint._SetCountingStyle('toplevel') cpplint._line_length = 100
def Lint(change_info, args): """Runs cpplint.py on all the files in |change_info|""" try: import cpplint except ImportError: ErrorExit("You need to install cpplint.py to lint C++ files.") # Change the current working directory before calling lint so that it # shows the correct base. previous_cwd = os.getcwd() os.chdir(change_info.GetLocalRoot()) # Process cpplints arguments if any. filenames = cpplint.ParseArguments(args + change_info.GetFileNames()) white_list = GetCodeReviewSetting("LINT_REGEX") if not white_list: white_list = DEFAULT_LINT_REGEX white_regex = re.compile(white_list) black_list = GetCodeReviewSetting("LINT_IGNORE_REGEX") if not black_list: black_list = DEFAULT_LINT_IGNORE_REGEX black_regex = re.compile(black_list) for filename in filenames: if white_regex.match(filename): if black_regex.match(filename): print "Ignoring file %s" % filename else: cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level) else: print "Skipping file %s" % filename print "Total errors found: %d\n" % cpplint._cpplint_state.error_count os.chdir(previous_cwd)
def CheckRecursivelyCppFiles(root_path, file_exts, arguments): ''' Recursively browse through all subfiles in "root_path" and applies cpplint to each one of them suffixed with one of the "file_exts" extension cpplint gets called with "arguments" ''' absolute_root = os.path.abspath(root_path) concatenated_filenames = "" for sub_root, filenames in [ (sub_root, filenames) for sub_root, _, filenames in os.walk(absolute_root) ]: for filename in [ filename for filename in filenames if (filename not in kIgnoredFilenames) ]: if os.path.splitext(filename)[1] in file_exts: concatenated_filenames += os.path.join(sub_root, filename) + ' ' arguments.append(concatenated_filenames) filenames = cpplint.ParseArguments(arguments) cpplint._cpplint_state.ResetErrorCounts() for filename in filenames[0].split(' '): cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level) cpplint._cpplint_state.PrintErrorCounts()
def check_style(args, white_list=None, black_list=None): """ Execute cpplint with the specified arguments. """ # Apply patches. cpplint.FileInfo.RepositoryName = patch_RepositoryName # Process cpplint arguments. filenames = cpplint.ParseArguments(args) if not white_list: white_list = DEFAULT_LINT_WHITELIST_REGEX white_regex = re.compile(white_list) if not black_list: black_list = DEFAULT_LINT_BLACKLIST_REGEX black_regex = re.compile(black_list) extra_check_functions = [ cpplint_chromium.CheckPointerDeclarationWhitespace ] for filename in filenames: if white_regex.match(filename): if black_regex.match(filename): print "Ignoring file %s" % filename else: cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level, extra_check_functions) else: print "Skipping file %s" % filename print "Total errors found: %d\n" % cpplint._cpplint_state.error_count return 1
def Lint(change_info, args): """Runs cpplint.py on all the files in |change_info|""" try: import cpplint except ImportError: ErrorExit("You need to install cpplint.py to lint C++ files.") # Change the current working directory before calling lint so that it # shows the correct base. previous_cwd = os.getcwd() os.chdir(GetRepositoryRoot()) # Process cpplints arguments if any. filenames = cpplint.ParseArguments(args + change_info.FileList()) for file in filenames: if len([file for suffix in CPP_EXTENSIONS if file.endswith(suffix)]): if len( [file for prefix in IGNORE_PATHS if file.startswith(prefix)]): print "Ignoring non-Google styled file %s" % file else: cpplint.ProcessFile(file, cpplint._cpplint_state.verbose_level) print "Total errors found: %d\n" % cpplint._cpplint_state.error_count os.chdir(previous_cwd)
def main(args): """Runs cpplint on the current changelist.""" """Adapted from git_cl.py CMDlint """ parser = git_cl.OptionParser() parser.add_option('--filter', action='append', metavar='-x,+y', help='Comma-separated list of cpplint\'s category-filters') parser.add_option('--project_root') parser.add_option('--base_branch') auth.add_auth_options(parser) options, args = parser.parse_args(args) auth_config = auth.extract_auth_config_from_options(options) # Access to a protected member _XX of a client class # pylint: disable=protected-access try: import cpplint import cpplint_chromium except ImportError: print('Your depot_tools is missing cpplint.py and/or cpplint_chromium.py.') return 1 # Change the current working directory before calling lint so that it # shows the correct base. settings = git_cl.settings previous_cwd = os.getcwd() os.chdir(settings.GetRoot()) try: cl = git_cl.Changelist(auth_config=auth_config) change = cl.GetChange(git_common.get_or_create_merge_base(cl.GetBranch(), options.base_branch), None) files = [f.LocalPath() for f in change.AffectedFiles()] if not files: print('Cannot lint an empty CL') return 0 # Process cpplints arguments if any. command = args + files if options.filter: command = ['--filter=' + ','.join(options.filter)] + command if options.project_root: command = ['--project_root=' + options.project_root] + command filenames = cpplint.ParseArguments(command) white_regex = re.compile(settings.GetLintRegex()) black_regex = re.compile(settings.GetLintIgnoreRegex()) extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace] for filename in filenames: if white_regex.match(filename): if black_regex.match(filename): print('Ignoring file %s' % filename) else: cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level, extra_check_functions) else: print('Skipping file %s' % filename) finally: os.chdir(previous_cwd) print('Total errors found: %d\n' % cpplint._cpplint_state.error_count) if cpplint._cpplint_state.error_count != 0: return 1 return 0
def do_cpp_lint(changeset, repo, args): # Try to import cpplint from depot_tools first try: import cpplint except ImportError: TryAddDepotToolsToPythonPath() try: import cpplint import cpplint_chromium import gcl except ImportError: sys.stderr.write("Can't find cpplint, please add your depot_tools "\ "to PATH or PYTHONPATH\n") raise print '_____ do cpp lint' if len(changeset) == 0: print 'changeset is empty except python files' return # pass the build/header_guard check if repo == 'cameo': os.rename('.git', '.git.rename') # Following code is referencing depot_tools/gcl.py: CMDlint try: # Process cpplints arguments if any. filenames = cpplint.ParseArguments(args + changeset) white_list = gcl.GetCodeReviewSetting("LINT_REGEX") if not white_list: white_list = gcl.DEFAULT_LINT_REGEX white_regex = re.compile(white_list) black_list = gcl.GetCodeReviewSetting("LINT_IGNORE_REGEX") if not black_list: black_list = gcl.DEFAULT_LINT_IGNORE_REGEX black_regex = re.compile(black_list) extra_check_functions = [ cpplint_chromium.CheckPointerDeclarationWhitespace ] # pylint: disable=W0212 cpplint_state = cpplint._cpplint_state for filename in filenames: if white_regex.match(filename): if black_regex.match(filename): print "Ignoring file %s" % filename else: cpplint.ProcessFile(filename, cpplint_state.verbose_level, extra_check_functions) else: print "Skipping file %s" % filename print "Total errors found: %d\n" % cpplint_state.error_count finally: if repo == 'cameo': os.rename('.git.rename', '.git')
def parse_arguments(): """Parses command line arguments. Returns: A tuple of two values. The first value is a dict of options that used by cclint itself, and the second value is a list of filenames passed to the command line client. """ # Creates a list of cclint's option names. cclint_options = list() for signature in _CCLINT_GETOPT_LONG_OPTIONS: if signature.endswith('='): cclint_options.append(signature[:-1]) else: cclint_options.append(signature) # Separates cpplint's and cclint's arguments. cclint_args = list() cpplint_args = list() for arg in sys.argv[1:]: if arg.startswith('--'): option_name = arg.split('=')[0][2:] if option_name in cclint_options: cclint_args.append(arg) continue cpplint_args.append(arg) # Parses cclint's arguments. options = parse_cclint_arguments(cclint_args) # Parses cpplint's arguments and filters passed filenames within the # exclude directories. filenames = list() for filename in cpplint.ParseArguments(cpplint_args): if (os.path.isdir(filename) and \ os.path.relpath(filename) in options['excludedirs']) or \ (os.path.isfile(filename) and \ os.path.dirname(filename) in options['excludedirs']): continue filenames.append(filename) return options, filenames
def CMDlint(change_info, args): """Runs cpplint.py on all the files in the change list. Checks all the files in the changelist for possible style violations. """ # Access to a protected member _XX of a client class # pylint: disable=W0212 try: import cpplint import cpplint_chromium except ImportError: ErrorExit("You need to install cpplint.py to lint C++ files.") # Change the current working directory before calling lint so that it # shows the correct base. previous_cwd = os.getcwd() os.chdir(change_info.GetLocalRoot()) try: # Process cpplints arguments if any. filenames = cpplint.ParseArguments(args + change_info.GetFileNames()) white_list = GetCodeReviewSetting("LINT_REGEX") if not white_list: white_list = DEFAULT_LINT_REGEX white_regex = re.compile(white_list) black_list = GetCodeReviewSetting("LINT_IGNORE_REGEX") if not black_list: black_list = DEFAULT_LINT_IGNORE_REGEX black_regex = re.compile(black_list) extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace] for filename in filenames: if white_regex.match(filename): if black_regex.match(filename): print "Ignoring file %s" % filename else: cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level, extra_check_functions) else: print "Skipping file %s" % filename finally: os.chdir(previous_cwd) print "Total errors found: %d\n" % cpplint._cpplint_state.error_count return 1
import os import sys root = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..') lint_path = os.path.normpath(os.path.join(root, 'third_party/cpplint')) sys.path.append(lint_path) import cpplint import cpplint_chromium src_root = os.path.normpath(os.path.join(root, 'sg')) os.chdir(src_root) extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace] cpplint.ParseArguments(['--filter=-build/include_what_you_use', 'dummy']) for root, dirs, files in os.walk(src_root): for file in files: ext = os.path.splitext(file)[1] if ext == '.cc' or ext == '.h': cpplint.ProcessFile(os.path.join(root, file), cpplint._cpplint_state.verbose_level, extra_check_functions) print "Total errors found: %d\n" % cpplint._cpplint_state.error_count
def run_lint(paths, nudgeOn=False): # errors are as of 10/14 # idea is not to let it any new type of error # as we knock one out, we should remove line # note: not all of these are things we want, so please check first nudge = [] # things we'd like to turn on sson, so don't make worse later = [] # things that are unlikely anytime soon, so meh never = [] # things we totally disagree with never.append('-build/header_guard') # errors found: 345 nudge.append('-build/include') # errors found: 924 nudge.append('-build/include_order') # errors found: 511 nudge.append('-build/include_what_you_use') # errors found: 986 nudge.append('-build/namespaces') # errors found: 131 never.append('-readability/braces') # errors found: 880 later.append('-readability/casting') # errors found: 748 nudge.append('-readability/function') # errors found: 49 later.append('-readability/streams') # errors found: 72 later.append('-readability/todo') # errors found: 309 nudge.append('-runtime/arrays') # errors found: 5 later.append('-runtime/explicit') # errors found: 322 later.append('-runtime/int') # errors found: 1420 later.append('-runtime/printf') # errors found: 29 nudge.append('-runtime/references') # errors found: 1338 nudge.append('-runtime/rtti') # errors found: 36 nudge.append('-runtime/sizeof') # errors found: 57 nudge.append('-runtime/string') # errors found: 6 nudge.append('-runtime/threadsafe_fn') # errors found: 46 never.append('-whitespace/blank_line') # errors found: 2080 never.append('-whitespace/braces') # errors found: 962 later.append('-whitespace/comma') # errors found: 621 later.append('-whitespace/comments') # errors found: 2189 later.append('-whitespace/end_of_line') # errors found: 4340 later.append('-whitespace/labels') # errors found: 58 later.append('-whitespace/line_length') # errors found: 14500 later.append('-whitespace/newline') # errors found: 1520 nudge.append('-whitespace/operators') # errors found: 2297 never.append('-whitespace/parens') # errors found: 49058 nudge.append('-whitespace/semicolon') # errors found: 121 nudge.append('-whitespace/tab') # errors found: 233 filters = later + never if not nudgeOn: filters = filters + nudge sourceFiles = [] for x in paths: utils.getAllSourceFiles(sourceFiles, x) args = ["--filter=" + ",".join(filters), "--counting=detailed" ] + sourceFiles filenames = cpplint.ParseArguments(args) def _ourIsTestFilename(fn): if fn.find("dbtests") >= 0: return True if fn.endswith("_test.cpp"): return True return False cpplint._IsTestFilename = _ourIsTestFilename # Change stderr to write with replacement characters so we don't die # if we try to print something containing non-ASCII characters. sys.stderr = codecs.StreamReaderWriter(sys.stderr, codecs.getreader('utf8'), codecs.getwriter('utf8'), 'replace') cpplint._cpplint_state.ResetErrorCounts() for filename in filenames: cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level) cpplint._cpplint_state.PrintErrorCounts() return cpplint._cpplint_state.error_count == 0
def do_cpp_lint(changeset, repo, args): # Try to import cpplint from depot_tools first try: import cpplint except ImportError: TryAddDepotToolsToPythonPath() try: import cpplint import cpplint_chromium import gcl except ImportError: sys.stderr.write("Can't find cpplint, please add your depot_tools "\ "to PATH or PYTHONPATH\n") raise origin_error = cpplint.Error def MyError(filename, linenum, category, confidence, message): # Skip no header guard error for MSVC generated files. if (filename.endswith('resource.h')): sys.stdout.write( 'Ignored Error:\n %s(%s): %s [%s] [%d]\n' % (filename, linenum, message, category, confidence)) # Skip no header guard error for ipc messages definition, # because they will be included multiple times for different macros. elif (filename.endswith('messages.h') and linenum == 0 and category == 'build/header_guard'): sys.stdout.write( 'Ignored Error:\n %s(%s): %s [%s] [%d]\n' % (filename, linenum, message, category, confidence)) else: origin_error(filename, linenum, category, confidence, message) cpplint.Error = MyError origin_FileInfo = cpplint.FileInfo class MyFileInfo(origin_FileInfo): def RepositoryName(self): ''' Origin FileInfo find the first .git and take it as project root, it's not the case for xwalk, header in xwalk should have guard relevant to root dir of chromium project, which is one level upper of the origin output of RepositoryName. ''' repo_name = origin_FileInfo.RepositoryName(self) if repo == "xwalk" and not repo_name.startswith('xwalk'): return 'xwalk/%s' % repo_name else: return repo_name cpplint.FileInfo = MyFileInfo print '_____ do cpp lint' if len(changeset) == 0: print 'changeset is empty except python files' return # Following code is referencing depot_tools/gcl.py: CMDlint # Process cpplints arguments if any. filenames = cpplint.ParseArguments(args + changeset) white_list = gcl.GetCodeReviewSetting("LINT_REGEX") if not white_list: white_list = gcl.DEFAULT_LINT_REGEX white_regex = re.compile(white_list) black_list = gcl.GetCodeReviewSetting("LINT_IGNORE_REGEX") if not black_list: black_list = gcl.DEFAULT_LINT_IGNORE_REGEX black_regex = re.compile(black_list) extra_check_functions = [ cpplint_chromium.CheckPointerDeclarationWhitespace ] # pylint: disable=W0212 cpplint_state = cpplint._cpplint_state for filename in filenames: if white_regex.match(filename): if black_regex.match(filename): print "Ignoring file %s" % filename else: cpplint.ProcessFile(filename, cpplint_state.verbose_level, extra_check_functions) else: print "Skipping file %s" % filename print "Total errors found: %d\n" % cpplint_state.error_count
def main(args): """Runs cpplint on the current changelist.""" """Adapted from git_cl.py CMDlint """ parser = git_cl.OptionParser() parser.add_option('--filter', action='append', metavar='-x,+y', help='Comma-separated list of cpplint\'s category-filters') parser.add_option('--project_root') parser.add_option('--base_branch') options, args = parser.parse_args(args) # Access to a protected member _XX of a client class # pylint: disable=protected-access try: import cpplint import cpplint_chromium except ImportError: print('Your depot_tools is missing cpplint.py and/or cpplint_chromium.py.') return 1 # Change the current working directory before calling lint so that it # shows the correct base. settings = git_cl.settings previous_cwd = os.getcwd() os.chdir(settings.GetRoot()) cl = git_cl.Changelist() base_branch = options.base_branch try: print('Running cpplint...') files = cl.GetAffectedFiles(git_common.get_or_create_merge_base(cl.GetBranch(), base_branch)) if not files: print('Cannot lint an empty CL') return 0 # Process cpplints arguments if any. command = args + files if options.filter: command = ['--filter=' + ','.join(options.filter)] + command if options.project_root: command = ['--project_root=' + options.project_root.replace('\\', '/')] + command filenames = cpplint.ParseArguments(command) white_regex = re.compile(settings.GetLintRegex()) black_regex = re.compile(settings.GetLintIgnoreRegex()) extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace] for filename in filenames: if white_regex.match(filename): if black_regex.match(filename): print('Ignoring file %s' % filename) else: cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level, extra_check_functions) else: print('Skipping file %s' % filename) # Run format checks upstream_branch = cl.GetUpstreamBranch() format_output = None if base_branch and not (base_branch in upstream_branch): print('Skipping clang/gn format check because base_branch is %s instead of %s' % (base_branch, upstream_branch)) else: format_output = RunFormatCheck(upstream_branch) finally: os.chdir(previous_cwd) if format_output: print(format_output) return 1 if cpplint._cpplint_state.error_count != 0: print('cpplint errors found: %d\n' % cpplint._cpplint_state.error_count) return 1 print('lint and format checks succeeded') return 0
def main(args): """Runs cpplint on the current changelist.""" """Adapted from git_cl.py CMDlint """ parser = git_cl.OptionParser() parser.add_option( '--filter', action='append', metavar='-x,+y', help='Comma-separated list of cpplint\'s category-filters') parser.add_option('--project_root') parser.add_option('--base_branch') options, args = parser.parse_args(args) # Access to a protected member _XX of a client class # pylint: disable=protected-access try: import cpplint import cpplint_chromium except ImportError: print( 'Your depot_tools is missing cpplint.py and/or cpplint_chromium.py.' ) return 1 # Change the current working directory before calling lint so that it # shows the correct base. settings = git_cl.settings previous_cwd = os.getcwd() os.chdir(settings.GetRoot()) exit_code = 0 # Check for clang/gn format errors. cl = git_cl.Changelist() upstream_branch = cl.GetUpstreamBranch() upstream_commit = git_cl.RunGit(['merge-base', 'HEAD', upstream_branch]) try: if HasFormatErrors(upstream_commit): print('Format check failed. Run npm format to fix.') exit_code = 1 except: e = sys.exc_info()[1] print('Error running format check: %s' % e.info) exit_code = 1 if exit_code == 0: print('Format check succeeded.') print('Running cpplint...') try: files = cl.GetAffectedFiles( git_common.get_or_create_merge_base(cl.GetBranch(), options.base_branch)) if not files: print('Cannot lint an empty CL') return 0 # Process cpplints arguments if any. command = args + files if options.filter: command = ['--filter=' + ','.join(options.filter)] + command if options.project_root: command = ['--project_root=' + options.project_root] + command filenames = cpplint.ParseArguments(command) white_regex = re.compile(settings.GetLintRegex()) black_regex = re.compile(settings.GetLintIgnoreRegex()) extra_check_functions = [ cpplint_chromium.CheckPointerDeclarationWhitespace ] for filename in filenames: if white_regex.match(filename): if black_regex.match(filename): print('Ignoring file %s' % filename) else: cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level, extra_check_functions) else: print('Skipping file %s' % filename) finally: os.chdir(previous_cwd) print('cpplint errors found: %d\n' % cpplint._cpplint_state.error_count) if cpplint._cpplint_state.error_count != 0: return 1 return exit_code