PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/v8/PRESUBMIT.py

https://gitlab.com/CORP-RESELLER/node
Python | 277 lines | 179 code | 39 blank | 59 comment | 30 complexity | 4f9930cfd27d4843d6a1191c1ddeff14 MD5 | raw file
  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. """Top-level presubmit script for V8.
  28. See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
  29. for more details about the presubmit API built into gcl.
  30. """
  31. import sys
  32. _EXCLUDED_PATHS = (
  33. r"^test[\\\/].*",
  34. r"^testing[\\\/].*",
  35. r"^third_party[\\\/].*",
  36. r"^tools[\\\/].*",
  37. )
  38. # Regular expression that matches code only used for test binaries
  39. # (best effort).
  40. _TEST_CODE_EXCLUDED_PATHS = (
  41. r'.+-unittest\.cc',
  42. # Has a method VisitForTest().
  43. r'src[\\\/]compiler[\\\/]ast-graph-builder\.cc',
  44. # Test extension.
  45. r'src[\\\/]extensions[\\\/]gc-extension\.cc',
  46. )
  47. _TEST_ONLY_WARNING = (
  48. 'You might be calling functions intended only for testing from\n'
  49. 'production code. It is OK to ignore this warning if you know what\n'
  50. 'you are doing, as the heuristics used to detect the situation are\n'
  51. 'not perfect. The commit queue will not block on this warning.')
  52. def _V8PresubmitChecks(input_api, output_api):
  53. """Runs the V8 presubmit checks."""
  54. import sys
  55. sys.path.append(input_api.os_path.join(
  56. input_api.PresubmitLocalPath(), 'tools'))
  57. from presubmit import CppLintProcessor
  58. from presubmit import SourceProcessor
  59. from presubmit import CheckExternalReferenceRegistration
  60. from presubmit import CheckAuthorizedAuthor
  61. from presubmit import CheckStatusFiles
  62. results = []
  63. if not CppLintProcessor().Run(input_api.PresubmitLocalPath()):
  64. results.append(output_api.PresubmitError("C++ lint check failed"))
  65. if not SourceProcessor().Run(input_api.PresubmitLocalPath()):
  66. results.append(output_api.PresubmitError(
  67. "Copyright header, trailing whitespaces and two empty lines " \
  68. "between declarations check failed"))
  69. if not CheckExternalReferenceRegistration(input_api.PresubmitLocalPath()):
  70. results.append(output_api.PresubmitError(
  71. "External references registration check failed"))
  72. if not CheckStatusFiles(input_api.PresubmitLocalPath()):
  73. results.append(output_api.PresubmitError("Status file check failed"))
  74. results.extend(CheckAuthorizedAuthor(input_api, output_api))
  75. return results
  76. def _CheckUnwantedDependencies(input_api, output_api):
  77. """Runs checkdeps on #include statements added in this
  78. change. Breaking - rules is an error, breaking ! rules is a
  79. warning.
  80. """
  81. # We need to wait until we have an input_api object and use this
  82. # roundabout construct to import checkdeps because this file is
  83. # eval-ed and thus doesn't have __file__.
  84. original_sys_path = sys.path
  85. try:
  86. sys.path = sys.path + [input_api.os_path.join(
  87. input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
  88. import checkdeps
  89. from cpp_checker import CppChecker
  90. from rules import Rule
  91. finally:
  92. # Restore sys.path to what it was before.
  93. sys.path = original_sys_path
  94. added_includes = []
  95. for f in input_api.AffectedFiles():
  96. if not CppChecker.IsCppFile(f.LocalPath()):
  97. continue
  98. changed_lines = [line for line_num, line in f.ChangedContents()]
  99. added_includes.append([f.LocalPath(), changed_lines])
  100. deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
  101. error_descriptions = []
  102. warning_descriptions = []
  103. for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
  104. added_includes):
  105. description_with_path = '%s\n %s' % (path, rule_description)
  106. if rule_type == Rule.DISALLOW:
  107. error_descriptions.append(description_with_path)
  108. else:
  109. warning_descriptions.append(description_with_path)
  110. results = []
  111. if error_descriptions:
  112. results.append(output_api.PresubmitError(
  113. 'You added one or more #includes that violate checkdeps rules.',
  114. error_descriptions))
  115. if warning_descriptions:
  116. results.append(output_api.PresubmitPromptOrNotify(
  117. 'You added one or more #includes of files that are temporarily\n'
  118. 'allowed but being removed. Can you avoid introducing the\n'
  119. '#include? See relevant DEPS file(s) for details and contacts.',
  120. warning_descriptions))
  121. return results
  122. def _CheckNoInlineHeaderIncludesInNormalHeaders(input_api, output_api):
  123. """Attempts to prevent inclusion of inline headers into normal header
  124. files. This tries to establish a layering where inline headers can be
  125. included by other inline headers or compilation units only."""
  126. file_inclusion_pattern = r'(?!.+-inl\.h).+\.h'
  127. include_directive_pattern = input_api.re.compile(r'#include ".+-inl.h"')
  128. include_warning = (
  129. 'You might be including an inline header (e.g. foo-inl.h) within a\n'
  130. 'normal header (e.g. bar.h) file. Can you avoid introducing the\n'
  131. '#include? The commit queue will not block on this warning.')
  132. def FilterFile(affected_file):
  133. black_list = (_EXCLUDED_PATHS +
  134. input_api.DEFAULT_BLACK_LIST)
  135. return input_api.FilterSourceFile(
  136. affected_file,
  137. white_list=(file_inclusion_pattern, ),
  138. black_list=black_list)
  139. problems = []
  140. for f in input_api.AffectedSourceFiles(FilterFile):
  141. local_path = f.LocalPath()
  142. for line_number, line in f.ChangedContents():
  143. if (include_directive_pattern.search(line)):
  144. problems.append(
  145. '%s:%d\n %s' % (local_path, line_number, line.strip()))
  146. if problems:
  147. return [output_api.PresubmitPromptOrNotify(include_warning, problems)]
  148. else:
  149. return []
  150. def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
  151. """Attempts to prevent use of functions intended only for testing in
  152. non-testing code. For now this is just a best-effort implementation
  153. that ignores header files and may have some false positives. A
  154. better implementation would probably need a proper C++ parser.
  155. """
  156. # We only scan .cc files, as the declaration of for-testing functions in
  157. # header files are hard to distinguish from calls to such functions without a
  158. # proper C++ parser.
  159. file_inclusion_pattern = r'.+\.cc'
  160. base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
  161. inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
  162. comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
  163. exclusion_pattern = input_api.re.compile(
  164. r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
  165. base_function_pattern, base_function_pattern))
  166. def FilterFile(affected_file):
  167. black_list = (_EXCLUDED_PATHS +
  168. _TEST_CODE_EXCLUDED_PATHS +
  169. input_api.DEFAULT_BLACK_LIST)
  170. return input_api.FilterSourceFile(
  171. affected_file,
  172. white_list=(file_inclusion_pattern, ),
  173. black_list=black_list)
  174. problems = []
  175. for f in input_api.AffectedSourceFiles(FilterFile):
  176. local_path = f.LocalPath()
  177. for line_number, line in f.ChangedContents():
  178. if (inclusion_pattern.search(line) and
  179. not comment_pattern.search(line) and
  180. not exclusion_pattern.search(line)):
  181. problems.append(
  182. '%s:%d\n %s' % (local_path, line_number, line.strip()))
  183. if problems:
  184. return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
  185. else:
  186. return []
  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. results.extend(
  199. _CheckNoInlineHeaderIncludesInNormalHeaders(input_api, output_api))
  200. return results
  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. if not input_api.AffectedSourceFiles(
  206. lambda file: file.LocalPath() == src_version):
  207. return False
  208. return input_api.environ.get('PRESUBMIT_TREE_CHECK') == 'skip'
  209. def _CheckChangeLogFlag(input_api, output_api, warn):
  210. """Checks usage of LOG= flag in the commit message."""
  211. results = []
  212. if (input_api.change.BUG and input_api.change.BUG != 'none' and
  213. not 'LOG' in input_api.change.tags):
  214. text = ('An issue reference (BUG=) requires a change log flag (LOG=). '
  215. 'Use LOG=Y for including this commit message in the change log. '
  216. 'Use LOG=N or leave blank otherwise.')
  217. if warn:
  218. results.append(output_api.PresubmitPromptWarning(text))
  219. else:
  220. results.append(output_api.PresubmitError(text))
  221. return results
  222. def CheckChangeOnUpload(input_api, output_api):
  223. results = []
  224. results.extend(_CommonChecks(input_api, output_api))
  225. results.extend(_CheckChangeLogFlag(input_api, output_api, True))
  226. return results
  227. def CheckChangeOnCommit(input_api, output_api):
  228. results = []
  229. results.extend(_CommonChecks(input_api, output_api))
  230. results.extend(_CheckChangeLogFlag(input_api, output_api, False))
  231. results.extend(input_api.canned_checks.CheckChangeHasDescription(
  232. input_api, output_api))
  233. if not _SkipTreeCheck(input_api, output_api):
  234. results.extend(input_api.canned_checks.CheckTreeIsOpen(
  235. input_api, output_api,
  236. json_url='http://v8-status.appspot.com/current?format=json'))
  237. return results