blob: b4d7da3eefed9c0f3f8fe3321f13cc4cc8529b9a [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
18from cpp_checker import CppChecker
19from rules import Rule
mark a. foltz01490a42020-11-19 10:33:36 -080020
mark a. foltz8c11e262020-07-22 14:29:37 -070021# Rather than pass this to all of the checks, we override the global excluded
22# list with this one.
mark a. foltz4410e8e2020-05-07 17:10:17 -070023_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 Tseng85ec17e2018-09-06 17:10:05 -070038
Jordan Baylese50a1d62020-11-30 12:31:47 -080039def _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 ]
btolsch9ba23712019-04-18 16:36:55 -070044
Jordan Baylese50a1d62020-11-30 12:31:47 -080045
46def _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]
btolsch9ba23712019-04-18 16:36:55 -070052
53
Jordan Bayles963b0a62020-12-02 13:23:59 -080054# 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. foltz01490a42020-11-19 10:33:36 -080058# 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 Bayles963b0a62020-12-02 13:23:59 -080063def _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
93def _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. foltz01490a42020-11-19 10:33:36 -0800106def _CheckNoexceptOnMove(filename, clean_lines, linenum, error):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800107 """Checks that move constructors are declared with 'noexcept'.
mark a. foltz01490a42020-11-19 10:33:36 -0800108
mark a. foltz01490a42020-11-19 10:33:36 -0800109 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 Baylese50a1d62020-11-30 12:31:47 -0800114 # We only check headers as noexcept is meaningful on declarations, not
115 # definitions. This may skip some definitions in .cc files though.
Jordan Bayles963b0a62020-12-02 13:23:59 -0800116 _CheckNoRegexMatches(_RE_PATTERN_MOVE_WITHOUT_NOEXCEPT, filename,
117 clean_lines, linenum, error, 'runtime/noexcept',
118 'Move constructor not declared \'noexcept\'', False)
mark a. foltz01490a42020-11-19 10:33:36 -0800119
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.
125def _CheckChangeLintsClean(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800126 """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. foltz01490a42020-11-19 10:33:36 -0800131
Jordan Baylese50a1d62020-11-30 12:31:47 -0800132 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 Bayles963b0a62020-12-02 13:23:59 -0800139 [_CheckNoexceptOnMove, _CheckNoValueDchecks])
mark a. foltz01490a42020-11-19 10:33:36 -0800140
Jordan Baylese50a1d62020-11-30 12:31:47 -0800141 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. foltz01490a42020-11-19 10:33:36 -0800147
Jordan Baylese50a1d62020-11-30 12:31:47 -0800148 return []
mark a. foltz01490a42020-11-19 10:33:36 -0800149
150
btolsch333aecd2019-04-18 16:21:23 -0700151def _CommonChecks(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800152 # 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. foltz4410e8e2020-05-07 17:10:17 -0700164
Jordan Baylese50a1d62020-11-30 12:31:47 -0800165 # 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. foltz4410e8e2020-05-07 17:10:17 -0700169
Jordan Baylese50a1d62020-11-30 12:31:47 -0800170 # Gender inclusivity
171 results.extend(
172 input_api.canned_checks.CheckGenderNeutral(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700173
Jordan Baylese50a1d62020-11-30 12:31:47 -0800174 # TODO(bug) format required
175 results.extend(
176 input_api.canned_checks.CheckChangeTodoHasOwner(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700177
Jordan Baylese50a1d62020-11-30 12:31:47 -0800178 # Linter.
179 results.extend(_CheckChangeLintsClean(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700180
Jordan Baylese50a1d62020-11-30 12:31:47 -0800181 # clang-format
182 results.extend(
183 input_api.canned_checks.CheckPatchFormatted(input_api,
184 output_api,
185 bypass_warnings=False))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700186
Jordan Baylese50a1d62020-11-30 12:31:47 -0800187 # GN formatting
188 results.extend(
189 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700190
Jordan Baylese50a1d62020-11-30 12:31:47 -0800191 # 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
btolsch333aecd2019-04-18 16:21:23 -0700198
199
Ryan Tseng85ec17e2018-09-06 17:10:05 -0700200def CheckChangeOnUpload(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800201 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
btolsch333aecd2019-04-18 16:21:23 -0700207
208
209def CheckChangeOnCommit(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800210 input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS
211 return _CommonChecks(input_api, output_api)