blob: 111abb03b0dfdaa9500b66a287ec997a6da0d16c [file] [log] [blame]
Nico Weber077f1a32015-08-06 15:08:57 -07001# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Presubmit script for pdfium.
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details about the presubmit API built into depot_tools.
9"""
10
Dan Sinclair22d66072016-02-22 11:56:05 -050011LINT_FILTERS = [
Dan Sinclair3ebd1212016-03-09 09:59:23 -050012 # Rvalue ref checks are unreliable.
dan sinclaird2019df2016-02-22 22:32:03 -050013 '-build/c++11',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050014 # Need to fix header names not matching cpp names.
dan sinclaird2019df2016-02-22 22:32:03 -050015 '-build/include',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050016 # Need to fix header names not matching cpp names.
dan sinclaird2019df2016-02-22 22:32:03 -050017 '-build/include_order',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050018 # Too many to fix at the moment.
dan sinclaird2019df2016-02-22 22:32:03 -050019 '-readability/casting',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050020 # Need to refactor large methods to fix.
dan sinclaird2019df2016-02-22 22:32:03 -050021 '-readability/fn_size',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050022 # Need to fix errors when making methods explicit.
dan sinclaird2019df2016-02-22 22:32:03 -050023 '-runtime/explicit',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050024 # Lots of usage to fix first.
dan sinclaird2019df2016-02-22 22:32:03 -050025 '-runtime/int',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050026 # Need to fix two snprintf TODOs
dan sinclaird2019df2016-02-22 22:32:03 -050027 '-runtime/printf',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050028 # Lots of non-const references need to be fixed
dan sinclaird2019df2016-02-22 22:32:03 -050029 '-runtime/references',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050030 # We are not thread safe, so this will never pass.
dan sinclaird2019df2016-02-22 22:32:03 -050031 '-runtime/threadsafe_fn',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050032 # Figure out how to deal with #defines that git cl format creates.
dan sinclaird2019df2016-02-22 22:32:03 -050033 '-whitespace/indent',
Dan Sinclair22d66072016-02-22 11:56:05 -050034]
35
Dan Sinclair544bbc62016-03-14 15:07:39 -040036
37def _CheckUnwantedDependencies(input_api, output_api):
38 """Runs checkdeps on #include statements added in this
39 change. Breaking - rules is an error, breaking ! rules is a
40 warning.
41 """
42 import sys
43 # We need to wait until we have an input_api object and use this
44 # roundabout construct to import checkdeps because this file is
45 # eval-ed and thus doesn't have __file__.
46 original_sys_path = sys.path
47 try:
48 sys.path = sys.path + [input_api.os_path.join(
49 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
50 import checkdeps
51 from cpp_checker import CppChecker
52 from rules import Rule
dsinclair4cd49e12016-04-05 10:28:48 -070053 except ImportError:
54 return [output_api.PresubmitError(
55 'Unable to run checkdeps, does pdfium/buildtools/checkdeps exist?')]
Dan Sinclair544bbc62016-03-14 15:07:39 -040056 finally:
57 # Restore sys.path to what it was before.
58 sys.path = original_sys_path
59
60 added_includes = []
61 for f in input_api.AffectedFiles():
62 if not CppChecker.IsCppFile(f.LocalPath()):
63 continue
64
65 changed_lines = [line for line_num, line in f.ChangedContents()]
66 added_includes.append([f.LocalPath(), changed_lines])
67
68 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
69
70 error_descriptions = []
71 warning_descriptions = []
72 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
73 added_includes):
74 description_with_path = '%s\n %s' % (path, rule_description)
75 if rule_type == Rule.DISALLOW:
76 error_descriptions.append(description_with_path)
77 else:
78 warning_descriptions.append(description_with_path)
79
80 results = []
81 if error_descriptions:
82 results.append(output_api.PresubmitError(
83 'You added one or more #includes that violate checkdeps rules.',
84 error_descriptions))
85 if warning_descriptions:
86 results.append(output_api.PresubmitPromptOrNotify(
87 'You added one or more #includes of files that are temporarily\n'
88 'allowed but being removed. Can you avoid introducing the\n'
89 '#include? See relevant DEPS file(s) for details and contacts.',
90 warning_descriptions))
91 return results
92
93
Nico Weber077f1a32015-08-06 15:08:57 -070094def CheckChangeOnUpload(input_api, output_api):
95 results = []
Dan Sinclair544bbc62016-03-14 15:07:39 -040096 results += _CheckUnwantedDependencies(input_api, output_api)
Nico Weber077f1a32015-08-06 15:08:57 -070097 results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
Dan Sinclair22d66072016-02-22 11:56:05 -050098 results += input_api.canned_checks.CheckChangeLintsClean(
99 input_api, output_api, None, LINT_FILTERS)
Dan Sinclair544bbc62016-03-14 15:07:39 -0400100
Nico Weber077f1a32015-08-06 15:08:57 -0700101 return results