blob: 03933b83adc86c855f8928ceb92142c4e9b39e3f [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
mflodman@webrtc.org2a452092012-07-01 05:55:23 +00009import os.path
10
andrew@webrtc.org53df1362012-01-26 21:24:23 +000011def _LicenseHeader(input_api):
12 """Returns the license header regexp."""
kjellander@webrtc.org6307dbf2012-08-31 07:07:11 +000013 # Accept any year number from 2011 to the current year
14 current_year = int(input_api.time.strftime('%Y'))
15 allowed_years = (str(s) for s in reversed(xrange(2011, current_year + 1)))
16 years_re = '(' + '|'.join(allowed_years) + ')'
andrew@webrtc.org53df1362012-01-26 21:24:23 +000017 license_header = (
andrew@webrtc.org2442de12012-01-23 17:45:41 +000018 r'.*? Copyright \(c\) %(year)s The WebRTC project authors\. '
19 r'All Rights Reserved\.\n'
20 r'.*?\n'
21 r'.*? Use of this source code is governed by a BSD-style license\n'
22 r'.*? that can be found in the LICENSE file in the root of the source\n'
23 r'.*? tree\. An additional intellectual property rights grant can be '
24 r'found\n'
25 r'.*? in the file PATENTS\. All contributing project authors may\n'
26 r'.*? be found in the AUTHORS file in the root of the source tree\.\n'
27 ) % {
kjellander@webrtc.org6307dbf2012-08-31 07:07:11 +000028 'year': years_re,
andrew@webrtc.org2442de12012-01-23 17:45:41 +000029 }
andrew@webrtc.org53df1362012-01-26 21:24:23 +000030 return license_header
andrew@webrtc.org2442de12012-01-23 17:45:41 +000031
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000032def _CheckNoIOStreamInHeaders(input_api, output_api):
33 """Checks to make sure no .h files include <iostream>."""
34 files = []
35 pattern = input_api.re.compile(r'^#include\s*<iostream>',
36 input_api.re.MULTILINE)
37 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
38 if not f.LocalPath().endswith('.h'):
39 continue
40 contents = input_api.ReadFile(f)
41 if pattern.search(contents):
42 files.append(f)
43
44 if len(files):
45 return [ output_api.PresubmitError(
46 'Do not #include <iostream> in header files, since it inserts static ' +
47 'initialization into every file including the header. Instead, ' +
48 '#include <ostream>. See http://crbug.com/94794',
49 files) ]
50 return []
51
52def _CheckNoFRIEND_TEST(input_api, output_api):
53 """Make sure that gtest's FRIEND_TEST() macro is not used, the
54 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
55 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
56 problems = []
57
58 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h'))
59 for f in input_api.AffectedFiles(file_filter=file_filter):
60 for line_num, line in f.ChangedContents():
61 if 'FRIEND_TEST(' in line:
62 problems.append(' %s:%d' % (f.LocalPath(), line_num))
63
64 if not problems:
65 return []
66 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
67 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
68 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
69
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000070def _IsLintWhitelisted(file_name):
71 """ Checks if a file is whitelisted for lint check."""
phoglund@webrtc.org899699e2013-01-21 15:57:34 +000072 return True
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000073
74def _CheckApprovedFilesLintClean(input_api, output_api,
75 source_file_filter=None):
76 """Checks that all new or whitelisted .cc and .h files pass cpplint.py.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000077 This check is based on _CheckChangeLintsClean in
78 depot_tools/presubmit_canned_checks.py but has less filters and only checks
79 added files."""
80 result = []
81
82 # Initialize cpplint.
83 import cpplint
84 # Access to a protected member _XX of a client class
85 # pylint: disable=W0212
86 cpplint._cpplint_state.ResetErrorCounts()
87
88 # Justifications for each filter:
89 #
90 # - build/header_guard : WebRTC coding style says they should be prefixed
91 # with WEBRTC_, which is not possible to configure in
92 # cpplint.py.
93 cpplint._SetFilters('-build/header_guard')
94
95 # Use the strictest verbosity level for cpplint.py (level 1) which is the
96 # default when running cpplint.py from command line.
97 # To make it possible to work with not-yet-converted code, we're only applying
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000098 # it to new (or moved/renamed) files and files listed in LINT_FOLDERS.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000099 verbosity_level = 1
100 files = []
101 for f in input_api.AffectedSourceFiles(source_file_filter):
102 # Note that moved/renamed files also count as added for svn.
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000103 if (f.Action() == 'A' or _IsLintWhitelisted(f.LocalPath())):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000104 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000105
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000106 for file_name in files:
107 cpplint.ProcessFile(file_name, verbosity_level)
108
109 if cpplint._cpplint_state.error_count > 0:
110 if input_api.is_committing:
111 # TODO(kjellander): Change back to PresubmitError below when we're
112 # confident with the lint settings.
113 res_type = output_api.PresubmitPromptWarning
114 else:
115 res_type = output_api.PresubmitPromptWarning
116 result = [res_type('Changelist failed cpplint.py check.')]
117
118 return result
119
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000120def _CommonChecks(input_api, output_api):
121 """Checks common to both upload and commit."""
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000122 # TODO(kjellander): Use presubmit_canned_checks.PanProjectChecks too.
niklase@google.comda159d62011-05-30 11:51:34 +0000123 results = []
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000124 results.extend(input_api.canned_checks.CheckLongLines(
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000125 input_api, output_api))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000126 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
127 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000128 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
129 input_api, output_api))
130 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
131 input_api, output_api))
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000132 results.extend(_CheckApprovedFilesLintClean(input_api, output_api))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000133 results.extend(input_api.canned_checks.CheckLicense(
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000134 input_api, output_api, _LicenseHeader(input_api)))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000135 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
136 results.extend(_CheckNoFRIEND_TEST(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000137 return results
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000138
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000139def CheckChangeOnUpload(input_api, output_api):
140 results = []
141 results.extend(_CommonChecks(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34 +0000142 return results
143
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000144def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24 +0000145 results = []
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000146 results.extend(_CommonChecks(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000147 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000148 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
149 input_api, output_api))
150 results.extend(input_api.canned_checks.CheckChangeHasDescription(
151 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000152 results.extend(input_api.canned_checks.CheckChangeHasBugField(
153 input_api, output_api))
154 results.extend(input_api.canned_checks.CheckChangeHasTestField(
155 input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000156 return results
kjellander@webrtc.orgf3ffcce2012-10-31 14:52:21 +0000157