GoogleGit

blob: 520642ea664fab16705f539995d8eebc408076bb [file] [log] [blame]
  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. """Top-level presubmit script for Chromium.
  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. _EXCLUDED_PATHS = (
  9. r"^breakpad[\\\/].*",
  10. r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
  11. r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
  12. r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
  13. r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
  14. r"^skia[\\\/].*",
  15. r"^v8[\\\/].*",
  16. r".*MakeFile$",
  17. r".+_autogen\.h$",
  18. r".+[\\\/]pnacl_shim\.c$",
  19. r"^gpu[\\\/]config[\\\/].*_list_json\.cc$",
  20. r"^chrome[\\\/]browser[\\\/]resources[\\\/]pdf[\\\/]index.js"
  21. )
  22. # The NetscapePlugIn library is excluded from pan-project as it will soon
  23. # be deleted together with the rest of the NPAPI and it's not worthwhile to
  24. # update the coding style until then.
  25. _TESTRUNNER_PATHS = (
  26. r"^content[\\\/]shell[\\\/]tools[\\\/]plugin[\\\/].*",
  27. )
  28. # Fragment of a regular expression that matches C++ and Objective-C++
  29. # implementation files.
  30. _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
  31. # Regular expression that matches code only used for test binaries
  32. # (best effort).
  33. _TEST_CODE_EXCLUDED_PATHS = (
  34. r'.*[\\\/](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
  35. r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
  36. r'.+_(api|browser|kif|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
  37. _IMPLEMENTATION_EXTENSIONS,
  38. r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
  39. r'.*[\\\/](test|tool(s)?)[\\\/].*',
  40. # content_shell is used for running layout tests.
  41. r'content[\\\/]shell[\\\/].*',
  42. # At request of folks maintaining this folder.
  43. r'chrome[\\\/]browser[\\\/]automation[\\\/].*',
  44. # Non-production example code.
  45. r'mojo[\\\/]examples[\\\/].*',
  46. # Launcher for running iOS tests on the simulator.
  47. r'testing[\\\/]iossim[\\\/]iossim\.mm$',
  48. )
  49. _TEST_ONLY_WARNING = (
  50. 'You might be calling functions intended only for testing from\n'
  51. 'production code. It is OK to ignore this warning if you know what\n'
  52. 'you are doing, as the heuristics used to detect the situation are\n'
  53. 'not perfect. The commit queue will not block on this warning.')
  54. _INCLUDE_ORDER_WARNING = (
  55. 'Your #include order seems to be broken. Remember to use the right '
  56. 'collation (LC_COLLATE=C) and check\nhttps://google-styleguide.googlecode'
  57. '.com/svn/trunk/cppguide.html#Names_and_Order_of_Includes')
  58. _BANNED_OBJC_FUNCTIONS = (
  59. (
  60. 'addTrackingRect:',
  61. (
  62. 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
  63. 'prohibited. Please use CrTrackingArea instead.',
  64. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  65. ),
  66. False,
  67. ),
  68. (
  69. r'/NSTrackingArea\W',
  70. (
  71. 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
  72. 'instead.',
  73. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  74. ),
  75. False,
  76. ),
  77. (
  78. 'convertPointFromBase:',
  79. (
  80. 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
  81. 'Please use |convertPoint:(point) fromView:nil| instead.',
  82. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  83. ),
  84. True,
  85. ),
  86. (
  87. 'convertPointToBase:',
  88. (
  89. 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
  90. 'Please use |convertPoint:(point) toView:nil| instead.',
  91. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  92. ),
  93. True,
  94. ),
  95. (
  96. 'convertRectFromBase:',
  97. (
  98. 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
  99. 'Please use |convertRect:(point) fromView:nil| instead.',
  100. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  101. ),
  102. True,
  103. ),
  104. (
  105. 'convertRectToBase:',
  106. (
  107. 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
  108. 'Please use |convertRect:(point) toView:nil| instead.',
  109. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  110. ),
  111. True,
  112. ),
  113. (
  114. 'convertSizeFromBase:',
  115. (
  116. 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
  117. 'Please use |convertSize:(point) fromView:nil| instead.',
  118. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  119. ),
  120. True,
  121. ),
  122. (
  123. 'convertSizeToBase:',
  124. (
  125. 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
  126. 'Please use |convertSize:(point) toView:nil| instead.',
  127. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  128. ),
  129. True,
  130. ),
  131. )
  132. _BANNED_CPP_FUNCTIONS = (
  133. # Make sure that gtest's FRIEND_TEST() macro is not used; the
  134. # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
  135. # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
  136. (
  137. 'FRIEND_TEST(',
  138. (
  139. 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
  140. 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
  141. ),
  142. False,
  143. (),
  144. ),
  145. (
  146. 'ScopedAllowIO',
  147. (
  148. 'New code should not use ScopedAllowIO. Post a task to the blocking',
  149. 'pool or the FILE thread instead.',
  150. ),
  151. True,
  152. (
  153. r"^base[\\\/]process[\\\/]process_metrics_linux\.cc$",
  154. r"^chrome[\\\/]browser[\\\/]chromeos[\\\/]boot_times_recorder\.cc$",
  155. r"^chrome[\\\/]browser[\\\/]chromeos[\\\/]"
  156. "customization_document_browsertest\.cc$",
  157. r"^components[\\\/]crash[\\\/]app[\\\/]breakpad_mac\.mm$",
  158. r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
  159. r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
  160. r"^mojo[\\\/]edk[\\\/]embedder[\\\/]" +
  161. r"simple_platform_shared_buffer_posix\.cc$",
  162. r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
  163. r"^net[\\\/]url_request[\\\/]test_url_fetcher_factory\.cc$",
  164. r"^ui[\\\/]ozone[\\\/]platform[\\\/]drm[\\\/]host[\\\/]"
  165. "drm_display_host_manager\.cc$",
  166. ),
  167. ),
  168. (
  169. 'SkRefPtr',
  170. (
  171. 'The use of SkRefPtr is prohibited. ',
  172. 'Please use skia::RefPtr instead.'
  173. ),
  174. True,
  175. (),
  176. ),
  177. (
  178. 'SkAutoRef',
  179. (
  180. 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
  181. 'Please use skia::RefPtr instead.'
  182. ),
  183. True,
  184. (),
  185. ),
  186. (
  187. 'SkAutoTUnref',
  188. (
  189. 'The use of SkAutoTUnref is dangerous because it implicitly ',
  190. 'converts to a raw pointer. Please use skia::RefPtr instead.'
  191. ),
  192. True,
  193. (),
  194. ),
  195. (
  196. 'SkAutoUnref',
  197. (
  198. 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
  199. 'because it implicitly converts to a raw pointer. ',
  200. 'Please use skia::RefPtr instead.'
  201. ),
  202. True,
  203. (),
  204. ),
  205. (
  206. r'/HANDLE_EINTR\(.*close',
  207. (
  208. 'HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file',
  209. 'descriptor will be closed, and it is incorrect to retry the close.',
  210. 'Either call close directly and ignore its return value, or wrap close',
  211. 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623'
  212. ),
  213. True,
  214. (),
  215. ),
  216. (
  217. r'/IGNORE_EINTR\((?!.*close)',
  218. (
  219. 'IGNORE_EINTR is only valid when wrapping close. To wrap other system',
  220. 'calls, use HANDLE_EINTR. See http://crbug.com/269623',
  221. ),
  222. True,
  223. (
  224. # Files that #define IGNORE_EINTR.
  225. r'^base[\\\/]posix[\\\/]eintr_wrapper\.h$',
  226. r'^ppapi[\\\/]tests[\\\/]test_broker\.cc$',
  227. ),
  228. ),
  229. (
  230. r'/v8::Extension\(',
  231. (
  232. 'Do not introduce new v8::Extensions into the code base, use',
  233. 'gin::Wrappable instead. See http://crbug.com/334679',
  234. ),
  235. True,
  236. (
  237. r'extensions[\\\/]renderer[\\\/]safe_builtins\.*',
  238. ),
  239. ),
  240. (
  241. '\<MessageLoopProxy\>',
  242. (
  243. 'MessageLoopProxy is deprecated. ',
  244. 'Please use SingleThreadTaskRunner or ThreadTaskRunnerHandle instead.'
  245. ),
  246. True,
  247. (
  248. # Internal message_loop related code may still use it.
  249. r'^base[\\\/]message_loop[\\\/].*',
  250. ),
  251. ),
  252. )
  253. _IPC_ENUM_TRAITS_DEPRECATED = (
  254. 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n'
  255. 'See http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc')
  256. _VALID_OS_MACROS = (
  257. # Please keep sorted.
  258. 'OS_ANDROID',
  259. 'OS_ANDROID_HOST',
  260. 'OS_BSD',
  261. 'OS_CAT', # For testing.
  262. 'OS_CHROMEOS',
  263. 'OS_FREEBSD',
  264. 'OS_IOS',
  265. 'OS_LINUX',
  266. 'OS_MACOSX',
  267. 'OS_NACL',
  268. 'OS_NACL_NONSFI',
  269. 'OS_NACL_SFI',
  270. 'OS_OPENBSD',
  271. 'OS_POSIX',
  272. 'OS_QNX',
  273. 'OS_SOLARIS',
  274. 'OS_WIN',
  275. )
  276. def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
  277. """Attempts to prevent use of functions intended only for testing in
  278. non-testing code. For now this is just a best-effort implementation
  279. that ignores header files and may have some false positives. A
  280. better implementation would probably need a proper C++ parser.
  281. """
  282. # We only scan .cc files and the like, as the declaration of
  283. # for-testing functions in header files are hard to distinguish from
  284. # calls to such functions without a proper C++ parser.
  285. file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
  286. base_function_pattern = r'[ :]test::[^\s]+|ForTest(s|ing)?|for_test(s|ing)?'
  287. inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
  288. comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
  289. exclusion_pattern = input_api.re.compile(
  290. r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
  291. base_function_pattern, base_function_pattern))
  292. def FilterFile(affected_file):
  293. black_list = (_EXCLUDED_PATHS +
  294. _TEST_CODE_EXCLUDED_PATHS +
  295. input_api.DEFAULT_BLACK_LIST)
  296. return input_api.FilterSourceFile(
  297. affected_file,
  298. white_list=(file_inclusion_pattern, ),
  299. black_list=black_list)
  300. problems = []
  301. for f in input_api.AffectedSourceFiles(FilterFile):
  302. local_path = f.LocalPath()
  303. for line_number, line in f.ChangedContents():
  304. if (inclusion_pattern.search(line) and
  305. not comment_pattern.search(line) and
  306. not exclusion_pattern.search(line)):
  307. problems.append(
  308. '%s:%d\n %s' % (local_path, line_number, line.strip()))
  309. if problems:
  310. return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
  311. else:
  312. return []
  313. def _CheckNoIOStreamInHeaders(input_api, output_api):
  314. """Checks to make sure no .h files include <iostream>."""
  315. files = []
  316. pattern = input_api.re.compile(r'^#include\s*<iostream>',
  317. input_api.re.MULTILINE)
  318. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  319. if not f.LocalPath().endswith('.h'):
  320. continue
  321. contents = input_api.ReadFile(f)
  322. if pattern.search(contents):
  323. files.append(f)
  324. if len(files):
  325. return [ output_api.PresubmitError(
  326. 'Do not #include <iostream> in header files, since it inserts static '
  327. 'initialization into every file including the header. Instead, '
  328. '#include <ostream>. See http://crbug.com/94794',
  329. files) ]
  330. return []
  331. def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
  332. """Checks to make sure no source files use UNIT_TEST"""
  333. problems = []
  334. for f in input_api.AffectedFiles():
  335. if (not f.LocalPath().endswith(('.cc', '.mm'))):
  336. continue
  337. for line_num, line in f.ChangedContents():
  338. if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'):
  339. problems.append(' %s:%d' % (f.LocalPath(), line_num))
  340. if not problems:
  341. return []
  342. return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
  343. '\n'.join(problems))]
  344. def _FindHistogramNameInLine(histogram_name, line):
  345. """Tries to find a histogram name or prefix in a line."""
  346. if not "affected-histogram" in line:
  347. return histogram_name in line
  348. # A histogram_suffixes tag type has an affected-histogram name as a prefix of
  349. # the histogram_name.
  350. if not '"' in line:
  351. return False
  352. histogram_prefix = line.split('\"')[1]
  353. return histogram_prefix in histogram_name
  354. def _CheckUmaHistogramChanges(input_api, output_api):
  355. """Check that UMA histogram names in touched lines can still be found in other
  356. lines of the patch or in histograms.xml. Note that this check would not catch
  357. the reverse: changes in histograms.xml not matched in the code itself."""
  358. touched_histograms = []
  359. histograms_xml_modifications = []
  360. pattern = input_api.re.compile('UMA_HISTOGRAM.*\("(.*)"')
  361. for f in input_api.AffectedFiles():
  362. # If histograms.xml itself is modified, keep the modified lines for later.
  363. if f.LocalPath().endswith(('histograms.xml')):
  364. histograms_xml_modifications = f.ChangedContents()
  365. continue
  366. if not f.LocalPath().endswith(('cc', 'mm', 'cpp')):
  367. continue
  368. for line_num, line in f.ChangedContents():
  369. found = pattern.search(line)
  370. if found:
  371. touched_histograms.append([found.group(1), f, line_num])
  372. # Search for the touched histogram names in the local modifications to
  373. # histograms.xml, and, if not found, on the base histograms.xml file.
  374. unmatched_histograms = []
  375. for histogram_info in touched_histograms:
  376. histogram_name_found = False
  377. for line_num, line in histograms_xml_modifications:
  378. histogram_name_found = _FindHistogramNameInLine(histogram_info[0], line)
  379. if histogram_name_found:
  380. break
  381. if not histogram_name_found:
  382. unmatched_histograms.append(histogram_info)
  383. histograms_xml_path = 'tools/metrics/histograms/histograms.xml'
  384. problems = []
  385. if unmatched_histograms:
  386. with open(histograms_xml_path) as histograms_xml:
  387. for histogram_name, f, line_num in unmatched_histograms:
  388. histograms_xml.seek(0)
  389. histogram_name_found = False
  390. for line in histograms_xml:
  391. histogram_name_found = _FindHistogramNameInLine(histogram_name, line)
  392. if histogram_name_found:
  393. break
  394. if not histogram_name_found:
  395. problems.append(' [%s:%d] %s' %
  396. (f.LocalPath(), line_num, histogram_name))
  397. if not problems:
  398. return []
  399. return [output_api.PresubmitPromptWarning('Some UMA_HISTOGRAM lines have '
  400. 'been modified and the associated histogram name has no match in either '
  401. '%s or the modifications of it:' % (histograms_xml_path), problems)]
  402. def _CheckNoNewWStrings(input_api, output_api):
  403. """Checks to make sure we don't introduce use of wstrings."""
  404. problems = []
  405. for f in input_api.AffectedFiles():
  406. if (not f.LocalPath().endswith(('.cc', '.h')) or
  407. f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h')) or
  408. '/win/' in f.LocalPath()):
  409. continue
  410. allowWString = False
  411. for line_num, line in f.ChangedContents():
  412. if 'presubmit: allow wstring' in line:
  413. allowWString = True
  414. elif not allowWString and 'wstring' in line:
  415. problems.append(' %s:%d' % (f.LocalPath(), line_num))
  416. allowWString = False
  417. else:
  418. allowWString = False
  419. if not problems:
  420. return []
  421. return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
  422. ' If you are calling a cross-platform API that accepts a wstring, '
  423. 'fix the API.\n' +
  424. '\n'.join(problems))]
  425. def _CheckNoDEPSGIT(input_api, output_api):
  426. """Make sure .DEPS.git is never modified manually."""
  427. if any(f.LocalPath().endswith('.DEPS.git') for f in
  428. input_api.AffectedFiles()):
  429. return [output_api.PresubmitError(
  430. 'Never commit changes to .DEPS.git. This file is maintained by an\n'
  431. 'automated system based on what\'s in DEPS and your changes will be\n'
  432. 'overwritten.\n'
  433. 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/get-the-code#Rolling_DEPS\n'
  434. 'for more information')]
  435. return []
  436. def _CheckValidHostsInDEPS(input_api, output_api):
  437. """Checks that DEPS file deps are from allowed_hosts."""
  438. # Run only if DEPS file has been modified to annoy fewer bystanders.
  439. if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()):
  440. return []
  441. # Outsource work to gclient verify
  442. try:
  443. input_api.subprocess.check_output(['gclient', 'verify'])
  444. return []
  445. except input_api.subprocess.CalledProcessError, error:
  446. return [output_api.PresubmitError(
  447. 'DEPS file must have only git dependencies.',
  448. long_text=error.output)]
  449. def _CheckNoBannedFunctions(input_api, output_api):
  450. """Make sure that banned functions are not used."""
  451. warnings = []
  452. errors = []
  453. file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
  454. for f in input_api.AffectedFiles(file_filter=file_filter):
  455. for line_num, line in f.ChangedContents():
  456. for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
  457. matched = False
  458. if func_name[0:1] == '/':
  459. regex = func_name[1:]
  460. if input_api.re.search(regex, line):
  461. matched = True
  462. elif func_name in line:
  463. matched = True
  464. if matched:
  465. problems = warnings;
  466. if error:
  467. problems = errors;
  468. problems.append(' %s:%d:' % (f.LocalPath(), line_num))
  469. for message_line in message:
  470. problems.append(' %s' % message_line)
  471. file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
  472. for f in input_api.AffectedFiles(file_filter=file_filter):
  473. for line_num, line in f.ChangedContents():
  474. for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
  475. def IsBlacklisted(affected_file, blacklist):
  476. local_path = affected_file.LocalPath()
  477. for item in blacklist:
  478. if input_api.re.match(item, local_path):
  479. return True
  480. return False
  481. if IsBlacklisted(f, excluded_paths):
  482. continue
  483. matched = False
  484. if func_name[0:1] == '/':
  485. regex = func_name[1:]
  486. if input_api.re.search(regex, line):
  487. matched = True
  488. elif func_name in line:
  489. matched = True
  490. if matched:
  491. problems = warnings;
  492. if error:
  493. problems = errors;
  494. problems.append(' %s:%d:' % (f.LocalPath(), line_num))
  495. for message_line in message:
  496. problems.append(' %s' % message_line)
  497. result = []
  498. if (warnings):
  499. result.append(output_api.PresubmitPromptWarning(
  500. 'Banned functions were used.\n' + '\n'.join(warnings)))
  501. if (errors):
  502. result.append(output_api.PresubmitError(
  503. 'Banned functions were used.\n' + '\n'.join(errors)))
  504. return result
  505. def _CheckNoPragmaOnce(input_api, output_api):
  506. """Make sure that banned functions are not used."""
  507. files = []
  508. pattern = input_api.re.compile(r'^#pragma\s+once',
  509. input_api.re.MULTILINE)
  510. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  511. if not f.LocalPath().endswith('.h'):
  512. continue
  513. contents = input_api.ReadFile(f)
  514. if pattern.search(contents):
  515. files.append(f)
  516. if files:
  517. return [output_api.PresubmitError(
  518. 'Do not use #pragma once in header files.\n'
  519. 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
  520. files)]
  521. return []
  522. def _CheckNoTrinaryTrueFalse(input_api, output_api):
  523. """Checks to make sure we don't introduce use of foo ? true : false."""
  524. problems = []
  525. pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
  526. for f in input_api.AffectedFiles():
  527. if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
  528. continue
  529. for line_num, line in f.ChangedContents():
  530. if pattern.match(line):
  531. problems.append(' %s:%d' % (f.LocalPath(), line_num))
  532. if not problems:
  533. return []
  534. return [output_api.PresubmitPromptWarning(
  535. 'Please consider avoiding the "? true : false" pattern if possible.\n' +
  536. '\n'.join(problems))]
  537. def _CheckUnwantedDependencies(input_api, output_api):
  538. """Runs checkdeps on #include statements added in this
  539. change. Breaking - rules is an error, breaking ! rules is a
  540. warning.
  541. """
  542. import sys
  543. # We need to wait until we have an input_api object and use this
  544. # roundabout construct to import checkdeps because this file is
  545. # eval-ed and thus doesn't have __file__.
  546. original_sys_path = sys.path
  547. try:
  548. sys.path = sys.path + [input_api.os_path.join(
  549. input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
  550. import checkdeps
  551. from cpp_checker import CppChecker
  552. from rules import Rule
  553. finally:
  554. # Restore sys.path to what it was before.
  555. sys.path = original_sys_path
  556. added_includes = []
  557. for f in input_api.AffectedFiles():
  558. if not CppChecker.IsCppFile(f.LocalPath()):
  559. continue
  560. changed_lines = [line for line_num, line in f.ChangedContents()]
  561. added_includes.append([f.LocalPath(), changed_lines])
  562. deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
  563. error_descriptions = []
  564. warning_descriptions = []
  565. for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
  566. added_includes):
  567. description_with_path = '%s\n %s' % (path, rule_description)
  568. if rule_type == Rule.DISALLOW:
  569. error_descriptions.append(description_with_path)
  570. else:
  571. warning_descriptions.append(description_with_path)
  572. results = []
  573. if error_descriptions:
  574. results.append(output_api.PresubmitError(
  575. 'You added one or more #includes that violate checkdeps rules.',
  576. error_descriptions))
  577. if warning_descriptions:
  578. results.append(output_api.PresubmitPromptOrNotify(
  579. 'You added one or more #includes of files that are temporarily\n'
  580. 'allowed but being removed. Can you avoid introducing the\n'
  581. '#include? See relevant DEPS file(s) for details and contacts.',
  582. warning_descriptions))
  583. return results
  584. def _CheckFilePermissions(input_api, output_api):
  585. """Check that all files have their permissions properly set."""
  586. if input_api.platform == 'win32':
  587. return []
  588. args = [input_api.python_executable, 'tools/checkperms/checkperms.py',
  589. '--root', input_api.change.RepositoryRoot()]
  590. for f in input_api.AffectedFiles():
  591. args += ['--file', f.LocalPath()]
  592. checkperms = input_api.subprocess.Popen(args,
  593. stdout=input_api.subprocess.PIPE)
  594. errors = checkperms.communicate()[0].strip()
  595. if errors:
  596. return [output_api.PresubmitError('checkperms.py failed.',
  597. errors.splitlines())]
  598. return []
  599. def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
  600. """Makes sure we don't include ui/aura/window_property.h
  601. in header files.
  602. """
  603. pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
  604. errors = []
  605. for f in input_api.AffectedFiles():
  606. if not f.LocalPath().endswith('.h'):
  607. continue
  608. for line_num, line in f.ChangedContents():
  609. if pattern.match(line):
  610. errors.append(' %s:%d' % (f.LocalPath(), line_num))
  611. results = []
  612. if errors:
  613. results.append(output_api.PresubmitError(
  614. 'Header files should not include ui/aura/window_property.h', errors))
  615. return results
  616. def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
  617. """Checks that the lines in scope occur in the right order.
  618. 1. C system files in alphabetical order
  619. 2. C++ system files in alphabetical order
  620. 3. Project's .h files
  621. """
  622. c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
  623. cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
  624. custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
  625. C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
  626. state = C_SYSTEM_INCLUDES
  627. previous_line = ''
  628. previous_line_num = 0
  629. problem_linenums = []
  630. out_of_order = " - line belongs before previous line"
  631. for line_num, line in scope:
  632. if c_system_include_pattern.match(line):
  633. if state != C_SYSTEM_INCLUDES:
  634. problem_linenums.append((line_num, previous_line_num,
  635. " - C system include file in wrong block"))
  636. elif previous_line and previous_line > line:
  637. problem_linenums.append((line_num, previous_line_num,
  638. out_of_order))
  639. elif cpp_system_include_pattern.match(line):
  640. if state == C_SYSTEM_INCLUDES:
  641. state = CPP_SYSTEM_INCLUDES
  642. elif state == CUSTOM_INCLUDES:
  643. problem_linenums.append((line_num, previous_line_num,
  644. " - c++ system include file in wrong block"))
  645. elif previous_line and previous_line > line:
  646. problem_linenums.append((line_num, previous_line_num, out_of_order))
  647. elif custom_include_pattern.match(line):
  648. if state != CUSTOM_INCLUDES:
  649. state = CUSTOM_INCLUDES
  650. elif previous_line and previous_line > line:
  651. problem_linenums.append((line_num, previous_line_num, out_of_order))
  652. else:
  653. problem_linenums.append((line_num, previous_line_num,
  654. "Unknown include type"))
  655. previous_line = line
  656. previous_line_num = line_num
  657. warnings = []
  658. for (line_num, previous_line_num, failure_type) in problem_linenums:
  659. if line_num in changed_linenums or previous_line_num in changed_linenums:
  660. warnings.append(' %s:%d:%s' % (file_path, line_num, failure_type))
  661. return warnings
  662. def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
  663. """Checks the #include order for the given file f."""
  664. system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
  665. # Exclude the following includes from the check:
  666. # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
  667. # specific order.
  668. # 2) <atlbase.h>, "build/build_config.h"
  669. excluded_include_pattern = input_api.re.compile(
  670. r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
  671. custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
  672. # Match the final or penultimate token if it is xxxtest so we can ignore it
  673. # when considering the special first include.
  674. test_file_tag_pattern = input_api.re.compile(
  675. r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
  676. if_pattern = input_api.re.compile(
  677. r'\s*#\s*(if|elif|else|endif|define|undef).*')
  678. # Some files need specialized order of includes; exclude such files from this
  679. # check.
  680. uncheckable_includes_pattern = input_api.re.compile(
  681. r'\s*#include '
  682. '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
  683. contents = f.NewContents()
  684. warnings = []
  685. line_num = 0
  686. # Handle the special first include. If the first include file is
  687. # some/path/file.h, the corresponding including file can be some/path/file.cc,
  688. # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
  689. # etc. It's also possible that no special first include exists.
  690. # If the included file is some/path/file_platform.h the including file could
  691. # also be some/path/file_xxxtest_platform.h.
  692. including_file_base_name = test_file_tag_pattern.sub(
  693. '', input_api.os_path.basename(f.LocalPath()))
  694. for line in contents:
  695. line_num += 1
  696. if system_include_pattern.match(line):
  697. # No special first include -> process the line again along with normal
  698. # includes.
  699. line_num -= 1
  700. break
  701. match = custom_include_pattern.match(line)
  702. if match:
  703. match_dict = match.groupdict()
  704. header_basename = test_file_tag_pattern.sub(
  705. '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
  706. if header_basename not in including_file_base_name:
  707. # No special first include -> process the line again along with normal
  708. # includes.
  709. line_num -= 1
  710. break
  711. # Split into scopes: Each region between #if and #endif is its own scope.
  712. scopes = []
  713. current_scope = []
  714. for line in contents[line_num:]:
  715. line_num += 1
  716. if uncheckable_includes_pattern.match(line):
  717. continue
  718. if if_pattern.match(line):
  719. scopes.append(current_scope)
  720. current_scope = []
  721. elif ((system_include_pattern.match(line) or
  722. custom_include_pattern.match(line)) and
  723. not excluded_include_pattern.match(line)):
  724. current_scope.append((line_num, line))
  725. scopes.append(current_scope)
  726. for scope in scopes:
  727. warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
  728. changed_linenums))
  729. return warnings
  730. def _CheckIncludeOrder(input_api, output_api):
  731. """Checks that the #include order is correct.
  732. 1. The corresponding header for source files.
  733. 2. C system files in alphabetical order
  734. 3. C++ system files in alphabetical order
  735. 4. Project's .h files in alphabetical order
  736. Each region separated by #if, #elif, #else, #endif, #define and #undef follows
  737. these rules separately.
  738. """
  739. def FileFilterIncludeOrder(affected_file):
  740. black_list = (_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
  741. return input_api.FilterSourceFile(affected_file, black_list=black_list)
  742. warnings = []
  743. for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder):
  744. if f.LocalPath().endswith(('.cc', '.h', '.mm')):
  745. changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
  746. warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
  747. results = []
  748. if warnings:
  749. results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
  750. warnings))
  751. return results
  752. def _CheckForVersionControlConflictsInFile(input_api, f):
  753. pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
  754. errors = []
  755. for line_num, line in f.ChangedContents():
  756. if f.LocalPath().endswith('.md'):
  757. # First-level headers in markdown look a lot like version control
  758. # conflict markers. http://daringfireball.net/projects/markdown/basics
  759. continue
  760. if pattern.match(line):
  761. errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
  762. return errors
  763. def _CheckForVersionControlConflicts(input_api, output_api):
  764. """Usually this is not intentional and will cause a compile failure."""
  765. errors = []
  766. for f in input_api.AffectedFiles():
  767. errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
  768. results = []
  769. if errors:
  770. results.append(output_api.PresubmitError(
  771. 'Version control conflict markers found, please resolve.', errors))
  772. return results
  773. def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
  774. def FilterFile(affected_file):
  775. """Filter function for use with input_api.AffectedSourceFiles,
  776. below. This filters out everything except non-test files from
  777. top-level directories that generally speaking should not hard-code
  778. service URLs (e.g. src/android_webview/, src/content/ and others).
  779. """
  780. return input_api.FilterSourceFile(
  781. affected_file,
  782. white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
  783. black_list=(_EXCLUDED_PATHS +
  784. _TEST_CODE_EXCLUDED_PATHS +
  785. input_api.DEFAULT_BLACK_LIST))
  786. base_pattern = '"[^"]*google\.com[^"]*"'
  787. comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
  788. pattern = input_api.re.compile(base_pattern)
  789. problems = [] # items are (filename, line_number, line)
  790. for f in input_api.AffectedSourceFiles(FilterFile):
  791. for line_num, line in f.ChangedContents():
  792. if not comment_pattern.search(line) and pattern.search(line):
  793. problems.append((f.LocalPath(), line_num, line))
  794. if problems:
  795. return [output_api.PresubmitPromptOrNotify(
  796. 'Most layers below src/chrome/ should not hardcode service URLs.\n'
  797. 'Are you sure this is correct?',
  798. [' %s:%d: %s' % (
  799. problem[0], problem[1], problem[2]) for problem in problems])]
  800. else:
  801. return []
  802. def _CheckNoAbbreviationInPngFileName(input_api, output_api):
  803. """Makes sure there are no abbreviations in the name of PNG files.
  804. The native_client_sdk directory is excluded because it has auto-generated PNG
  805. files for documentation.
  806. """
  807. errors = []
  808. white_list = (r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$',)
  809. black_list = (r'^native_client_sdk[\\\/]',)
  810. file_filter = lambda f: input_api.FilterSourceFile(
  811. f, white_list=white_list, black_list=black_list)
  812. for f in input_api.AffectedFiles(include_deletes=False,
  813. file_filter=file_filter):
  814. errors.append(' %s' % f.LocalPath())
  815. results = []
  816. if errors:
  817. results.append(output_api.PresubmitError(
  818. 'The name of PNG files should not have abbreviations. \n'
  819. 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
  820. 'Contact oshima@chromium.org if you have questions.', errors))
  821. return results
  822. def _FilesToCheckForIncomingDeps(re, changed_lines):
  823. """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
  824. a set of DEPS entries that we should look up.
  825. For a directory (rather than a specific filename) we fake a path to
  826. a specific filename by adding /DEPS. This is chosen as a file that
  827. will seldom or never be subject to per-file include_rules.
  828. """
  829. # We ignore deps entries on auto-generated directories.
  830. AUTO_GENERATED_DIRS = ['grit', 'jni']
  831. # This pattern grabs the path without basename in the first
  832. # parentheses, and the basename (if present) in the second. It
  833. # relies on the simple heuristic that if there is a basename it will
  834. # be a header file ending in ".h".
  835. pattern = re.compile(
  836. r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
  837. results = set()
  838. for changed_line in changed_lines:
  839. m = pattern.match(changed_line)
  840. if m:
  841. path = m.group(1)
  842. if path.split('/')[0] not in AUTO_GENERATED_DIRS:
  843. if m.group(2):
  844. results.add('%s%s' % (path, m.group(2)))
  845. else:
  846. results.add('%s/DEPS' % path)
  847. return results
  848. def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
  849. """When a dependency prefixed with + is added to a DEPS file, we
  850. want to make sure that the change is reviewed by an OWNER of the
  851. target file or directory, to avoid layering violations from being
  852. introduced. This check verifies that this happens.
  853. """
  854. changed_lines = set()
  855. for f in input_api.AffectedFiles():
  856. filename = input_api.os_path.basename(f.LocalPath())
  857. if filename == 'DEPS':
  858. changed_lines |= set(line.strip()
  859. for line_num, line
  860. in f.ChangedContents())
  861. if not changed_lines:
  862. return []
  863. virtual_depended_on_files = _FilesToCheckForIncomingDeps(input_api.re,
  864. changed_lines)
  865. if not virtual_depended_on_files:
  866. return []
  867. if input_api.is_committing:
  868. if input_api.tbr:
  869. return [output_api.PresubmitNotifyResult(
  870. '--tbr was specified, skipping OWNERS check for DEPS additions')]
  871. if not input_api.change.issue:
  872. return [output_api.PresubmitError(
  873. "DEPS approval by OWNERS check failed: this change has "
  874. "no Rietveld issue number, so we can't check it for approvals.")]
  875. output = output_api.PresubmitError
  876. else:
  877. output = output_api.PresubmitNotifyResult
  878. owners_db = input_api.owners_db
  879. owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
  880. input_api,
  881. owners_db.email_regexp,
  882. approval_needed=input_api.is_committing)
  883. owner_email = owner_email or input_api.change.author_email
  884. reviewers_plus_owner = set(reviewers)
  885. if owner_email:
  886. reviewers_plus_owner.add(owner_email)
  887. missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
  888. reviewers_plus_owner)
  889. # We strip the /DEPS part that was added by
  890. # _FilesToCheckForIncomingDeps to fake a path to a file in a
  891. # directory.
  892. def StripDeps(path):
  893. start_deps = path.rfind('/DEPS')
  894. if start_deps != -1:
  895. return path[:start_deps]
  896. else:
  897. return path
  898. unapproved_dependencies = ["'+%s'," % StripDeps(path)
  899. for path in missing_files]
  900. if unapproved_dependencies:
  901. output_list = [
  902. output('Missing LGTM from OWNERS of dependencies added to DEPS:\n %s' %
  903. '\n '.join(sorted(unapproved_dependencies)))]
  904. if not input_api.is_committing:
  905. suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
  906. output_list.append(output(
  907. 'Suggested missing target path OWNERS:\n %s' %
  908. '\n '.join(suggested_owners or [])))
  909. return output_list
  910. return []
  911. def _CheckSpamLogging(input_api, output_api):
  912. file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
  913. black_list = (_EXCLUDED_PATHS +
  914. _TEST_CODE_EXCLUDED_PATHS +
  915. input_api.DEFAULT_BLACK_LIST +
  916. (r"^base[\\\/]logging\.h$",
  917. r"^base[\\\/]logging\.cc$",
  918. r"^chrome[\\\/]app[\\\/]chrome_main_delegate\.cc$",
  919. r"^chrome[\\\/]browser[\\\/]chrome_browser_main\.cc$",
  920. r"^chrome[\\\/]browser[\\\/]ui[\\\/]startup[\\\/]"
  921. r"startup_browser_creator\.cc$",
  922. r"^chrome[\\\/]installer[\\\/]setup[\\\/].*",
  923. r"chrome[\\\/]browser[\\\/]diagnostics[\\\/]" +
  924. r"diagnostics_writer\.cc$",
  925. r"^chrome_elf[\\\/]dll_hash[\\\/]dll_hash_main\.cc$",
  926. r"^chromecast[\\\/]",
  927. r"^cloud_print[\\\/]",
  928. r"^components[\\\/]html_viewer[\\\/]"
  929. r"web_test_delegate_impl\.cc$",
  930. r"^content[\\\/]common[\\\/]gpu[\\\/]client[\\\/]"
  931. r"gl_helper_benchmark\.cc$",
  932. r"^courgette[\\\/]courgette_tool\.cc$",
  933. r"^extensions[\\\/]renderer[\\\/]logging_native_handler\.cc$",
  934. r"^ipc[\\\/]ipc_logging\.cc$",
  935. r"^native_client_sdk[\\\/]",
  936. r"^remoting[\\\/]base[\\\/]logging\.h$",
  937. r"^remoting[\\\/]host[\\\/].*",
  938. r"^sandbox[\\\/]linux[\\\/].*",
  939. r"^tools[\\\/]",
  940. r"^ui[\\\/]aura[\\\/]bench[\\\/]bench_main\.cc$",
  941. r"^storage[\\\/]browser[\\\/]fileapi[\\\/]" +
  942. r"dump_file_system.cc$",))
  943. source_file_filter = lambda x: input_api.FilterSourceFile(
  944. x, white_list=(file_inclusion_pattern,), black_list=black_list)
  945. log_info = []
  946. printf = []
  947. for f in input_api.AffectedSourceFiles(source_file_filter):
  948. contents = input_api.ReadFile(f, 'rb')
  949. if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
  950. log_info.append(f.LocalPath())
  951. elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", contents):
  952. log_info.append(f.LocalPath())
  953. if input_api.re.search(r"\bprintf\(", contents):
  954. printf.append(f.LocalPath())
  955. elif input_api.re.search(r"\bfprintf\((stdout|stderr)", contents):
  956. printf.append(f.LocalPath())
  957. if log_info:
  958. return [output_api.PresubmitError(
  959. 'These files spam the console log with LOG(INFO):',
  960. items=log_info)]
  961. if printf:
  962. return [output_api.PresubmitError(
  963. 'These files spam the console log with printf/fprintf:',
  964. items=printf)]
  965. return []
  966. def _CheckForAnonymousVariables(input_api, output_api):
  967. """These types are all expected to hold locks while in scope and
  968. so should never be anonymous (which causes them to be immediately
  969. destroyed)."""
  970. they_who_must_be_named = [
  971. 'base::AutoLock',
  972. 'base::AutoReset',
  973. 'base::AutoUnlock',
  974. 'SkAutoAlphaRestore',
  975. 'SkAutoBitmapShaderInstall',
  976. 'SkAutoBlitterChoose',
  977. 'SkAutoBounderCommit',
  978. 'SkAutoCallProc',
  979. 'SkAutoCanvasRestore',
  980. 'SkAutoCommentBlock',
  981. 'SkAutoDescriptor',
  982. 'SkAutoDisableDirectionCheck',
  983. 'SkAutoDisableOvalCheck',
  984. 'SkAutoFree',
  985. 'SkAutoGlyphCache',
  986. 'SkAutoHDC',
  987. 'SkAutoLockColors',
  988. 'SkAutoLockPixels',
  989. 'SkAutoMalloc',
  990. 'SkAutoMaskFreeImage',
  991. 'SkAutoMutexAcquire',
  992. 'SkAutoPathBoundsUpdate',
  993. 'SkAutoPDFRelease',
  994. 'SkAutoRasterClipValidate',
  995. 'SkAutoRef',
  996. 'SkAutoTime',
  997. 'SkAutoTrace',
  998. 'SkAutoUnref',
  999. ]
  1000. anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named)
  1001. # bad: base::AutoLock(lock.get());
  1002. # not bad: base::AutoLock lock(lock.get());
  1003. bad_pattern = input_api.re.compile(anonymous)
  1004. # good: new base::AutoLock(lock.get())
  1005. good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous)
  1006. errors = []
  1007. for f in input_api.AffectedFiles():
  1008. if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
  1009. continue
  1010. for linenum, line in f.ChangedContents():
  1011. if bad_pattern.search(line) and not good_pattern.search(line):
  1012. errors.append('%s:%d' % (f.LocalPath(), linenum))
  1013. if errors:
  1014. return [output_api.PresubmitError(
  1015. 'These lines create anonymous variables that need to be named:',
  1016. items=errors)]
  1017. return []
  1018. def _CheckCygwinShell(input_api, output_api):
  1019. source_file_filter = lambda x: input_api.FilterSourceFile(
  1020. x, white_list=(r'.+\.(gyp|gypi)$',))
  1021. cygwin_shell = []
  1022. for f in input_api.AffectedSourceFiles(source_file_filter):
  1023. for linenum, line in f.ChangedContents():
  1024. if 'msvs_cygwin_shell' in line:
  1025. cygwin_shell.append(f.LocalPath())
  1026. break
  1027. if cygwin_shell:
  1028. return [output_api.PresubmitError(
  1029. 'These files should not use msvs_cygwin_shell (the default is 0):',
  1030. items=cygwin_shell)]
  1031. return []
  1032. def _CheckUserActionUpdate(input_api, output_api):
  1033. """Checks if any new user action has been added."""
  1034. if any('actions.xml' == input_api.os_path.basename(f) for f in
  1035. input_api.LocalPaths()):
  1036. # If actions.xml is already included in the changelist, the PRESUBMIT
  1037. # for actions.xml will do a more complete presubmit check.
  1038. return []
  1039. file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm'))
  1040. action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)'
  1041. current_actions = None
  1042. for f in input_api.AffectedFiles(file_filter=file_filter):
  1043. for line_num, line in f.ChangedContents():
  1044. match = input_api.re.search(action_re, line)
  1045. if match:
  1046. # Loads contents in tools/metrics/actions/actions.xml to memory. It's
  1047. # loaded only once.
  1048. if not current_actions:
  1049. with open('tools/metrics/actions/actions.xml') as actions_f:
  1050. current_actions = actions_f.read()
  1051. # Search for the matched user action name in |current_actions|.
  1052. for action_name in match.groups():
  1053. action = 'name="{0}"'.format(action_name)
  1054. if action not in current_actions:
  1055. return [output_api.PresubmitPromptWarning(
  1056. 'File %s line %d: %s is missing in '
  1057. 'tools/metrics/actions/actions.xml. Please run '
  1058. 'tools/metrics/actions/extract_actions.py to update.'
  1059. % (f.LocalPath(), line_num, action_name))]
  1060. return []
  1061. def _GetJSONParseError(input_api, filename, eat_comments=True):
  1062. try:
  1063. contents = input_api.ReadFile(filename)
  1064. if eat_comments:
  1065. import sys
  1066. original_sys_path = sys.path
  1067. try:
  1068. sys.path = sys.path + [input_api.os_path.join(
  1069. input_api.PresubmitLocalPath(),
  1070. 'tools', 'json_comment_eater')]
  1071. import json_comment_eater
  1072. finally:
  1073. sys.path = original_sys_path
  1074. contents = json_comment_eater.Nom(contents)
  1075. input_api.json.loads(contents)
  1076. except ValueError as e:
  1077. return e
  1078. return None
  1079. def _GetIDLParseError(input_api, filename):
  1080. try:
  1081. contents = input_api.ReadFile(filename)
  1082. idl_schema = input_api.os_path.join(
  1083. input_api.PresubmitLocalPath(),
  1084. 'tools', 'json_schema_compiler', 'idl_schema.py')
  1085. process = input_api.subprocess.Popen(
  1086. [input_api.python_executable, idl_schema],
  1087. stdin=input_api.subprocess.PIPE,
  1088. stdout=input_api.subprocess.PIPE,
  1089. stderr=input_api.subprocess.PIPE,
  1090. universal_newlines=True)
  1091. (_, error) = process.communicate(input=contents)
  1092. return error or None
  1093. except ValueError as e:
  1094. return e
  1095. def _CheckParseErrors(input_api, output_api):
  1096. """Check that IDL and JSON files do not contain syntax errors."""
  1097. actions = {
  1098. '.idl': _GetIDLParseError,
  1099. '.json': _GetJSONParseError,
  1100. }
  1101. # These paths contain test data and other known invalid JSON files.
  1102. excluded_patterns = [
  1103. r'test[\\\/]data[\\\/]',
  1104. r'^components[\\\/]policy[\\\/]resources[\\\/]policy_templates\.json$',
  1105. ]
  1106. # Most JSON files are preprocessed and support comments, but these do not.
  1107. json_no_comments_patterns = [
  1108. r'^testing[\\\/]',
  1109. ]
  1110. # Only run IDL checker on files in these directories.
  1111. idl_included_patterns = [
  1112. r'^chrome[\\\/]common[\\\/]extensions[\\\/]api[\\\/]',
  1113. r'^extensions[\\\/]common[\\\/]api[\\\/]',
  1114. ]
  1115. def get_action(affected_file):
  1116. filename = affected_file.LocalPath()
  1117. return actions.get(input_api.os_path.splitext(filename)[1])
  1118. def MatchesFile(patterns, path):
  1119. for pattern in patterns:
  1120. if input_api.re.search(pattern, path):
  1121. return True
  1122. return False
  1123. def FilterFile(affected_file):
  1124. action = get_action(affected_file)
  1125. if not action:
  1126. return False
  1127. path = affected_file.LocalPath()
  1128. if MatchesFile(excluded_patterns, path):
  1129. return False
  1130. if (action == _GetIDLParseError and
  1131. not MatchesFile(idl_included_patterns, path)):
  1132. return False
  1133. return True
  1134. results = []
  1135. for affected_file in input_api.AffectedFiles(
  1136. file_filter=FilterFile, include_deletes=False):
  1137. action = get_action(affected_file)
  1138. kwargs = {}
  1139. if (action == _GetJSONParseError and
  1140. MatchesFile(json_no_comments_patterns, affected_file.LocalPath())):
  1141. kwargs['eat_comments'] = False
  1142. parse_error = action(input_api,
  1143. affected_file.AbsoluteLocalPath(),
  1144. **kwargs)
  1145. if parse_error:
  1146. results.append(output_api.PresubmitError('%s could not be parsed: %s' %
  1147. (affected_file.LocalPath(), parse_error)))
  1148. return results
  1149. def _CheckJavaStyle(input_api, output_api):
  1150. """Runs checkstyle on changed java files and returns errors if any exist."""
  1151. import sys
  1152. original_sys_path = sys.path
  1153. try:
  1154. sys.path = sys.path + [input_api.os_path.join(
  1155. input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle')]
  1156. import checkstyle
  1157. finally:
  1158. # Restore sys.path to what it was before.
  1159. sys.path = original_sys_path
  1160. return checkstyle.RunCheckstyle(
  1161. input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml',
  1162. black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
  1163. def _CheckAndroidCrLogUsage(input_api, output_api):
  1164. """Checks that new logs using org.chromium.base.Log:
  1165. - Are using 'TAG' as variable name for the tags (warn)
  1166. - Are using the suggested name format for the tags: "cr.<PackageTag>" (warn)
  1167. - Are using a tag that is shorter than 23 characters (error)
  1168. """
  1169. cr_log_import_pattern = input_api.re.compile(
  1170. r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE)
  1171. class_in_base_pattern = input_api.re.compile(
  1172. r'^package org\.chromium\.base;$', input_api.re.MULTILINE)
  1173. has_some_log_import_pattern = input_api.re.compile(
  1174. r'^import .*\.Log;$', input_api.re.MULTILINE)
  1175. # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");`
  1176. log_call_pattern = input_api.re.compile(r'^\s*Log\.\w\((?P<tag>\"?\w+\"?)\,')
  1177. log_decl_pattern = input_api.re.compile(
  1178. r'^\s*private static final String TAG = "(?P<name>(.*)")',
  1179. input_api.re.MULTILINE)
  1180. log_name_pattern = input_api.re.compile(r'^cr[.\w]*')
  1181. REF_MSG = ('See base/android/java/src/org/chromium/base/README_logging.md '
  1182. 'or contact dgn@chromium.org for more info.')
  1183. sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',))
  1184. tag_decl_errors = []
  1185. tag_length_errors = []
  1186. tag_errors = []
  1187. util_log_errors = []
  1188. for f in input_api.AffectedSourceFiles(sources):
  1189. file_content = input_api.ReadFile(f)
  1190. has_modified_logs = False
  1191. # Per line checks
  1192. if (cr_log_import_pattern.search(file_content) or
  1193. (class_in_base_pattern.search(file_content) and
  1194. not has_some_log_import_pattern.search(file_content))):
  1195. # Checks to run for files using cr log
  1196. for line_num, line in f.ChangedContents():
  1197. # Check if the new line is doing some logging
  1198. match = log_call_pattern.search(line)
  1199. if match:
  1200. has_modified_logs = True
  1201. # Make sure it uses "TAG"
  1202. if not match.group('tag') == 'TAG':
  1203. tag_errors.append("%s:%d" % (f.LocalPath(), line_num))
  1204. else:
  1205. # Report non cr Log function calls in changed lines
  1206. for line_num, line in f.ChangedContents():
  1207. if log_call_pattern.search(line):
  1208. util_log_errors.append("%s:%d" % (f.LocalPath(), line_num))
  1209. # Per file checks
  1210. if has_modified_logs:
  1211. # Make sure the tag is using the "cr" prefix and is not too long
  1212. match = log_decl_pattern.search(file_content)
  1213. tag_name = match.group('name') if match else ''
  1214. if not log_name_pattern.search(tag_name ):
  1215. tag_decl_errors.append(f.LocalPath())
  1216. if len(tag_name) > 23:
  1217. tag_length_errors.append(f.LocalPath())
  1218. results = []
  1219. if tag_decl_errors:
  1220. results.append(output_api.PresubmitPromptWarning(
  1221. 'Please define your tags using the suggested format: .\n'
  1222. '"private static final String TAG = "cr.<package tag>".\n' + REF_MSG,
  1223. tag_decl_errors))
  1224. if tag_length_errors:
  1225. results.append(output_api.PresubmitError(
  1226. 'The tag length is restricted by the system to be at most '
  1227. '23 characters.\n' + REF_MSG,
  1228. tag_length_errors))
  1229. if tag_errors:
  1230. results.append(output_api.PresubmitPromptWarning(
  1231. 'Please use a variable named "TAG" for your log tags.\n' + REF_MSG,
  1232. tag_errors))
  1233. if util_log_errors:
  1234. results.append(output_api.PresubmitPromptWarning(
  1235. 'Please use org.chromium.base.Log for new logs.\n' + REF_MSG,
  1236. util_log_errors))
  1237. return results
  1238. def _CheckForCopyrightedCode(input_api, output_api):
  1239. """Verifies that newly added code doesn't contain copyrighted material
  1240. and is properly licensed under the standard Chromium license.
  1241. As there can be false positives, we maintain a whitelist file. This check
  1242. also verifies that the whitelist file is up to date.
  1243. """
  1244. import sys
  1245. original_sys_path = sys.path
  1246. try:
  1247. sys.path = sys.path + [input_api.os_path.join(
  1248. input_api.PresubmitLocalPath(), 'tools')]
  1249. from copyright_scanner import copyright_scanner
  1250. finally:
  1251. # Restore sys.path to what it was before.
  1252. sys.path = original_sys_path
  1253. return copyright_scanner.ScanAtPresubmit(input_api, output_api)
  1254. def _CheckSingletonInHeaders(input_api, output_api):
  1255. """Checks to make sure no header files have |Singleton<|."""
  1256. def FileFilter(affected_file):
  1257. # It's ok for base/memory/singleton.h to have |Singleton<|.
  1258. black_list = (_EXCLUDED_PATHS +
  1259. input_api.DEFAULT_BLACK_LIST +
  1260. (r"^base[\\\/]memory[\\\/]singleton\.h$",))
  1261. return input_api.FilterSourceFile(affected_file, black_list=black_list)
  1262. pattern = input_api.re.compile(r'(?<!class\s)Singleton\s*<')
  1263. files = []
  1264. for f in input_api.AffectedSourceFiles(FileFilter):
  1265. if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx') or
  1266. f.LocalPath().endswith('.hpp') or f.LocalPath().endswith('.inl')):
  1267. contents = input_api.ReadFile(f)
  1268. for line in contents.splitlines(False):
  1269. if (not input_api.re.match(r'//', line) and # Strip C++ comment.
  1270. pattern.search(line)):
  1271. files.append(f)
  1272. break
  1273. if files:
  1274. return [ output_api.PresubmitError(
  1275. 'Found Singleton<T> in the following header files.\n' +
  1276. 'Please move them to an appropriate source file so that the ' +
  1277. 'template gets instantiated in a single compilation unit.',
  1278. files) ]
  1279. return []
  1280. _DEPRECATED_CSS = [
  1281. # Values
  1282. ( "-webkit-box", "flex" ),
  1283. ( "-webkit-inline-box", "inline-flex" ),
  1284. ( "-webkit-flex", "flex" ),
  1285. ( "-webkit-inline-flex", "inline-flex" ),
  1286. ( "-webkit-min-content", "min-content" ),
  1287. ( "-webkit-max-content", "max-content" ),
  1288. # Properties
  1289. ( "-webkit-background-clip", "background-clip" ),
  1290. ( "-webkit-background-origin", "background-origin" ),
  1291. ( "-webkit-background-size", "background-size" ),
  1292. ( "-webkit-box-shadow", "box-shadow" ),
  1293. # Functions
  1294. ( "-webkit-gradient", "gradient" ),
  1295. ( "-webkit-repeating-gradient", "repeating-gradient" ),
  1296. ( "-webkit-linear-gradient", "linear-gradient" ),
  1297. ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ),
  1298. ( "-webkit-radial-gradient", "radial-gradient" ),
  1299. ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ),
  1300. ]
  1301. def _CheckNoDeprecatedCSS(input_api, output_api):
  1302. """ Make sure that we don't use deprecated CSS
  1303. properties, functions or values. Our external
  1304. documentation and iOS CSS for dom distiller
  1305. (reader mode) are ignored by the hooks as it
  1306. needs to be consumed by WebKit. """
  1307. results = []
  1308. file_inclusion_pattern = (r".+\.css$",)
  1309. black_list = (_EXCLUDED_PATHS +
  1310. _TEST_CODE_EXCLUDED_PATHS +
  1311. input_api.DEFAULT_BLACK_LIST +
  1312. (r"^chrome/common/extensions/docs",
  1313. r"^chrome/docs",
  1314. r"^components/dom_distiller/core/css/distilledpage_ios.css",
  1315. r"^native_client_sdk"))
  1316. file_filter = lambda f: input_api.FilterSourceFile(
  1317. f, white_list=file_inclusion_pattern, black_list=black_list)
  1318. for fpath in input_api.AffectedFiles(file_filter=file_filter):
  1319. for line_num, line in fpath.ChangedContents():
  1320. for (deprecated_value, value) in _DEPRECATED_CSS:
  1321. if deprecated_value in line:
  1322. results.append(output_api.PresubmitError(
  1323. "%s:%d: Use of deprecated CSS %s, use %s instead" %
  1324. (fpath.LocalPath(), line_num, deprecated_value, value)))
  1325. return results
  1326. _DEPRECATED_JS = [
  1327. ( "__lookupGetter__", "Object.getOwnPropertyDescriptor" ),
  1328. ( "__defineGetter__", "Object.defineProperty" ),
  1329. ( "__defineSetter__", "Object.defineProperty" ),
  1330. ]
  1331. def _CheckNoDeprecatedJS(input_api, output_api):
  1332. """Make sure that we don't use deprecated JS in Chrome code."""
  1333. results = []
  1334. file_inclusion_pattern = (r".+\.js$",) # TODO(dbeam): .html?
  1335. black_list = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
  1336. input_api.DEFAULT_BLACK_LIST)
  1337. file_filter = lambda f: input_api.FilterSourceFile(
  1338. f, white_list=file_inclusion_pattern, black_list=black_list)
  1339. for fpath in input_api.AffectedFiles(file_filter=file_filter):
  1340. for lnum, line in fpath.ChangedContents():
  1341. for (deprecated, replacement) in _DEPRECATED_JS:
  1342. if deprecated in line:
  1343. results.append(output_api.PresubmitError(
  1344. "%s:%d: Use of deprecated JS %s, use %s instead" %
  1345. (fpath.LocalPath(), lnum, deprecated, replacement)))
  1346. return results
  1347. def _AndroidSpecificOnUploadChecks(input_api, output_api):
  1348. """Groups checks that target android code."""
  1349. results = []
  1350. results.extend(_CheckAndroidCrLogUsage(input_api, output_api))
  1351. return results
  1352. def _CommonChecks(input_api, output_api):
  1353. """Checks common to both upload and commit."""
  1354. results = []
  1355. results.extend(input_api.canned_checks.PanProjectChecks(
  1356. input_api, output_api,
  1357. excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS))
  1358. results.extend(_CheckAuthorizedAuthor(input_api, output_api))
  1359. results.extend(
  1360. _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
  1361. results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
  1362. results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
  1363. results.extend(_CheckNoNewWStrings(input_api, output_api))
  1364. results.extend(_CheckNoDEPSGIT(input_api, output_api))
  1365. results.extend(_CheckNoBannedFunctions(input_api, output_api))
  1366. results.extend(_CheckNoPragmaOnce(input_api, output_api))
  1367. results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
  1368. results.extend(_CheckUnwantedDependencies(input_api, output_api))
  1369. results.extend(_CheckFilePermissions(input_api, output_api))
  1370. results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
  1371. results.extend(_CheckIncludeOrder(input_api, output_api))
  1372. results.extend(_CheckForVersionControlConflicts(input_api, output_api))
  1373. results.extend(_CheckPatchFiles(input_api, output_api))
  1374. results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
  1375. results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
  1376. results.extend(_CheckForInvalidOSMacros(input_api, output_api))
  1377. results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api))
  1378. # TODO(danakj): Remove this when base/move.h is removed.
  1379. results.extend(_CheckForUsingSideEffectsOfPass(input_api, output_api))
  1380. results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
  1381. results.extend(
  1382. input_api.canned_checks.CheckChangeHasNoTabs(
  1383. input_api,
  1384. output_api,
  1385. source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
  1386. results.extend(_CheckSpamLogging(input_api, output_api))
  1387. results.extend(_CheckForAnonymousVariables(input_api, output_api))
  1388. results.extend(_CheckCygwinShell(input_api, output_api))
  1389. results.extend(_CheckUserActionUpdate(input_api, output_api))
  1390. results.extend(_CheckNoDeprecatedCSS(input_api, output_api))
  1391. results.extend(_CheckNoDeprecatedJS(input_api, output_api))
  1392. results.extend(_CheckParseErrors(input_api, output_api))
  1393. results.extend(_CheckForIPCRules(input_api, output_api))
  1394. results.extend(_CheckForCopyrightedCode(input_api, output_api))
  1395. results.extend(_CheckForWindowsLineEndings(input_api, output_api))
  1396. results.extend(_CheckSingletonInHeaders(input_api, output_api))
  1397. if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
  1398. results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
  1399. input_api, output_api,
  1400. input_api.PresubmitLocalPath(),
  1401. whitelist=[r'^PRESUBMIT_test\.py$']))
  1402. return results
  1403. def _CheckAuthorizedAuthor(input_api, output_api):
  1404. """For non-googler/chromites committers, verify the author's email address is
  1405. in AUTHORS.
  1406. """
  1407. # TODO(maruel): Add it to input_api?
  1408. import fnmatch
  1409. author = input_api.change.author_email
  1410. if not author:
  1411. input_api.logging.info('No author, skipping AUTHOR check')
  1412. return []
  1413. authors_path = input_api.os_path.join(
  1414. input_api.PresubmitLocalPath(), 'AUTHORS')
  1415. valid_authors = (
  1416. input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
  1417. for line in open(authors_path))
  1418. valid_authors = [item.group(1).lower() for item in valid_authors if item]
  1419. if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
  1420. input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
  1421. return [output_api.PresubmitPromptWarning(
  1422. ('%s is not in AUTHORS file. If you are a new contributor, please visit'
  1423. '\n'
  1424. 'http://www.chromium.org/developers/contributing-code and read the '
  1425. '"Legal" section\n'
  1426. 'If you are a chromite, verify the contributor signed the CLA.') %
  1427. author)]
  1428. return []
  1429. def _CheckPatchFiles(input_api, output_api):
  1430. problems = [f.LocalPath() for f in input_api.AffectedFiles()
  1431. if f.LocalPath().endswith(('.orig', '.rej'))]
  1432. if problems:
  1433. return [output_api.PresubmitError(
  1434. "Don't commit .rej and .orig files.", problems)]
  1435. else:
  1436. return []
  1437. def _DidYouMeanOSMacro(bad_macro):
  1438. try:
  1439. return {'A': 'OS_ANDROID',
  1440. 'B': 'OS_BSD',
  1441. 'C': 'OS_CHROMEOS',
  1442. 'F': 'OS_FREEBSD',
  1443. 'L': 'OS_LINUX',
  1444. 'M': 'OS_MACOSX',
  1445. 'N': 'OS_NACL',
  1446. 'O': 'OS_OPENBSD',
  1447. 'P': 'OS_POSIX',
  1448. 'S': 'OS_SOLARIS',
  1449. 'W': 'OS_WIN'}[bad_macro[3].upper()]
  1450. except KeyError:
  1451. return ''
  1452. def _CheckForInvalidOSMacrosInFile(input_api, f):
  1453. """Check for sensible looking, totally invalid OS macros."""
  1454. preprocessor_statement = input_api.re.compile(r'^\s*#')
  1455. os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
  1456. results = []
  1457. for lnum, line in f.ChangedContents():
  1458. if preprocessor_statement.search(line):
  1459. for match in os_macro.finditer(line):
  1460. if not match.group(1) in _VALID_OS_MACROS:
  1461. good = _DidYouMeanOSMacro(match.group(1))
  1462. did_you_mean = ' (did you mean %s?)' % good if good else ''
  1463. results.append(' %s:%d %s%s' % (f.LocalPath(),
  1464. lnum,
  1465. match.group(1),
  1466. did_you_mean))
  1467. return results
  1468. def _CheckForInvalidOSMacros(input_api, output_api):
  1469. """Check all affected files for invalid OS macros."""
  1470. bad_macros = []
  1471. for f in input_api.AffectedFiles():
  1472. if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
  1473. bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
  1474. if not bad_macros:
  1475. return []
  1476. return [output_api.PresubmitError(
  1477. 'Possibly invalid OS macro[s] found. Please fix your code\n'
  1478. 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
  1479. def _CheckForInvalidIfDefinedMacrosInFile(input_api, f):
  1480. """Check all affected files for invalid "if defined" macros."""
  1481. ALWAYS_DEFINED_MACROS = (
  1482. "TARGET_CPU_PPC",
  1483. "TARGET_CPU_PPC64",
  1484. "TARGET_CPU_68K",
  1485. "TARGET_CPU_X86",
  1486. "TARGET_CPU_ARM",
  1487. "TARGET_CPU_MIPS",
  1488. "TARGET_CPU_SPARC",
  1489. "TARGET_CPU_ALPHA",
  1490. "TARGET_IPHONE_SIMULATOR",
  1491. "TARGET_OS_EMBEDDED",
  1492. "TARGET_OS_IPHONE",
  1493. "TARGET_OS_MAC",
  1494. "TARGET_OS_UNIX",
  1495. "TARGET_OS_WIN32",
  1496. )
  1497. ifdef_macro = input_api.re.compile(r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)')
  1498. results = []
  1499. for lnum, line in f.ChangedContents():
  1500. for match in ifdef_macro.finditer(line):
  1501. if match.group(1) in ALWAYS_DEFINED_MACROS:
  1502. always_defined = ' %s is always defined. ' % match.group(1)
  1503. did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1)
  1504. results.append(' %s:%d %s\n\t%s' % (f.LocalPath(),
  1505. lnum,
  1506. always_defined,
  1507. did_you_mean))
  1508. return results
  1509. def _CheckForInvalidIfDefinedMacros(input_api, output_api):
  1510. """Check all affected files for invalid "if defined" macros."""
  1511. bad_macros = []
  1512. for f in input_api.AffectedFiles():
  1513. if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
  1514. bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f))
  1515. if not bad_macros:
  1516. return []
  1517. return [output_api.PresubmitError(
  1518. 'Found ifdef check on always-defined macro[s]. Please fix your code\n'
  1519. 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.',
  1520. bad_macros)]
  1521. def _CheckForUsingSideEffectsOfPass(input_api, output_api):
  1522. """Check all affected files for using side effects of Pass."""
  1523. errors = []
  1524. for f in input_api.AffectedFiles():
  1525. if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
  1526. for lnum, line in f.ChangedContents():
  1527. # Disallow Foo(*my_scoped_thing.Pass()); See crbug.com/418297.
  1528. if input_api.re.search(r'\*[a-zA-Z0-9_]+\.Pass\(\)', line):
  1529. errors.append(output_api.PresubmitError(
  1530. ('%s:%d uses *foo.Pass() to delete the contents of scoped_ptr. ' +
  1531. 'See crbug.com/418297.') % (f.LocalPath(), lnum)))
  1532. return errors
  1533. def _CheckForIPCRules(input_api, output_api):
  1534. """Check for same IPC rules described in
  1535. http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc
  1536. """
  1537. base_pattern = r'IPC_ENUM_TRAITS\('
  1538. inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern)
  1539. comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern)
  1540. problems = []
  1541. for f in input_api.AffectedSourceFiles(None):
  1542. local_path = f.LocalPath()
  1543. if not local_path.endswith('.h'):
  1544. continue
  1545. for line_number, line in f.ChangedContents():
  1546. if inclusion_pattern.search(line) and not comment_pattern.search(line):
  1547. problems.append(
  1548. '%s:%d\n %s' % (local_path, line_number, line.strip()))
  1549. if problems:
  1550. return [output_api.PresubmitPromptWarning(
  1551. _IPC_ENUM_TRAITS_DEPRECATED, problems)]
  1552. else:
  1553. return []
  1554. def _CheckForWindowsLineEndings(input_api, output_api):
  1555. """Check source code and known ascii text files for Windows style line
  1556. endings.
  1557. """
  1558. known_text_files = r'.*\.(txt|html|htm|mhtml|py|gyp|gypi|gn|isolate)$'
  1559. file_inclusion_pattern = (
  1560. known_text_files,
  1561. r'.+%s' % _IMPLEMENTATION_EXTENSIONS
  1562. )
  1563. filter = lambda f: input_api.FilterSourceFile(
  1564. f, white_list=file_inclusion_pattern, black_list=None)
  1565. files = [f.LocalPath() for f in
  1566. input_api.AffectedSourceFiles(filter)]
  1567. problems = []
  1568. for file in files:
  1569. fp = open(file, 'r')
  1570. for line in fp:
  1571. if line.endswith('\r\n'):
  1572. problems.append(file)
  1573. break
  1574. fp.close()
  1575. if problems:
  1576. return [output_api.PresubmitPromptWarning('Are you sure that you want '
  1577. 'these files to contain Windows style line endings?\n' +
  1578. '\n'.join(problems))]
  1579. return []
  1580. def CheckChangeOnUpload(input_api, output_api):
  1581. results = []
  1582. results.extend(_CommonChecks(input_api, output_api))
  1583. results.extend(_CheckValidHostsInDEPS(input_api, output_api))
  1584. results.extend(_CheckJavaStyle(input_api, output_api))
  1585. results.extend(
  1586. input_api.canned_checks.CheckGNFormatted(input_api, output_api))
  1587. results.extend(_CheckUmaHistogramChanges(input_api, output_api))
  1588. results.extend(_AndroidSpecificOnUploadChecks(input_api, output_api))
  1589. return results
  1590. def GetTryServerMasterForBot(bot):
  1591. """Returns the Try Server master for the given bot.
  1592. It tries to guess the master from the bot name, but may still fail
  1593. and return None. There is no longer a default master.
  1594. """
  1595. # Potentially ambiguous bot names are listed explicitly.
  1596. master_map = {
  1597. 'chromium_presubmit': 'tryserver.chromium.linux',
  1598. 'blink_presubmit': 'tryserver.chromium.linux',
  1599. 'tools_build_presubmit': 'tryserver.chromium.linux',
  1600. }
  1601. master = master_map.get(bot)
  1602. if not master:
  1603. if 'linux' in bot or 'android' in bot or 'presubmit' in bot:
  1604. master = 'tryserver.chromium.linux'
  1605. elif 'win' in bot:
  1606. master = 'tryserver.chromium.win'
  1607. elif 'mac' in bot or 'ios' in bot:
  1608. master = 'tryserver.chromium.mac'
  1609. return master
  1610. def GetDefaultTryConfigs(bots):
  1611. """Returns a list of ('bot', set(['tests']), filtered by [bots].
  1612. """
  1613. builders_and_tests = dict((bot, set(['defaulttests'])) for bot in bots)
  1614. # Build up the mapping from tryserver master to bot/test.
  1615. out = dict()
  1616. for bot, tests in builders_and_tests.iteritems():
  1617. out.setdefault(GetTryServerMasterForBot(bot), {})[bot] = tests
  1618. return out
  1619. def CheckChangeOnCommit(input_api, output_api):
  1620. results = []
  1621. results.extend(_CommonChecks(input_api, output_api))
  1622. # TODO(thestig) temporarily disabled, doesn't work in third_party/
  1623. #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
  1624. # input_api, output_api, sources))
  1625. # Make sure the tree is 'open'.
  1626. results.extend(input_api.canned_checks.CheckTreeIsOpen(
  1627. input_api,
  1628. output_api,
  1629. json_url='http://chromium-status.appspot.com/current?format=json'))
  1630. results.extend(input_api.canned_checks.CheckChangeHasBugField(
  1631. input_api, output_api))
  1632. results.extend(input_api.canned_checks.CheckChangeHasDescription(
  1633. input_api, output_api))
  1634. return results
  1635. def GetPreferredTryMasters(project, change):
  1636. import json
  1637. import os.path
  1638. import platform
  1639. import subprocess
  1640. cq_config_path = os.path.join(
  1641. change.RepositoryRoot(), 'infra', 'config', 'cq.cfg')
  1642. # commit_queue.py below is a script in depot_tools directory, which has a
  1643. # 'builders' command to retrieve a list of CQ builders from the CQ config.
  1644. is_win = platform.system() == 'Windows'
  1645. masters = json.loads(subprocess.check_output(
  1646. ['commit_queue', 'builders', cq_config_path], shell=is_win))
  1647. try_config = {}
  1648. for master in masters:
  1649. try_config.setdefault(master, {})
  1650. for builder in masters[master]:
  1651. # Do not trigger presubmit builders, since they're likely to fail
  1652. # (e.g. OWNERS checks before finished code review), and we're
  1653. # running local presubmit anyway.
  1654. if 'presubmit' not in builder:
  1655. try_config[master][builder] = ['defaulttests']
  1656. return try_config