Commit 55f9f381 authored by joi@chromium.org's avatar joi@chromium.org

Add checkdeps presubmit check. Warns on new #includes of dependencies

we are working to eliminate, and errors on new #includes of disallowed
dependencies.

BUG=138280

Review URL: https://chromiumcodereview.appspot.com/10806049

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149167 0039d316-1c4b-4281-b951-d872f2087c98
parent e68504da
...@@ -10,6 +10,7 @@ for more details about the presubmit API built into gcl. ...@@ -10,6 +10,7 @@ for more details about the presubmit API built into gcl.
import re import re
import sys
_EXCLUDED_PATHS = ( _EXCLUDED_PATHS = (
...@@ -374,6 +375,59 @@ def _CheckNoPragmaOnce(input_api, output_api): ...@@ -374,6 +375,59 @@ def _CheckNoPragmaOnce(input_api, output_api):
return [] return []
def _CheckUnwantedDependencies(input_api, output_api):
"""Runs checkdeps on #include statements added in this
change. Breaking - rules is an error, breaking ! rules is a
warning.
"""
# We need to wait until we have an input_api object and use this
# roundabout construct to import checkdeps because this file is
# eval-ed and thus doesn't have __file__.
original_sys_path = sys.path
try:
sys.path = sys.path + [input_api.os_path.join(
input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
import checkdeps
from cpp_checker import CppChecker
from rules import Rule
finally:
# Restore sys.path to what it was before.
sys.path = original_sys_path
added_includes = []
for f in input_api.AffectedFiles():
if not CppChecker.IsCppFile(f.LocalPath()):
continue
changed_lines = [line for line_num, line in f.ChangedContents()]
added_includes.append([f.LocalPath(), changed_lines])
deps_checker = checkdeps.DepsChecker()
error_descriptions = []
warning_descriptions = []
for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
added_includes):
description_with_path = '%s\n %s' % (path, rule_description)
if rule_type == Rule.DISALLOW:
error_descriptions.append(description_with_path)
else:
warning_descriptions.append(description_with_path)
results = []
if error_descriptions:
results.append(output_api.PresubmitError(
'You added one or more #includes that violate checkdeps rules.',
error_descriptions))
if warning_descriptions:
results.append(output_api.PresubmitPromptWarning(
'You added one or more #includes of files that are temporarily\n'
'allowed but being removed. Can you avoid introducing the\n'
'#include? See relevant DEPS file(s) for details and contacts.',
warning_descriptions))
return results
def _CommonChecks(input_api, output_api): def _CommonChecks(input_api, output_api):
"""Checks common to both upload and commit.""" """Checks common to both upload and commit."""
results = [] results = []
...@@ -388,6 +442,7 @@ def _CommonChecks(input_api, output_api): ...@@ -388,6 +442,7 @@ def _CommonChecks(input_api, output_api):
results.extend(_CheckNoDEPSGIT(input_api, output_api)) results.extend(_CheckNoDEPSGIT(input_api, output_api))
results.extend(_CheckNoBannedFunctions(input_api, output_api)) results.extend(_CheckNoBannedFunctions(input_api, output_api))
results.extend(_CheckNoPragmaOnce(input_api, output_api)) results.extend(_CheckNoPragmaOnce(input_api, output_api))
results.extend(_CheckUnwantedDependencies(input_api, output_api))
return results return results
......
...@@ -340,7 +340,7 @@ class DepsChecker(object): ...@@ -340,7 +340,7 @@ class DepsChecker(object):
problems = [] problems = []
for file_path, include_lines in added_includes: for file_path, include_lines in added_includes:
# TODO(joi): Make this cover Java as well. # TODO(joi): Make this cover Java as well.
if not os.path.splitext(file_path)[1] in cpp.EXTENSIONS: if not cpp.IsCppFile(file_path):
pass pass
rules_for_file = GetDirectoryRules(os.path.dirname(file_path)) rules_for_file = GetDirectoryRules(os.path.dirname(file_path))
if rules_for_file: if rules_for_file:
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
"""Checks C++ and Objective-C files for illegal includes.""" """Checks C++ and Objective-C files for illegal includes."""
import codecs import codecs
import os
import re import re
from rules import Rule from rules import Rule
...@@ -113,3 +114,10 @@ class CppChecker(object): ...@@ -113,3 +114,10 @@ class CppChecker(object):
ret_val += line_status ret_val += line_status
return ret_val return ret_val
@staticmethod
def IsCppFile(file_path):
"""Returns True iff the given path ends in one of the extensions
handled by this checker.
"""
return os.path.splitext(file_path)[1] in CppChecker.EXTENSIONS
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment