Flutter Engine
The Flutter Engine
PRESUBMIT.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
6import os
7import cpplint
8import re
9
10USE_PYTHON3 = True
11
12# memcpy does not handle overlapping memory regions. Even though this
13# is well documented it seems to be used in error quite often. To avoid
14# problems we disallow the direct use of memcpy. The exceptions are in
15# third-party code and in platform/globals.h which uses it to implement
16# bit_cast and bit_copy.
17def CheckMemcpy(filename):
18 if filename.endswith(os.path.join('platform', 'globals.h')) or \
19 filename.find('third_party') != -1:
20 return 0
21 fh = open(filename, 'r')
22 content = fh.read()
23 match = re.search('\\bmemcpy\\b', content)
24 if match:
25 offset = match.start()
26 end_of_line = content.index('\n', offset)
27 # We allow explicit use of memcpy with an opt-in via NOLINT
28 if 'NOLINT' not in content[offset:end_of_line]:
29 line_number = content[0:match.start()].count('\n') + 1
30 print("%s:%d: use of memcpy is forbidden" % (filename, line_number))
31 return 1
32 return 0
33
34
35def RunLint(input_api, output_api):
36 result = []
37 cpplint._cpplint_state.ResetErrorCounts()
38 memcpy_match_count = 0
39 # Find all .cc and .h files in the change list.
40 for git_file in input_api.AffectedTextFiles():
41 filename = git_file.AbsoluteLocalPath()
42 if filename.endswith('.cc') or (
43 # cpplint complains about the style of #ifndefs in our .pbzero.h
44 # files, but they are generated by the protozero compiler, so we
45 # can't fix this.
46 not filename.endswith('.pbzero.h') and filename.endswith('.h')):
47 # Run cpplint on the file.
48 cpplint.ProcessFile(filename, 1)
49 # Check for memcpy use.
50 memcpy_match_count += CheckMemcpy(filename)
51
52 # Report a presubmit error if any of the files had an error.
53 if cpplint._cpplint_state.error_count > 0 or memcpy_match_count > 0:
54 result = [output_api.PresubmitError('Failed cpplint check.')]
55 return result
56
57
58def CheckGn(input_api, output_api):
59 return input_api.canned_checks.CheckGNFormatted(input_api, output_api)
60
61
62def CheckFormatted(input_api, output_api):
63
64 def convert_warning_to_error(presubmit_result):
65 if not presubmit_result.fatal:
66 # Convert this warning to an error.
67 result_json = presubmit_result.json_format()
68 return output_api.PresubmitError(
69 message=result_json['message'],
70 items=result_json['items'],
71 long_text=result_json['long_text'])
72 return presubmit_result
73
74 results = input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
75 return [convert_warning_to_error(r) for r in results]
76
77
78def CheckChangeOnUpload(input_api, output_api):
79 return (RunLint(input_api, output_api) + CheckGn(input_api, output_api) +
80 CheckFormatted(input_api, output_api))
81
82
83def CheckChangeOnCommit(input_api, output_api):
84 return (RunLint(input_api, output_api) + CheckGn(input_api, output_api) +
85 CheckFormatted(input_api, output_api))
int count
Definition: FontMgrTest.cpp:50
def CheckChangeOnCommit(input_api, output_api)
Definition: PRESUBMIT.py:66
def CheckChangeOnUpload(input_api, output_api)
Definition: PRESUBMIT.py:70
def CheckMemcpy(filename)
Definition: PRESUBMIT.py:17
def CheckGn(input_api, output_api)
Definition: PRESUBMIT.py:58
def CheckFormatted(input_api, output_api)
Definition: PRESUBMIT.py:62
def RunLint(input_api, output_api)
Definition: PRESUBMIT.py:35
def print(*args, **kwargs)
Definition: run_tests.py:49