summaryrefslogtreecommitdiff
path: root/chromium/base/PRESUBMIT.py
blob: 33ec547b2b67ffebb8f8266548b650544d27db87 (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
# Copyright (c) 2012 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.

"""Chromium presubmit script for src/base.

See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details on the presubmit API built into depot_tools.
"""

def _CheckNoInterfacesInBase(input_api, output_api):
  """Checks to make sure no files in libbase.a have |@interface|."""
  pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE)
  files = []
  for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
    if (f.LocalPath().startswith('base/') and
        not "/ios/" in f.LocalPath() and
        not "/test/" in f.LocalPath() and
        not f.LocalPath().endswith('.java') and
        not f.LocalPath().endswith('_unittest.mm') and
        not f.LocalPath().endswith('mac/sdk_forward_declarations.h')):
      contents = input_api.ReadFile(f)
      if pattern.search(contents):
        files.append(f)

  if len(files):
    return [ output_api.PresubmitError(
        'Objective-C interfaces or categories are forbidden in libbase. ' +
        'See http://groups.google.com/a/chromium.org/group/chromium-dev/' +
        'browse_thread/thread/efb28c10435987fd',
        files) ]
  return []


def _CheckNoTraceEventInclude(input_api, output_api):
  """Verify that //base includes base_tracing.h instead of trace event headers.

  Checks that files outside trace event implementation include the
  base_tracing.h header instead of specific trace event implementation headers
  to maintain compatibility with the gn flag "enable_base_tracing = false".
  """
  discouraged_includes = [
    r'^#include "base/trace_event/(?!base_tracing\.h)',
  ]

  files_to_check = [
    r".*\.(h|cc|mm)$",
  ]
  files_to_skip = [
    r".*[\\/]test[\\/].*",
    r".*[\\/]trace_event[\\/].*",
    r".*[\\/]tracing[\\/].*",
  ]
  no_presubmit = r"// no-presubmit-check"

  def FilterFile(affected_file):
    return input_api.FilterSourceFile(
      affected_file,
      files_to_check=files_to_check,
      files_to_skip=files_to_skip)

  locations = []
  for f in input_api.AffectedSourceFiles(FilterFile):
    for line_num, line in f.ChangedContents():
      for include in discouraged_includes:
        if (input_api.re.search(include, line) and
            not input_api.re.search(no_presubmit, line)):
          locations.append("    %s:%d" % (f.LocalPath(), line_num))
          break

  if locations:
    return [ output_api.PresubmitError(
        'Base code should include "base/trace_event/base_tracing.h" instead\n' +
        'of trace_event implementation headers. If you need to include an\n' +
        'implementation header, verify that base_unittests still passes\n' +
        'with gn arg "enable_base_tracing = false" and add\n' +
        '"// no-presubmit-check" after the include. \n' +
        '\n'.join(locations)) ]
  return []


def _CommonChecks(input_api, output_api):
  """Checks common to both upload and commit."""
  results = []
  results.extend(_CheckNoInterfacesInBase(input_api, output_api))
  results.extend(_CheckNoTraceEventInclude(input_api, output_api))
  return results


def CheckChangeOnUpload(input_api, output_api):
  results = []
  results.extend(_CommonChecks(input_api, output_api))
  return results


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