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 | |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 54 | # Matches Foo(Foo&&) when not followed by noexcept. |
| 55 | _RE_PATTERN_MOVE_WITHOUT_NOEXCEPT = re.compile( |
| 56 | r'\s*(?P<classname>\w+)\((?P=classname)&&[^)]*\)\s*(?!noexcept)\s*[{;=]') |
| 57 | |
| 58 | |
| 59 | def _CheckNoexceptOnMove(filename, clean_lines, linenum, error): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 60 | """Checks that move constructors are declared with 'noexcept'. |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 61 | |
| 62 | Args: |
| 63 | filename: The name of the current file. |
| 64 | clean_lines: A CleansedLines instance containing the file. |
| 65 | linenum: The number of the line to check. |
| 66 | error: The function to call with any errors found. |
| 67 | """ |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 68 | # We only check headers as noexcept is meaningful on declarations, not |
| 69 | # definitions. This may skip some definitions in .cc files though. |
| 70 | if not filename.endswith('.h'): |
| 71 | return |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 72 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 73 | line = clean_lines.elided[linenum] |
| 74 | matched = _RE_PATTERN_MOVE_WITHOUT_NOEXCEPT.match(line) |
| 75 | if matched: |
| 76 | error( |
| 77 | filename, linenum, 'runtime/noexcept', 4, |
| 78 | 'Move constructor of {} not declared \'noexcept\' in {}'.format( |
| 79 | matched.group('classname'), |
| 80 | matched.group(0).strip())) |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 81 | |
| 82 | # - We disable c++11 header checks since Open Screen allows them. |
| 83 | # - We disable whitespace/braces because of various false positives. |
| 84 | # - There are some false positives with 'explicit' checks, but it's useful |
| 85 | # enough to keep. |
| 86 | # - We add a custom check for 'noexcept' usage. |
| 87 | def _CheckChangeLintsClean(input_api, output_api): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 88 | """Checks that all '.cc' and '.h' files pass cpplint.py.""" |
| 89 | cpplint = input_api.cpplint |
| 90 | # Access to a protected member _XX of a client class |
| 91 | # pylint: disable=protected-access |
| 92 | cpplint._cpplint_state.ResetErrorCounts() |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 93 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 94 | cpplint._SetFilters('-build/c++11,-whitespace/braces') |
| 95 | files = [ |
| 96 | f.AbsoluteLocalPath() for f in input_api.AffectedSourceFiles(None) |
| 97 | ] |
| 98 | CPPLINT_VERBOSE_LEVEL = 4 |
| 99 | for file_name in files: |
| 100 | cpplint.ProcessFile(file_name, CPPLINT_VERBOSE_LEVEL, |
| 101 | [_CheckNoexceptOnMove]) |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 102 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 103 | if cpplint._cpplint_state.error_count: |
| 104 | if input_api.is_committing: |
| 105 | res_type = output_api.PresubmitError |
| 106 | else: |
| 107 | res_type = output_api.PresubmitPromptWarning |
| 108 | return [res_type('Changelist failed cpplint.py check.')] |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 109 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 110 | return [] |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame] | 111 | |
| 112 | |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 113 | def _CommonChecks(input_api, output_api): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 114 | # PanProjectChecks include: |
| 115 | # CheckLongLines (@ 80 cols) |
| 116 | # CheckChangeHasNoTabs |
| 117 | # CheckChangeHasNoStrayWhitespace |
| 118 | # CheckLicense |
| 119 | # CheckChangeWasUploaded (if committing) |
| 120 | # CheckChangeHasDescription |
| 121 | # CheckDoNotSubmitInDescription |
| 122 | # CheckDoNotSubmitInFiles |
| 123 | results = input_api.canned_checks.PanProjectChecks(input_api, |
| 124 | output_api, |
| 125 | owners_check=False) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 126 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 127 | # No carriage return characters, files end with one EOL (\n). |
| 128 | results.extend( |
| 129 | input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol( |
| 130 | input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 131 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 132 | # Gender inclusivity |
| 133 | results.extend( |
| 134 | input_api.canned_checks.CheckGenderNeutral(input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 135 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 136 | # TODO(bug) format required |
| 137 | results.extend( |
| 138 | input_api.canned_checks.CheckChangeTodoHasOwner(input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 139 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 140 | # Linter. |
| 141 | results.extend(_CheckChangeLintsClean(input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 142 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 143 | # clang-format |
| 144 | results.extend( |
| 145 | input_api.canned_checks.CheckPatchFormatted(input_api, |
| 146 | output_api, |
| 147 | bypass_warnings=False)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 148 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 149 | # GN formatting |
| 150 | results.extend( |
| 151 | input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 152 | |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 153 | # buildtools/checkdeps |
| 154 | results.extend(_CheckDeps(input_api, output_api)) |
| 155 | |
| 156 | # tools/licenses |
| 157 | results.extend(_CheckLicenses(input_api, output_api)) |
| 158 | |
| 159 | return results |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 160 | |
| 161 | |
Ryan Tseng | 85ec17e | 2018-09-06 17:10:05 -0700 | [diff] [blame] | 162 | def CheckChangeOnUpload(input_api, output_api): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 163 | input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS |
| 164 | # We always run the OnCommit checks, as well as some additional checks. |
| 165 | results = CheckChangeOnCommit(input_api, output_api) |
| 166 | results.extend( |
| 167 | input_api.canned_checks.CheckChangedLUCIConfigs(input_api, output_api)) |
| 168 | return results |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 169 | |
| 170 | |
| 171 | def CheckChangeOnCommit(input_api, output_api): |
Jordan Bayles | e50a1d6 | 2020-11-30 12:31:47 -0800 | [diff] [blame^] | 172 | input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS |
| 173 | return _CommonChecks(input_api, output_api) |