aboutsummaryrefslogtreecommitdiff
path: root/PRESUBMIT.py
blob: dafbc5ea2af0a0ff2907eb7a5b3be830e69583cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright (c) 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import re

# Rather than pass this to all of the checks, we override the global excluded
# list with this one.
_EXCLUDED_PATHS = (
  # Exclude all of third_party/ except for BUILD.gns that we maintain.
  r'third_party[\\\/].*(?<!BUILD.gn)$',
  # Exclude everything under third_party/chromium_quic/{src|build}
  r'third_party/chromium_quic/(src|build)/.*',
  # Output directories (just in case)
  r'.*\bDebug[\\\/].*',
  r'.*\bRelease[\\\/].*',
  r'.*\bxcodebuild[\\\/].*',
  r'.*\bout[\\\/].*',
  # There is no point in processing a patch file.
  r'.+\.diff$',
  r'.+\.patch$',
)


def _CheckDeps(input_api, output_api):
  results = []
  import sys
  original_sys_path = sys.path
  try:
    sys.path = sys.path + [input_api.os_path.join(
        input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
    import checkdeps
    from cpp_checker import CppChecker
    from rules import Rule
  finally:
    sys.path = original_sys_path

  deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
  deps_checker.CheckDirectory(input_api.PresubmitLocalPath())
  deps_results = deps_checker.results_formatter.GetResults()
  for violation in deps_results:
    results.append(output_api.PresubmitError(violation))
  return results


# Matches Foo(Foo&&) when not followed by noexcept.
_RE_PATTERN_MOVE_WITHOUT_NOEXCEPT = re.compile(
    r'\s*(?P<classname>\w+)\((?P=classname)&&[^)]*\)\s*(?!noexcept)\s*[{;=]')


def _CheckNoexceptOnMove(filename, clean_lines, linenum, error):
  """Checks that move constructors are declared with 'noexcept'.

  Args:
    filename: The name of the current file.
    clean_lines: A CleansedLines instance containing the file.
    linenum: The number of the line to check.
    error: The function to call with any errors found.
  """
  # We only check headers as noexcept is meaningful on declarations, not
  # definitions.  This may skip some definitions in .cc files though.
  if not filename.endswith('.h'):
    return

  line = clean_lines.elided[linenum]
  matched = _RE_PATTERN_MOVE_WITHOUT_NOEXCEPT.match(line)
  if matched:
    error(filename, linenum, 'runtime/noexcept', 4,
          'Move constructor of %s not declared \'noexcept\' in %s' %
          (matched.group('classname'), matched.group(0).strip()))

# - We disable c++11 header checks since Open Screen allows them.
# - We disable whitespace/braces because of various false positives.
# - There are some false positives with 'explicit' checks, but it's useful
#   enough to keep.
# - We add a custom check for 'noexcept' usage.
def _CheckChangeLintsClean(input_api, output_api):
  """Checks that all '.cc' and '.h' files pass cpplint.py."""
  result = []

  cpplint = input_api.cpplint
  # Access to a protected member _XX of a client class
  # pylint: disable=protected-access
  cpplint._cpplint_state.ResetErrorCounts()

  cpplint._SetFilters('-build/c++11,-whitespace/braces')
  files = [f.AbsoluteLocalPath() for f in input_api.AffectedSourceFiles(None)]
  for file_name in files:
    # 4 = verbose_level
    cpplint.ProcessFile(file_name, 4, [_CheckNoexceptOnMove])

  if cpplint._cpplint_state.error_count > 0:
    if input_api.is_committing:
      res_type = output_api.PresubmitError
    else:
      res_type = output_api.PresubmitPromptWarning
    result = [res_type('Changelist failed cpplint.py check.')]

  return result


def _CommonChecks(input_api, output_api):
  results = []
  # PanProjectChecks include:
  #   CheckLongLines (@ 80 cols)
  #   CheckChangeHasNoTabs
  #   CheckChangeHasNoStrayWhitespace
  #   CheckLicense
  #   CheckChangeWasUploaded (if committing)
  #   CheckChangeHasDescription
  #   CheckDoNotSubmitInDescription
  #   CheckDoNotSubmitInFiles
  results.extend(input_api.canned_checks.PanProjectChecks(
    input_api, output_api, owners_check=False));

  # No carriage return characters, files end with one EOL (\n).
  results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
    input_api, output_api));

  # Gender inclusivity
  results.extend(input_api.canned_checks.CheckGenderNeutral(
    input_api, output_api))

  # TODO(bug) format required
  results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
    input_api, output_api))

  # Linter.
  results.extend(_CheckChangeLintsClean(input_api, output_api))

  # clang-format
  results.extend(input_api.canned_checks.CheckPatchFormatted(
    input_api, output_api, bypass_warnings=False))

  # GN formatting
  results.extend(input_api.canned_checks.CheckGNFormatted(
    input_api, output_api))

  # buildtools/checkdeps
  results.extend(_CheckDeps(input_api, output_api))
  return results


def CheckChangeOnUpload(input_api, output_api):
  input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS;
  results = []
  results.extend(_CommonChecks(input_api, output_api))
  results.extend(
      input_api.canned_checks.CheckChangedLUCIConfigs(input_api, output_api))
  return results


def CheckChangeOnCommit(input_api, output_api):
  input_api.DEFAULT_FILES_TO_SKIP = _EXCLUDED_PATHS;
  results = []
  results.extend(_CommonChecks(input_api, output_api))
  return results