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.
5 """Presubmit script for ios.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into depot_tools.
13 NULLABILITY_PATTERN = r'(nonnull|nullable|_Nullable|_Nonnull)'
14 TODO_PATTERN = r'TO[D]O\(([^\)]*)\)'
15 CRBUG_PATTERN = r'crbug\.com/\d+$'
17 '#if !defined(__has_feature) || !__has_feature(objc_arc)',
18 '#error "This file requires ARC support."',
22 def IsSubListOf(needle, hay):
23 """Returns whether there is a slice of |hay| equal to |needle|."""
24 for i, line in enumerate(hay):
26 if needle == hay[i:i+len(needle)]:
30 def _CheckARCCompilationGuard(input_api, output_api):
31 """ Checks whether new objc files have proper ARC compile guards."""
32 files_without_headers = []
33 for f in input_api.AffectedFiles():
37 _, ext = os.path.splitext(f.LocalPath())
38 if ext not in ('.m', '.mm'):
41 if not IsSubListOf(ARC_COMPILE_GUARD, f.NewContents()):
42 files_without_headers.append(f.LocalPath())
44 if not files_without_headers:
47 plural_suffix = '' if len(files_without_headers) == 1 else 's'
48 error_message = '\n'.join([
49 'Found new Objective-C implementation file%(plural)s without compile'
50 ' guard%(plural)s. Please use the following compile guard'
51 ':' % {'plural': plural_suffix}
52 ] + ARC_COMPILE_GUARD + files_without_headers) + '\n'
54 return [output_api.PresubmitError(error_message)]
57 def _CheckNullabilityAnnotations(input_api, output_api):
58 """ Checks whether there are nullability annotations in ios code."""
59 nullability_regex = input_api.re.compile(NULLABILITY_PATTERN)
62 for f in input_api.AffectedFiles():
63 for line_num, line in f.ChangedContents():
64 if nullability_regex.search(line):
65 errors.append('%s:%s' % (f.LocalPath(), line_num))
69 plural_suffix = '' if len(errors) == 1 else 's'
70 error_message = ('Found Nullability annotation%(plural)s. '
71 'Prefer DCHECKs in ios code to check for nullness:'
72 % {'plural': plural_suffix})
74 return [output_api.PresubmitPromptWarning(error_message, items=errors)]
77 def _CheckBugInToDo(input_api, output_api):
78 """ Checks whether TODOs in ios code are identified by a bug number."""
80 for f in input_api.AffectedFiles():
81 for line_num, line in f.ChangedContents():
82 if _HasToDoWithNoBug(input_api, line):
83 errors.append('%s:%s' % (f.LocalPath(), line_num))
87 plural_suffix = '' if len(errors) == 1 else 's'
88 error_message = '\n'.join([
89 'Found TO''DO%(plural)s without bug number%(plural)s (expected format is '
90 '\"TO''DO(crbug.com/######)\":' % {'plural': plural_suffix}
93 return [output_api.PresubmitError(error_message)]
96 def _HasToDoWithNoBug(input_api, line):
97 """ Returns True if TODO is not identified by a bug number."""
98 todo_regex = input_api.re.compile(TODO_PATTERN)
99 crbug_regex = input_api.re.compile(CRBUG_PATTERN)
101 todo_match = todo_regex.search(line)
104 crbug_match = crbug_regex.match(todo_match.group(1))
105 return not crbug_match
108 def CheckChangeOnUpload(input_api, output_api):
110 results.extend(_CheckBugInToDo(input_api, output_api))
111 results.extend(_CheckNullabilityAnnotations(input_api, output_api))
112 results.extend(_CheckARCCompilationGuard(input_api, output_api))