andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 1 | # 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.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 8 | |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 9 | import json |
kjellander@webrtc.org | aefe61a | 2014-12-08 13:00:30 +0000 | [diff] [blame] | 10 | import os |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 11 | import re |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 12 | import sys |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 13 | from collections import defaultdict |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 14 | from contextlib import contextmanager |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 15 | |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 16 | # Files and directories that are *skipped* by cpplint in the presubmit script. |
| 17 | CPPLINT_BLACKLIST = [ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | 'api/video_codecs/video_decoder.h', |
| 19 | 'common_types.cc', |
| 20 | 'common_types.h', |
| 21 | 'examples/objc', |
Amit Hilbuch | ce7032b | 2019-01-22 16:19:35 -0800 | [diff] [blame^] | 22 | 'media/base/stream_params.h', |
| 23 | 'media/base/video_common.h', |
| 24 | 'media/sctp/sctp_transport.cc', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | 'modules/audio_coding', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | 'modules/audio_device', |
| 27 | 'modules/audio_processing', |
| 28 | 'modules/desktop_capture', |
| 29 | 'modules/include/module_common_types.h', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | 'modules/utility', |
| 31 | 'modules/video_capture', |
Amit Hilbuch | ce7032b | 2019-01-22 16:19:35 -0800 | [diff] [blame^] | 32 | 'p2p/base/pseudo_tcp.cc', |
| 33 | 'p2p/base/pseudo_tcp.h', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | 'rtc_base', |
| 35 | 'sdk/android/src/jni', |
| 36 | 'sdk/objc', |
| 37 | 'system_wrappers', |
| 38 | 'test', |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 39 | 'tools_webrtc', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 40 | 'voice_engine', |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 41 | ] |
| 42 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 43 | # These filters will always be removed, even if the caller specifies a filter |
| 44 | # set, as they are problematic or broken in some way. |
| 45 | # |
| 46 | # Justifications for each filter: |
| 47 | # - build/c++11 : Rvalue ref checks are unreliable (false positives), |
| 48 | # include file and feature blacklists are |
| 49 | # google3-specific. |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 50 | # - whitespace/operators: Same as above (doesn't seem sufficient to eliminate |
| 51 | # all move-related errors). |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 52 | BLACKLIST_LINT_FILTERS = [ |
| 53 | '-build/c++11', |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 54 | '-whitespace/operators', |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 55 | ] |
| 56 | |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 57 | # List of directories of "supported" native APIs. That means changes to headers |
| 58 | # will be done in a compatible way following this scheme: |
| 59 | # 1. Non-breaking changes are made. |
| 60 | # 2. The old APIs as marked as deprecated (with comments). |
| 61 | # 3. Deprecation is announced to discuss-webrtc@googlegroups.com and |
| 62 | # webrtc-users@google.com (internal list). |
| 63 | # 4. (later) The deprecated APIs are removed. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 64 | NATIVE_API_DIRS = ( |
Karl Wiberg | ef52d8b8 | 2017-10-25 13:20:03 +0200 | [diff] [blame] | 65 | 'api', # All subdirectories of api/ are included as well. |
Mirko Bonadei | a4eeeff | 2018-01-11 13:16:52 +0100 | [diff] [blame] | 66 | 'media/base', |
| 67 | 'media/engine', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 68 | 'modules/audio_device/include', |
| 69 | 'pc', |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 70 | ) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 71 | |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 72 | # These directories should not be used but are maintained only to avoid breaking |
| 73 | # some legacy downstream code. |
| 74 | LEGACY_API_DIRS = ( |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 75 | 'common_audio/include', |
| 76 | 'modules/audio_coding/include', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 77 | 'modules/audio_processing/include', |
| 78 | 'modules/bitrate_controller/include', |
| 79 | 'modules/congestion_controller/include', |
| 80 | 'modules/include', |
| 81 | 'modules/remote_bitrate_estimator/include', |
| 82 | 'modules/rtp_rtcp/include', |
| 83 | 'modules/rtp_rtcp/source', |
| 84 | 'modules/utility/include', |
| 85 | 'modules/video_coding/codecs/h264/include', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 86 | 'modules/video_coding/codecs/vp8/include', |
| 87 | 'modules/video_coding/codecs/vp9/include', |
| 88 | 'modules/video_coding/include', |
| 89 | 'rtc_base', |
| 90 | 'system_wrappers/include', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 91 | ) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 92 | |
Karl Wiberg | d4f01c1 | 2017-11-10 10:55:45 +0100 | [diff] [blame] | 93 | # NOTE: The set of directories in API_DIRS should be the same as those |
| 94 | # listed in the table in native-api.md. |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 95 | API_DIRS = NATIVE_API_DIRS[:] + LEGACY_API_DIRS[:] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 96 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 97 | # TARGET_RE matches a GN target, and extracts the target name and the contents. |
| 98 | TARGET_RE = re.compile(r'(?P<indent>\s*)\w+\("(?P<target_name>\w+)"\) {' |
| 99 | r'(?P<target_contents>.*?)' |
| 100 | r'(?P=indent)}', |
| 101 | re.MULTILINE | re.DOTALL) |
| 102 | |
| 103 | # SOURCES_RE matches a block of sources inside a GN target. |
| 104 | SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]', |
| 105 | re.MULTILINE | re.DOTALL) |
| 106 | |
| 107 | # FILE_PATH_RE matchies a file path. |
| 108 | FILE_PATH_RE = re.compile(r'"(?P<file_path>(\w|\/)+)(?P<extension>\.\w+)"') |
| 109 | |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 110 | |
Mirko Bonadei | d866544 | 2018-09-04 12:17:27 +0200 | [diff] [blame] | 111 | def FindSrcDirPath(starting_dir): |
| 112 | """Returns the abs path to the src/ dir of the project.""" |
| 113 | src_dir = starting_dir |
| 114 | while os.path.basename(src_dir) != 'src': |
| 115 | src_dir = os.path.normpath(os.path.join(src_dir, os.pardir)) |
| 116 | return src_dir |
| 117 | |
| 118 | |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 119 | @contextmanager |
| 120 | def _AddToPath(*paths): |
| 121 | original_sys_path = sys.path |
| 122 | sys.path.extend(paths) |
| 123 | try: |
| 124 | yield |
| 125 | finally: |
| 126 | # Restore sys.path to what it was before. |
| 127 | sys.path = original_sys_path |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 128 | |
| 129 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 130 | def VerifyNativeApiHeadersListIsValid(input_api, output_api): |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 131 | """Ensures the list of native API header directories is up to date.""" |
| 132 | non_existing_paths = [] |
| 133 | native_api_full_paths = [ |
| 134 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 135 | *path.split('/')) for path in API_DIRS] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 136 | for path in native_api_full_paths: |
| 137 | if not os.path.isdir(path): |
| 138 | non_existing_paths.append(path) |
| 139 | if non_existing_paths: |
| 140 | return [output_api.PresubmitError( |
| 141 | 'Directories to native API headers have changed which has made the ' |
| 142 | 'list in PRESUBMIT.py outdated.\nPlease update it to the current ' |
| 143 | 'location of our native APIs.', |
| 144 | non_existing_paths)] |
| 145 | return [] |
| 146 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 147 | |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 148 | API_CHANGE_MSG = """ |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 149 | You seem to be changing native API header files. Please make sure that you: |
oprypin | 375b9ac | 2017-02-13 04:13:23 -0800 | [diff] [blame] | 150 | 1. Make compatible changes that don't break existing clients. Usually |
| 151 | this is done by keeping the existing method signatures unchanged. |
| 152 | 2. Mark the old stuff as deprecated (see RTC_DEPRECATED macro). |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 153 | 3. Create a timeline and plan for when the deprecated stuff will be |
| 154 | removed. (The amount of time we give users to change their code |
| 155 | should be informed by how much work it is for them. If they just |
| 156 | need to replace one name with another or something equally |
| 157 | simple, 1-2 weeks might be good; if they need to do serious work, |
| 158 | up to 3 months may be called for.) |
| 159 | 4. Update/inform existing downstream code owners to stop using the |
| 160 | deprecated stuff. (Send announcements to |
| 161 | discuss-webrtc@googlegroups.com and webrtc-users@google.com.) |
| 162 | 5. Remove the deprecated stuff, once the agreed-upon amount of time |
| 163 | has passed. |
| 164 | Related files: |
| 165 | """ |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 166 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 167 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 168 | def CheckNativeApiHeaderChanges(input_api, output_api): |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 169 | """Checks to remind proper changing of native APIs.""" |
| 170 | files = [] |
Karl Wiberg | 6bfac03 | 2017-10-27 15:14:20 +0200 | [diff] [blame] | 171 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 172 | x, white_list=[r'.+\.(gn|gni|h)$']) |
| 173 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 174 | for path in API_DIRS: |
| 175 | dn = os.path.dirname(f.LocalPath()) |
| 176 | if path == 'api': |
| 177 | # Special case: Subdirectories included. |
| 178 | if dn == 'api' or dn.startswith('api/'): |
| 179 | files.append(f) |
| 180 | else: |
| 181 | # Normal case: Subdirectories not included. |
| 182 | if dn == path: |
| 183 | files.append(f) |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 184 | |
| 185 | if files: |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 186 | return [output_api.PresubmitNotifyResult(API_CHANGE_MSG, files)] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 187 | return [] |
| 188 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 189 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 190 | def CheckNoIOStreamInHeaders(input_api, output_api, |
| 191 | source_file_filter): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 192 | """Checks to make sure no .h files include <iostream>.""" |
| 193 | files = [] |
| 194 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 195 | input_api.re.MULTILINE) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 196 | file_filter = lambda x: (input_api.FilterSourceFile(x) |
| 197 | and source_file_filter(x)) |
| 198 | for f in input_api.AffectedSourceFiles(file_filter): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 199 | if not f.LocalPath().endswith('.h'): |
| 200 | continue |
| 201 | contents = input_api.ReadFile(f) |
| 202 | if pattern.search(contents): |
| 203 | files.append(f) |
| 204 | |
| 205 | if len(files): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 206 | return [output_api.PresubmitError( |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 207 | 'Do not #include <iostream> in header files, since it inserts static ' + |
| 208 | 'initialization into every file including the header. Instead, ' + |
| 209 | '#include <ostream>. See http://crbug.com/94794', |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 210 | files)] |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 211 | return [] |
| 212 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 213 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 214 | def CheckNoPragmaOnce(input_api, output_api, |
| 215 | source_file_filter): |
kjellander | 6aeef74 | 2017-02-20 01:13:18 -0800 | [diff] [blame] | 216 | """Make sure that banned functions are not used.""" |
| 217 | files = [] |
| 218 | pattern = input_api.re.compile(r'^#pragma\s+once', |
| 219 | input_api.re.MULTILINE) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 220 | file_filter = lambda x: (input_api.FilterSourceFile(x) |
| 221 | and source_file_filter(x)) |
| 222 | for f in input_api.AffectedSourceFiles(file_filter): |
kjellander | 6aeef74 | 2017-02-20 01:13:18 -0800 | [diff] [blame] | 223 | if not f.LocalPath().endswith('.h'): |
| 224 | continue |
| 225 | contents = input_api.ReadFile(f) |
| 226 | if pattern.search(contents): |
| 227 | files.append(f) |
| 228 | |
| 229 | if files: |
| 230 | return [output_api.PresubmitError( |
| 231 | 'Do not use #pragma once in header files.\n' |
| 232 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 233 | files)] |
| 234 | return [] |
| 235 | |
| 236 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 237 | def CheckNoFRIEND_TEST(input_api, output_api, # pylint: disable=invalid-name |
| 238 | source_file_filter): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 239 | """Make sure that gtest's FRIEND_TEST() macro is not used, the |
| 240 | FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be |
| 241 | used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes.""" |
| 242 | problems = [] |
| 243 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 244 | file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.h')) |
| 245 | and source_file_filter(f)) |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 246 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 247 | for line_num, line in f.ChangedContents(): |
| 248 | if 'FRIEND_TEST(' in line: |
| 249 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 250 | |
| 251 | if not problems: |
| 252 | return [] |
| 253 | return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use ' |
| 254 | 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and ' |
| 255 | 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))] |
| 256 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 257 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 258 | def IsLintBlacklisted(blacklist_paths, file_path): |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 259 | """ Checks if a file is blacklisted for lint check.""" |
| 260 | for path in blacklist_paths: |
| 261 | if file_path == path or os.path.dirname(file_path).startswith(path): |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 262 | return True |
| 263 | return False |
| 264 | |
| 265 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 266 | def CheckApprovedFilesLintClean(input_api, output_api, |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 267 | source_file_filter=None): |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 268 | """Checks that all new or non-blacklisted .cc and .h files pass cpplint.py. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 269 | This check is based on CheckChangeLintsClean in |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 270 | depot_tools/presubmit_canned_checks.py but has less filters and only checks |
| 271 | added files.""" |
| 272 | result = [] |
| 273 | |
| 274 | # Initialize cpplint. |
| 275 | import cpplint |
| 276 | # Access to a protected member _XX of a client class |
| 277 | # pylint: disable=W0212 |
| 278 | cpplint._cpplint_state.ResetErrorCounts() |
| 279 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 280 | lint_filters = cpplint._Filters() |
| 281 | lint_filters.extend(BLACKLIST_LINT_FILTERS) |
| 282 | cpplint._SetFilters(','.join(lint_filters)) |
| 283 | |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 284 | # Create a platform independent blacklist for cpplint. |
| 285 | blacklist_paths = [input_api.os_path.join(*path.split('/')) |
| 286 | for path in CPPLINT_BLACKLIST] |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 287 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 288 | # Use the strictest verbosity level for cpplint.py (level 1) which is the |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 289 | # default when running cpplint.py from command line. To make it possible to |
| 290 | # work with not-yet-converted code, we're only applying it to new (or |
| 291 | # moved/renamed) files and files not listed in CPPLINT_BLACKLIST. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 292 | verbosity_level = 1 |
| 293 | files = [] |
| 294 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 295 | # Note that moved/renamed files also count as added. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 296 | if f.Action() == 'A' or not IsLintBlacklisted(blacklist_paths, |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 297 | f.LocalPath()): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 298 | files.append(f.AbsoluteLocalPath()) |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 299 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 300 | for file_name in files: |
| 301 | cpplint.ProcessFile(file_name, verbosity_level) |
| 302 | |
| 303 | if cpplint._cpplint_state.error_count > 0: |
| 304 | if input_api.is_committing: |
oprypin | 8e58d65 | 2017-03-21 07:52:41 -0700 | [diff] [blame] | 305 | res_type = output_api.PresubmitError |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 306 | else: |
| 307 | res_type = output_api.PresubmitPromptWarning |
| 308 | result = [res_type('Changelist failed cpplint.py check.')] |
| 309 | |
| 310 | return result |
| 311 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 312 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 313 | def CheckNoSourcesAbove(input_api, gn_files, output_api): |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 314 | # Disallow referencing source files with paths above the GN file location. |
| 315 | source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]', |
| 316 | re.MULTILINE | re.DOTALL) |
| 317 | file_pattern = input_api.re.compile(r'"((\.\./.*?)|(//.*?))"') |
| 318 | violating_gn_files = set() |
| 319 | violating_source_entries = [] |
| 320 | for gn_file in gn_files: |
| 321 | contents = input_api.ReadFile(gn_file) |
| 322 | for source_block_match in source_pattern.finditer(contents): |
| 323 | # Find all source list entries starting with ../ in the source block |
| 324 | # (exclude overrides entries). |
| 325 | for file_list_match in file_pattern.finditer(source_block_match.group(1)): |
| 326 | source_file = file_list_match.group(1) |
| 327 | if 'overrides/' not in source_file: |
| 328 | violating_source_entries.append(source_file) |
| 329 | violating_gn_files.add(gn_file) |
| 330 | if violating_gn_files: |
| 331 | return [output_api.PresubmitError( |
| 332 | 'Referencing source files above the directory of the GN file is not ' |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 333 | 'allowed. Please introduce new GN targets in the proper location ' |
| 334 | 'instead.\n' |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 335 | 'Invalid source entries:\n' |
| 336 | '%s\n' |
| 337 | 'Violating GN files:' % '\n'.join(violating_source_entries), |
| 338 | items=violating_gn_files)] |
| 339 | return [] |
| 340 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 341 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 342 | def CheckNoMixingSources(input_api, gn_files, output_api): |
| 343 | """Disallow mixing C, C++ and Obj-C/Obj-C++ in the same target. |
| 344 | |
| 345 | See bugs.webrtc.org/7743 for more context. |
| 346 | """ |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 347 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 348 | def _MoreThanOneSourceUsed(*sources_lists): |
| 349 | sources_used = 0 |
| 350 | for source_list in sources_lists: |
| 351 | if len(source_list): |
| 352 | sources_used += 1 |
| 353 | return sources_used > 1 |
| 354 | |
| 355 | errors = defaultdict(lambda: []) |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 356 | for gn_file in gn_files: |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 357 | gn_file_content = input_api.ReadFile(gn_file) |
| 358 | for target_match in TARGET_RE.finditer(gn_file_content): |
| 359 | # list_of_sources is a list of tuples of the form |
| 360 | # (c_files, cc_files, objc_files) that keeps track of all the sources |
| 361 | # defined in a target. A GN target can have more that on definition of |
| 362 | # sources (since it supports if/else statements). |
| 363 | # E.g.: |
| 364 | # rtc_static_library("foo") { |
| 365 | # if (is_win) { |
| 366 | # sources = [ "foo.cc" ] |
| 367 | # } else { |
| 368 | # sources = [ "foo.mm" ] |
| 369 | # } |
| 370 | # } |
| 371 | # This is allowed and the presubmit check should support this case. |
| 372 | list_of_sources = [] |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 373 | c_files = [] |
| 374 | cc_files = [] |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 375 | objc_files = [] |
| 376 | target_name = target_match.group('target_name') |
| 377 | target_contents = target_match.group('target_contents') |
| 378 | for sources_match in SOURCES_RE.finditer(target_contents): |
| 379 | if '+=' not in sources_match.group(0): |
| 380 | if c_files or cc_files or objc_files: |
| 381 | list_of_sources.append((c_files, cc_files, objc_files)) |
| 382 | c_files = [] |
| 383 | cc_files = [] |
| 384 | objc_files = [] |
| 385 | for file_match in FILE_PATH_RE.finditer(sources_match.group(1)): |
| 386 | file_path = file_match.group('file_path') |
| 387 | extension = file_match.group('extension') |
| 388 | if extension == '.c': |
| 389 | c_files.append(file_path + extension) |
| 390 | if extension == '.cc': |
| 391 | cc_files.append(file_path + extension) |
| 392 | if extension in ['.m', '.mm']: |
| 393 | objc_files.append(file_path + extension) |
| 394 | list_of_sources.append((c_files, cc_files, objc_files)) |
| 395 | for c_files_list, cc_files_list, objc_files_list in list_of_sources: |
| 396 | if _MoreThanOneSourceUsed(c_files_list, cc_files_list, objc_files_list): |
| 397 | all_sources = sorted(c_files_list + cc_files_list + objc_files_list) |
| 398 | errors[gn_file.LocalPath()].append((target_name, all_sources)) |
| 399 | if errors: |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 400 | return [output_api.PresubmitError( |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 401 | 'GN targets cannot mix .c, .cc and .m (or .mm) source files.\n' |
| 402 | 'Please create a separate target for each collection of sources.\n' |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 403 | 'Mixed sources: \n' |
| 404 | '%s\n' |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 405 | 'Violating GN files:\n%s\n' % (json.dumps(errors, indent=2), |
| 406 | '\n'.join(errors.keys())))] |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 407 | return [] |
| 408 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 409 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 410 | def CheckNoPackageBoundaryViolations(input_api, gn_files, output_api): |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 411 | cwd = input_api.PresubmitLocalPath() |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 412 | with _AddToPath(input_api.os_path.join( |
| 413 | cwd, 'tools_webrtc', 'presubmit_checks_lib')): |
| 414 | from check_package_boundaries import CheckPackageBoundaries |
| 415 | build_files = [os.path.join(cwd, gn_file.LocalPath()) for gn_file in gn_files] |
| 416 | errors = CheckPackageBoundaries(cwd, build_files)[:5] |
| 417 | if errors: |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 418 | return [output_api.PresubmitError( |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 419 | 'There are package boundary violations in the following GN files:', |
| 420 | long_text='\n\n'.join(str(err) for err in errors))] |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 421 | return [] |
| 422 | |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 423 | |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 424 | def _ReportFileAndLine(filename, line_num): |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 425 | """Default error formatter for _FindNewViolationsOfRule.""" |
| 426 | return '%s (line %s)' % (filename, line_num) |
| 427 | |
| 428 | |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 429 | def CheckNoWarningSuppressionFlagsAreAdded(gn_files, input_api, output_api, |
| 430 | error_formatter=_ReportFileAndLine): |
| 431 | """Make sure that warning suppression flags are not added wihtout a reason.""" |
| 432 | msg = ('Usage of //build/config/clang:extra_warnings is discouraged ' |
| 433 | 'in WebRTC.\n' |
| 434 | 'If you are not adding this code (e.g. you are just moving ' |
| 435 | 'existing code) or you want to add an exception,\n' |
| 436 | 'you can add a comment on the line that causes the problem:\n\n' |
| 437 | '"-Wno-odr" # no-presubmit-check TODO(bugs.webrtc.org/BUG_ID)\n' |
| 438 | '\n' |
| 439 | 'Affected files:\n') |
| 440 | errors = [] # 2-element tuples with (file, line number) |
| 441 | clang_warn_re = input_api.re.compile(r'//build/config/clang:extra_warnings') |
| 442 | no_presubmit_re = input_api.re.compile( |
| 443 | r'# no-presubmit-check TODO\(bugs\.webrtc\.org/\d+\)') |
| 444 | for f in gn_files: |
| 445 | for line_num, line in f.ChangedContents(): |
| 446 | if clang_warn_re.search(line) and not no_presubmit_re.search(line): |
| 447 | errors.append(error_formatter(f.LocalPath(), line_num)) |
| 448 | if errors: |
| 449 | return [output_api.PresubmitError(msg, errors)] |
| 450 | return [] |
| 451 | |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 452 | def CheckNoStreamUsageIsAdded(input_api, output_api, |
Artem Titov | 739351d | 2018-05-11 12:21:36 +0200 | [diff] [blame] | 453 | source_file_filter, |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 454 | error_formatter=_ReportFileAndLine): |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 455 | """Make sure that no more dependencies on stringstream are added.""" |
| 456 | error_msg = ('Usage of <sstream>, <istream> and <ostream> in WebRTC is ' |
| 457 | 'deprecated.\n' |
| 458 | 'This includes the following types:\n' |
| 459 | 'std::istringstream, std::ostringstream, std::wistringstream, ' |
| 460 | 'std::wostringstream,\n' |
| 461 | 'std::wstringstream, std::ostream, std::wostream, std::istream,' |
| 462 | 'std::wistream,\n' |
| 463 | 'std::iostream, std::wiostream.\n' |
| 464 | 'If you are not adding this code (e.g. you are just moving ' |
| 465 | 'existing code),\n' |
| 466 | 'you can add a comment on the line that causes the problem:\n\n' |
| 467 | '#include <sstream> // no-presubmit-check TODO(webrtc:8982)\n' |
| 468 | 'std::ostream& F() { // no-presubmit-check TODO(webrtc:8982)\n' |
| 469 | '\n' |
Karl Wiberg | ebd01e8 | 2018-03-14 15:08:39 +0100 | [diff] [blame] | 470 | 'If you are adding new code, consider using ' |
| 471 | 'rtc::SimpleStringBuilder\n' |
| 472 | '(in rtc_base/strings/string_builder.h).\n' |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 473 | 'Affected files:\n') |
| 474 | errors = [] # 2-element tuples with (file, line number) |
| 475 | include_re = input_api.re.compile(r'#include <(i|o|s)stream>') |
| 476 | usage_re = input_api.re.compile(r'std::(w|i|o|io|wi|wo|wio)(string)*stream') |
| 477 | no_presubmit_re = input_api.re.compile( |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 478 | r'// no-presubmit-check TODO\(webrtc:8982\)') |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 479 | file_filter = lambda x: (input_api.FilterSourceFile(x) |
| 480 | and source_file_filter(x)) |
| 481 | for f in input_api.AffectedSourceFiles(file_filter): |
Mirko Bonadei | 44ca9a3 | 2018-11-13 11:16:40 +0100 | [diff] [blame] | 482 | # Usage of stringstream is allowed under examples/. |
| 483 | if f.LocalPath() == 'PRESUBMIT.py' or f.LocalPath().startswith('examples'): |
Mirko Bonadei | d2c8332 | 2018-03-19 10:31:47 +0000 | [diff] [blame] | 484 | continue |
| 485 | for line_num, line in f.ChangedContents(): |
| 486 | if ((include_re.search(line) or usage_re.search(line)) |
| 487 | and not no_presubmit_re.search(line)): |
| 488 | errors.append(error_formatter(f.LocalPath(), line_num)) |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 489 | if errors: |
| 490 | return [output_api.PresubmitError(error_msg, errors)] |
| 491 | return [] |
| 492 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 493 | |
Mirko Bonadei | a05d47e | 2018-05-09 11:03:38 +0200 | [diff] [blame] | 494 | def CheckPublicDepsIsNotUsed(gn_files, input_api, output_api): |
| 495 | """Checks that public_deps is not used without a good reason.""" |
Mirko Bonadei | 5c1ad59 | 2017-12-12 11:52:27 +0100 | [diff] [blame] | 496 | result = [] |
Mirko Bonadei | a05d47e | 2018-05-09 11:03:38 +0200 | [diff] [blame] | 497 | no_presubmit_check_re = input_api.re.compile( |
| 498 | r'# no-presubmit-check TODO\(webrtc:8603\)') |
| 499 | error_msg = ('public_deps is not recommended in WebRTC BUILD.gn files ' |
| 500 | 'because it doesn\'t map well to downstream build systems.\n' |
| 501 | 'Used in: %s (line %d).\n' |
| 502 | 'If you are not adding this code (e.g. you are just moving ' |
| 503 | 'existing code) or you have a good reason, you can add a ' |
| 504 | 'comment on the line that causes the problem:\n\n' |
| 505 | 'public_deps = [ # no-presubmit-check TODO(webrtc:8603)\n') |
Mirko Bonadei | 5c1ad59 | 2017-12-12 11:52:27 +0100 | [diff] [blame] | 506 | for affected_file in gn_files: |
| 507 | for (line_number, affected_line) in affected_file.ChangedContents(): |
Mirko Bonadei | a05d47e | 2018-05-09 11:03:38 +0200 | [diff] [blame] | 508 | if ('public_deps' in affected_line |
| 509 | and not no_presubmit_check_re.search(affected_line)): |
Mirko Bonadei | 5c1ad59 | 2017-12-12 11:52:27 +0100 | [diff] [blame] | 510 | result.append( |
| 511 | output_api.PresubmitError(error_msg % (affected_file.LocalPath(), |
| 512 | line_number))) |
| 513 | return result |
| 514 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 515 | |
Patrik Höglund | 6f49106 | 2018-01-11 12:04:23 +0100 | [diff] [blame] | 516 | def CheckCheckIncludesIsNotUsed(gn_files, output_api): |
| 517 | result = [] |
| 518 | error_msg = ('check_includes overrides are not allowed since it can cause ' |
| 519 | 'incorrect dependencies to form. It effectively means that your ' |
| 520 | 'module can include any .h file without depending on its ' |
| 521 | 'corresponding target. There are some exceptional cases when ' |
| 522 | 'this is allowed: if so, get approval from a .gn owner in the' |
| 523 | 'root OWNERS file.\n' |
| 524 | 'Used in: %s (line %d).') |
| 525 | for affected_file in gn_files: |
| 526 | for (line_number, affected_line) in affected_file.ChangedContents(): |
| 527 | if 'check_includes' in affected_line: |
| 528 | result.append( |
| 529 | output_api.PresubmitError(error_msg % (affected_file.LocalPath(), |
| 530 | line_number))) |
| 531 | return result |
| 532 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 533 | |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 534 | def CheckGnChanges(input_api, output_api): |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 535 | file_filter = lambda x: (input_api.FilterSourceFile( |
Oleh Prypin | afe0165 | 2017-10-04 15:56:08 +0200 | [diff] [blame] | 536 | x, white_list=(r'.+\.(gn|gni)$',), |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 537 | black_list=(r'.*/presubmit_checks_lib/testdata/.*',))) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 538 | |
| 539 | gn_files = [] |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 540 | for f in input_api.AffectedSourceFiles(file_filter): |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 541 | gn_files.append(f) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 542 | |
| 543 | result = [] |
| 544 | if gn_files: |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 545 | result.extend(CheckNoSourcesAbove(input_api, gn_files, output_api)) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 546 | result.extend(CheckNoMixingSources(input_api, gn_files, output_api)) |
| 547 | result.extend(CheckNoPackageBoundaryViolations(input_api, gn_files, |
| 548 | output_api)) |
Mirko Bonadei | a05d47e | 2018-05-09 11:03:38 +0200 | [diff] [blame] | 549 | result.extend(CheckPublicDepsIsNotUsed(gn_files, input_api, output_api)) |
Patrik Höglund | 6f49106 | 2018-01-11 12:04:23 +0100 | [diff] [blame] | 550 | result.extend(CheckCheckIncludesIsNotUsed(gn_files, output_api)) |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 551 | result.extend(CheckNoWarningSuppressionFlagsAreAdded(gn_files, input_api, |
| 552 | output_api)) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 553 | return result |
| 554 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 555 | |
Oleh Prypin | 920b653 | 2017-10-05 11:28:51 +0200 | [diff] [blame] | 556 | def CheckGnGen(input_api, output_api): |
| 557 | """Runs `gn gen --check` with default args to detect mismatches between |
| 558 | #includes and dependencies in the BUILD.gn files, as well as general build |
| 559 | errors. |
| 560 | """ |
| 561 | with _AddToPath(input_api.os_path.join( |
| 562 | input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')): |
| 563 | from gn_check import RunGnCheck |
Mirko Bonadei | d866544 | 2018-09-04 12:17:27 +0200 | [diff] [blame] | 564 | errors = RunGnCheck(FindSrcDirPath(input_api.PresubmitLocalPath()))[:5] |
Oleh Prypin | 920b653 | 2017-10-05 11:28:51 +0200 | [diff] [blame] | 565 | if errors: |
| 566 | return [output_api.PresubmitPromptWarning( |
| 567 | 'Some #includes do not match the build dependency graph. Please run:\n' |
| 568 | ' gn gen --check <out_dir>', |
| 569 | long_text='\n\n'.join(errors))] |
| 570 | return [] |
| 571 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 572 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 573 | def CheckUnwantedDependencies(input_api, output_api, source_file_filter): |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 574 | """Runs checkdeps on #include statements added in this |
| 575 | change. Breaking - rules is an error, breaking ! rules is a |
| 576 | warning. |
| 577 | """ |
| 578 | # Copied from Chromium's src/PRESUBMIT.py. |
| 579 | |
| 580 | # We need to wait until we have an input_api object and use this |
| 581 | # roundabout construct to import checkdeps because this file is |
| 582 | # eval-ed and thus doesn't have __file__. |
Mirko Bonadei | d866544 | 2018-09-04 12:17:27 +0200 | [diff] [blame] | 583 | src_path = FindSrcDirPath(input_api.PresubmitLocalPath()) |
| 584 | checkdeps_path = input_api.os_path.join(src_path, 'buildtools', 'checkdeps') |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 585 | if not os.path.exists(checkdeps_path): |
| 586 | return [output_api.PresubmitError( |
| 587 | 'Cannot find checkdeps at %s\nHave you run "gclient sync" to ' |
| 588 | 'download all the DEPS entries?' % checkdeps_path)] |
| 589 | with _AddToPath(checkdeps_path): |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 590 | import checkdeps |
| 591 | from cpp_checker import CppChecker |
| 592 | from rules import Rule |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 593 | |
| 594 | added_includes = [] |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 595 | for f in input_api.AffectedFiles(file_filter=source_file_filter): |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 596 | if not CppChecker.IsCppFile(f.LocalPath()): |
| 597 | continue |
| 598 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 599 | changed_lines = [line for _, line in f.ChangedContents()] |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 600 | added_includes.append([f.LocalPath(), changed_lines]) |
| 601 | |
| 602 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 603 | |
| 604 | error_descriptions = [] |
| 605 | warning_descriptions = [] |
| 606 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 607 | added_includes): |
| 608 | description_with_path = '%s\n %s' % (path, rule_description) |
| 609 | if rule_type == Rule.DISALLOW: |
| 610 | error_descriptions.append(description_with_path) |
| 611 | else: |
| 612 | warning_descriptions.append(description_with_path) |
| 613 | |
| 614 | results = [] |
| 615 | if error_descriptions: |
| 616 | results.append(output_api.PresubmitError( |
kjellander | a7066a3 | 2017-03-23 03:47:05 -0700 | [diff] [blame] | 617 | 'You added one or more #includes that violate checkdeps rules.\n' |
| 618 | 'Check that the DEPS files in these locations contain valid rules.\n' |
| 619 | 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for ' |
| 620 | 'more details about checkdeps.', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 621 | error_descriptions)) |
| 622 | if warning_descriptions: |
| 623 | results.append(output_api.PresubmitPromptOrNotify( |
| 624 | 'You added one or more #includes of files that are temporarily\n' |
| 625 | 'allowed but being removed. Can you avoid introducing the\n' |
kjellander | a7066a3 | 2017-03-23 03:47:05 -0700 | [diff] [blame] | 626 | '#include? See relevant DEPS file(s) for details and contacts.\n' |
| 627 | 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for ' |
| 628 | 'more details about checkdeps.', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 629 | warning_descriptions)) |
| 630 | return results |
| 631 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 632 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 633 | def CheckCommitMessageBugEntry(input_api, output_api): |
| 634 | """Check that bug entries are well-formed in commit message.""" |
| 635 | bogus_bug_msg = ( |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 636 | 'Bogus Bug entry: %s. Please specify the issue tracker prefix and the ' |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 637 | 'issue number, separated by a colon, e.g. webrtc:123 or chromium:12345.') |
| 638 | results = [] |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 639 | for bug in input_api.change.BugsFromDescription(): |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 640 | bug = bug.strip() |
| 641 | if bug.lower() == 'none': |
| 642 | continue |
charujain | 81a58c7 | 2017-09-25 13:25:45 +0200 | [diff] [blame] | 643 | if 'b/' not in bug and ':' not in bug: |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 644 | try: |
| 645 | if int(bug) > 100000: |
| 646 | # Rough indicator for current chromium bugs. |
| 647 | prefix_guess = 'chromium' |
| 648 | else: |
| 649 | prefix_guess = 'webrtc' |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 650 | results.append('Bug entry requires issue tracker prefix, e.g. %s:%s' % |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 651 | (prefix_guess, bug)) |
| 652 | except ValueError: |
| 653 | results.append(bogus_bug_msg % bug) |
charujain | 81a58c7 | 2017-09-25 13:25:45 +0200 | [diff] [blame] | 654 | elif not (re.match(r'\w+:\d+', bug) or re.match(r'b/\d+', bug)): |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 655 | results.append(bogus_bug_msg % bug) |
| 656 | return [output_api.PresubmitError(r) for r in results] |
| 657 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 658 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 659 | def CheckChangeHasBugField(input_api, output_api): |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 660 | """Requires that the changelist is associated with a bug. |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 661 | |
| 662 | This check is stricter than the one in depot_tools/presubmit_canned_checks.py |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 663 | since it fails the presubmit if the bug field is missing or doesn't contain |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 664 | a bug reference. |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 665 | |
| 666 | This supports both 'BUG=' and 'Bug:' since we are in the process of migrating |
| 667 | to Gerrit and it encourages the usage of 'Bug:'. |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 668 | """ |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 669 | if input_api.change.BugsFromDescription(): |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 670 | return [] |
| 671 | else: |
| 672 | return [output_api.PresubmitError( |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 673 | 'The "Bug: [bug number]" footer is mandatory. Please create a bug and ' |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 674 | 'reference it using either of:\n' |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 675 | ' * https://bugs.webrtc.org - reference it using Bug: webrtc:XXXX\n' |
| 676 | ' * https://crbug.com - reference it using Bug: chromium:XXXXXX')] |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 677 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 678 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 679 | def CheckJSONParseErrors(input_api, output_api, source_file_filter): |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 680 | """Check that JSON files do not contain syntax errors.""" |
| 681 | |
| 682 | def FilterFile(affected_file): |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 683 | return (input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json' |
| 684 | and source_file_filter(affected_file)) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 685 | |
| 686 | def GetJSONParseError(input_api, filename): |
| 687 | try: |
| 688 | contents = input_api.ReadFile(filename) |
| 689 | input_api.json.loads(contents) |
| 690 | except ValueError as e: |
| 691 | return e |
| 692 | return None |
| 693 | |
| 694 | results = [] |
| 695 | for affected_file in input_api.AffectedFiles( |
| 696 | file_filter=FilterFile, include_deletes=False): |
| 697 | parse_error = GetJSONParseError(input_api, |
| 698 | affected_file.AbsoluteLocalPath()) |
| 699 | if parse_error: |
| 700 | results.append(output_api.PresubmitError('%s could not be parsed: %s' % |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 701 | (affected_file.LocalPath(), |
| 702 | parse_error))) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 703 | return results |
| 704 | |
| 705 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 706 | def RunPythonTests(input_api, output_api): |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 707 | def Join(*args): |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 708 | return input_api.os_path.join(input_api.PresubmitLocalPath(), *args) |
| 709 | |
| 710 | test_directories = [ |
Edward Lemur | 6d01f6d | 2017-09-14 17:02:01 +0200 | [diff] [blame] | 711 | input_api.PresubmitLocalPath(), |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 712 | Join('rtc_tools', 'py_event_log_analyzer'), |
| 713 | Join('rtc_tools'), |
| 714 | Join('audio', 'test', 'unittests'), |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 715 | ] + [ |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 716 | root for root, _, files in os.walk(Join('tools_webrtc')) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 717 | if any(f.endswith('_test.py') for f in files) |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 718 | ] |
| 719 | |
| 720 | tests = [] |
| 721 | for directory in test_directories: |
| 722 | tests.extend( |
| 723 | input_api.canned_checks.GetUnitTestsInDirectory( |
| 724 | input_api, |
| 725 | output_api, |
| 726 | directory, |
| 727 | whitelist=[r'.+_test\.py$'])) |
| 728 | return input_api.RunTests(tests, parallel=True) |
| 729 | |
| 730 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 731 | def CheckUsageOfGoogleProtobufNamespace(input_api, output_api, |
| 732 | source_file_filter): |
mbonadei | 38415b2 | 2017-04-07 05:38:01 -0700 | [diff] [blame] | 733 | """Checks that the namespace google::protobuf has not been used.""" |
| 734 | files = [] |
| 735 | pattern = input_api.re.compile(r'google::protobuf') |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 736 | proto_utils_path = os.path.join('rtc_base', 'protobuf_utils.h') |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 737 | file_filter = lambda x: (input_api.FilterSourceFile(x) |
| 738 | and source_file_filter(x)) |
| 739 | for f in input_api.AffectedSourceFiles(file_filter): |
mbonadei | 38415b2 | 2017-04-07 05:38:01 -0700 | [diff] [blame] | 740 | if f.LocalPath() in [proto_utils_path, 'PRESUBMIT.py']: |
| 741 | continue |
| 742 | contents = input_api.ReadFile(f) |
| 743 | if pattern.search(contents): |
| 744 | files.append(f) |
| 745 | |
| 746 | if files: |
| 747 | return [output_api.PresubmitError( |
| 748 | 'Please avoid to use namespace `google::protobuf` directly.\n' |
| 749 | 'Add a using directive in `%s` and include that header instead.' |
| 750 | % proto_utils_path, files)] |
| 751 | return [] |
| 752 | |
| 753 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 754 | def _LicenseHeader(input_api): |
| 755 | """Returns the license header regexp.""" |
| 756 | # Accept any year number from 2003 to the current year |
| 757 | current_year = int(input_api.time.strftime('%Y')) |
| 758 | allowed_years = (str(s) for s in reversed(xrange(2003, current_year + 1))) |
| 759 | years_re = '(' + '|'.join(allowed_years) + ')' |
| 760 | license_header = ( |
| 761 | r'.*? Copyright( \(c\))? %(year)s The WebRTC [Pp]roject [Aa]uthors\. ' |
| 762 | r'All [Rr]ights [Rr]eserved\.\n' |
| 763 | r'.*?\n' |
| 764 | r'.*? Use of this source code is governed by a BSD-style license\n' |
| 765 | r'.*? that can be found in the LICENSE file in the root of the source\n' |
| 766 | r'.*? tree\. An additional intellectual property rights grant can be ' |
| 767 | r'found\n' |
| 768 | r'.*? in the file PATENTS\. All contributing project authors may\n' |
| 769 | r'.*? be found in the AUTHORS file in the root of the source tree\.\n' |
| 770 | ) % { |
| 771 | 'year': years_re, |
| 772 | } |
| 773 | return license_header |
| 774 | |
| 775 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 776 | def CommonChecks(input_api, output_api): |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 777 | """Checks common to both upload and commit.""" |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 778 | results = [] |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 779 | # Filter out files that are in objc or ios dirs from being cpplint-ed since |
| 780 | # they do not follow C++ lint rules. |
| 781 | black_list = input_api.DEFAULT_BLACK_LIST + ( |
| 782 | r".*\bobjc[\\\/].*", |
Kári Tristan Helgason | 3fa3517 | 2016-09-09 08:55:05 +0000 | [diff] [blame] | 783 | r".*objc\.[hcm]+$", |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 784 | ) |
| 785 | source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 786 | results.extend(CheckApprovedFilesLintClean( |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 787 | input_api, output_api, source_file_filter)) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 788 | results.extend(input_api.canned_checks.CheckLicense( |
| 789 | input_api, output_api, _LicenseHeader(input_api))) |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 790 | results.extend(input_api.canned_checks.RunPylint(input_api, output_api, |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 791 | black_list=(r'^base[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 792 | r'^build[\\\/].*\.py$', |
| 793 | r'^buildtools[\\\/].*\.py$', |
kjellander | 38c65c8 | 2017-04-12 22:43:38 -0700 | [diff] [blame] | 794 | r'^infra[\\\/].*\.py$', |
Henrik Kjellander | 0779e8f | 2016-12-22 12:01:17 +0100 | [diff] [blame] | 795 | r'^ios[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 796 | r'^out.*[\\\/].*\.py$', |
| 797 | r'^testing[\\\/].*\.py$', |
| 798 | r'^third_party[\\\/].*\.py$', |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 799 | r'^tools[\\\/].*\.py$', |
kjellander | afd5494 | 2016-12-17 12:21:39 -0800 | [diff] [blame] | 800 | # TODO(phoglund): should arguably be checked. |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 801 | r'^tools_webrtc[\\\/]mb[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 802 | r'^xcodebuild.*[\\\/].*\.py$',), |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 803 | pylintrc='pylintrc')) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 804 | |
nisse | 3d21e23 | 2016-09-02 03:07:06 -0700 | [diff] [blame] | 805 | # TODO(nisse): talk/ is no more, so make below checks simpler? |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 806 | # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since |
| 807 | # we need to have different license checks in talk/ and webrtc/ directories. |
| 808 | # Instead, hand-picked checks are included below. |
Henrik Kjellander | 6322467 | 2015-09-08 08:03:56 +0200 | [diff] [blame] | 809 | |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 810 | # .m and .mm files are ObjC files. For simplicity we will consider .h files in |
| 811 | # ObjC subdirectories ObjC headers. |
| 812 | objc_filter_list = (r'.+\.m$', r'.+\.mm$', r'.+objc\/.+\.h$') |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 813 | # Skip long-lines check for DEPS and GN files. |
| 814 | build_file_filter_list = (r'.+\.gn$', r'.+\.gni$', 'DEPS') |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 815 | # Also we will skip most checks for third_party directory. |
Artem Titov | 42f0d78 | 2018-06-27 13:23:17 +0200 | [diff] [blame] | 816 | third_party_filter_list = (r'^third_party[\\\/].+',) |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 817 | eighty_char_sources = lambda x: input_api.FilterSourceFile(x, |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 818 | black_list=build_file_filter_list + objc_filter_list + |
| 819 | third_party_filter_list) |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 820 | hundred_char_sources = lambda x: input_api.FilterSourceFile(x, |
| 821 | white_list=objc_filter_list) |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 822 | non_third_party_sources = lambda x: input_api.FilterSourceFile(x, |
| 823 | black_list=third_party_filter_list) |
| 824 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 825 | results.extend(input_api.canned_checks.CheckLongLines( |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 826 | input_api, output_api, maxlen=80, source_file_filter=eighty_char_sources)) |
| 827 | results.extend(input_api.canned_checks.CheckLongLines( |
| 828 | input_api, output_api, maxlen=100, |
| 829 | source_file_filter=hundred_char_sources)) |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 830 | results.extend(input_api.canned_checks.CheckChangeHasNoTabs( |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 831 | input_api, output_api, source_file_filter=non_third_party_sources)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 832 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 833 | input_api, output_api, source_file_filter=non_third_party_sources)) |
kjellander | e5dc62a | 2016-12-14 00:16:21 -0800 | [diff] [blame] | 834 | results.extend(input_api.canned_checks.CheckAuthorizedAuthor( |
Oleh Prypin | e073514 | 2018-10-04 11:15:54 +0200 | [diff] [blame] | 835 | input_api, output_api, bot_whitelist=[ |
| 836 | 'chromium-webrtc-autoroll@webrtc-ci.iam.gserviceaccount.com' |
| 837 | ])) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 838 | results.extend(input_api.canned_checks.CheckChangeTodoHasOwner( |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 839 | input_api, output_api, source_file_filter=non_third_party_sources)) |
Yves Gerey | 87a9353 | 2018-06-20 15:51:49 +0200 | [diff] [blame] | 840 | results.extend(input_api.canned_checks.CheckPatchFormatted( |
| 841 | input_api, output_api)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 842 | results.extend(CheckNativeApiHeaderChanges(input_api, output_api)) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 843 | results.extend(CheckNoIOStreamInHeaders( |
| 844 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 845 | results.extend(CheckNoPragmaOnce( |
| 846 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 847 | results.extend(CheckNoFRIEND_TEST( |
| 848 | input_api, output_api, source_file_filter=non_third_party_sources)) |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 849 | results.extend(CheckGnChanges(input_api, output_api)) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 850 | results.extend(CheckUnwantedDependencies( |
| 851 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 852 | results.extend(CheckJSONParseErrors( |
| 853 | input_api, output_api, source_file_filter=non_third_party_sources)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 854 | results.extend(RunPythonTests(input_api, output_api)) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 855 | results.extend(CheckUsageOfGoogleProtobufNamespace( |
| 856 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 857 | results.extend(CheckOrphanHeaders( |
| 858 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 859 | results.extend(CheckNewlineAtTheEndOfProtoFiles( |
| 860 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 861 | results.extend(CheckNoStreamUsageIsAdded( |
Artem Titov | 739351d | 2018-05-11 12:21:36 +0200 | [diff] [blame] | 862 | input_api, output_api, non_third_party_sources)) |
Mirko Bonadei | 7e4ee6e | 2018-09-28 11:45:23 +0200 | [diff] [blame] | 863 | results.extend(CheckAddedDepsHaveTargetApprovals(input_api, output_api)) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 864 | results.extend(CheckApiDepsFileIsUpToDate(input_api, output_api)) |
tzik | a06bf85 | 2018-11-15 20:37:35 +0900 | [diff] [blame] | 865 | results.extend(CheckAbslMemoryInclude( |
| 866 | input_api, output_api, non_third_party_sources)) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 867 | return results |
| 868 | |
| 869 | |
| 870 | def CheckApiDepsFileIsUpToDate(input_api, output_api): |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 871 | """Check that 'include_rules' in api/DEPS is up to date. |
| 872 | |
| 873 | The file api/DEPS must be kept up to date in order to avoid to avoid to |
| 874 | include internal header from WebRTC's api/ headers. |
| 875 | |
| 876 | This check is focused on ensuring that 'include_rules' contains a deny |
| 877 | rule for each root level directory. More focused allow rules can be |
| 878 | added to 'specific_include_rules'. |
| 879 | """ |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 880 | results = [] |
| 881 | api_deps = os.path.join(input_api.PresubmitLocalPath(), 'api', 'DEPS') |
| 882 | with open(api_deps) as f: |
| 883 | deps_content = _ParseDeps(f.read()) |
| 884 | |
| 885 | include_rules = deps_content.get('include_rules', []) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 886 | |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 887 | # Only check top level directories affected by the current CL. |
| 888 | dirs_to_check = set() |
| 889 | for f in input_api.AffectedFiles(): |
| 890 | path_tokens = [t for t in f.LocalPath().split(os.sep) if t] |
| 891 | if len(path_tokens) > 1: |
| 892 | if (path_tokens[0] != 'api' and |
| 893 | os.path.isdir(os.path.join(input_api.PresubmitLocalPath(), |
| 894 | path_tokens[0]))): |
| 895 | dirs_to_check.add(path_tokens[0]) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 896 | |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 897 | missing_include_rules = set() |
| 898 | for p in dirs_to_check: |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 899 | rule = '-%s' % p |
| 900 | if rule not in include_rules: |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 901 | missing_include_rules.add(rule) |
| 902 | |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 903 | if missing_include_rules: |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 904 | error_msg = [ |
| 905 | 'include_rules = [\n', |
| 906 | ' ...\n', |
| 907 | ] |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 908 | |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 909 | for r in sorted(missing_include_rules): |
| 910 | error_msg.append(' "%s",\n' % str(r)) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 911 | |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 912 | error_msg.append(' ...\n') |
| 913 | error_msg.append(']\n') |
| 914 | |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 915 | results.append(output_api.PresubmitError( |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 916 | 'New root level directory detected! WebRTC api/ headers should ' |
| 917 | 'not #include headers from \n' |
| 918 | 'the new directory, so please update "include_rules" in file\n' |
| 919 | '"%s". Example:\n%s\n' % (api_deps, ''.join(error_msg)))) |
| 920 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 921 | return results |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 922 | |
tzik | a06bf85 | 2018-11-15 20:37:35 +0900 | [diff] [blame] | 923 | def CheckAbslMemoryInclude(input_api, output_api, source_file_filter): |
| 924 | pattern = input_api.re.compile( |
| 925 | r'^#include\s*"absl/memory/memory.h"', input_api.re.MULTILINE) |
| 926 | file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.h')) |
| 927 | and source_file_filter(f)) |
| 928 | |
| 929 | files = [] |
| 930 | for f in input_api.AffectedFiles( |
| 931 | include_deletes=False, file_filter=file_filter): |
| 932 | contents = input_api.ReadFile(f) |
| 933 | if pattern.search(contents): |
| 934 | continue |
| 935 | for _, line in f.ChangedContents(): |
| 936 | if 'absl::make_unique' in line: |
| 937 | files.append(f) |
| 938 | break |
| 939 | |
| 940 | if len(files): |
| 941 | return [output_api.PresubmitError( |
| 942 | 'Please include "absl/memory/memory.h" header for' |
| 943 | ' absl::make_unique.\nThis header may or may not be included' |
| 944 | ' transitively depends on the C++ standard version.', |
| 945 | files)] |
| 946 | return [] |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 947 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 948 | def CheckChangeOnUpload(input_api, output_api): |
| 949 | results = [] |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 950 | results.extend(CommonChecks(input_api, output_api)) |
Oleh Prypin | 920b653 | 2017-10-05 11:28:51 +0200 | [diff] [blame] | 951 | results.extend(CheckGnGen(input_api, output_api)) |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 952 | results.extend( |
| 953 | input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 954 | return results |
| 955 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 956 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 957 | def CheckChangeOnCommit(input_api, output_api): |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 958 | results = [] |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 959 | results.extend(CommonChecks(input_api, output_api)) |
| 960 | results.extend(VerifyNativeApiHeadersListIsValid(input_api, output_api)) |
Artem Titov | 42f0d78 | 2018-06-27 13:23:17 +0200 | [diff] [blame] | 961 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 962 | results.extend(input_api.canned_checks.CheckChangeWasUploaded( |
| 963 | input_api, output_api)) |
| 964 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 965 | input_api, output_api)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 966 | results.extend(CheckChangeHasBugField(input_api, output_api)) |
| 967 | results.extend(CheckCommitMessageBugEntry(input_api, output_api)) |
kjellander@webrtc.org | 12cb88c | 2014-02-13 11:53:43 +0000 | [diff] [blame] | 968 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
| 969 | input_api, output_api, |
| 970 | json_url='http://webrtc-status.appspot.com/current?format=json')) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 971 | return results |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 972 | |
| 973 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 974 | def CheckOrphanHeaders(input_api, output_api, source_file_filter): |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 975 | # We need to wait until we have an input_api object and use this |
| 976 | # roundabout construct to import prebubmit_checks_lib because this file is |
| 977 | # eval-ed and thus doesn't have __file__. |
Patrik Höglund | 2f3f722 | 2017-12-19 11:08:56 +0100 | [diff] [blame] | 978 | error_msg = """{} should be listed in {}.""" |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 979 | results = [] |
Patrik Höglund | 7e60de2 | 2018-01-09 14:22:00 +0100 | [diff] [blame] | 980 | orphan_blacklist = [ |
| 981 | os.path.join('tools_webrtc', 'ios', 'SDK'), |
| 982 | ] |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 983 | with _AddToPath(input_api.os_path.join( |
| 984 | input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')): |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 985 | from check_orphan_headers import GetBuildGnPathFromFilePath |
| 986 | from check_orphan_headers import IsHeaderInBuildGn |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 987 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 988 | file_filter = lambda x: input_api.FilterSourceFile( |
| 989 | x, black_list=orphan_blacklist) and source_file_filter(x) |
| 990 | for f in input_api.AffectedSourceFiles(file_filter): |
Patrik Höglund | 7e60de2 | 2018-01-09 14:22:00 +0100 | [diff] [blame] | 991 | if f.LocalPath().endswith('.h'): |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 992 | file_path = os.path.abspath(f.LocalPath()) |
| 993 | root_dir = os.getcwd() |
| 994 | gn_file_path = GetBuildGnPathFromFilePath(file_path, os.path.exists, |
| 995 | root_dir) |
| 996 | in_build_gn = IsHeaderInBuildGn(file_path, gn_file_path) |
| 997 | if not in_build_gn: |
| 998 | results.append(output_api.PresubmitError(error_msg.format( |
Patrik Höglund | 2f3f722 | 2017-12-19 11:08:56 +0100 | [diff] [blame] | 999 | f.LocalPath(), os.path.relpath(gn_file_path)))) |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 1000 | return results |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 1001 | |
| 1002 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 1003 | def CheckNewlineAtTheEndOfProtoFiles(input_api, output_api, source_file_filter): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 1004 | """Checks that all .proto files are terminated with a newline.""" |
| 1005 | error_msg = 'File {} must end with exactly one newline.' |
| 1006 | results = [] |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 1007 | file_filter = lambda x: input_api.FilterSourceFile( |
| 1008 | x, white_list=(r'.+\.proto$',)) and source_file_filter(x) |
| 1009 | for f in input_api.AffectedSourceFiles(file_filter): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 1010 | file_path = f.LocalPath() |
| 1011 | with open(file_path) as f: |
| 1012 | lines = f.readlines() |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 1013 | if len(lines) > 0 and not lines[-1].endswith('\n'): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 1014 | results.append(output_api.PresubmitError(error_msg.format(file_path))) |
| 1015 | return results |
Mirko Bonadei | 7e4ee6e | 2018-09-28 11:45:23 +0200 | [diff] [blame] | 1016 | |
| 1017 | |
| 1018 | def _ExtractAddRulesFromParsedDeps(parsed_deps): |
| 1019 | """Extract the rules that add dependencies from a parsed DEPS file. |
| 1020 | |
| 1021 | Args: |
| 1022 | parsed_deps: the locals dictionary from evaluating the DEPS file.""" |
| 1023 | add_rules = set() |
| 1024 | add_rules.update([ |
| 1025 | rule[1:] for rule in parsed_deps.get('include_rules', []) |
| 1026 | if rule.startswith('+') or rule.startswith('!') |
| 1027 | ]) |
| 1028 | for _, rules in parsed_deps.get('specific_include_rules', |
| 1029 | {}).iteritems(): |
| 1030 | add_rules.update([ |
| 1031 | rule[1:] for rule in rules |
| 1032 | if rule.startswith('+') or rule.startswith('!') |
| 1033 | ]) |
| 1034 | return add_rules |
| 1035 | |
| 1036 | |
| 1037 | def _ParseDeps(contents): |
| 1038 | """Simple helper for parsing DEPS files.""" |
| 1039 | # Stubs for handling special syntax in the root DEPS file. |
| 1040 | class VarImpl(object): |
| 1041 | |
| 1042 | def __init__(self, local_scope): |
| 1043 | self._local_scope = local_scope |
| 1044 | |
| 1045 | def Lookup(self, var_name): |
| 1046 | """Implements the Var syntax.""" |
| 1047 | try: |
| 1048 | return self._local_scope['vars'][var_name] |
| 1049 | except KeyError: |
| 1050 | raise Exception('Var is not defined: %s' % var_name) |
| 1051 | |
| 1052 | local_scope = {} |
| 1053 | global_scope = { |
| 1054 | 'Var': VarImpl(local_scope).Lookup, |
| 1055 | } |
| 1056 | exec contents in global_scope, local_scope |
| 1057 | return local_scope |
| 1058 | |
| 1059 | |
| 1060 | def _CalculateAddedDeps(os_path, old_contents, new_contents): |
| 1061 | """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns |
| 1062 | a set of DEPS entries that we should look up. |
| 1063 | |
| 1064 | For a directory (rather than a specific filename) we fake a path to |
| 1065 | a specific filename by adding /DEPS. This is chosen as a file that |
| 1066 | will seldom or never be subject to per-file include_rules. |
| 1067 | """ |
| 1068 | # We ignore deps entries on auto-generated directories. |
| 1069 | auto_generated_dirs = ['grit', 'jni'] |
| 1070 | |
| 1071 | old_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(old_contents)) |
| 1072 | new_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(new_contents)) |
| 1073 | |
| 1074 | added_deps = new_deps.difference(old_deps) |
| 1075 | |
| 1076 | results = set() |
| 1077 | for added_dep in added_deps: |
| 1078 | if added_dep.split('/')[0] in auto_generated_dirs: |
| 1079 | continue |
| 1080 | # Assume that a rule that ends in .h is a rule for a specific file. |
| 1081 | if added_dep.endswith('.h'): |
| 1082 | results.add(added_dep) |
| 1083 | else: |
| 1084 | results.add(os_path.join(added_dep, 'DEPS')) |
| 1085 | return results |
| 1086 | |
| 1087 | |
| 1088 | def CheckAddedDepsHaveTargetApprovals(input_api, output_api): |
| 1089 | """When a dependency prefixed with + is added to a DEPS file, we |
| 1090 | want to make sure that the change is reviewed by an OWNER of the |
| 1091 | target file or directory, to avoid layering violations from being |
| 1092 | introduced. This check verifies that this happens. |
| 1093 | """ |
| 1094 | virtual_depended_on_files = set() |
| 1095 | |
| 1096 | file_filter = lambda f: not input_api.re.match( |
| 1097 | r"^third_party[\\\/](WebKit|blink)[\\\/].*", f.LocalPath()) |
| 1098 | for f in input_api.AffectedFiles(include_deletes=False, |
| 1099 | file_filter=file_filter): |
| 1100 | filename = input_api.os_path.basename(f.LocalPath()) |
| 1101 | if filename == 'DEPS': |
| 1102 | virtual_depended_on_files.update(_CalculateAddedDeps( |
| 1103 | input_api.os_path, |
| 1104 | '\n'.join(f.OldContents()), |
| 1105 | '\n'.join(f.NewContents()))) |
| 1106 | |
| 1107 | if not virtual_depended_on_files: |
| 1108 | return [] |
| 1109 | |
| 1110 | if input_api.is_committing: |
| 1111 | if input_api.tbr: |
| 1112 | return [output_api.PresubmitNotifyResult( |
| 1113 | '--tbr was specified, skipping OWNERS check for DEPS additions')] |
| 1114 | if input_api.dry_run: |
| 1115 | return [output_api.PresubmitNotifyResult( |
| 1116 | 'This is a dry run, skipping OWNERS check for DEPS additions')] |
| 1117 | if not input_api.change.issue: |
| 1118 | return [output_api.PresubmitError( |
| 1119 | "DEPS approval by OWNERS check failed: this change has " |
| 1120 | "no change number, so we can't check it for approvals.")] |
| 1121 | output = output_api.PresubmitError |
| 1122 | else: |
| 1123 | output = output_api.PresubmitNotifyResult |
| 1124 | |
| 1125 | owners_db = input_api.owners_db |
| 1126 | owner_email, reviewers = ( |
| 1127 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
| 1128 | input_api, |
| 1129 | owners_db.email_regexp, |
| 1130 | approval_needed=input_api.is_committing)) |
| 1131 | |
| 1132 | owner_email = owner_email or input_api.change.author_email |
| 1133 | |
| 1134 | reviewers_plus_owner = set(reviewers) |
| 1135 | if owner_email: |
| 1136 | reviewers_plus_owner.add(owner_email) |
| 1137 | missing_files = owners_db.files_not_covered_by(virtual_depended_on_files, |
| 1138 | reviewers_plus_owner) |
| 1139 | |
| 1140 | # We strip the /DEPS part that was added by |
| 1141 | # _FilesToCheckForIncomingDeps to fake a path to a file in a |
| 1142 | # directory. |
| 1143 | def StripDeps(path): |
| 1144 | start_deps = path.rfind('/DEPS') |
| 1145 | if start_deps != -1: |
| 1146 | return path[:start_deps] |
| 1147 | else: |
| 1148 | return path |
| 1149 | unapproved_dependencies = ["'+%s'," % StripDeps(path) |
| 1150 | for path in missing_files] |
| 1151 | |
| 1152 | if unapproved_dependencies: |
| 1153 | output_list = [ |
| 1154 | output('You need LGTM from owners of depends-on paths in DEPS that were ' |
| 1155 | 'modified in this CL:\n %s' % |
| 1156 | '\n '.join(sorted(unapproved_dependencies)))] |
| 1157 | suggested_owners = owners_db.reviewers_for(missing_files, owner_email) |
| 1158 | output_list.append(output( |
| 1159 | 'Suggested missing target path OWNERS:\n %s' % |
| 1160 | '\n '.join(suggested_owners or []))) |
| 1161 | return output_list |
| 1162 | |
| 1163 | return [] |