blob: 78e7482efbab5da80a2a0f5b8efa40ca4861c79d [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6# * Redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer.
8# * Redistributions in binary form must reproduce the above
9# copyright notice, this list of conditions and the following
10# disclaimer in the documentation and/or other materials provided
11# with the distribution.
12# * Neither the name of Google Inc. nor the names of its
13# contributors may be used to endorse or promote products derived
14# from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28"""Top-level presubmit script for V8.
29
30See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
31for more details about the presubmit API built into gcl.
32"""
33
34import sys
35
36
37_EXCLUDED_PATHS = (
38 r"^test[\\\/].*",
39 r"^testing[\\\/].*",
40 r"^third_party[\\\/].*",
41 r"^tools[\\\/].*",
42)
43
44
45# Regular expression that matches code only used for test binaries
46# (best effort).
47_TEST_CODE_EXCLUDED_PATHS = (
48 r'.+-unittest\.cc',
49 # Has a method VisitForTest().
50 r'src[\\\/]compiler[\\\/]ast-graph-builder\.cc',
51 # Test extension.
52 r'src[\\\/]extensions[\\\/]gc-extension\.cc',
53)
54
55
56_TEST_ONLY_WARNING = (
57 'You might be calling functions intended only for testing from\n'
58 'production code. It is OK to ignore this warning if you know what\n'
59 'you are doing, as the heuristics used to detect the situation are\n'
60 'not perfect. The commit queue will not block on this warning.')
61
62
63def _V8PresubmitChecks(input_api, output_api):
64 """Runs the V8 presubmit checks."""
65 import sys
66 sys.path.append(input_api.os_path.join(
67 input_api.PresubmitLocalPath(), 'tools'))
68 from presubmit import CppLintProcessor
69 from presubmit import SourceProcessor
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 from presubmit import CheckExternalReferenceRegistration
Ben Murdoch014dc512016-03-22 12:00:34 +000071 from presubmit import CheckAuthorizedAuthor
72 from presubmit import CheckStatusFiles
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073
74 results = []
75 if not CppLintProcessor().Run(input_api.PresubmitLocalPath()):
76 results.append(output_api.PresubmitError("C++ lint check failed"))
77 if not SourceProcessor().Run(input_api.PresubmitLocalPath()):
78 results.append(output_api.PresubmitError(
79 "Copyright header, trailing whitespaces and two empty lines " \
80 "between declarations check failed"))
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 if not CheckExternalReferenceRegistration(input_api.PresubmitLocalPath()):
82 results.append(output_api.PresubmitError(
83 "External references registration check failed"))
Ben Murdoch014dc512016-03-22 12:00:34 +000084 if not CheckStatusFiles(input_api.PresubmitLocalPath()):
85 results.append(output_api.PresubmitError("Status file check failed"))
86 results.extend(CheckAuthorizedAuthor(input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 return results
88
89
90def _CheckUnwantedDependencies(input_api, output_api):
91 """Runs checkdeps on #include statements added in this
92 change. Breaking - rules is an error, breaking ! rules is a
93 warning.
94 """
95 # We need to wait until we have an input_api object and use this
96 # roundabout construct to import checkdeps because this file is
97 # eval-ed and thus doesn't have __file__.
98 original_sys_path = sys.path
99 try:
100 sys.path = sys.path + [input_api.os_path.join(
101 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
102 import checkdeps
103 from cpp_checker import CppChecker
104 from rules import Rule
105 finally:
106 # Restore sys.path to what it was before.
107 sys.path = original_sys_path
108
109 added_includes = []
110 for f in input_api.AffectedFiles():
111 if not CppChecker.IsCppFile(f.LocalPath()):
112 continue
113
114 changed_lines = [line for line_num, line in f.ChangedContents()]
115 added_includes.append([f.LocalPath(), changed_lines])
116
117 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
118
119 error_descriptions = []
120 warning_descriptions = []
121 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
122 added_includes):
123 description_with_path = '%s\n %s' % (path, rule_description)
124 if rule_type == Rule.DISALLOW:
125 error_descriptions.append(description_with_path)
126 else:
127 warning_descriptions.append(description_with_path)
128
129 results = []
130 if error_descriptions:
131 results.append(output_api.PresubmitError(
132 'You added one or more #includes that violate checkdeps rules.',
133 error_descriptions))
134 if warning_descriptions:
135 results.append(output_api.PresubmitPromptOrNotify(
136 'You added one or more #includes of files that are temporarily\n'
137 'allowed but being removed. Can you avoid introducing the\n'
138 '#include? See relevant DEPS file(s) for details and contacts.',
139 warning_descriptions))
140 return results
141
142
Ben Murdoch014dc512016-03-22 12:00:34 +0000143def _CheckNoInlineHeaderIncludesInNormalHeaders(input_api, output_api):
144 """Attempts to prevent inclusion of inline headers into normal header
145 files. This tries to establish a layering where inline headers can be
146 included by other inline headers or compilation units only."""
147 file_inclusion_pattern = r'(?!.+-inl\.h).+\.h'
148 include_directive_pattern = input_api.re.compile(r'#include ".+-inl.h"')
149 include_warning = (
150 'You might be including an inline header (e.g. foo-inl.h) within a\n'
151 'normal header (e.g. bar.h) file. Can you avoid introducing the\n'
152 '#include? The commit queue will not block on this warning.')
153
154 def FilterFile(affected_file):
155 black_list = (_EXCLUDED_PATHS +
156 input_api.DEFAULT_BLACK_LIST)
157 return input_api.FilterSourceFile(
158 affected_file,
159 white_list=(file_inclusion_pattern, ),
160 black_list=black_list)
161
162 problems = []
163 for f in input_api.AffectedSourceFiles(FilterFile):
164 local_path = f.LocalPath()
165 for line_number, line in f.ChangedContents():
166 if (include_directive_pattern.search(line)):
167 problems.append(
168 '%s:%d\n %s' % (local_path, line_number, line.strip()))
169
170 if problems:
171 return [output_api.PresubmitPromptOrNotify(include_warning, problems)]
172 else:
173 return []
174
175
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
177 """Attempts to prevent use of functions intended only for testing in
178 non-testing code. For now this is just a best-effort implementation
179 that ignores header files and may have some false positives. A
180 better implementation would probably need a proper C++ parser.
181 """
182 # We only scan .cc files, as the declaration of for-testing functions in
183 # header files are hard to distinguish from calls to such functions without a
184 # proper C++ parser.
185 file_inclusion_pattern = r'.+\.cc'
186
187 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
188 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
189 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
190 exclusion_pattern = input_api.re.compile(
191 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
192 base_function_pattern, base_function_pattern))
193
194 def FilterFile(affected_file):
195 black_list = (_EXCLUDED_PATHS +
196 _TEST_CODE_EXCLUDED_PATHS +
197 input_api.DEFAULT_BLACK_LIST)
198 return input_api.FilterSourceFile(
199 affected_file,
200 white_list=(file_inclusion_pattern, ),
201 black_list=black_list)
202
203 problems = []
204 for f in input_api.AffectedSourceFiles(FilterFile):
205 local_path = f.LocalPath()
206 for line_number, line in f.ChangedContents():
207 if (inclusion_pattern.search(line) and
208 not comment_pattern.search(line) and
209 not exclusion_pattern.search(line)):
210 problems.append(
211 '%s:%d\n %s' % (local_path, line_number, line.strip()))
212
213 if problems:
214 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
215 else:
216 return []
217
218
Ben Murdochf3b273f2017-01-17 12:11:28 +0000219def _CheckMissingFiles(input_api, output_api):
220 """Runs verify_source_deps.py to ensure no files were added that are not in
221 GN.
222 """
223 # We need to wait until we have an input_api object and use this
224 # roundabout construct to import checkdeps because this file is
225 # eval-ed and thus doesn't have __file__.
226 original_sys_path = sys.path
227 try:
228 sys.path = sys.path + [input_api.os_path.join(
229 input_api.PresubmitLocalPath(), 'tools')]
230 from verify_source_deps import missing_gn_files, missing_gyp_files
231 finally:
232 # Restore sys.path to what it was before.
233 sys.path = original_sys_path
234
235 gn_files = missing_gn_files()
236 gyp_files = missing_gyp_files()
237 results = []
238 if gn_files:
239 results.append(output_api.PresubmitError(
240 "You added one or more source files but didn't update the\n"
241 "corresponding BUILD.gn files:\n",
242 gn_files))
243 if gyp_files:
244 results.append(output_api.PresubmitError(
245 "You added one or more source files but didn't update the\n"
246 "corresponding gyp files:\n",
247 gyp_files))
248 return results
249
250
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251def _CommonChecks(input_api, output_api):
252 """Checks common to both upload and commit."""
253 results = []
254 results.extend(input_api.canned_checks.CheckOwners(
255 input_api, output_api, source_file_filter=None))
256 results.extend(input_api.canned_checks.CheckPatchFormatted(
257 input_api, output_api))
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100258 results.extend(input_api.canned_checks.CheckGenderNeutral(
259 input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 results.extend(_V8PresubmitChecks(input_api, output_api))
261 results.extend(_CheckUnwantedDependencies(input_api, output_api))
262 results.extend(
263 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
Ben Murdoch014dc512016-03-22 12:00:34 +0000264 results.extend(
265 _CheckNoInlineHeaderIncludesInNormalHeaders(input_api, output_api))
Ben Murdochf3b273f2017-01-17 12:11:28 +0000266 results.extend(_CheckMissingFiles(input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267 return results
268
269
270def _SkipTreeCheck(input_api, output_api):
271 """Check the env var whether we want to skip tree check.
Ben Murdoch014dc512016-03-22 12:00:34 +0000272 Only skip if include/v8-version.h has been updated."""
273 src_version = 'include/v8-version.h'
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274 if not input_api.AffectedSourceFiles(
275 lambda file: file.LocalPath() == src_version):
276 return False
277 return input_api.environ.get('PRESUBMIT_TREE_CHECK') == 'skip'
278
279
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280def CheckChangeOnUpload(input_api, output_api):
281 results = []
282 results.extend(_CommonChecks(input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283 return results
284
285
286def CheckChangeOnCommit(input_api, output_api):
287 results = []
288 results.extend(_CommonChecks(input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289 results.extend(input_api.canned_checks.CheckChangeHasDescription(
290 input_api, output_api))
291 if not _SkipTreeCheck(input_api, output_api):
292 results.extend(input_api.canned_checks.CheckTreeIsOpen(
293 input_api, output_api,
294 json_url='http://v8-status.appspot.com/current?format=json'))
295 return results