1 # Copyright (c) 2012 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.
5 """Chromium presubmit script for src/base.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details on the presubmit API built into depot_tools.
15 def _CheckNoInterfacesInBase(input_api, output_api):
16 """Checks to make sure no files in libbase.a have |@interface|."""
17 pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE)
19 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
20 if (f.LocalPath().startswith('base/') and
21 not "/ios/" in f.LocalPath() and
22 not "/test/" in f.LocalPath() and
23 not f.LocalPath().endswith('.java') and
24 not f.LocalPath().endswith('_unittest.mm') and
25 not f.LocalPath().endswith('mac/sdk_forward_declarations.h')):
26 contents = input_api.ReadFile(f)
27 if pattern.search(contents):
31 return [ output_api.PresubmitError(
32 'Objective-C interfaces or categories are forbidden in libbase. ' +
33 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' +
34 'browse_thread/thread/efb28c10435987fd',
39 def _FindLocations(input_api, search_regexes, files_to_check, files_to_skip):
40 """Returns locations matching one of the search_regexes."""
41 def FilterFile(affected_file):
42 return input_api.FilterSourceFile(
44 files_to_check=files_to_check,
45 files_to_skip=files_to_skip)
47 no_presubmit = r"// no-presubmit-check"
49 for f in input_api.AffectedSourceFiles(FilterFile):
50 for line_num, line in f.ChangedContents():
51 for search_regex in search_regexes:
52 if (input_api.re.search(search_regex, line) and
53 not input_api.re.search(no_presubmit, line)):
54 locations.append(" %s:%d" % (f.LocalPath(), line_num))
59 def _CheckNoTraceEventInclude(input_api, output_api):
60 """Verify that //base includes base_tracing.h instead of trace event headers.
62 Checks that files outside trace event implementation include the
63 base_tracing.h header instead of specific trace event implementation headers
64 to maintain compatibility with the gn flag "enable_base_tracing = false".
66 discouraged_includes = [
67 r'^#include "base/trace_event/(?!base_tracing\.h|base_tracing_forward\.h)',
68 r'^#include "third_party/perfetto/include/',
75 r".*[\\/]test[\\/].*",
76 r".*[\\/]trace_event[\\/].*",
77 r".*[\\/]tracing[\\/].*",
80 locations = _FindLocations(input_api, discouraged_includes, files_to_check,
83 return [ output_api.PresubmitError(
84 'Base code should include "base/trace_event/base_tracing.h" instead\n' +
85 'of trace_event implementation headers. If you need to include an\n' +
86 'implementation header, verify that "gn check" and base_unittests\n' +
87 'still pass with gn arg "enable_base_tracing = false" and add\n' +
88 '"// no-presubmit-check" after the include. \n' +
89 '\n'.join(locations)) ]
93 def _WarnPbzeroIncludes(input_api, output_api):
94 """Warn to check enable_base_tracing=false when including a pbzero header.
96 Emits a warning when including a perfetto pbzero header, encouraging the
97 user to verify that //base still builds with enable_base_tracing=false.
100 r'^#include "third_party/perfetto/protos/',
101 r'^#include "base/tracing/protos/',
108 r".*[\\/]test[\\/].*",
109 r".*[\\/]trace_event[\\/].*",
110 r".*[\\/]tracing[\\/].*",
113 locations = _FindLocations(input_api, warn_includes, files_to_check,
116 return [ output_api.PresubmitPromptWarning(
117 'Please verify that "gn check" and base_unittests still pass with\n' +
118 'gn arg "enable_base_tracing = false" when adding typed trace\n' +
119 'events to //base. You can use "#if BUILDFLAG(ENABLE_BASE_TRACING)"\n' +
120 'to exclude pbzero headers and anything not supported by\n' +
121 '//base/trace_event/trace_event_stub.h.\n' +
122 '\n'.join(locations)) ]
126 def _CommonChecks(input_api, output_api):
127 """Checks common to both upload and commit."""
129 results.extend(_CheckNoInterfacesInBase(input_api, output_api))
130 results.extend(_CheckNoTraceEventInclude(input_api, output_api))
131 results.extend(_WarnPbzeroIncludes(input_api, output_api))
135 def CheckChangeOnUpload(input_api, output_api):
137 results.extend(_CommonChecks(input_api, output_api))
141 def CheckChangeOnCommit(input_api, output_api):
143 results.extend(_CommonChecks(input_api, output_api))