blob: f9b08876a5ea009a41702a0d6d9be2986bfd2198 [file] [log] [blame]
Ryan Tseng85ec17e2018-09-06 17:10:05 -07001# 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 Baylese50a1d62020-11-30 12:31:47 -08005import os
mark a. foltz01490a42020-11-19 10:33:36 -08006import re
Jordan Baylese50a1d62020-11-30 12:31:47 -08007import 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.
14sys.path.extend(os.path.join(_REPO_PATH, p) for p in _IMPORT_SUBFOLDERS)
15
16import licenses
17from checkdeps import DepsChecker
Jordan Bayles6e279a02021-05-27 14:24:12 -070018
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.
22USE_PYTHON3 = True
mark a. foltz01490a42020-11-19 10:33:36 -080023
mark a. foltz8c11e262020-07-22 14:29:37 -070024# Rather than pass this to all of the checks, we override the global excluded
25# list with this one.
mark a. foltz4410e8e2020-05-07 17:10:17 -070026_EXCLUDED_PATHS = (
27 # Exclude all of third_party/ except for BUILD.gns that we maintain.
28 r'third_party[\\\/].*(?<!BUILD.gn)$',
29 # Exclude everything under third_party/chromium_quic/{src|build}
30 r'third_party/chromium_quic/(src|build)/.*',
31 # Output directories (just in case)
32 r'.*\bDebug[\\\/].*',
33 r'.*\bRelease[\\\/].*',
34 r'.*\bxcodebuild[\\\/].*',
35 r'.*\bout[\\\/].*',
36 # There is no point in processing a patch file.
37 r'.+\.diff$',
38 r'.+\.patch$',
39)
40
Ryan Tseng85ec17e2018-09-06 17:10:05 -070041
Jordan Baylese50a1d62020-11-30 12:31:47 -080042def _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 ]
btolsch9ba23712019-04-18 16:36:55 -070047
Jordan Baylese50a1d62020-11-30 12:31:47 -080048
49def _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]
btolsch9ba23712019-04-18 16:36:55 -070055
56
Jordan Bayles963b0a62020-12-02 13:23:59 -080057# 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. foltz01490a42020-11-19 10:33:36 -080061# 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 Bayles963b0a62020-12-02 13:23:59 -080066def _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
96def _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. foltz01490a42020-11-19 10:33:36 -0800109def _CheckNoexceptOnMove(filename, clean_lines, linenum, error):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800110 """Checks that move constructors are declared with 'noexcept'.
mark a. foltz01490a42020-11-19 10:33:36 -0800111
mark a. foltz01490a42020-11-19 10:33:36 -0800112 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 Baylese50a1d62020-11-30 12:31:47 -0800117 # We only check headers as noexcept is meaningful on declarations, not
118 # definitions. This may skip some definitions in .cc files though.
Jordan Bayles963b0a62020-12-02 13:23:59 -0800119 _CheckNoRegexMatches(_RE_PATTERN_MOVE_WITHOUT_NOEXCEPT, filename,
120 clean_lines, linenum, error, 'runtime/noexcept',
121 'Move constructor not declared \'noexcept\'', False)
mark a. foltz01490a42020-11-19 10:33:36 -0800122
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.
128def _CheckChangeLintsClean(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800129 """Checks that all '.cc' and '.h' files pass cpplint.py."""
130 cpplint = input_api.cpplint
131 # Access to a protected member _XX of a client class
132 # pylint: disable=protected-access
133 cpplint._cpplint_state.ResetErrorCounts()
mark a. foltz01490a42020-11-19 10:33:36 -0800134
Jordan Baylese50a1d62020-11-30 12:31:47 -0800135 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 Bayles963b0a62020-12-02 13:23:59 -0800142 [_CheckNoexceptOnMove, _CheckNoValueDchecks])
mark a. foltz01490a42020-11-19 10:33:36 -0800143
Jordan Baylese50a1d62020-11-30 12:31:47 -0800144 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. foltz01490a42020-11-19 10:33:36 -0800150
Jordan Baylese50a1d62020-11-30 12:31:47 -0800151 return []
mark a. foltz01490a42020-11-19 10:33:36 -0800152
153
btolsch333aecd2019-04-18 16:21:23 -0700154def _CommonChecks(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800155 # PanProjectChecks include:
156 # CheckLongLines (@ 80 cols)
157 # CheckChangeHasNoTabs
158 # CheckChangeHasNoStrayWhitespace
159 # CheckLicense
160 # CheckChangeWasUploaded (if committing)
161 # CheckChangeHasDescription
162 # CheckDoNotSubmitInDescription
163 # CheckDoNotSubmitInFiles
164 results = input_api.canned_checks.PanProjectChecks(input_api,
165 output_api,
166 owners_check=False)
mark a. foltz4410e8e2020-05-07 17:10:17 -0700167
Jordan Baylese50a1d62020-11-30 12:31:47 -0800168 # No carriage return characters, files end with one EOL (\n).
169 results.extend(
170 input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
171 input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700172
Jordan Baylese50a1d62020-11-30 12:31:47 -0800173 # Gender inclusivity
174 results.extend(
175 input_api.canned_checks.CheckGenderNeutral(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700176
Jordan Baylese50a1d62020-11-30 12:31:47 -0800177 # TODO(bug) format required
178 results.extend(
179 input_api.canned_checks.CheckChangeTodoHasOwner(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700180
Jordan Baylese50a1d62020-11-30 12:31:47 -0800181 # Linter.
182 results.extend(_CheckChangeLintsClean(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700183
Jordan Baylese50a1d62020-11-30 12:31:47 -0800184 # clang-format
185 results.extend(
186 input_api.canned_checks.CheckPatchFormatted(input_api,
187 output_api,
188 bypass_warnings=False))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700189
Jordan Baylese50a1d62020-11-30 12:31:47 -0800190 # GN formatting
191 results.extend(
192 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700193
Jordan Baylese50a1d62020-11-30 12:31:47 -0800194 # buildtools/checkdeps
195 results.extend(_CheckDeps(input_api, output_api))
196
197 # tools/licenses
198 results.extend(_CheckLicenses(input_api, output_api))
199
200 return results
btolsch333aecd2019-04-18 16:21:23 -0700201
202
Ryan Tseng85ec17e2018-09-06 17:10:05 -0700203def CheckChangeOnUpload(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800204 input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS
205 # We always run the OnCommit checks, as well as some additional checks.
206 results = CheckChangeOnCommit(input_api, output_api)
207 results.extend(
208 input_api.canned_checks.CheckChangedLUCIConfigs(input_api, output_api))
209 return results
btolsch333aecd2019-04-18 16:21:23 -0700210
211
212def CheckChangeOnCommit(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800213 input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS
214 return _CommonChecks(input_api, output_api)