1 # 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
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.
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.
28 """Top-level presubmit script for V8.
30 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
31 for more details about the presubmit API built into gcl.
40 r"^third_party[\\\/].*",
45 # Regular expression that matches code only used for test binaries
47 _TEST_CODE_EXCLUDED_PATHS = (
49 # Has a method VisitForTest().
50 r'src[\\\/]compiler[\\\/]ast-graph-builder\.cc',
52 r'src[\\\/]extensions[\\\/]gc-extension\.cc',
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.')
63 def _V8PresubmitChecks(input_api, output_api):
64 """Runs the V8 presubmit checks."""
66 sys.path.append(input_api.os_path.join(
67 input_api.PresubmitLocalPath(), 'tools'))
68 from presubmit import CppLintProcessor
69 from presubmit import SourceProcessor
70 from presubmit import CheckRuntimeVsNativesNameClashes
71 from presubmit import CheckExternalReferenceRegistration
72 from presubmit import CheckAuthorizedAuthor
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"))
81 if not CheckRuntimeVsNativesNameClashes(input_api.PresubmitLocalPath()):
82 results.append(output_api.PresubmitError(
83 "Runtime/natives name clash check failed"))
84 if not CheckExternalReferenceRegistration(input_api.PresubmitLocalPath()):
85 results.append(output_api.PresubmitError(
86 "External references registration check failed"))
87 results.extend(CheckAuthorizedAuthor(input_api, output_api))
91 def _CheckUnwantedDependencies(input_api, output_api):
92 """Runs checkdeps on #include statements added in this
93 change. Breaking - rules is an error, breaking ! rules is a
96 # We need to wait until we have an input_api object and use this
97 # roundabout construct to import checkdeps because this file is
98 # eval-ed and thus doesn't have __file__.
99 original_sys_path = sys.path
101 sys.path = sys.path + [input_api.os_path.join(
102 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
104 from cpp_checker import CppChecker
105 from rules import Rule
107 # Restore sys.path to what it was before.
108 sys.path = original_sys_path
111 for f in input_api.AffectedFiles():
112 if not CppChecker.IsCppFile(f.LocalPath()):
115 changed_lines = [line for line_num, line in f.ChangedContents()]
116 added_includes.append([f.LocalPath(), changed_lines])
118 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
120 error_descriptions = []
121 warning_descriptions = []
122 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
124 description_with_path = '%s\n %s' % (path, rule_description)
125 if rule_type == Rule.DISALLOW:
126 error_descriptions.append(description_with_path)
128 warning_descriptions.append(description_with_path)
131 if error_descriptions:
132 results.append(output_api.PresubmitError(
133 'You added one or more #includes that violate checkdeps rules.',
135 if warning_descriptions:
136 results.append(output_api.PresubmitPromptOrNotify(
137 'You added one or more #includes of files that are temporarily\n'
138 'allowed but being removed. Can you avoid introducing the\n'
139 '#include? See relevant DEPS file(s) for details and contacts.',
140 warning_descriptions))
144 def _CheckNoInlineHeaderIncludesInNormalHeaders(input_api, output_api):
145 """Attempts to prevent inclusion of inline headers into normal header
146 files. This tries to establish a layering where inline headers can be
147 included by other inline headers or compilation units only."""
148 file_inclusion_pattern = r'(?!.+-inl\.h).+\.h'
149 include_directive_pattern = input_api.re.compile(r'#include ".+-inl.h"')
151 'You might be including an inline header (e.g. foo-inl.h) within a\n'
152 'normal header (e.g. bar.h) file. Can you avoid introducing the\n'
153 '#include? The commit queue will not block on this warning.')
155 def FilterFile(affected_file):
156 black_list = (_EXCLUDED_PATHS +
157 input_api.DEFAULT_BLACK_LIST)
158 return input_api.FilterSourceFile(
160 white_list=(file_inclusion_pattern, ),
161 black_list=black_list)
164 for f in input_api.AffectedSourceFiles(FilterFile):
165 local_path = f.LocalPath()
166 for line_number, line in f.ChangedContents():
167 if (include_directive_pattern.search(line)):
169 '%s:%d\n %s' % (local_path, line_number, line.strip()))
172 return [output_api.PresubmitPromptOrNotify(include_warning, problems)]
177 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
178 """Attempts to prevent use of functions intended only for testing in
179 non-testing code. For now this is just a best-effort implementation
180 that ignores header files and may have some false positives. A
181 better implementation would probably need a proper C++ parser.
183 # We only scan .cc files, as the declaration of for-testing functions in
184 # header files are hard to distinguish from calls to such functions without a
186 file_inclusion_pattern = r'.+\.cc'
188 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
189 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
190 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
191 exclusion_pattern = input_api.re.compile(
192 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
193 base_function_pattern, base_function_pattern))
195 def FilterFile(affected_file):
196 black_list = (_EXCLUDED_PATHS +
197 _TEST_CODE_EXCLUDED_PATHS +
198 input_api.DEFAULT_BLACK_LIST)
199 return input_api.FilterSourceFile(
201 white_list=(file_inclusion_pattern, ),
202 black_list=black_list)
205 for f in input_api.AffectedSourceFiles(FilterFile):
206 local_path = f.LocalPath()
207 for line_number, line in f.ChangedContents():
208 if (inclusion_pattern.search(line) and
209 not comment_pattern.search(line) and
210 not exclusion_pattern.search(line)):
212 '%s:%d\n %s' % (local_path, line_number, line.strip()))
215 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
220 def _CommonChecks(input_api, output_api):
221 """Checks common to both upload and commit."""
223 results.extend(input_api.canned_checks.CheckOwners(
224 input_api, output_api, source_file_filter=None))
225 results.extend(input_api.canned_checks.CheckPatchFormatted(
226 input_api, output_api))
227 results.extend(_V8PresubmitChecks(input_api, output_api))
228 results.extend(_CheckUnwantedDependencies(input_api, output_api))
230 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
232 _CheckNoInlineHeaderIncludesInNormalHeaders(input_api, output_api))
236 def _SkipTreeCheck(input_api, output_api):
237 """Check the env var whether we want to skip tree check.
238 Only skip if include/v8-version.h has been updated."""
239 src_version = 'include/v8-version.h'
240 FilterFile = lambda file: file.LocalPath() == src_version
241 if not input_api.AffectedSourceFiles(
242 lambda file: file.LocalPath() == src_version):
244 return input_api.environ.get('PRESUBMIT_TREE_CHECK') == 'skip'
247 def _CheckChangeLogFlag(input_api, output_api, warn):
248 """Checks usage of LOG= flag in the commit message."""
250 if (input_api.change.BUG and input_api.change.BUG != 'none' and
251 not 'LOG' in input_api.change.tags):
252 text = ('An issue reference (BUG=) requires a change log flag (LOG=). '
253 'Use LOG=Y for including this commit message in the change log. '
254 'Use LOG=N or leave blank otherwise.')
256 results.append(output_api.PresubmitPromptWarning(text))
258 results.append(output_api.PresubmitError(text))
262 def CheckChangeOnUpload(input_api, output_api):
264 results.extend(_CommonChecks(input_api, output_api))
265 results.extend(_CheckChangeLogFlag(input_api, output_api, True))
269 def CheckChangeOnCommit(input_api, output_api):
271 results.extend(_CommonChecks(input_api, output_api))
272 results.extend(_CheckChangeLogFlag(input_api, output_api, False))
273 results.extend(input_api.canned_checks.CheckChangeHasDescription(
274 input_api, output_api))
275 if not _SkipTreeCheck(input_api, output_api):
276 results.extend(input_api.canned_checks.CheckTreeIsOpen(
277 input_api, output_api,
278 json_url='http://v8-status.appspot.com/current?format=json'))
282 def GetPreferredTryMasters(project, change):
285 'v8_linux_rel': set(['defaulttests']),
286 'v8_linux_dbg': set(['defaulttests']),
287 'v8_linux_nodcheck_rel': set(['defaulttests']),
288 'v8_linux_gcc_compile_rel': set(['defaulttests']),
289 'v8_linux64_rel': set(['defaulttests']),
290 'v8_linux64_asan_rel': set(['defaulttests']),
291 'v8_linux64_avx2_rel': set(['defaulttests']),
292 'v8_win_rel': set(['defaulttests']),
293 'v8_win_compile_dbg': set(['defaulttests']),
294 'v8_win_nosnap_shared_compile_rel': set(['defaulttests']),
295 'v8_win64_rel': set(['defaulttests']),
296 'v8_mac_rel': set(['defaulttests']),
297 'v8_linux_arm_rel': set(['defaulttests']),
298 'v8_linux_arm64_rel': set(['defaulttests']),
299 'v8_linux_mipsel_compile_rel': set(['defaulttests']),
300 'v8_linux_mips64el_compile_rel': set(['defaulttests']),
301 'v8_android_arm_compile_rel': set(['defaulttests']),
302 'v8_linux_chromium_gn_rel': set(['defaulttests']),