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