Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / cc / PRESUBMIT.py
blob76358f39b024f264920e9c80da4e690b585dc21e
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.
5 """Top-level presubmit script for cc.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
8 details on the presubmit API built into gcl.
9 """
11 import re
12 import string
14 CC_SOURCE_FILES=(r'^cc/.*\.(cc|h)$',)
15 CC_PERF_TEST =(r'^.*_perftest.*\.(cc|h)$',)
17 def CheckChangeLintsClean(input_api, output_api):
18 input_api.cpplint._cpplint_state.ResetErrorCounts() # reset global state
19 source_filter = lambda x: input_api.FilterSourceFile(
20 x, white_list=CC_SOURCE_FILES, black_list=None)
21 files = [f.AbsoluteLocalPath() for f in
22 input_api.AffectedSourceFiles(source_filter)]
23 level = 1 # strict, but just warn
25 for file_name in files:
26 input_api.cpplint.ProcessFile(file_name, level)
28 if not input_api.cpplint._cpplint_state.error_count:
29 return []
31 return [output_api.PresubmitPromptWarning(
32 'Changelist failed cpplint.py check.')]
34 def CheckAsserts(input_api, output_api, white_list=CC_SOURCE_FILES, black_list=None):
35 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
36 source_file_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list)
38 assert_files = []
39 notreached_files = []
41 for f in input_api.AffectedSourceFiles(source_file_filter):
42 contents = input_api.ReadFile(f, 'rb')
43 # WebKit ASSERT() is not allowed.
44 if re.search(r"\bASSERT\(", contents):
45 assert_files.append(f.LocalPath())
46 # WebKit ASSERT_NOT_REACHED() is not allowed.
47 if re.search(r"ASSERT_NOT_REACHED\(", contents):
48 notreached_files.append(f.LocalPath())
50 if assert_files:
51 return [output_api.PresubmitError(
52 'These files use ASSERT instead of using DCHECK:',
53 items=assert_files)]
54 if notreached_files:
55 return [output_api.PresubmitError(
56 'These files use ASSERT_NOT_REACHED instead of using NOTREACHED:',
57 items=notreached_files)]
58 return []
60 def CheckStdAbs(input_api, output_api,
61 white_list=CC_SOURCE_FILES, black_list=None):
62 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
63 source_file_filter = lambda x: input_api.FilterSourceFile(x,
64 white_list,
65 black_list)
67 using_std_abs_files = []
68 found_fabs_files = []
69 missing_std_prefix_files = []
71 for f in input_api.AffectedSourceFiles(source_file_filter):
72 contents = input_api.ReadFile(f, 'rb')
73 if re.search(r"using std::f?abs;", contents):
74 using_std_abs_files.append(f.LocalPath())
75 if re.search(r"\bfabsf?\(", contents):
76 found_fabs_files.append(f.LocalPath());
78 no_std_prefix = r"(?<!std::)"
79 # Matches occurrences of abs/absf/fabs/fabsf without a "std::" prefix.
80 abs_without_prefix = r"%s(\babsf?\()" % no_std_prefix
81 fabs_without_prefix = r"%s(\bfabsf?\()" % no_std_prefix
82 # Skips matching any lines that have "// NOLINT".
83 no_nolint = r"(?![^\n]*//\s+NOLINT)"
85 expression = re.compile("(%s|%s)%s" %
86 (abs_without_prefix, fabs_without_prefix, no_nolint))
87 if expression.search(contents):
88 missing_std_prefix_files.append(f.LocalPath())
90 result = []
91 if using_std_abs_files:
92 result.append(output_api.PresubmitError(
93 'These files have "using std::abs" which is not permitted.',
94 items=using_std_abs_files))
95 if found_fabs_files:
96 result.append(output_api.PresubmitError(
97 'std::abs() should be used instead of std::fabs() for consistency.',
98 items=found_fabs_files))
99 if missing_std_prefix_files:
100 result.append(output_api.PresubmitError(
101 'These files use abs(), absf(), fabs(), or fabsf() without qualifying '
102 'the std namespace. Please use std::abs() in all places.',
103 items=missing_std_prefix_files))
104 return result
106 def CheckSpamLogging(input_api,
107 output_api,
108 white_list=CC_SOURCE_FILES,
109 black_list=None):
110 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
111 source_file_filter = lambda x: input_api.FilterSourceFile(x,
112 white_list,
113 black_list)
115 log_info = []
116 printf = []
118 for f in input_api.AffectedSourceFiles(source_file_filter):
119 contents = input_api.ReadFile(f, 'rb')
120 if re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
121 log_info.append(f.LocalPath())
122 if re.search(r"\bf?printf\(", contents):
123 printf.append(f.LocalPath())
125 if log_info:
126 return [output_api.PresubmitError(
127 'These files spam the console log with LOG(INFO):',
128 items=log_info)]
129 if printf:
130 return [output_api.PresubmitError(
131 'These files spam the console log with printf/fprintf:',
132 items=printf)]
133 return []
135 def CheckPassByValue(input_api,
136 output_api,
137 white_list=CC_SOURCE_FILES,
138 black_list=None):
139 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
140 source_file_filter = lambda x: input_api.FilterSourceFile(x,
141 white_list,
142 black_list)
144 local_errors = []
146 # Well-defined simple classes containing only <= 4 ints, or <= 2 floats.
147 pass_by_value_types = ['base::Time',
148 'base::TimeTicks',
149 'gfx::Point',
150 'gfx::PointF',
151 'gfx::Rect',
152 'gfx::Size',
153 'gfx::SizeF',
154 'gfx::Vector2d',
155 'gfx::Vector2dF',
158 for f in input_api.AffectedSourceFiles(source_file_filter):
159 contents = input_api.ReadFile(f, 'rb')
160 match = re.search(
161 r'\bconst +' + '(?P<type>(%s))&' %
162 string.join(pass_by_value_types, '|'),
163 contents)
164 if match:
165 local_errors.append(output_api.PresubmitError(
166 '%s passes %s by const ref instead of by value.' %
167 (f.LocalPath(), match.group('type'))))
168 return local_errors
170 def CheckTodos(input_api, output_api):
171 errors = []
173 source_file_filter = lambda x: x
174 for f in input_api.AffectedSourceFiles(source_file_filter):
175 contents = input_api.ReadFile(f, 'rb')
176 if ('FIX'+'ME') in contents:
177 errors.append(f.LocalPath())
179 if errors:
180 return [output_api.PresubmitError(
181 'All TODO comments should be of the form TODO(name).',
182 items=errors)]
183 return []
186 def CheckChangeOnUpload(input_api, output_api):
187 results = []
188 results += CheckAsserts(input_api, output_api)
189 results += CheckStdAbs(input_api, output_api)
190 results += CheckSpamLogging(input_api, output_api, black_list=CC_PERF_TEST)
191 results += CheckPassByValue(input_api, output_api)
192 results += CheckChangeLintsClean(input_api, output_api)
193 results += CheckTodos(input_api, output_api)
194 return results
196 def GetPreferredTrySlaves(project, change):
197 return [
198 'linux_layout_rel',
199 'win_gpu',
200 'linux_gpu',
201 'mac_gpu',
202 'mac_gpu_retina',