blob: 56c8d5b5eb1845fb105947f7965338665936a8b9 [file] [log] [blame]
andrew@webrtc.org2442de12012-01-23 17:45:41 +00001# 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:34 +00008
andrew@webrtc.org53df1362012-01-26 21:24:23 +00009def _LicenseHeader(input_api):
10 """Returns the license header regexp."""
11 license_header = (
andrew@webrtc.org2442de12012-01-23 17:45:41 +000012 r'.*? Copyright \(c\) %(year)s The WebRTC project authors\. '
13 r'All Rights Reserved\.\n'
14 r'.*?\n'
15 r'.*? Use of this source code is governed by a BSD-style license\n'
16 r'.*? that can be found in the LICENSE file in the root of the source\n'
17 r'.*? tree\. An additional intellectual property rights grant can be '
18 r'found\n'
19 r'.*? in the file PATENTS\. All contributing project authors may\n'
20 r'.*? be found in the AUTHORS file in the root of the source tree\.\n'
21 ) % {
22 'year': input_api.time.strftime('%Y'),
23 }
andrew@webrtc.org53df1362012-01-26 21:24:23 +000024 return license_header
andrew@webrtc.org2442de12012-01-23 17:45:41 +000025
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000026def _CheckNoIOStreamInHeaders(input_api, output_api):
27 """Checks to make sure no .h files include <iostream>."""
28 files = []
29 pattern = input_api.re.compile(r'^#include\s*<iostream>',
30 input_api.re.MULTILINE)
31 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
32 if not f.LocalPath().endswith('.h'):
33 continue
34 contents = input_api.ReadFile(f)
35 if pattern.search(contents):
36 files.append(f)
37
38 if len(files):
39 return [ output_api.PresubmitError(
40 'Do not #include <iostream> in header files, since it inserts static ' +
41 'initialization into every file including the header. Instead, ' +
42 '#include <ostream>. See http://crbug.com/94794',
43 files) ]
44 return []
45
46def _CheckNoFRIEND_TEST(input_api, output_api):
47 """Make sure that gtest's FRIEND_TEST() macro is not used, the
48 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
49 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
50 problems = []
51
52 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h'))
53 for f in input_api.AffectedFiles(file_filter=file_filter):
54 for line_num, line in f.ChangedContents():
55 if 'FRIEND_TEST(' in line:
56 problems.append(' %s:%d' % (f.LocalPath(), line_num))
57
58 if not problems:
59 return []
60 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
61 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
62 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
63
64def _CheckNewFilesLintClean(input_api, output_api, source_file_filter=None):
65 """Checks that all NEW '.cc' and '.h' files pass cpplint.py.
66 This check is based on _CheckChangeLintsClean in
67 depot_tools/presubmit_canned_checks.py but has less filters and only checks
68 added files."""
69 result = []
70
71 # Initialize cpplint.
72 import cpplint
73 # Access to a protected member _XX of a client class
74 # pylint: disable=W0212
75 cpplint._cpplint_state.ResetErrorCounts()
76
77 # Justifications for each filter:
78 #
79 # - build/header_guard : WebRTC coding style says they should be prefixed
80 # with WEBRTC_, which is not possible to configure in
81 # cpplint.py.
82 cpplint._SetFilters('-build/header_guard')
83
84 # Use the strictest verbosity level for cpplint.py (level 1) which is the
85 # default when running cpplint.py from command line.
86 # To make it possible to work with not-yet-converted code, we're only applying
87 # it to new (or moved/renamed) files.
88 verbosity_level = 1
89 files = []
90 for f in input_api.AffectedSourceFiles(source_file_filter):
91 # Note that moved/renamed files also count as added for svn.
92 if (f.Action() == 'A'):
93 files.append(f.AbsoluteLocalPath())
94 for file_name in files:
95 cpplint.ProcessFile(file_name, verbosity_level)
96
97 if cpplint._cpplint_state.error_count > 0:
98 if input_api.is_committing:
99 # TODO(kjellander): Change back to PresubmitError below when we're
100 # confident with the lint settings.
101 res_type = output_api.PresubmitPromptWarning
102 else:
103 res_type = output_api.PresubmitPromptWarning
104 result = [res_type('Changelist failed cpplint.py check.')]
105
106 return result
107
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000108def _CommonChecks(input_api, output_api):
109 """Checks common to both upload and commit."""
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000110 # TODO(kjellander): Use presubmit_canned_checks.PanProjectChecks too.
niklase@google.comda159d62011-05-30 11:51:34 +0000111 results = []
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000112 results.extend(input_api.canned_checks.CheckLongLines(
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000113 input_api, output_api))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000114 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
115 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000116 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
117 input_api, output_api))
118 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
119 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000120 results.extend(_CheckNewFilesLintClean(input_api, output_api))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000121 results.extend(input_api.canned_checks.CheckLicense(
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000122 input_api, output_api, _LicenseHeader(input_api)))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000123 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
124 results.extend(_CheckNoFRIEND_TEST(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000125 return results
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000126
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000127def CheckChangeOnUpload(input_api, output_api):
128 results = []
129 results.extend(_CommonChecks(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34 +0000130 return results
131
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000132def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24 +0000133 results = []
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000134 results.extend(_CommonChecks(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000135 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000136 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
137 input_api, output_api))
138 results.extend(input_api.canned_checks.CheckChangeHasDescription(
139 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000140 results.extend(input_api.canned_checks.CheckChangeHasBugField(
141 input_api, output_api))
142 results.extend(input_api.canned_checks.CheckChangeHasTestField(
143 input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000144 return results