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