blob: dafbc5ea2af0a0ff2907eb7a5b3be830e69583cb [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
mark a. foltz01490a42020-11-19 10:33:36 -08005import re
6
mark a. foltz8c11e262020-07-22 14:29:37 -07007# Rather than pass this to all of the checks, we override the global excluded
8# list with this one.
mark a. foltz4410e8e2020-05-07 17:10:17 -07009_EXCLUDED_PATHS = (
10 # Exclude all of third_party/ except for BUILD.gns that we maintain.
11 r'third_party[\\\/].*(?<!BUILD.gn)$',
12 # Exclude everything under third_party/chromium_quic/{src|build}
13 r'third_party/chromium_quic/(src|build)/.*',
14 # Output directories (just in case)
15 r'.*\bDebug[\\\/].*',
16 r'.*\bRelease[\\\/].*',
17 r'.*\bxcodebuild[\\\/].*',
18 r'.*\bout[\\\/].*',
19 # There is no point in processing a patch file.
20 r'.+\.diff$',
21 r'.+\.patch$',
22)
23
Ryan Tseng85ec17e2018-09-06 17:10:05 -070024
btolsch9ba23712019-04-18 16:36:55 -070025def _CheckDeps(input_api, output_api):
26 results = []
27 import sys
28 original_sys_path = sys.path
29 try:
30 sys.path = sys.path + [input_api.os_path.join(
31 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
32 import checkdeps
33 from cpp_checker import CppChecker
34 from rules import Rule
35 finally:
36 sys.path = original_sys_path
37
btolsch9ba23712019-04-18 16:36:55 -070038 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
mark a. foltz743b6202019-08-29 14:43:43 -070039 deps_checker.CheckDirectory(input_api.PresubmitLocalPath())
40 deps_results = deps_checker.results_formatter.GetResults()
41 for violation in deps_results:
42 results.append(output_api.PresubmitError(violation))
btolsch9ba23712019-04-18 16:36:55 -070043 return results
44
45
mark a. foltz01490a42020-11-19 10:33:36 -080046# Matches Foo(Foo&&) when not followed by noexcept.
47_RE_PATTERN_MOVE_WITHOUT_NOEXCEPT = re.compile(
48 r'\s*(?P<classname>\w+)\((?P=classname)&&[^)]*\)\s*(?!noexcept)\s*[{;=]')
49
50
51def _CheckNoexceptOnMove(filename, clean_lines, linenum, error):
52 """Checks that move constructors are declared with 'noexcept'.
53
54 Args:
55 filename: The name of the current file.
56 clean_lines: A CleansedLines instance containing the file.
57 linenum: The number of the line to check.
58 error: The function to call with any errors found.
59 """
60 # We only check headers as noexcept is meaningful on declarations, not
61 # definitions. This may skip some definitions in .cc files though.
62 if not filename.endswith('.h'):
63 return
64
65 line = clean_lines.elided[linenum]
66 matched = _RE_PATTERN_MOVE_WITHOUT_NOEXCEPT.match(line)
67 if matched:
68 error(filename, linenum, 'runtime/noexcept', 4,
69 'Move constructor of %s not declared \'noexcept\' in %s' %
70 (matched.group('classname'), matched.group(0).strip()))
71
72# - We disable c++11 header checks since Open Screen allows them.
73# - We disable whitespace/braces because of various false positives.
74# - There are some false positives with 'explicit' checks, but it's useful
75# enough to keep.
76# - We add a custom check for 'noexcept' usage.
77def _CheckChangeLintsClean(input_api, output_api):
78 """Checks that all '.cc' and '.h' files pass cpplint.py."""
79 result = []
80
81 cpplint = input_api.cpplint
82 # Access to a protected member _XX of a client class
83 # pylint: disable=protected-access
84 cpplint._cpplint_state.ResetErrorCounts()
85
86 cpplint._SetFilters('-build/c++11,-whitespace/braces')
87 files = [f.AbsoluteLocalPath() for f in input_api.AffectedSourceFiles(None)]
88 for file_name in files:
89 # 4 = verbose_level
90 cpplint.ProcessFile(file_name, 4, [_CheckNoexceptOnMove])
91
92 if cpplint._cpplint_state.error_count > 0:
93 if input_api.is_committing:
94 res_type = output_api.PresubmitError
95 else:
96 res_type = output_api.PresubmitPromptWarning
97 result = [res_type('Changelist failed cpplint.py check.')]
98
99 return result
100
101
btolsch333aecd2019-04-18 16:21:23 -0700102def _CommonChecks(input_api, output_api):
103 results = []
mark a. foltz4410e8e2020-05-07 17:10:17 -0700104 # PanProjectChecks include:
105 # CheckLongLines (@ 80 cols)
106 # CheckChangeHasNoTabs
107 # CheckChangeHasNoStrayWhitespace
108 # CheckLicense
109 # CheckChangeWasUploaded (if committing)
110 # CheckChangeHasDescription
111 # CheckDoNotSubmitInDescription
112 # CheckDoNotSubmitInFiles
113 results.extend(input_api.canned_checks.PanProjectChecks(
114 input_api, output_api, owners_check=False));
115
116 # No carriage return characters, files end with one EOL (\n).
117 results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
118 input_api, output_api));
119
120 # Gender inclusivity
121 results.extend(input_api.canned_checks.CheckGenderNeutral(
122 input_api, output_api))
123
124 # TODO(bug) format required
125 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
126 input_api, output_api))
127
mark a. foltzb4e53be2020-05-28 11:42:36 -0700128 # Linter.
mark a. foltz01490a42020-11-19 10:33:36 -0800129 results.extend(_CheckChangeLintsClean(input_api, output_api))
mark a. foltz4410e8e2020-05-07 17:10:17 -0700130
131 # clang-format
132 results.extend(input_api.canned_checks.CheckPatchFormatted(
133 input_api, output_api, bypass_warnings=False))
134
135 # GN formatting
136 results.extend(input_api.canned_checks.CheckGNFormatted(
137 input_api, output_api))
138
139 # buildtools/checkdeps
btolsch9ba23712019-04-18 16:36:55 -0700140 results.extend(_CheckDeps(input_api, output_api))
btolsch333aecd2019-04-18 16:21:23 -0700141 return results
142
143
Ryan Tseng85ec17e2018-09-06 17:10:05 -0700144def CheckChangeOnUpload(input_api, output_api):
Josip Sokcevic3cc83632020-07-23 13:30:22 -0700145 input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS;
btolsch333aecd2019-04-18 16:21:23 -0700146 results = []
147 results.extend(_CommonChecks(input_api, output_api))
148 results.extend(
149 input_api.canned_checks.CheckChangedLUCIConfigs(input_api, output_api))
150 return results
151
152
153def CheckChangeOnCommit(input_api, output_api):
Josip Sokcevic3cc83632020-07-23 13:30:22 -0700154 input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS;
btolsch333aecd2019-04-18 16:21:23 -0700155 results = []
156 results.extend(_CommonChecks(input_api, output_api))
157 return results