2011-10-05 05:20:07 +00:00
|
|
|
# Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
|
|
|
# for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
# BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
import os
|
2012-01-20 00:08:49 +00:00
|
|
|
import cpplint
|
2013-09-20 12:25:08 +00:00
|
|
|
import re
|
2016-11-30 22:52:52 +00:00
|
|
|
import StringIO
|
2012-01-20 00:08:49 +00:00
|
|
|
|
2016-10-26 07:26:03 +00:00
|
|
|
# memcpy does not handle overlapping memory regions. Even though this
|
|
|
|
# is well documented it seems to be used in error quite often. To avoid
|
|
|
|
# problems we disallow the direct use of memcpy. The exceptions are in
|
|
|
|
# third-party code and in platform/globals.h which uses it to implement
|
|
|
|
# bit_cast and bit_copy.
|
|
|
|
def CheckMemcpy(filename):
|
|
|
|
if filename.endswith(os.path.join('platform', 'globals.h')) or \
|
|
|
|
filename.find('third_party') != -1:
|
|
|
|
return 0
|
|
|
|
fh = open(filename, 'r')
|
|
|
|
content = fh.read()
|
|
|
|
match = re.search('\\bmemcpy\\b', content)
|
|
|
|
if match:
|
|
|
|
line_number = content[0:match.start()].count('\n') + 1
|
|
|
|
print "%s:%d: use of memcpy is forbidden" % (filename, line_number)
|
|
|
|
return 1
|
|
|
|
return 0
|
2012-01-20 00:08:49 +00:00
|
|
|
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
def RunLint(input_api, output_api):
|
|
|
|
result = []
|
|
|
|
cpplint._cpplint_state.ResetErrorCounts()
|
2013-09-20 12:25:08 +00:00
|
|
|
memcpy_match_count = 0
|
2011-10-05 05:20:07 +00:00
|
|
|
# Find all .cc and .h files in the change list.
|
2016-10-26 07:26:03 +00:00
|
|
|
for git_file in input_api.AffectedTextFiles():
|
|
|
|
filename = git_file.AbsoluteLocalPath()
|
2011-10-05 05:20:07 +00:00
|
|
|
if filename.endswith('.cc') or filename.endswith('.h'):
|
|
|
|
# Run cpplint on the file.
|
|
|
|
cpplint.ProcessFile(filename, 1)
|
2016-10-26 07:26:03 +00:00
|
|
|
# Check for memcpy use.
|
|
|
|
memcpy_match_count += CheckMemcpy(filename)
|
2013-09-20 12:25:08 +00:00
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
# Report a presubmit error if any of the files had an error.
|
2013-09-20 12:25:08 +00:00
|
|
|
if cpplint._cpplint_state.error_count > 0 or memcpy_match_count > 0:
|
2011-10-05 05:20:07 +00:00
|
|
|
result = [output_api.PresubmitError('Failed cpplint check.')]
|
|
|
|
return result
|
|
|
|
|
2012-01-20 00:08:49 +00:00
|
|
|
|
2016-10-26 15:50:54 +00:00
|
|
|
def CheckGn(input_api, output_api):
|
|
|
|
return input_api.canned_checks.CheckGNFormatted(input_api, output_api)
|
|
|
|
|
|
|
|
|
2016-11-09 20:43:57 +00:00
|
|
|
def CheckFormatted(input_api, output_api):
|
2016-11-30 22:52:52 +00:00
|
|
|
def convert_warning_to_error(presubmit_result):
|
|
|
|
if not presubmit_result.fatal:
|
|
|
|
# Convert this warning to an error.
|
|
|
|
stream = StringIO.StringIO()
|
|
|
|
presubmit_result.handle(stream)
|
|
|
|
message = stream.getvalue()
|
|
|
|
return output_api.PresubmitError(message)
|
|
|
|
return presubmit_result
|
|
|
|
|
|
|
|
results = input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
|
|
|
|
return [convert_warning_to_error(r) for r in results]
|
2016-11-09 20:43:57 +00:00
|
|
|
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
def CheckChangeOnUpload(input_api, output_api):
|
2016-10-26 15:50:54 +00:00
|
|
|
return (RunLint(input_api, output_api) +
|
2016-11-09 20:43:57 +00:00
|
|
|
CheckGn(input_api, output_api) +
|
|
|
|
CheckFormatted(input_api, output_api))
|
2011-10-05 05:20:07 +00:00
|
|
|
|
2012-01-20 00:08:49 +00:00
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
def CheckChangeOnCommit(input_api, output_api):
|
2016-10-26 15:50:54 +00:00
|
|
|
return (RunLint(input_api, output_api) +
|
2016-11-09 20:43:57 +00:00
|
|
|
CheckGn(input_api, output_api) +
|
|
|
|
CheckFormatted(input_api, output_api))
|