blob: 819c4e64fdcd2c4592c0332f61bd4c1141e1e165 [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
53 finally:
54 # Restore sys.path to what it was before.
55 sys.path = original_sys_path
56
57 added_includes = []
58 for f in input_api.AffectedFiles():
59 if not CppChecker.IsCppFile(f.LocalPath()):
60 continue
61
62 changed_lines = [line for line_num, line in f.ChangedContents()]
63 added_includes.append([f.LocalPath(), changed_lines])
64
65 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
66
67 error_descriptions = []
68 warning_descriptions = []
69 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
70 added_includes):
71 description_with_path = '%s\n %s' % (path, rule_description)
72 if rule_type == Rule.DISALLOW:
73 error_descriptions.append(description_with_path)
74 else:
75 warning_descriptions.append(description_with_path)
76
77 results = []
78 if error_descriptions:
79 results.append(output_api.PresubmitError(
80 'You added one or more #includes that violate checkdeps rules.',
81 error_descriptions))
82 if warning_descriptions:
83 results.append(output_api.PresubmitPromptOrNotify(
84 'You added one or more #includes of files that are temporarily\n'
85 'allowed but being removed. Can you avoid introducing the\n'
86 '#include? See relevant DEPS file(s) for details and contacts.',
87 warning_descriptions))
88 return results
89
90
Nico Weber077f1a32015-08-06 15:08:57 -070091def CheckChangeOnUpload(input_api, output_api):
92 results = []
Dan Sinclair544bbc62016-03-14 15:07:39 -040093 results += _CheckUnwantedDependencies(input_api, output_api)
Nico Weber077f1a32015-08-06 15:08:57 -070094 results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
Dan Sinclair22d66072016-02-22 11:56:05 -050095 results += input_api.canned_checks.CheckChangeLintsClean(
96 input_api, output_api, None, LINT_FILTERS)
Dan Sinclair544bbc62016-03-14 15:07:39 -040097
Nico Weber077f1a32015-08-06 15:08:57 -070098 return results