Ryan Tseng | 85ec17e | 2018-09-06 17:10:05 -0700 | [diff] [blame] | 1 | # Copyright (c) 2018 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 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 5 | import os |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 6 | import re |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 7 | import sys |
| 8 | |
| 9 | _REPO_PATH = os.path.dirname(os.path.realpath('__file__')) |
| 10 | _IMPORT_SUBFOLDERS = ['tools', os.path.join('buildtools', 'checkdeps')] |
| 11 | |
| 12 | # git-cl upload is not compatible with __init__.py based subfolder imports, so |
| 13 | # we extend the system path instead. |
| 14 | sys.path.extend(os.path.join(_REPO_PATH, p) for p in _IMPORT_SUBFOLDERS) |
| 15 | |
| 16 | import licenses |
| 17 | from checkdeps import DepsChecker |
Jordan Bayles | 6e279a0 | 2021-05-27 14:24:12 -0700 | [diff] [blame] | 18 | |
| 19 | # Opt-in to using Python3 instead of Python2, as part of the ongoing Python2 |
| 20 | # deprecation. For more information, see |
| 21 | # https://issuetracker.google.com/173766869. |
| 22 | USE_PYTHON3 = True |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 23 | |
mark a. foltz | 8c11e26 | 2020-07-22 14:29:37 -0700 | [diff] [blame] | 24 | # Rather than pass this to all of the checks, we override the global excluded |
| 25 | # list with this one. |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 26 | _EXCLUDED_PATHS = ( |
| 27 | # Exclude all of third_party/ except for BUILD.gns that we maintain. |
| 28 | r'third_party[\\\/].*(?<!BUILD.gn)$', |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 29 | |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 30 | # Exclude everything under third_party/chromium_quic/{src|build} |
| 31 | r'third_party/chromium_quic/(src|build)/.*', |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 32 | |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 33 | # Output directories (just in case) |
| 34 | r'.*\bDebug[\\\/].*', |
| 35 | r'.*\bRelease[\\\/].*', |
| 36 | r'.*\bxcodebuild[\\\/].*', |
| 37 | r'.*\bout[\\\/].*', |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 38 | |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 39 | # There is no point in processing a patch file. |
| 40 | r'.+\.diff$', |
| 41 | r'.+\.patch$', |
| 42 | ) |
| 43 | |
Ryan Tseng | 85ec17e | 2018-09-06 17:10:05 -0700 | [diff] [blame] | 44 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 45 | def _CheckLicenses(input_api, output_api): |
| 46 | """Checks third party licenses and returns a list of violations.""" |
| 47 | return [ |
| 48 | output_api.PresubmitError(v) for v in licenses.ScanThirdPartyDirs() |
| 49 | ] |
btolsch | 9ba2371 | 2019-04-18 16:36:55 -0700 | [diff] [blame] | 50 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 51 | |
| 52 | def _CheckDeps(input_api, output_api): |
| 53 | """Checks DEPS rules and returns a list of violations.""" |
| 54 | deps_checker = DepsChecker(input_api.PresubmitLocalPath()) |
| 55 | deps_checker.CheckDirectory(input_api.PresubmitLocalPath()) |
| 56 | deps_results = deps_checker.results_formatter.GetResults() |
| 57 | return [output_api.PresubmitError(v) for v in deps_results] |
btolsch | 9ba2371 | 2019-04-18 16:36:55 -0700 | [diff] [blame] | 58 | |
| 59 | |
Jordan Bayles | 963b0a6 | 2020-12-02 13:23:59 -0800 | [diff] [blame] | 60 | # Matches OSP_CHECK(foo.is_value()) or OSP_DCHECK(foo.is_value()) |
| 61 | _RE_PATTERN_VALUE_CHECK = re.compile( |
| 62 | r'\s*OSP_D?CHECK\([^)]*\.is_value\(\)\);\s*') |
| 63 | |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 64 | # Matches Foo(Foo&&) when not followed by noexcept. |
| 65 | _RE_PATTERN_MOVE_WITHOUT_NOEXCEPT = re.compile( |
| 66 | r'\s*(?P<classname>\w+)\((?P=classname)&&[^)]*\)\s*(?!noexcept)\s*[{;=]') |
| 67 | |
| 68 | |
Jordan Bayles | 963b0a6 | 2020-12-02 13:23:59 -0800 | [diff] [blame] | 69 | def _CheckNoRegexMatches(regex, |
| 70 | filename, |
| 71 | clean_lines, |
| 72 | linenum, |
| 73 | error, |
| 74 | error_type, |
| 75 | error_msg, |
| 76 | include_cpp_files=True): |
| 77 | """Checks that there are no matches for a specific regex. |
| 78 | |
| 79 | Args: |
| 80 | regex: regex to use for matching. |
| 81 | filename: The name of the current file. |
| 82 | clean_lines: A CleansedLines instance containing the file. |
| 83 | linenum: The number of the line to check. |
| 84 | error: The function to call with any errors found. |
| 85 | error_type: type of error, e.g. runtime/noexcept |
| 86 | error_msg: Specific message to prepend when regex match is found. |
| 87 | """ |
| 88 | if not include_cpp_files and not filename.endswith('.h'): |
| 89 | return |
| 90 | |
| 91 | line = clean_lines.elided[linenum] |
| 92 | matched = regex.match(line) |
| 93 | if matched: |
| 94 | error(filename, linenum, error_type, 4, |
| 95 | 'Error: {} at {}'.format(error_msg, |
| 96 | matched.group(0).strip())) |
| 97 | |
| 98 | |
| 99 | def _CheckNoValueDchecks(filename, clean_lines, linenum, error): |
| 100 | """Checks that there are no OSP_DCHECK(foo.is_value()) instances. |
| 101 | |
| 102 | filename: The name of the current file. |
| 103 | clean_lines: A CleansedLines instance containing the file. |
| 104 | linenum: The number of the line to check. |
| 105 | error: The function to call with any errors found. |
| 106 | """ |
| 107 | _CheckNoRegexMatches(_RE_PATTERN_VALUE_CHECK, filename, clean_lines, |
| 108 | linenum, error, 'runtime/is_value_dchecks', |
| 109 | 'Unnecessary CHECK for ErrorOr::is_value()') |
| 110 | |
| 111 | |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 112 | def _CheckNoexceptOnMove(filename, clean_lines, linenum, error): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 113 | """Checks that move constructors are declared with 'noexcept'. |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 114 | |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 115 | filename: The name of the current file. |
| 116 | clean_lines: A CleansedLines instance containing the file. |
| 117 | linenum: The number of the line to check. |
| 118 | error: The function to call with any errors found. |
| 119 | """ |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 120 | # We only check headers as noexcept is meaningful on declarations, not |
| 121 | # definitions. This may skip some definitions in .cc files though. |
Jordan Bayles | 963b0a6 | 2020-12-02 13:23:59 -0800 | [diff] [blame] | 122 | _CheckNoRegexMatches(_RE_PATTERN_MOVE_WITHOUT_NOEXCEPT, filename, |
| 123 | clean_lines, linenum, error, 'runtime/noexcept', |
| 124 | 'Move constructor not declared \'noexcept\'', False) |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 125 | |
| 126 | # - We disable c++11 header checks since Open Screen allows them. |
| 127 | # - We disable whitespace/braces because of various false positives. |
| 128 | # - There are some false positives with 'explicit' checks, but it's useful |
| 129 | # enough to keep. |
| 130 | # - We add a custom check for 'noexcept' usage. |
| 131 | def _CheckChangeLintsClean(input_api, output_api): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 132 | """Checks that all '.cc' and '.h' files pass cpplint.py.""" |
| 133 | cpplint = input_api.cpplint |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 134 | # Directive that allows access to a protected member _XX of a client class. |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 135 | # pylint: disable=protected-access |
| 136 | cpplint._cpplint_state.ResetErrorCounts() |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 137 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 138 | cpplint._SetFilters('-build/c++11,-whitespace/braces') |
| 139 | files = [ |
| 140 | f.AbsoluteLocalPath() for f in input_api.AffectedSourceFiles(None) |
| 141 | ] |
| 142 | CPPLINT_VERBOSE_LEVEL = 4 |
| 143 | for file_name in files: |
| 144 | cpplint.ProcessFile(file_name, CPPLINT_VERBOSE_LEVEL, |
Jordan Bayles | 963b0a6 | 2020-12-02 13:23:59 -0800 | [diff] [blame] | 145 | [_CheckNoexceptOnMove, _CheckNoValueDchecks]) |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 146 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 147 | if cpplint._cpplint_state.error_count: |
| 148 | if input_api.is_committing: |
| 149 | res_type = output_api.PresubmitError |
| 150 | else: |
| 151 | res_type = output_api.PresubmitPromptWarning |
| 152 | return [res_type('Changelist failed cpplint.py check.')] |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 153 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 154 | return [] |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 155 | |
| 156 | |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 157 | def _CommonChecks(input_api, output_api): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 158 | # PanProjectChecks include: |
| 159 | # CheckLongLines (@ 80 cols) |
| 160 | # CheckChangeHasNoTabs |
| 161 | # CheckChangeHasNoStrayWhitespace |
| 162 | # CheckLicense |
| 163 | # CheckChangeWasUploaded (if committing) |
| 164 | # CheckChangeHasDescription |
| 165 | # CheckDoNotSubmitInDescription |
| 166 | # CheckDoNotSubmitInFiles |
| 167 | results = input_api.canned_checks.PanProjectChecks(input_api, |
| 168 | output_api, |
| 169 | owners_check=False) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 170 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 171 | # No carriage return characters, files end with one EOL (\n). |
| 172 | results.extend( |
| 173 | input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol( |
| 174 | input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 175 | |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 176 | # Ensure code change is gender inclusive. |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 177 | results.extend( |
| 178 | input_api.canned_checks.CheckGenderNeutral(input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 179 | |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 180 | # Ensure code change to do items uses TODO(bug) or TODO(user) format. |
| 181 | # TODO(bug) is generally preferred. |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 182 | results.extend( |
| 183 | input_api.canned_checks.CheckChangeTodoHasOwner(input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 184 | |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 185 | # Ensure code change passes linter cleanly. |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 186 | results.extend(_CheckChangeLintsClean(input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 187 | |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 188 | # Ensure code change has already had clang-format ran. |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 189 | results.extend( |
| 190 | input_api.canned_checks.CheckPatchFormatted(input_api, |
| 191 | output_api, |
| 192 | bypass_warnings=False)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 193 | |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 194 | # Ensure code change has had GN formatting ran. |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 195 | results.extend( |
| 196 | input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 197 | |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 198 | # Run buildtools/checkdeps on code change. |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 199 | results.extend(_CheckDeps(input_api, output_api)) |
| 200 | |
Jordan Bayles | 425c136 | 2021-05-27 14:33:11 -0700 | [diff] [blame] | 201 | # Run tools/licenses on code change. |
mark a. foltz | dec3ae5 | 2021-06-01 15:54:50 -0700 | [diff] [blame] | 202 | # TODO(https://crbug.com/1215335): licenses check is confused by our |
| 203 | # buildtools checkout that doesn't actually check out the libraries. |
| 204 | licenses.PRUNE_PATHS.add(os.path.join('buildtools', 'third_party')); |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 205 | results.extend(_CheckLicenses(input_api, output_api)) |
| 206 | |
| 207 | return results |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 208 | |
| 209 | |
Ryan Tseng | 85ec17e | 2018-09-06 17:10:05 -0700 | [diff] [blame] | 210 | def CheckChangeOnUpload(input_api, output_api): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 211 | input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS |
| 212 | # We always run the OnCommit checks, as well as some additional checks. |
| 213 | results = CheckChangeOnCommit(input_api, output_api) |
Jordan Bayles | 5139930 | 2021-06-18 11:38:39 -0700 | [diff] [blame] | 214 | # TODO(crbug.com/1220846): Open Screen needs a `main` config_set. |
| 215 | #results.extend( |
| 216 | # input_api.canned_checks.CheckChangedLUCIConfigs(input_api, output_api)) |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 217 | return results |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 218 | |
| 219 | |
| 220 | def CheckChangeOnCommit(input_api, output_api): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame] | 221 | input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS |
| 222 | return _CommonChecks(input_api, output_api) |