blob: b8a702fa284e6ba63f8fcefdededaaf9225d2101 [file] [log] [blame]
andrew@webrtc.org2442de12012-01-23 17:45:411# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
2#
3# Use of this source code is governed by a BSD-style license
4# that can be found in the LICENSE file in the root of the source
5# tree. An additional intellectual property rights grant can be found
6# in the file PATENTS. All contributing project authors may
7# be found in the AUTHORS file in the root of the source tree.
niklase@google.comda159d62011-05-30 11:51:348
kjellander@webrtc.org51198f12012-02-21 17:53:469def _CheckNoIOStreamInHeaders(input_api, output_api):
10 """Checks to make sure no .h files include <iostream>."""
11 files = []
12 pattern = input_api.re.compile(r'^#include\s*<iostream>',
13 input_api.re.MULTILINE)
14 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
15 if not f.LocalPath().endswith('.h'):
16 continue
17 contents = input_api.ReadFile(f)
18 if pattern.search(contents):
19 files.append(f)
20
21 if len(files):
22 return [ output_api.PresubmitError(
23 'Do not #include <iostream> in header files, since it inserts static ' +
24 'initialization into every file including the header. Instead, ' +
25 '#include <ostream>. See http://crbug.com/94794',
26 files) ]
27 return []
28
29def _CheckNoFRIEND_TEST(input_api, output_api):
30 """Make sure that gtest's FRIEND_TEST() macro is not used, the
31 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
32 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
33 problems = []
34
35 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h'))
36 for f in input_api.AffectedFiles(file_filter=file_filter):
37 for line_num, line in f.ChangedContents():
38 if 'FRIEND_TEST(' in line:
39 problems.append(' %s:%d' % (f.LocalPath(), line_num))
40
41 if not problems:
42 return []
43 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
44 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
45 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
46
mflodman@webrtc.org2a452092012-07-01 05:55:2347def _CheckApprovedFilesLintClean(input_api, output_api,
48 source_file_filter=None):
49 """Checks that all new or whitelisted .cc and .h files pass cpplint.py.
kjellander@webrtc.org51198f12012-02-21 17:53:4650 This check is based on _CheckChangeLintsClean in
51 depot_tools/presubmit_canned_checks.py but has less filters and only checks
52 added files."""
53 result = []
54
55 # Initialize cpplint.
56 import cpplint
57 # Access to a protected member _XX of a client class
58 # pylint: disable=W0212
59 cpplint._cpplint_state.ResetErrorCounts()
60
61 # Justifications for each filter:
62 #
63 # - build/header_guard : WebRTC coding style says they should be prefixed
64 # with WEBRTC_, which is not possible to configure in
65 # cpplint.py.
66 cpplint._SetFilters('-build/header_guard')
67
68 # Use the strictest verbosity level for cpplint.py (level 1) which is the
69 # default when running cpplint.py from command line.
70 # To make it possible to work with not-yet-converted code, we're only applying
mflodman@webrtc.org2a452092012-07-01 05:55:2371 # it to new (or moved/renamed) files and files listed in LINT_FOLDERS.
kjellander@webrtc.org51198f12012-02-21 17:53:4672 verbosity_level = 1
73 files = []
74 for f in input_api.AffectedSourceFiles(source_file_filter):
75 # Note that moved/renamed files also count as added for svn.
phoglund@webrtc.org5d3713932013-03-07 09:59:4376 if (f.Action() == 'A'):
kjellander@webrtc.org51198f12012-02-21 17:53:4677 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:2378
kjellander@webrtc.org51198f12012-02-21 17:53:4679 for file_name in files:
80 cpplint.ProcessFile(file_name, verbosity_level)
81
82 if cpplint._cpplint_state.error_count > 0:
83 if input_api.is_committing:
84 # TODO(kjellander): Change back to PresubmitError below when we're
85 # confident with the lint settings.
86 res_type = output_api.PresubmitPromptWarning
87 else:
88 res_type = output_api.PresubmitPromptWarning
89 result = [res_type('Changelist failed cpplint.py check.')]
90
91 return result
92
andrew@webrtc.org53df1362012-01-26 21:24:2393def _CommonChecks(input_api, output_api):
94 """Checks common to both upload and commit."""
kjellander@webrtc.org51198f12012-02-21 17:53:4695 # TODO(kjellander): Use presubmit_canned_checks.PanProjectChecks too.
niklase@google.comda159d62011-05-30 11:51:3496 results = []
phoglund@webrtc.org5d3713932013-03-07 09:59:4397 results.extend(input_api.canned_checks.RunPylint(input_api, output_api,
98 black_list=(r'^.*gviz_api\.py$',
99 r'^.*gaeunit\.py$',
fischman@webrtc.org33584f92013-07-25 16:43:30100 # Embedded shell-script fakes out pylint.
101 r'^talk/site_scons/site_tools/talk_linux.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43102 r'^third_party/.*\.py$',
103 r'^testing/.*\.py$',
104 r'^tools/gyp/.*\.py$',
105 r'^tools/perf_expectations/.*\.py$',
phoglund@webrtc.org6d07ad92013-05-14 09:42:39106 r'^tools/protoc_wrapper/.*\.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43107 r'^tools/python/.*\.py$',
108 r'^tools/python_charts/data/.*\.py$',
kjellander@webrtc.org33654222013-08-22 07:57:00109 r'^tools/refactoring/.*\.py$',
110 r'^tools/swarm_client/.*\.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43111 # TODO(phoglund): should arguably be checked.
112 r'^tools/valgrind-webrtc/.*\.py$',
113 r'^tools/valgrind/.*\.py$',
114 # TODO(phoglund): should arguably be checked.
115 r'^webrtc/build/.*\.py$',
116 r'^build/.*\.py$',
117 r'^out/.*\.py$',),
118 disabled_warnings=['F0401', # Failed to import x
119 'E0611', # No package y in x
120 'W0232', # Class has no __init__ method
121 ]))
andrew@webrtc.org2442de12012-01-23 17:45:41122 results.extend(input_api.canned_checks.CheckLongLines(
pbos@webrtc.orgf2e7bc62013-04-08 15:46:07123 input_api, output_api, maxlen=80))
andrew@webrtc.org2442de12012-01-23 17:45:41124 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
125 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23126 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
127 input_api, output_api))
128 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
129 input_api, output_api))
mflodman@webrtc.org2a452092012-07-01 05:55:23130 results.extend(_CheckApprovedFilesLintClean(input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46131 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
132 results.extend(_CheckNoFRIEND_TEST(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23133 return results
andrew@webrtc.org2442de12012-01-23 17:45:41134
andrew@webrtc.org53df1362012-01-26 21:24:23135def CheckChangeOnUpload(input_api, output_api):
136 results = []
137 results.extend(_CommonChecks(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34138 return results
139
andrew@webrtc.org2442de12012-01-23 17:45:41140def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24141 results = []
andrew@webrtc.org53df1362012-01-26 21:24:23142 results.extend(_CommonChecks(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24143 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23144 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
145 input_api, output_api))
146 results.extend(input_api.canned_checks.CheckChangeHasDescription(
147 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46148 results.extend(input_api.canned_checks.CheckChangeHasBugField(
149 input_api, output_api))
150 results.extend(input_api.canned_checks.CheckChangeHasTestField(
151 input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24152 return results