Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 | for more details about the presubmit API built into depot_tools. |
| 9 | """ |
| 10 | |
Dan Sinclair | 22d6607 | 2016-02-22 11:56:05 -0500 | [diff] [blame] | 11 | LINT_FILTERS = [ |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 12 | # Rvalue ref checks are unreliable. |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 13 | '-build/c++11', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 14 | # Need to fix header names not matching cpp names. |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 15 | '-build/include', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 16 | # Need to fix header names not matching cpp names. |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 17 | '-build/include_order', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 18 | # Too many to fix at the moment. |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 19 | '-readability/casting', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 20 | # Need to refactor large methods to fix. |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 21 | '-readability/fn_size', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 22 | # Need to fix errors when making methods explicit. |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 23 | '-runtime/explicit', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 24 | # Lots of usage to fix first. |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 25 | '-runtime/int', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 26 | # Need to fix two snprintf TODOs |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 27 | '-runtime/printf', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 28 | # Lots of non-const references need to be fixed |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 29 | '-runtime/references', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 30 | # We are not thread safe, so this will never pass. |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 31 | '-runtime/threadsafe_fn', |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 32 | # Figure out how to deal with #defines that git cl format creates. |
dan sinclair | d2019df | 2016-02-22 22:32:03 -0500 | [diff] [blame] | 33 | '-whitespace/indent', |
Dan Sinclair | 22d6607 | 2016-02-22 11:56:05 -0500 | [diff] [blame] | 34 | ] |
| 35 | |
Dan Sinclair | 544bbc6 | 2016-03-14 15:07:39 -0400 | [diff] [blame] | 36 | |
| 37 | def _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 |
dsinclair | 4cd49e1 | 2016-04-05 10:28:48 -0700 | [diff] [blame] | 53 | except ImportError: |
| 54 | return [output_api.PresubmitError( |
| 55 | 'Unable to run checkdeps, does pdfium/buildtools/checkdeps exist?')] |
Dan Sinclair | 544bbc6 | 2016-03-14 15:07:39 -0400 | [diff] [blame] | 56 | 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 Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 94 | def CheckChangeOnUpload(input_api, output_api): |
| 95 | results = [] |
Dan Sinclair | 544bbc6 | 2016-03-14 15:07:39 -0400 | [diff] [blame] | 96 | results += _CheckUnwantedDependencies(input_api, output_api) |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 97 | results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api) |
Dan Sinclair | 22d6607 | 2016-02-22 11:56:05 -0500 | [diff] [blame] | 98 | results += input_api.canned_checks.CheckChangeLintsClean( |
| 99 | input_api, output_api, None, LINT_FILTERS) |
Dan Sinclair | 544bbc6 | 2016-03-14 15:07:39 -0400 | [diff] [blame] | 100 | |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 101 | return results |