blob: 07d567c5ac5177debc0b810cdc8a19f76f115cd5 [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
mark a. foltz01490a42020-11-19 10:33:36 -080054# 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
59def _CheckNoexceptOnMove(filename, clean_lines, linenum, error):
Jordan Baylese50a1d62020-11-30 12:31:47 -080060 """Checks that move constructors are declared with 'noexcept'.
mark a. foltz01490a42020-11-19 10:33:36 -080061
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 Baylese50a1d62020-11-30 12:31:47 -080068 # 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. foltz01490a42020-11-19 10:33:36 -080072
Jordan Baylese50a1d62020-11-30 12:31:47 -080073 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. foltz01490a42020-11-19 10:33:36 -080081
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.
87def _CheckChangeLintsClean(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -080088 """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. foltz01490a42020-11-19 10:33:36 -080093
Jordan Baylese50a1d62020-11-30 12:31:47 -080094 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. foltz01490a42020-11-19 10:33:36 -0800102
Jordan Baylese50a1d62020-11-30 12:31:47 -0800103 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. foltz01490a42020-11-19 10:33:36 -0800109
Jordan Baylese50a1d62020-11-30 12:31:47 -0800110 return []
mark a. foltz01490a42020-11-19 10:33:36 -0800111
112
btolsch333aecd2019-04-18 16:21:23 -0700113def _CommonChecks(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800114 # 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. foltz4410e8e2020-05-07 17:10:17 -0700126
Jordan Baylese50a1d62020-11-30 12:31:47 -0800127 # 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. foltz4410e8e2020-05-07 17:10:17 -0700131
Jordan Baylese50a1d62020-11-30 12:31:47 -0800132 # Gender inclusivity
133 results.extend(
134 input_api.canned_checks.CheckGenderNeutral(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700135
Jordan Baylese50a1d62020-11-30 12:31:47 -0800136 # TODO(bug) format required
137 results.extend(
138 input_api.canned_checks.CheckChangeTodoHasOwner(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700139
Jordan Baylese50a1d62020-11-30 12:31:47 -0800140 # Linter.
141 results.extend(_CheckChangeLintsClean(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700142
Jordan Baylese50a1d62020-11-30 12:31:47 -0800143 # clang-format
144 results.extend(
145 input_api.canned_checks.CheckPatchFormatted(input_api,
146 output_api,
147 bypass_warnings=False))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700148
Jordan Baylese50a1d62020-11-30 12:31:47 -0800149 # GN formatting
150 results.extend(
151 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700152
Jordan Baylese50a1d62020-11-30 12:31:47 -0800153 # 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
btolsch333aecd2019-04-18 16:21:23 -0700160
161
Ryan Tseng85ec17e2018-09-06 17:10:05 -0700162def CheckChangeOnUpload(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800163 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
btolsch333aecd2019-04-18 16:21:23 -0700169
170
171def CheckChangeOnCommit(input_api, output_api):
Jordan Baylese50a1d62020-11-30 12:31:47 -0800172 input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS
173 return _CommonChecks(input_api, output_api)