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