Ryan Tseng | 85ec17e | 2018-09-06 17:10:05 -0700 | [diff] [blame] | 1 | # Copyright (c) 2018 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame^] | 5 | import re |
| 6 | |
mark a. foltz | 8c11e26 | 2020-07-22 14:29:37 -0700 | [diff] [blame] | 7 | # Rather than pass this to all of the checks, we override the global excluded |
| 8 | # list with this one. |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 9 | _EXCLUDED_PATHS = ( |
| 10 | # Exclude all of third_party/ except for BUILD.gns that we maintain. |
| 11 | r'third_party[\\\/].*(?<!BUILD.gn)$', |
| 12 | # Exclude everything under third_party/chromium_quic/{src|build} |
| 13 | r'third_party/chromium_quic/(src|build)/.*', |
| 14 | # Output directories (just in case) |
| 15 | r'.*\bDebug[\\\/].*', |
| 16 | r'.*\bRelease[\\\/].*', |
| 17 | r'.*\bxcodebuild[\\\/].*', |
| 18 | r'.*\bout[\\\/].*', |
| 19 | # There is no point in processing a patch file. |
| 20 | r'.+\.diff$', |
| 21 | r'.+\.patch$', |
| 22 | ) |
| 23 | |
Ryan Tseng | 85ec17e | 2018-09-06 17:10:05 -0700 | [diff] [blame] | 24 | |
btolsch | 9ba2371 | 2019-04-18 16:36:55 -0700 | [diff] [blame] | 25 | def _CheckDeps(input_api, output_api): |
| 26 | results = [] |
| 27 | import sys |
| 28 | original_sys_path = sys.path |
| 29 | try: |
| 30 | sys.path = sys.path + [input_api.os_path.join( |
| 31 | input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')] |
| 32 | import checkdeps |
| 33 | from cpp_checker import CppChecker |
| 34 | from rules import Rule |
| 35 | finally: |
| 36 | sys.path = original_sys_path |
| 37 | |
btolsch | 9ba2371 | 2019-04-18 16:36:55 -0700 | [diff] [blame] | 38 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
mark a. foltz | 743b620 | 2019-08-29 14:43:43 -0700 | [diff] [blame] | 39 | deps_checker.CheckDirectory(input_api.PresubmitLocalPath()) |
| 40 | deps_results = deps_checker.results_formatter.GetResults() |
| 41 | for violation in deps_results: |
| 42 | results.append(output_api.PresubmitError(violation)) |
btolsch | 9ba2371 | 2019-04-18 16:36:55 -0700 | [diff] [blame] | 43 | return results |
| 44 | |
| 45 | |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame^] | 46 | # Matches Foo(Foo&&) when not followed by noexcept. |
| 47 | _RE_PATTERN_MOVE_WITHOUT_NOEXCEPT = re.compile( |
| 48 | r'\s*(?P<classname>\w+)\((?P=classname)&&[^)]*\)\s*(?!noexcept)\s*[{;=]') |
| 49 | |
| 50 | |
| 51 | def _CheckNoexceptOnMove(filename, clean_lines, linenum, error): |
| 52 | """Checks that move constructors are declared with 'noexcept'. |
| 53 | |
| 54 | Args: |
| 55 | filename: The name of the current file. |
| 56 | clean_lines: A CleansedLines instance containing the file. |
| 57 | linenum: The number of the line to check. |
| 58 | error: The function to call with any errors found. |
| 59 | """ |
| 60 | # We only check headers as noexcept is meaningful on declarations, not |
| 61 | # definitions. This may skip some definitions in .cc files though. |
| 62 | if not filename.endswith('.h'): |
| 63 | return |
| 64 | |
| 65 | line = clean_lines.elided[linenum] |
| 66 | matched = _RE_PATTERN_MOVE_WITHOUT_NOEXCEPT.match(line) |
| 67 | if matched: |
| 68 | error(filename, linenum, 'runtime/noexcept', 4, |
| 69 | 'Move constructor of %s not declared \'noexcept\' in %s' % |
| 70 | (matched.group('classname'), matched.group(0).strip())) |
| 71 | |
| 72 | # - We disable c++11 header checks since Open Screen allows them. |
| 73 | # - We disable whitespace/braces because of various false positives. |
| 74 | # - There are some false positives with 'explicit' checks, but it's useful |
| 75 | # enough to keep. |
| 76 | # - We add a custom check for 'noexcept' usage. |
| 77 | def _CheckChangeLintsClean(input_api, output_api): |
| 78 | """Checks that all '.cc' and '.h' files pass cpplint.py.""" |
| 79 | result = [] |
| 80 | |
| 81 | cpplint = input_api.cpplint |
| 82 | # Access to a protected member _XX of a client class |
| 83 | # pylint: disable=protected-access |
| 84 | cpplint._cpplint_state.ResetErrorCounts() |
| 85 | |
| 86 | cpplint._SetFilters('-build/c++11,-whitespace/braces') |
| 87 | files = [f.AbsoluteLocalPath() for f in input_api.AffectedSourceFiles(None)] |
| 88 | for file_name in files: |
| 89 | # 4 = verbose_level |
| 90 | cpplint.ProcessFile(file_name, 4, [_CheckNoexceptOnMove]) |
| 91 | |
| 92 | if cpplint._cpplint_state.error_count > 0: |
| 93 | if input_api.is_committing: |
| 94 | res_type = output_api.PresubmitError |
| 95 | else: |
| 96 | res_type = output_api.PresubmitPromptWarning |
| 97 | result = [res_type('Changelist failed cpplint.py check.')] |
| 98 | |
| 99 | return result |
| 100 | |
| 101 | |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 102 | def _CommonChecks(input_api, output_api): |
| 103 | results = [] |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 104 | # PanProjectChecks include: |
| 105 | # CheckLongLines (@ 80 cols) |
| 106 | # CheckChangeHasNoTabs |
| 107 | # CheckChangeHasNoStrayWhitespace |
| 108 | # CheckLicense |
| 109 | # CheckChangeWasUploaded (if committing) |
| 110 | # CheckChangeHasDescription |
| 111 | # CheckDoNotSubmitInDescription |
| 112 | # CheckDoNotSubmitInFiles |
| 113 | results.extend(input_api.canned_checks.PanProjectChecks( |
| 114 | input_api, output_api, owners_check=False)); |
| 115 | |
| 116 | # No carriage return characters, files end with one EOL (\n). |
| 117 | results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol( |
| 118 | input_api, output_api)); |
| 119 | |
| 120 | # Gender inclusivity |
| 121 | results.extend(input_api.canned_checks.CheckGenderNeutral( |
| 122 | input_api, output_api)) |
| 123 | |
| 124 | # TODO(bug) format required |
| 125 | results.extend(input_api.canned_checks.CheckChangeTodoHasOwner( |
| 126 | input_api, output_api)) |
| 127 | |
mark a. foltz | b4e53be | 2020-05-28 11:42:36 -0700 | [diff] [blame] | 128 | # Linter. |
mark a. foltz | 01490a4 | 2020-11-19 10:33:36 -0800 | [diff] [blame^] | 129 | results.extend(_CheckChangeLintsClean(input_api, output_api)) |
mark a. foltz | 4410e8e | 2020-05-07 17:10:17 -0700 | [diff] [blame] | 130 | |
| 131 | # clang-format |
| 132 | results.extend(input_api.canned_checks.CheckPatchFormatted( |
| 133 | input_api, output_api, bypass_warnings=False)) |
| 134 | |
| 135 | # GN formatting |
| 136 | results.extend(input_api.canned_checks.CheckGNFormatted( |
| 137 | input_api, output_api)) |
| 138 | |
| 139 | # buildtools/checkdeps |
btolsch | 9ba2371 | 2019-04-18 16:36:55 -0700 | [diff] [blame] | 140 | results.extend(_CheckDeps(input_api, output_api)) |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 141 | return results |
| 142 | |
| 143 | |
Ryan Tseng | 85ec17e | 2018-09-06 17:10:05 -0700 | [diff] [blame] | 144 | def CheckChangeOnUpload(input_api, output_api): |
Josip Sokcevic | 3cc8363 | 2020-07-23 13:30:22 -0700 | [diff] [blame] | 145 | input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS; |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 146 | results = [] |
| 147 | results.extend(_CommonChecks(input_api, output_api)) |
| 148 | results.extend( |
| 149 | input_api.canned_checks.CheckChangedLUCIConfigs(input_api, output_api)) |
| 150 | return results |
| 151 | |
| 152 | |
| 153 | def CheckChangeOnCommit(input_api, output_api): |
Josip Sokcevic | 3cc8363 | 2020-07-23 13:30:22 -0700 | [diff] [blame] | 154 | input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS; |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 155 | results = [] |
| 156 | results.extend(_CommonChecks(input_api, output_api)) |
| 157 | return results |