deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / PRESUBMIT.py
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
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
30 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
31 for more details about the presubmit API built into gcl.
32 """
33
34 import 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
63 def _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
70   from presubmit import CheckRuntimeVsNativesNameClashes
71   from presubmit import CheckExternalReferenceRegistration
72   from presubmit import CheckAuthorizedAuthor
73
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"))
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))
88   return results
89
90
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
94   warning.
95   """
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
100   try:
101     sys.path = sys.path + [input_api.os_path.join(
102         input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
103     import checkdeps
104     from cpp_checker import CppChecker
105     from rules import Rule
106   finally:
107     # Restore sys.path to what it was before.
108     sys.path = original_sys_path
109
110   added_includes = []
111   for f in input_api.AffectedFiles():
112     if not CppChecker.IsCppFile(f.LocalPath()):
113       continue
114
115     changed_lines = [line for line_num, line in f.ChangedContents()]
116     added_includes.append([f.LocalPath(), changed_lines])
117
118   deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
119
120   error_descriptions = []
121   warning_descriptions = []
122   for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
123       added_includes):
124     description_with_path = '%s\n    %s' % (path, rule_description)
125     if rule_type == Rule.DISALLOW:
126       error_descriptions.append(description_with_path)
127     else:
128       warning_descriptions.append(description_with_path)
129
130   results = []
131   if error_descriptions:
132     results.append(output_api.PresubmitError(
133         'You added one or more #includes that violate checkdeps rules.',
134         error_descriptions))
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))
141   return results
142
143
144 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
145   """Attempts to prevent use of functions intended only for testing in
146   non-testing code. For now this is just a best-effort implementation
147   that ignores header files and may have some false positives. A
148   better implementation would probably need a proper C++ parser.
149   """
150   # We only scan .cc files, as the declaration of for-testing functions in
151   # header files are hard to distinguish from calls to such functions without a
152   # proper C++ parser.
153   file_inclusion_pattern = r'.+\.cc'
154
155   base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
156   inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
157   comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
158   exclusion_pattern = input_api.re.compile(
159     r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
160       base_function_pattern, base_function_pattern))
161
162   def FilterFile(affected_file):
163     black_list = (_EXCLUDED_PATHS +
164                   _TEST_CODE_EXCLUDED_PATHS +
165                   input_api.DEFAULT_BLACK_LIST)
166     return input_api.FilterSourceFile(
167       affected_file,
168       white_list=(file_inclusion_pattern, ),
169       black_list=black_list)
170
171   problems = []
172   for f in input_api.AffectedSourceFiles(FilterFile):
173     local_path = f.LocalPath()
174     for line_number, line in f.ChangedContents():
175       if (inclusion_pattern.search(line) and
176           not comment_pattern.search(line) and
177           not exclusion_pattern.search(line)):
178         problems.append(
179           '%s:%d\n    %s' % (local_path, line_number, line.strip()))
180
181   if problems:
182     return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
183   else:
184     return []
185
186
187 def _CommonChecks(input_api, output_api):
188   """Checks common to both upload and commit."""
189   results = []
190   results.extend(input_api.canned_checks.CheckOwners(
191       input_api, output_api, source_file_filter=None))
192   results.extend(input_api.canned_checks.CheckPatchFormatted(
193       input_api, output_api))
194   results.extend(_V8PresubmitChecks(input_api, output_api))
195   results.extend(_CheckUnwantedDependencies(input_api, output_api))
196   results.extend(
197       _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
198   return results
199
200
201 def _SkipTreeCheck(input_api, output_api):
202   """Check the env var whether we want to skip tree check.
203      Only skip if include/v8-version.h has been updated."""
204   src_version = 'include/v8-version.h'
205   FilterFile = lambda file: file.LocalPath() == src_version
206   if not input_api.AffectedSourceFiles(
207       lambda file: file.LocalPath() == src_version):
208     return False
209   return input_api.environ.get('PRESUBMIT_TREE_CHECK') == 'skip'
210
211
212 def _CheckChangeLogFlag(input_api, output_api):
213   """Checks usage of LOG= flag in the commit message."""
214   results = []
215   if input_api.change.BUG and not 'LOG' in input_api.change.tags:
216     results.append(output_api.PresubmitError(
217         'An issue reference (BUG=) requires a change log flag (LOG=). '
218         'Use LOG=Y for including this commit message in the change log. '
219         'Use LOG=N or leave blank otherwise.'))
220   return results
221
222
223 def CheckChangeOnUpload(input_api, output_api):
224   results = []
225   results.extend(_CommonChecks(input_api, output_api))
226   results.extend(_CheckChangeLogFlag(input_api, output_api))
227   return results
228
229
230 def CheckChangeOnCommit(input_api, output_api):
231   results = []
232   results.extend(_CommonChecks(input_api, output_api))
233   results.extend(_CheckChangeLogFlag(input_api, output_api))
234   results.extend(input_api.canned_checks.CheckChangeHasDescription(
235       input_api, output_api))
236   if not _SkipTreeCheck(input_api, output_api):
237     results.extend(input_api.canned_checks.CheckTreeIsOpen(
238         input_api, output_api,
239         json_url='http://v8-status.appspot.com/current?format=json'))
240   return results
241
242
243 def GetPreferredTryMasters(project, change):
244   return {
245     'tryserver.v8': {
246       'v8_linux_rel': set(['defaulttests']),
247       'v8_linux_dbg': set(['defaulttests']),
248       'v8_linux_nodcheck_rel': set(['defaulttests']),
249       'v8_linux_gcc_compile_rel': set(['defaulttests']),
250       'v8_linux64_rel': set(['defaulttests']),
251       'v8_linux64_asan_rel': set(['defaulttests']),
252       'v8_win_rel': set(['defaulttests']),
253       'v8_win_compile_dbg': set(['defaulttests']),
254       'v8_win64_rel': set(['defaulttests']),
255       'v8_mac_rel': set(['defaulttests']),
256       'v8_linux_arm_rel': set(['defaulttests']),
257       'v8_linux_arm64_rel': set(['defaulttests']),
258       'v8_android_arm_compile_rel': set(['defaulttests']),
259       'v8_linux_chromium_gn_rel': set(['defaulttests']),
260     },
261   }