presubmit_checks.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. # Copyright 2017 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. """Presubmit checks used in viz"""
  5. import re
  6. def CheckChangeLintsClean(input_api, output_api, allowlist, denylist=None):
  7. source_filter = lambda x: input_api.FilterSourceFile(x, allowlist, denylist)
  8. return input_api.canned_checks.CheckChangeLintsClean(
  9. input_api, output_api, source_filter, lint_filters=[], verbose_level=1)
  10. def CheckAsserts(input_api, output_api, allowlist, denylist=None):
  11. denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
  12. source_file_filter = lambda x: input_api.FilterSourceFile(x, allowlist, denylist)
  13. assert_files = []
  14. for f in input_api.AffectedSourceFiles(source_file_filter):
  15. contents = input_api.ReadFile(f, 'rb')
  16. # WebKit ASSERT() is not allowed.
  17. if re.search(r"\bASSERT\(", contents):
  18. assert_files.append(f.LocalPath())
  19. if assert_files:
  20. return [output_api.PresubmitError(
  21. 'These files use ASSERT instead of using DCHECK:',
  22. items=assert_files)]
  23. return []
  24. def CheckStdAbs(input_api, output_api,
  25. allowlist, denylist=None):
  26. denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
  27. source_file_filter = lambda x: input_api.FilterSourceFile(x,
  28. allowlist,
  29. denylist)
  30. using_std_abs_files = []
  31. found_fabs_files = []
  32. missing_std_prefix_files = []
  33. for f in input_api.AffectedSourceFiles(source_file_filter):
  34. contents = input_api.ReadFile(f, 'rb')
  35. if re.search(r"using std::f?abs;", contents):
  36. using_std_abs_files.append(f.LocalPath())
  37. if re.search(r"\bfabsf?\(", contents):
  38. found_fabs_files.append(f.LocalPath());
  39. no_std_prefix = r"(?<!std::)"
  40. # Matches occurrences of abs/absf/fabs/fabsf without a "std::" prefix.
  41. abs_without_prefix = r"%s(\babsf?\()" % no_std_prefix
  42. fabs_without_prefix = r"%s(\bfabsf?\()" % no_std_prefix
  43. # Skips matching any lines that have "// NOLINT".
  44. no_nolint = r"(?![^\n]*//\s+NOLINT)"
  45. expression = re.compile("(%s|%s)%s" %
  46. (abs_without_prefix, fabs_without_prefix, no_nolint))
  47. if expression.search(contents):
  48. missing_std_prefix_files.append(f.LocalPath())
  49. result = []
  50. if using_std_abs_files:
  51. result.append(output_api.PresubmitError(
  52. 'These files have "using std::abs" which is not permitted.',
  53. items=using_std_abs_files))
  54. if found_fabs_files:
  55. result.append(output_api.PresubmitError(
  56. 'std::abs() should be used instead of std::fabs() for consistency.',
  57. items=found_fabs_files))
  58. if missing_std_prefix_files:
  59. result.append(output_api.PresubmitError(
  60. 'These files use abs(), absf(), fabs(), or fabsf() without qualifying '
  61. 'the std namespace. Please use std::abs() in all places.',
  62. items=missing_std_prefix_files))
  63. return result
  64. def CheckPassByValue(input_api,
  65. output_api,
  66. allowlist,
  67. denylist=None):
  68. denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
  69. source_file_filter = lambda x: input_api.FilterSourceFile(x,
  70. allowlist,
  71. denylist)
  72. local_errors = []
  73. # Well-defined simple classes the same size as a primitive type.
  74. pass_by_value_types = ['base::Time',
  75. 'base::TimeTicks',
  76. ]
  77. for f in input_api.AffectedSourceFiles(source_file_filter):
  78. contents = input_api.ReadFile(f, 'rb')
  79. match = re.search(
  80. r'\bconst +' + '(?P<type>(%s))&' %
  81. '|'.join(pass_by_value_types),
  82. contents)
  83. if match:
  84. local_errors.append(output_api.PresubmitError(
  85. '%s passes %s by const ref instead of by value.' %
  86. (f.LocalPath(), match.group('type'))))
  87. return local_errors
  88. def CheckTodos(input_api, output_api):
  89. errors = []
  90. source_file_filter = lambda x: x
  91. for f in input_api.AffectedSourceFiles(source_file_filter):
  92. contents = input_api.ReadFile(f, 'rb')
  93. if ('FIX'+'ME') in contents:
  94. errors.append(f.LocalPath())
  95. if errors:
  96. return [output_api.PresubmitError(
  97. 'All TODO comments should be of the form TODO(name/bug). ' +
  98. 'Use TODO instead of FIX' + 'ME',
  99. items=errors)]
  100. return []
  101. def CheckDoubleAngles(input_api, output_api, allowlist,
  102. denylist=None):
  103. errors = []
  104. source_file_filter = lambda x: input_api.FilterSourceFile(x,
  105. allowlist,
  106. denylist)
  107. for f in input_api.AffectedSourceFiles(source_file_filter):
  108. contents = input_api.ReadFile(f, 'rb')
  109. if ('> >') in contents:
  110. errors.append(f.LocalPath())
  111. if errors:
  112. return [output_api.PresubmitError('Use >> instead of > >:', items=errors)]
  113. return []
  114. def FindUnquotedQuote(contents, pos):
  115. match = re.search(r"(?<!\\)(?P<quote>\")", contents[pos:])
  116. return -1 if not match else match.start("quote") + pos
  117. def FindUselessIfdefs(input_api, output_api):
  118. errors = []
  119. source_file_filter = lambda x: x
  120. for f in input_api.AffectedSourceFiles(source_file_filter):
  121. contents = input_api.ReadFile(f, 'rb')
  122. if re.search(r'#if\s*0\s', contents):
  123. errors.append(f.LocalPath())
  124. if errors:
  125. return [output_api.PresubmitError(
  126. 'Don\'t use #if '+'0; just delete the code.',
  127. items=errors)]
  128. return []
  129. def FindNamespaceInBlock(pos, namespace, contents, allowlist=[]):
  130. open_brace = -1
  131. close_brace = -1
  132. quote = -1
  133. name = -1
  134. brace_count = 1
  135. quote_count = 0
  136. while pos < len(contents) and brace_count > 0:
  137. if open_brace < pos: open_brace = contents.find("{", pos)
  138. if close_brace < pos: close_brace = contents.find("}", pos)
  139. if quote < pos: quote = FindUnquotedQuote(contents, pos)
  140. if name < pos: name = contents.find(("%s::" % namespace), pos)
  141. if name < 0:
  142. return False # The namespace is not used at all.
  143. if open_brace < 0:
  144. open_brace = len(contents)
  145. if close_brace < 0:
  146. close_brace = len(contents)
  147. if quote < 0:
  148. quote = len(contents)
  149. next = min(open_brace, min(close_brace, min(quote, name)))
  150. if next == open_brace:
  151. brace_count += 1
  152. elif next == close_brace:
  153. brace_count -= 1
  154. elif next == quote:
  155. quote_count = 0 if quote_count else 1
  156. elif next == name and not quote_count:
  157. in_allowlist = False
  158. for w in allowlist:
  159. if re.match(w, contents[next:]):
  160. in_allowlist = True
  161. break
  162. if not in_allowlist:
  163. return True
  164. pos = next + 1
  165. return False
  166. # Checks for the use of viz:: within the viz namespace, which is usually
  167. # redundant.
  168. def CheckNamespace(input_api, output_api):
  169. errors = []
  170. source_file_filter = lambda x: x
  171. for f in input_api.AffectedSourceFiles(source_file_filter):
  172. contents = input_api.ReadFile(f, 'rb')
  173. match = re.search(r'namespace\s*viz\s*{', contents)
  174. if match:
  175. allowlist = []
  176. if FindNamespaceInBlock(match.end(), 'viz', contents, allowlist=allowlist):
  177. errors.append(f.LocalPath())
  178. if errors:
  179. return [output_api.PresubmitError(
  180. 'Do not use viz:: inside of the viz namespace.',
  181. items=errors)]
  182. return []
  183. # Verifies that we use the right module name (viz.mojom) in mojom files and we
  184. # don't specify module name viz.mojom when referring to types in viz.mojom.
  185. def CheckMojoms(input_api, output_api):
  186. source_file_filter = lambda x: input_api.FilterSourceFile(x,
  187. ['.*\.mojom$'],
  188. [])
  189. wrong_module_name=[]
  190. omit_module_name=[]
  191. for f in input_api.AffectedSourceFiles(source_file_filter):
  192. contents = input_api.ReadFile(f, 'rb')
  193. if 'module viz.mojom;' not in contents:
  194. wrong_module_name.append(f.LocalPath())
  195. elif 'viz.mojom.' in contents:
  196. omit_module_name.append(f.LocalPath())
  197. errors=[]
  198. if wrong_module_name:
  199. errors.append(output_api.PresubmitError(
  200. 'Use viz.mojom as the module name in mojom files.',
  201. items=wrong_module_name))
  202. if omit_module_name:
  203. errors.append(output_api.PresubmitError(
  204. 'Do not specify module name viz.mojom when referring to types '
  205. + 'in the same module.', items=omit_module_name))
  206. return errors
  207. def CheckForUseOfWrongClock(input_api,
  208. output_api,
  209. allowlist,
  210. denylist=None):
  211. """Make sure new lines of code don't use a clock susceptible to skew."""
  212. denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
  213. source_file_filter = lambda x: input_api.FilterSourceFile(x,
  214. allowlist,
  215. denylist)
  216. # Regular expression that should detect any explicit references to the
  217. # base::Time type (or base::Clock/DefaultClock), whether in using decls,
  218. # typedefs, or to call static methods.
  219. base_time_type_pattern = r'(^|\W)base::(Time|Clock|DefaultClock)(\W|$)'
  220. # Regular expression that should detect references to the base::Time class
  221. # members, such as a call to base::Time::Now.
  222. base_time_member_pattern = r'(^|\W)(Time|Clock|DefaultClock)::'
  223. # Regular expression to detect "using base::Time" declarations. We want to
  224. # prevent these from triggerring a warning. For example, it's perfectly
  225. # reasonable for code to be written like this:
  226. #
  227. # using base::Time;
  228. # ...
  229. # int64 foo_us = foo_s * Time::kMicrosecondsPerSecond;
  230. using_base_time_decl_pattern = r'^\s*using\s+(::)?base::Time\s*;'
  231. # Regular expression to detect references to the kXXX constants in the
  232. # base::Time class. We want to prevent these from triggerring a warning.
  233. base_time_konstant_pattern = r'(^|\W)Time::k\w+'
  234. problem_re = input_api.re.compile(
  235. r'(' + base_time_type_pattern + r')|(' + base_time_member_pattern + r')')
  236. exception_re = input_api.re.compile(
  237. r'(' + using_base_time_decl_pattern + r')|(' +
  238. base_time_konstant_pattern + r')')
  239. problems = []
  240. for f in input_api.AffectedSourceFiles(source_file_filter):
  241. for line_number, line in f.ChangedContents():
  242. if problem_re.search(line):
  243. if not exception_re.search(line):
  244. problems.append(
  245. ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip()))
  246. if problems:
  247. return [output_api.PresubmitPromptOrNotify(
  248. 'You added one or more references to the base::Time class and/or one\n'
  249. 'of its member functions (or base::Clock/DefaultClock). In cc code,\n'
  250. 'it is most certainly incorrect! Instead use base::TimeTicks.\n\n'
  251. '\n'.join(problems))]
  252. else:
  253. return []
  254. def RunAllChecks(input_api, output_api, allowlist):
  255. results = []
  256. results += CheckAsserts(input_api, output_api, allowlist)
  257. results += CheckStdAbs(input_api, output_api, allowlist)
  258. results += CheckPassByValue(input_api, output_api, allowlist)
  259. results += CheckChangeLintsClean(input_api, output_api, allowlist)
  260. results += CheckTodos(input_api, output_api)
  261. results += CheckDoubleAngles(input_api, output_api, allowlist)
  262. results += CheckNamespace(input_api, output_api)
  263. results += CheckMojoms(input_api, output_api)
  264. results += CheckForUseOfWrongClock(input_api, output_api, allowlist)
  265. results += FindUselessIfdefs(input_api, output_api)
  266. return results