blob: 0715149bf9bf319741a168506c46677331dde02f [file] [log] [blame]
#!/usr/bin/env vpython
# Copyright 2017 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.
"""Utilities for checking for disallowed usage of non-Blink declarations.
The scanner assumes that usage of non-Blink code is always namespace qualified.
Identifiers in the global namespace are always ignored. For convenience, the
script can be run in standalone mode to check for existing violations.
Example command:
$ git ls-files third_party/blink \
| python third_party/blink/tools/
import os
import re
import sys
'paths': ['third_party/blink/renderer/'],
'allowed': [
# TODO(dcheng): Should these be in a more specific config?
# //base constructs that are allowed everywhere
# //base/bind_helpers.h.
# //base/callback.h is allowed, but you need to use WTF::Bind or
# WTF::BindRepeating to create callbacks in Blink.
# //base/memory/ptr_util.h.
# //base/allocator/partition_allocator/oom_callback.h.
# //base/metrics/histogram_functions.h
# //base/metrics/field_trial_params.h.
# //base/numerics/safe_conversions.h.
# //base/strings/char_traits.h.
# //base/synchronization/waitable_event.h.
# //base/numerics/checked_math.h.
# Debugging helpers from //base/debug are allowed everywhere.
# Base atomic utilities
# Task traits
# Byte order
# (Cryptographic) random number generation
# Feature list checking.
# PartitionAlloc
# For MessageLoop::TaskObserver.
# cc painting types.
# Chromium geometry types.
# Wrapper of SkRegion used in Chromium.
# A geometric set of TouchActions associated with areas, and only
# depends on the geometry types above.
# Selection bounds.
# cc::Layers.
# cc::Layer helper data structs.
# cc::Layer helper enums.
# Scrolling
# Standalone utility libraries that only depend on //base
# Nested namespaces under the blink namespace
# Third-party libraries that don't depend on non-Blink Chrome code
# are OK.
'testing::.+', # googlemock / googletest
# Inspector instrumentation and protocol
# Blink code shouldn't need to be qualified with the Blink namespace,
# but there are exceptions.
# Assume that identifiers where the first qualifier is internal are
# nested in the blink namespace.
# Network service.
# Some test helpers live in the blink::test namespace.
# Blink uses Mojo, so it needs mojo::Binding, mojo::InterfacePtr, et
# cetera, as well as generated Mojo bindings.
# Note that the Mojo callback helpers are explicitly forbidden:
# Blink already has a signal for contexts being destroyed, and
# other types of failures should be explicitly signalled.
# TODO(dcheng): Remove this once Connector isn't needed in Blink
# anymore.
# STL containers such as std::string and std::vector are discouraged
# but still needed for interop with WebKit/common. Note that other
# STL types such as std::unique_ptr are encouraged.
# Blink uses UKM for logging e.g. always-on leak detection (crbug/757374)
'disallowed': [
'Use WTF::Bind or WTF::BindRepeating.'),
'paths': ['third_party/blink/renderer/bindings/'],
'allowed': ['gin::.+'],
'paths': ['third_party/blink/renderer/bindings/core/v8/'],
'allowed': [
# For memory reduction histogram.
'paths': ['third_party/blink/renderer/core/animation'],
'allowed': [
'paths': ['third_party/blink/renderer/core/clipboard'],
'allowed': ['gfx::PNGCodec', 'net::EscapeForHTML'],
'paths': ['third_party/blink/renderer/core/css'],
'allowed': [
# Internal implementation details for CSS.
'paths': ['third_party/blink/renderer/core/css/'],
'allowed': [
'paths': ['third_party/blink/renderer/core/fetch/'],
'allowed': [
# The existing code already contains gin::IsolateHolder.
'paths': ['third_party/blink/renderer/core/fileapi/'],
'allowed': [
'paths': ['third_party/blink/renderer/core/paint'],
'allowed': [
# cc painting types.
'paths': ['third_party/blink/renderer/core/page/scrolling'],
'allowed': [
# cc scrollbar layer types.
'paths': ['third_party/blink/renderer/core/page'],
'allowed': [
'paths': ['third_party/blink/renderer/core/style/computed_style.h'],
'allowed': [
'paths': ['third_party/blink/renderer/core/inspector/'],
'allowed': [
'paths': ['third_party/blink/renderer/core/inspector/'],
'allowed': [
# cc painting types.
'paths': ['third_party/blink/renderer/core/inspector/'],
'allowed': [
'paths': [
'allowed': [
'paths': [
# The modules listed above need access to the following GL drawing and
# display-related types.
'allowed': [
'paths': [
'allowed': [
'paths': [
# The WebGPU Blink module needs access to the WebGPU control
# command buffer interface.
'allowed': [
'paths': [
# Suppress almost all checks on platform since code in this directory
# is meant to be a bridge between Blink and non-Blink code. However,
# base::RefCounted should still be explicitly blocked, since
# WTF::RefCounted should be used instead.
'allowed': ['(?!base::RefCounted).+'],
'paths': [
# base::RefCounted is prohibited in platform/ as defined above, but
# SingleThreadIdleTaskRunner needs to be constructed before WTF and
# PartitionAlloc are initialized, which forces us to use
# base::RefCountedThreadSafe for it.
'allowed': ['.+'],
'paths': [
'allowed': [
'paths': [
'allowed': [
'paths': [
'allowed': ['audio_utilities::.+'],
'paths': [
'allowed': ['sql::.+'],
'paths': [
'allowed': ['ui::NativeTheme.*'],
'paths': [
'allowed': ['list_marker_text::.+'],
'paths': [
'allowed': ['crypto::.+'],
'paths': [
'allowed': [
'paths': [
# The code in adapters/ wraps WebRTC APIs using STL/WebRTC types only.
# Thus, the restriction that objects should only be created and
# destroyed on the same thread can be relaxed since no Blink types (like
# AtomicString or HeapVector) are used cross thread. These Blink types
# are converted to the STL/WebRTC counterparts in the parent directory.
'allowed': [
def _precompile_config():
"""Turns the raw config into a config of compiled regex."""
match_nothing_re = re.compile('.^')
def compile_regexp(match_list):
"""Turns a match list into a compiled regexp.
If match_list is None, a regexp that matches nothing is returned.
if match_list:
return re.compile('(?:%s)$' % '|'.join(match_list))
return match_nothing_re
def compile_disallowed(disallowed_list):
"""Transforms the disallowed list to one with the regexps compiled."""
if not disallowed_list:
return match_nothing_re, []
match_list = []
advice_list = []
for entry in disallowed_list:
if isinstance(entry, tuple):
match, advice = entry
advice_list.append((compile_regexp(match), advice))
# Just a string
return compile_regexp(match_list), advice_list
compiled_config = []
for raw_entry in _CONFIG:
disallowed, advice = compile_disallowed(raw_entry.get('disallowed'))
'paths': raw_entry['paths'],
'allowed': compile_regexp(raw_entry.get('allowed')),
'disallowed': disallowed,
'advice': advice,
return compiled_config
_COMPILED_CONFIG = _precompile_config()
# Attempt to match identifiers qualified with a namespace. Since parsing C++ in
# Python is hard, this regex assumes that namespace names only contain lowercase
# letters, numbers, and underscores, matching the Google C++ style guide. This
# is intended to minimize the number of matches where :: is used to qualify a
# name with a class or enum name.
# As a bit of a minor hack, this regex also hardcodes a check for GURL, since
# GURL isn't namespace qualified and wouldn't match otherwise.
def _find_matching_entries(path):
"""Finds entries that should be used for path.
A list of entries, sorted in order of relevance. Each entry is a
dictionary with keys:
allowed: A regexp for identifiers that should be allowed.
disallowed: A regexp for identifiers that should not be allowed.
advice: (optional) A regexp for identifiers along with advice
entries = []
for entry in _COMPILED_CONFIG:
for entry_path in entry['paths']:
if path.startswith(entry_path):
entries.append({'sortkey': len(entry_path), 'entry': entry})
# The path length is used as the sort key: a longer path implies more
# relevant, since that config is a more exact match.
entries.sort(key=lambda x: x['sortkey'], reverse=True)
return [entry['entry'] for entry in entries]
def _check_entries_for_identifier(entries, identifier):
"""Check if an identifier is allowed"""
for entry in entries:
if entry['allowed'].match(identifier):
return True
if entry['disallowed'].match(identifier):
return False
# Disallow by default.
return False
def _find_advice_for_identifier(entries, identifier):
advice_list = []
for entry in entries:
for matcher, advice in entry.get('advice', []):
if matcher.match(identifier):
return advice_list
class BadIdentifier(object):
"""Represents a single instance of a bad identifier."""
def __init__(self, identifier, line, advice=None):
self.identifier = identifier
self.line = line
self.advice = advice
def check(path, contents):
"""Checks for disallowed usage of non-Blink classes, functions, et cetera.
path: The path of the file to check.
contents: An array of line number, line tuples to check.
A list of (line number, disallowed identifier, advice) tuples.
results = []
# Because Windows.
path = path.replace('\\', '/')
basename, ext = os.path.splitext(path)
# Only check code. Ignore tests.
# TODO(tkent): Remove 'Test' after the great mv.
if (ext not in ('.cc', '.cpp', '.h', '.mm')
or path.find('/testing/') >= 0
or path.find('/tests/') >= 0
or basename.endswith('Test')
or basename.endswith('_test')
or basename.endswith('_test_helpers')
or basename.endswith('_unittest')):
return results
entries = _find_matching_entries(path)
if not entries:
for line_number, line in contents:
idx = line.find('//')
if idx >= 0:
line = line[:idx]
match =
if match:
identifier =
if not _check_entries_for_identifier(entries, identifier):
advice = _find_advice_for_identifier(entries, identifier)
BadIdentifier(identifier, line_number, advice))
return results
def main():
for path in
with open(path, 'r') as f:
contents =
disallowed_identifiers = check(path, [
(i + 1, l) for i, l in
if disallowed_identifiers:
print '%s uses disallowed identifiers:' % path
for i in disallowed_identifiers:
print (i.line, i.identifier, i.advice)
except IOError as e:
print 'could not open %s: %s' % (path, e)
if __name__ == '__main__':