Fix revert "[chromeos] Remove dependencies of StatisticsProvider on chrome."
[chromium-blink-merge.git] / cc / PRESUBMIT.py
blobe133e9ab5f069b79d4c5ab00c759f57a03dd51cc
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 CheckSpamLogging(input_api,
61 output_api,
62 white_list=CC_SOURCE_FILES,
63 black_list=None):
64 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
65 source_file_filter = lambda x: input_api.FilterSourceFile(x,
66 white_list,
67 black_list)
69 log_info = []
70 printf = []
72 for f in input_api.AffectedSourceFiles(source_file_filter):
73 contents = input_api.ReadFile(f, 'rb')
74 if re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
75 log_info.append(f.LocalPath())
76 if re.search(r"\bf?printf\(", contents):
77 printf.append(f.LocalPath())
79 if log_info:
80 return [output_api.PresubmitError(
81 'These files spam the console log with LOG(INFO):',
82 items=log_info)]
83 if printf:
84 return [output_api.PresubmitError(
85 'These files spam the console log with printf/fprintf:',
86 items=printf)]
87 return []
89 def CheckPassByValue(input_api,
90 output_api,
91 white_list=CC_SOURCE_FILES,
92 black_list=None):
93 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
94 source_file_filter = lambda x: input_api.FilterSourceFile(x,
95 white_list,
96 black_list)
98 local_errors = []
100 # Well-defined simple classes containing only <= 4 ints, or <= 2 floats.
101 pass_by_value_types = ['base::Time',
102 'base::TimeTicks',
103 'gfx::Point',
104 'gfx::PointF',
105 'gfx::Rect',
106 'gfx::Size',
107 'gfx::SizeF',
108 'gfx::Vector2d',
109 'gfx::Vector2dF',
112 for f in input_api.AffectedSourceFiles(source_file_filter):
113 contents = input_api.ReadFile(f, 'rb')
114 match = re.search(
115 r'\bconst +' + '(?P<type>(%s))&' %
116 string.join(pass_by_value_types, '|'),
117 contents)
118 if match:
119 local_errors.append(output_api.PresubmitError(
120 '%s passes %s by const ref instead of by value.' %
121 (f.LocalPath(), match.group('type'))))
122 return local_errors
124 def CheckChangeOnUpload(input_api, output_api):
125 results = []
126 results += CheckAsserts(input_api, output_api)
127 results += CheckSpamLogging(input_api, output_api, black_list=CC_PERF_TEST)
128 results += CheckPassByValue(input_api, output_api)
129 results += CheckChangeLintsClean(input_api, output_api)
130 return results
132 def GetPreferredTrySlaves(project, change):
133 return [
134 'linux_layout_rel',