PRESUBMIT.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # Copyright 2013 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. """Top-level presubmit script for Chromium media component.
  5. See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
  6. for more details about the presubmit API built into depot_tools.
  7. """
  8. import re
  9. # This line is 'magic' in that git-cl looks for it to decide whether to
  10. # use Python3 instead of Python2 when running the code in this file.
  11. USE_PYTHON3 = True
  12. # Well-defined simple classes containing only <= 4 ints, or <= 2 floats.
  13. BASE_TIME_TYPES = [
  14. 'base::Time',
  15. 'base::TimeDelta',
  16. 'base::TimeTicks',
  17. ]
  18. BASE_TIME_TYPES_RE = re.compile(r'\bconst (%s)&' % '|'.join(BASE_TIME_TYPES))
  19. def _FilterFile(affected_file):
  20. """Return true if the file could contain code requiring a presubmit check."""
  21. return affected_file.LocalPath().endswith(
  22. ('.h', '.cc', '.cpp', '.cxx', '.mm'))
  23. def _CheckForUseOfWrongClock(input_api, output_api):
  24. """Make sure new lines of media code don't use a clock susceptible to skew."""
  25. # Regular expression that should detect any explicit references to the
  26. # base::Time type (or base::Clock/DefaultClock), whether in using decls,
  27. # typedefs, or to call static methods.
  28. base_time_type_pattern = r'(^|\W)base::(Time|Clock|DefaultClock)(\W|$)'
  29. # Regular expression that should detect references to the base::Time class
  30. # members, such as a call to base::Time::Now.
  31. base_time_member_pattern = r'(^|\W)(Time|Clock|DefaultClock)::'
  32. # Regular expression to detect "using base::Time" declarations. We want to
  33. # prevent these from triggerring a warning. For example, it's perfectly
  34. # reasonable for code to be written like this:
  35. #
  36. # using base::Time;
  37. # ...
  38. # int64_t foo_us = foo_s * Time::kMicrosecondsPerSecond;
  39. using_base_time_decl_pattern = r'^\s*using\s+(::)?base::Time\s*;'
  40. # Regular expression to detect references to the kXXX constants in the
  41. # base::Time class. We want to prevent these from triggerring a warning.
  42. base_time_konstant_pattern = r'(^|\W)Time::k\w+'
  43. # Regular expression to detect usage of openscreen clock types, which are
  44. # allowed depending on DEPS rules.
  45. openscreen_time_pattern = r'(^|\W)openscreen::Clock\s*'
  46. problem_re = input_api.re.compile(
  47. r'(' + base_time_type_pattern + r')|(' + base_time_member_pattern + r')')
  48. exception_re = input_api.re.compile(
  49. r'(' + using_base_time_decl_pattern + r')|(' +
  50. base_time_konstant_pattern + r')|(' +
  51. openscreen_time_pattern + r')')
  52. problems = []
  53. for f in input_api.AffectedSourceFiles(_FilterFile):
  54. for line_number, line in f.ChangedContents():
  55. if problem_re.search(line):
  56. if not exception_re.search(line):
  57. problems.append(
  58. ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip()))
  59. if problems:
  60. return [output_api.PresubmitPromptOrNotify(
  61. 'You added one or more references to the base::Time class and/or one\n'
  62. 'of its member functions (or base::Clock/DefaultClock). In media\n'
  63. 'code, it is rarely correct to use a clock susceptible to time skew!\n'
  64. 'Instead, could you use base::TimeTicks to track the passage of\n'
  65. 'real-world time?\n\n' +
  66. '\n'.join(problems))]
  67. else:
  68. return []
  69. def _CheckForHistogramOffByOne(input_api, output_api):
  70. """Make sure histogram enum maxes are used properly"""
  71. # A general-purpose chunk of regex to match whitespace and/or comments
  72. # that may be interspersed with the code we're interested in:
  73. comment = r'/\*.*?\*/|//[^\n]*'
  74. whitespace = r'(?:[\n\t ]|(?:' + comment + r'))*'
  75. # The name is assumed to be a literal string.
  76. histogram_name = r'"[^"]*"'
  77. # This can be an arbitrary expression, so just ensure it isn't a ; to prevent
  78. # matching past the end of this statement.
  79. histogram_value = r'[^;]*'
  80. # In parens so we can retrieve it for further checks.
  81. histogram_max = r'([^;,]*)'
  82. # This should match a uma histogram enumeration macro expression.
  83. uma_macro_re = input_api.re.compile(
  84. r'\bUMA_HISTOGRAM_ENUMERATION\(' + whitespace + histogram_name + r',' +
  85. whitespace + histogram_value + r',' + whitespace + histogram_max +
  86. whitespace + r'\)' + whitespace + r';(?:' + whitespace +
  87. r'\/\/ (PRESUBMIT_IGNORE_UMA_MAX))?')
  88. uma_max_re = input_api.re.compile(r'.*(?:Max|MAX).* \+ 1')
  89. problems = []
  90. for f in input_api.AffectedSourceFiles(_FilterFile):
  91. contents = input_api.ReadFile(f)
  92. # We want to match across lines, but still report a line number, so we keep
  93. # track of the line we're on as we search through the file.
  94. line_number = 1
  95. # We search the entire file, then check if any violations are in the changed
  96. # areas, this is inefficient, but simple. A UMA_HISTOGRAM_ENUMERATION call
  97. # will often span multiple lines, so finding a match looking just at the
  98. # deltas line-by-line won't catch problems.
  99. match = uma_macro_re.search(contents)
  100. while match:
  101. line_number += contents.count('\n', 0, match.start())
  102. max_arg = match.group(1) # The third argument.
  103. if (not uma_max_re.match(max_arg) and match.group(2) !=
  104. 'PRESUBMIT_IGNORE_UMA_MAX'):
  105. uma_range = range(match.start(), match.end() + 1)
  106. # Check if any part of the match is in the changed lines:
  107. for num, line in f.ChangedContents():
  108. if line_number <= num <= line_number + match.group().count('\n'):
  109. problems.append('%s:%d' % (f, line_number))
  110. break
  111. # Strip off the file contents up to the end of the match and update the
  112. # line number.
  113. contents = contents[match.end():]
  114. line_number += match.group().count('\n')
  115. match = uma_macro_re.search(contents)
  116. if problems:
  117. return [output_api.PresubmitError(
  118. 'UMA_HISTOGRAM_ENUMERATION reports in src/media/ are expected to adhere\n'
  119. 'to the following guidelines:\n'
  120. ' - The max value (3rd argument) should be an enum value equal to the\n'
  121. ' last valid value, e.g. FOO_MAX = LAST_VALID_FOO.\n'
  122. ' - 1 must be added to that max value.\n'
  123. 'Contact dalecurtis@chromium.org if you have questions.' , problems)]
  124. return []
  125. def _CheckPassByValue(input_api, output_api):
  126. """Check that base::Time and derived classes are passed by value, and not by
  127. const reference """
  128. problems = []
  129. for f in input_api.AffectedSourceFiles(_FilterFile):
  130. for line_number, line in f.ChangedContents():
  131. if BASE_TIME_TYPES_RE.search(line):
  132. problems.append('%s:%d' % (f, line_number))
  133. if problems:
  134. return [output_api.PresubmitError(
  135. 'base::Time and derived classes should be passed by value and not by\n'
  136. 'const ref, see base/time/time.h for more information.', problems)]
  137. return []
  138. def _CheckForUseOfLazyInstance(input_api, output_api):
  139. """Check that base::LazyInstance is not used."""
  140. problems = []
  141. lazy_instance_re = re.compile(r'(^|\W)base::LazyInstance<')
  142. for f in input_api.AffectedSourceFiles(_FilterFile):
  143. for line_number, line in f.ChangedContents():
  144. if lazy_instance_re.search(line):
  145. problems.append('%s:%d' % (f, line_number))
  146. if problems:
  147. return [output_api.PresubmitError(
  148. 'base::LazyInstance is deprecated; use a thread safe static.', problems)]
  149. return []
  150. def _CheckNoLoggingOverrideInHeaders(input_api, output_api):
  151. """Checks to make sure no .h files include logging_override_if_enabled.h."""
  152. files = []
  153. pattern = input_api.re.compile(
  154. r'^#include\s*"media/base/logging_override_if_enabled.h"',
  155. input_api.re.MULTILINE)
  156. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  157. if not f.LocalPath().endswith('.h'):
  158. continue
  159. contents = input_api.ReadFile(f)
  160. if pattern.search(contents):
  161. files.append(f)
  162. if len(files):
  163. return [output_api.PresubmitError(
  164. 'Do not #include "logging_override_if_enabled.h" in header files, '
  165. 'since it overrides DVLOG() in every file including the header. '
  166. 'Instead, only include it in source files.',
  167. files) ]
  168. return []
  169. def _CheckForNoV4L2AggregateInitialization(input_api, output_api):
  170. """Check that struct v4l2_* are not initialized as aggregates with a
  171. braced-init-list"""
  172. problems = []
  173. v4l2_aggregate_initializer_re = re.compile(r'(^|\W)struct.+v4l2_.+=.+{+}+;')
  174. for f in input_api.AffectedSourceFiles(_FilterFile):
  175. for line_number, line in f.ChangedContents():
  176. if v4l2_aggregate_initializer_re.search(line):
  177. problems.append('%s:%d' % (f, line_number))
  178. if problems:
  179. return [output_api.PresubmitPromptWarning(
  180. 'Avoid initializing V4L2 structures with braced-init-lists, i.e. as '
  181. 'aggregates. V4L2 structs often contain unions of various sized members: '
  182. 'when a union is initialized by aggregate initialization, only the first '
  183. 'non-static member is initialized, leaving other members unitialized if '
  184. 'they are larger. Use memset instead.',
  185. problems)]
  186. return []
  187. def _CheckChange(input_api, output_api):
  188. results = []
  189. results.extend(_CheckForUseOfWrongClock(input_api, output_api))
  190. results.extend(_CheckPassByValue(input_api, output_api))
  191. results.extend(_CheckForHistogramOffByOne(input_api, output_api))
  192. results.extend(_CheckForUseOfLazyInstance(input_api, output_api))
  193. results.extend(_CheckNoLoggingOverrideInHeaders(input_api, output_api))
  194. results.extend(_CheckForNoV4L2AggregateInitialization(input_api, output_api))
  195. return results
  196. def CheckChangeOnUpload(input_api, output_api):
  197. return _CheckChange(input_api, output_api)
  198. def CheckChangeOnCommit(input_api, output_api):
  199. return _CheckChange(input_api, output_api)