blob: 7570e4f9b6732a05a91de353dd3ca6987d3110be [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
11# All folders in LINT_FOLDERS will be scanned by cpplint by the presubmit
12# script. Note that subfolders are not included.
13LINT_FOLDERS = ['src/video_engine']
14
andrew@webrtc.org53df1362012-01-26 21:24:23 +000015def _LicenseHeader(input_api):
16 """Returns the license header regexp."""
17 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 ) % {
28 'year': input_api.time.strftime('%Y'),
29 }
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."""
72 # TODO(mflodman) Include subfolders in the check.
73 return (os.path.dirname(file_name) in LINT_FOLDERS)
74
75def _CheckApprovedFilesLintClean(input_api, output_api,
76 source_file_filter=None):
77 """Checks that all new or whitelisted .cc and .h files pass cpplint.py.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000078 This check is based on _CheckChangeLintsClean in
79 depot_tools/presubmit_canned_checks.py but has less filters and only checks
80 added files."""
81 result = []
82
83 # Initialize cpplint.
84 import cpplint
85 # Access to a protected member _XX of a client class
86 # pylint: disable=W0212
87 cpplint._cpplint_state.ResetErrorCounts()
88
89 # Justifications for each filter:
90 #
91 # - build/header_guard : WebRTC coding style says they should be prefixed
92 # with WEBRTC_, which is not possible to configure in
93 # cpplint.py.
94 cpplint._SetFilters('-build/header_guard')
95
96 # Use the strictest verbosity level for cpplint.py (level 1) which is the
97 # default when running cpplint.py from command line.
98 # To make it possible to work with not-yet-converted code, we're only applying
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000099 # it to new (or moved/renamed) files and files listed in LINT_FOLDERS.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000100 verbosity_level = 1
101 files = []
102 for f in input_api.AffectedSourceFiles(source_file_filter):
103 # Note that moved/renamed files also count as added for svn.
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000104 if (f.Action() == 'A' or _IsLintWhitelisted(f.LocalPath())):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000105 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000106
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000107 for file_name in files:
108 cpplint.ProcessFile(file_name, verbosity_level)
109
110 if cpplint._cpplint_state.error_count > 0:
111 if input_api.is_committing:
112 # TODO(kjellander): Change back to PresubmitError below when we're
113 # confident with the lint settings.
114 res_type = output_api.PresubmitPromptWarning
115 else:
116 res_type = output_api.PresubmitPromptWarning
117 result = [res_type('Changelist failed cpplint.py check.')]
118
119 return result
120
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000121def _CommonChecks(input_api, output_api):
122 """Checks common to both upload and commit."""
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000123 # TODO(kjellander): Use presubmit_canned_checks.PanProjectChecks too.
niklase@google.comda159d62011-05-30 11:51:34 +0000124 results = []
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000125 results.extend(input_api.canned_checks.CheckLongLines(
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000126 input_api, output_api))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000127 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
128 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000129 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
130 input_api, output_api))
131 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
132 input_api, output_api))
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000133 results.extend(_CheckApprovedFilesLintClean(input_api, output_api))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000134 results.extend(input_api.canned_checks.CheckLicense(
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000135 input_api, output_api, _LicenseHeader(input_api)))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000136 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
137 results.extend(_CheckNoFRIEND_TEST(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000138 return results
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000139
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000140def CheckChangeOnUpload(input_api, output_api):
141 results = []
142 results.extend(_CommonChecks(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34 +0000143 return results
144
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000145def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24 +0000146 results = []
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000147 results.extend(_CommonChecks(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000148 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000149 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
150 input_api, output_api))
151 results.extend(input_api.canned_checks.CheckChangeHasDescription(
152 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000153 results.extend(input_api.canned_checks.CheckChangeHasBugField(
154 input_api, output_api))
155 results.extend(input_api.canned_checks.CheckChangeHasTestField(
156 input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000157 return results