PRESUBMIT.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright (c) 2012 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. """Chromium presubmit script for src/base.
  5. See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
  6. for more details on the presubmit API built into depot_tools.
  7. """
  8. USE_PYTHON3 = True
  9. def CheckChangeLintsClean(input_api, output_api):
  10. """Makes sure that the code is cpplint clean."""
  11. # lint_filters=[] stops the OFF_BY_DEFAULT_LINT_FILTERS from being disabled,
  12. # finding many more issues. verbose_level=1 finds a small number of additional
  13. # issues.
  14. # The only valid extensions for cpplint are .cc, .h, .cpp, .cu, and .ch.
  15. # Only process those extensions which are used in Chromium, in directories
  16. # that currently lint clean.
  17. CLEAN_CPP_FILES_ONLY = (r'base[\\/]win[\\/].*\.(cc|h)$', )
  18. source_file_filter = lambda x: input_api.FilterSourceFile(
  19. x,
  20. files_to_check=CLEAN_CPP_FILES_ONLY,
  21. files_to_skip=input_api.DEFAULT_FILES_TO_SKIP)
  22. return input_api.canned_checks.CheckChangeLintsClean(
  23. input_api, output_api, source_file_filter=source_file_filter,
  24. lint_filters=[], verbose_level=1)
  25. def _CheckNoInterfacesInBase(input_api, output_api):
  26. """Checks to make sure no files in libbase.a have |@interface|."""
  27. pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE)
  28. files = []
  29. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  30. if (f.LocalPath().startswith('base/') and
  31. not "/ios/" in f.LocalPath() and
  32. not "/test/" in f.LocalPath() and
  33. not f.LocalPath().endswith('.java') and
  34. not f.LocalPath().endswith('_unittest.mm') and
  35. not f.LocalPath().endswith('mac/sdk_forward_declarations.h')):
  36. contents = input_api.ReadFile(f)
  37. if pattern.search(contents):
  38. files.append(f)
  39. if len(files):
  40. return [ output_api.PresubmitError(
  41. 'Objective-C interfaces or categories are forbidden in libbase. ' +
  42. 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' +
  43. 'browse_thread/thread/efb28c10435987fd',
  44. files) ]
  45. return []
  46. def _FindLocations(input_api, search_regexes, files_to_check, files_to_skip):
  47. """Returns locations matching one of the search_regexes."""
  48. def FilterFile(affected_file):
  49. return input_api.FilterSourceFile(
  50. affected_file,
  51. files_to_check=files_to_check,
  52. files_to_skip=files_to_skip)
  53. no_presubmit = r"// no-presubmit-check"
  54. locations = []
  55. for f in input_api.AffectedSourceFiles(FilterFile):
  56. for line_num, line in f.ChangedContents():
  57. for search_regex in search_regexes:
  58. if (input_api.re.search(search_regex, line) and
  59. not input_api.re.search(no_presubmit, line)):
  60. locations.append(" %s:%d" % (f.LocalPath(), line_num))
  61. break
  62. return locations
  63. def _CheckNoTraceEventInclude(input_api, output_api):
  64. """Verify that //base includes base_tracing.h instead of trace event headers.
  65. Checks that files outside trace event implementation include the
  66. base_tracing.h header instead of specific trace event implementation headers
  67. to maintain compatibility with the gn flag "enable_base_tracing = false".
  68. """
  69. discouraged_includes = [
  70. r'^#include "base/trace_event/(?!base_tracing\.h|base_tracing_forward\.h)',
  71. r'^#include "third_party/perfetto/include/',
  72. ]
  73. files_to_check = [
  74. r".*\.(h|cc|mm)$",
  75. ]
  76. files_to_skip = [
  77. r".*[\\/]test[\\/].*",
  78. r".*[\\/]trace_event[\\/].*",
  79. r".*[\\/]tracing[\\/].*",
  80. ]
  81. locations = _FindLocations(input_api, discouraged_includes, files_to_check,
  82. files_to_skip)
  83. if locations:
  84. return [ output_api.PresubmitError(
  85. 'Base code should include "base/trace_event/base_tracing.h" instead\n' +
  86. 'of trace_event implementation headers. If you need to include an\n' +
  87. 'implementation header, verify that "gn check" and base_unittests\n' +
  88. 'still pass with gn arg "enable_base_tracing = false" and add\n' +
  89. '"// no-presubmit-check" after the include. \n' +
  90. '\n'.join(locations)) ]
  91. return []
  92. def _WarnPbzeroIncludes(input_api, output_api):
  93. """Warn to check enable_base_tracing=false when including a pbzero header.
  94. Emits a warning when including a perfetto pbzero header, encouraging the
  95. user to verify that //base still builds with enable_base_tracing=false.
  96. """
  97. warn_includes = [
  98. r'^#include "third_party/perfetto/protos/',
  99. r'^#include "base/tracing/protos/',
  100. ]
  101. files_to_check = [
  102. r".*\.(h|cc|mm)$",
  103. ]
  104. files_to_skip = [
  105. r".*[\\/]test[\\/].*",
  106. r".*[\\/]trace_event[\\/].*",
  107. r".*[\\/]tracing[\\/].*",
  108. ]
  109. locations = _FindLocations(input_api, warn_includes, files_to_check,
  110. files_to_skip)
  111. if locations:
  112. return [ output_api.PresubmitPromptWarning(
  113. 'Please verify that "gn check" and base_unittests still pass with\n' +
  114. 'gn arg "enable_base_tracing = false" when adding typed trace\n' +
  115. 'events to //base. You can use "#if BUILDFLAG(ENABLE_BASE_TRACING)"\n' +
  116. 'to exclude pbzero headers and anything not supported by\n' +
  117. '//base/trace_event/trace_event_stub.h.\n' +
  118. '\n'.join(locations)) ]
  119. return []
  120. def _CommonChecks(input_api, output_api):
  121. """Checks common to both upload and commit."""
  122. results = []
  123. results.extend(_CheckNoInterfacesInBase(input_api, output_api))
  124. results.extend(_CheckNoTraceEventInclude(input_api, output_api))
  125. results.extend(_WarnPbzeroIncludes(input_api, output_api))
  126. results.extend(CheckChangeLintsClean(input_api, output_api))
  127. return results
  128. def CheckChangeOnUpload(input_api, output_api):
  129. results = []
  130. results.extend(_CommonChecks(input_api, output_api))
  131. return results
  132. def CheckChangeOnCommit(input_api, output_api):
  133. results = []
  134. results.extend(_CommonChecks(input_api, output_api))
  135. return results