PRESUBMIT.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. problem_re = input_api.re.compile(
  44. r'(' + base_time_type_pattern + r')|(' + base_time_member_pattern + r')')
  45. exception_re = input_api.re.compile(
  46. r'(' + using_base_time_decl_pattern + r')|(' +
  47. base_time_konstant_pattern + r')')
  48. problems = []
  49. for f in input_api.AffectedSourceFiles(_FilterFile):
  50. for line_number, line in f.ChangedContents():
  51. if problem_re.search(line):
  52. if not exception_re.search(line):
  53. problems.append(
  54. ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip()))
  55. if problems:
  56. return [output_api.PresubmitPromptOrNotify(
  57. 'You added one or more references to the base::Time class and/or one\n'
  58. 'of its member functions (or base::Clock/DefaultClock). In media\n'
  59. 'code, it is rarely correct to use a clock susceptible to time skew!\n'
  60. 'Instead, could you use base::TimeTicks to track the passage of\n'
  61. 'real-world time?\n\n' +
  62. '\n'.join(problems))]
  63. else:
  64. return []
  65. def _CheckForHistogramOffByOne(input_api, output_api):
  66. """Make sure histogram enum maxes are used properly"""
  67. # A general-purpose chunk of regex to match whitespace and/or comments
  68. # that may be interspersed with the code we're interested in:
  69. comment = r'/\*.*?\*/|//[^\n]*'
  70. whitespace = r'(?:[\n\t ]|(?:' + comment + r'))*'
  71. # The name is assumed to be a literal string.
  72. histogram_name = r'"[^"]*"'
  73. # This can be an arbitrary expression, so just ensure it isn't a ; to prevent
  74. # matching past the end of this statement.
  75. histogram_value = r'[^;]*'
  76. # In parens so we can retrieve it for further checks.
  77. histogram_max = r'([^;,]*)'
  78. # This should match a uma histogram enumeration macro expression.
  79. uma_macro_re = input_api.re.compile(
  80. r'\bUMA_HISTOGRAM_ENUMERATION\(' + whitespace + histogram_name + r',' +
  81. whitespace + histogram_value + r',' + whitespace + histogram_max +
  82. whitespace + r'\)' + whitespace + r';(?:' + whitespace +
  83. r'\/\/ (PRESUBMIT_IGNORE_UMA_MAX))?')
  84. uma_max_re = input_api.re.compile(r'.*(?:Max|MAX).* \+ 1')
  85. problems = []
  86. for f in input_api.AffectedSourceFiles(_FilterFile):
  87. contents = input_api.ReadFile(f)
  88. # We want to match across lines, but still report a line number, so we keep
  89. # track of the line we're on as we search through the file.
  90. line_number = 1
  91. # We search the entire file, then check if any violations are in the changed
  92. # areas, this is inefficient, but simple. A UMA_HISTOGRAM_ENUMERATION call
  93. # will often span multiple lines, so finding a match looking just at the
  94. # deltas line-by-line won't catch problems.
  95. match = uma_macro_re.search(contents)
  96. while match:
  97. line_number += contents.count('\n', 0, match.start())
  98. max_arg = match.group(1) # The third argument.
  99. if (not uma_max_re.match(max_arg) and match.group(2) !=
  100. 'PRESUBMIT_IGNORE_UMA_MAX'):
  101. uma_range = range(match.start(), match.end() + 1)
  102. # Check if any part of the match is in the changed lines:
  103. for num, line in f.ChangedContents():
  104. if line_number <= num <= line_number + match.group().count('\n'):
  105. problems.append('%s:%d' % (f, line_number))
  106. break
  107. # Strip off the file contents up to the end of the match and update the
  108. # line number.
  109. contents = contents[match.end():]
  110. line_number += match.group().count('\n')
  111. match = uma_macro_re.search(contents)
  112. if problems:
  113. return [output_api.PresubmitError(
  114. 'UMA_HISTOGRAM_ENUMERATION reports in src/media/ are expected to adhere\n'
  115. 'to the following guidelines:\n'
  116. ' - The max value (3rd argument) should be an enum value equal to the\n'
  117. ' last valid value, e.g. FOO_MAX = LAST_VALID_FOO.\n'
  118. ' - 1 must be added to that max value.\n'
  119. 'Contact dalecurtis@chromium.org if you have questions.' , problems)]
  120. return []
  121. def _CheckPassByValue(input_api, output_api):
  122. """Check that base::Time and derived classes are passed by value, and not by
  123. const reference """
  124. problems = []
  125. for f in input_api.AffectedSourceFiles(_FilterFile):
  126. for line_number, line in f.ChangedContents():
  127. if BASE_TIME_TYPES_RE.search(line):
  128. problems.append('%s:%d' % (f, line_number))
  129. if problems:
  130. return [output_api.PresubmitError(
  131. 'base::Time and derived classes should be passed by value and not by\n'
  132. 'const ref, see base/time/time.h for more information.', problems)]
  133. return []
  134. def _CheckForUseOfLazyInstance(input_api, output_api):
  135. """Check that base::LazyInstance is not used."""
  136. problems = []
  137. lazy_instance_re = re.compile(r'(^|\W)base::LazyInstance<')
  138. for f in input_api.AffectedSourceFiles(_FilterFile):
  139. for line_number, line in f.ChangedContents():
  140. if lazy_instance_re.search(line):
  141. problems.append('%s:%d' % (f, line_number))
  142. if problems:
  143. return [output_api.PresubmitError(
  144. 'base::LazyInstance is deprecated; use a thread safe static.', problems)]
  145. return []
  146. def _CheckNoLoggingOverrideInHeaders(input_api, output_api):
  147. """Checks to make sure no .h files include logging_override_if_enabled.h."""
  148. files = []
  149. pattern = input_api.re.compile(
  150. r'^#include\s*"media/base/logging_override_if_enabled.h"',
  151. input_api.re.MULTILINE)
  152. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  153. if not f.LocalPath().endswith('.h'):
  154. continue
  155. contents = input_api.ReadFile(f)
  156. if pattern.search(contents):
  157. files.append(f)
  158. if len(files):
  159. return [output_api.PresubmitError(
  160. 'Do not #include "logging_override_if_enabled.h" in header files, '
  161. 'since it overrides DVLOG() in every file including the header. '
  162. 'Instead, only include it in source files.',
  163. files) ]
  164. return []
  165. def _CheckForNoV4L2AggregateInitialization(input_api, output_api):
  166. """Check that struct v4l2_* are not initialized as aggregates with a
  167. braced-init-list"""
  168. problems = []
  169. v4l2_aggregate_initializer_re = re.compile(r'(^|\W)struct.+v4l2_.+=.+{+}+;')
  170. for f in input_api.AffectedSourceFiles(_FilterFile):
  171. for line_number, line in f.ChangedContents():
  172. if v4l2_aggregate_initializer_re.search(line):
  173. problems.append('%s:%d' % (f, line_number))
  174. if problems:
  175. return [output_api.PresubmitPromptWarning(
  176. 'Avoid initializing V4L2 structures with braced-init-lists, i.e. as '
  177. 'aggregates. V4L2 structs often contain unions of various sized members: '
  178. 'when a union is initialized by aggregate initialization, only the first '
  179. 'non-static member is initialized, leaving other members unitialized if '
  180. 'they are larger. Use memset instead.',
  181. problems)]
  182. return []
  183. def _CheckChange(input_api, output_api):
  184. results = []
  185. results.extend(_CheckForUseOfWrongClock(input_api, output_api))
  186. results.extend(_CheckPassByValue(input_api, output_api))
  187. results.extend(_CheckForHistogramOffByOne(input_api, output_api))
  188. results.extend(_CheckForUseOfLazyInstance(input_api, output_api))
  189. results.extend(_CheckNoLoggingOverrideInHeaders(input_api, output_api))
  190. results.extend(_CheckForNoV4L2AggregateInitialization(input_api, output_api))
  191. return results
  192. def CheckChangeOnUpload(input_api, output_api):
  193. return _CheckChange(input_api, output_api)
  194. def CheckChangeOnCommit(input_api, output_api):
  195. return _CheckChange(input_api, output_api)