blob: 0f936979877702f33bce811d091149f03586f88f [file] [log] [blame]
Nico Weber077f1a32015-08-06 15:08:57 -07001# Copyright 2015 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.
4
5"""Presubmit script for pdfium.
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details about the presubmit API built into depot_tools.
9"""
10
Dan Sinclair22d66072016-02-22 11:56:05 -050011LINT_FILTERS = [
Dan Sinclair3ebd1212016-03-09 09:59:23 -050012 # Rvalue ref checks are unreliable.
dan sinclaird2019df2016-02-22 22:32:03 -050013 '-build/c++11',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050014 # Need to fix header names not matching cpp names.
dan sinclaird2019df2016-02-22 22:32:03 -050015 '-build/include',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050016 # Need to fix header names not matching cpp names.
dan sinclaird2019df2016-02-22 22:32:03 -050017 '-build/include_order',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050018 # Too many to fix at the moment.
dan sinclaird2019df2016-02-22 22:32:03 -050019 '-readability/casting',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050020 # Need to refactor large methods to fix.
dan sinclaird2019df2016-02-22 22:32:03 -050021 '-readability/fn_size',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050022 # Need to fix errors when making methods explicit.
dan sinclaird2019df2016-02-22 22:32:03 -050023 '-runtime/explicit',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050024 # Lots of usage to fix first.
dan sinclaird2019df2016-02-22 22:32:03 -050025 '-runtime/int',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050026 # Need to fix two snprintf TODOs
dan sinclaird2019df2016-02-22 22:32:03 -050027 '-runtime/printf',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050028 # Lots of non-const references need to be fixed
dan sinclaird2019df2016-02-22 22:32:03 -050029 '-runtime/references',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050030 # We are not thread safe, so this will never pass.
dan sinclaird2019df2016-02-22 22:32:03 -050031 '-runtime/threadsafe_fn',
Dan Sinclair3ebd1212016-03-09 09:59:23 -050032 # Figure out how to deal with #defines that git cl format creates.
dan sinclaird2019df2016-02-22 22:32:03 -050033 '-whitespace/indent',
Dan Sinclair22d66072016-02-22 11:56:05 -050034]
35
Dan Sinclair544bbc62016-03-14 15:07:39 -040036
dsinclair2ca2da52016-09-13 18:10:34 -070037_INCLUDE_ORDER_WARNING = (
38 'Your #include order seems to be broken. Remember to use the right '
39 'collation (LC_COLLATE=C) and check\nhttps://google.github.io/styleguide/'
40 'cppguide.html#Names_and_Order_of_Includes')
41
42
Dan Sinclair544bbc62016-03-14 15:07:39 -040043def _CheckUnwantedDependencies(input_api, output_api):
44 """Runs checkdeps on #include statements added in this
45 change. Breaking - rules is an error, breaking ! rules is a
46 warning.
47 """
48 import sys
49 # We need to wait until we have an input_api object and use this
50 # roundabout construct to import checkdeps because this file is
51 # eval-ed and thus doesn't have __file__.
52 original_sys_path = sys.path
53 try:
54 sys.path = sys.path + [input_api.os_path.join(
55 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
56 import checkdeps
57 from cpp_checker import CppChecker
58 from rules import Rule
dsinclair4cd49e12016-04-05 10:28:48 -070059 except ImportError:
60 return [output_api.PresubmitError(
61 'Unable to run checkdeps, does pdfium/buildtools/checkdeps exist?')]
Dan Sinclair544bbc62016-03-14 15:07:39 -040062 finally:
63 # Restore sys.path to what it was before.
64 sys.path = original_sys_path
65
66 added_includes = []
67 for f in input_api.AffectedFiles():
68 if not CppChecker.IsCppFile(f.LocalPath()):
69 continue
70
71 changed_lines = [line for line_num, line in f.ChangedContents()]
72 added_includes.append([f.LocalPath(), changed_lines])
73
74 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
75
76 error_descriptions = []
77 warning_descriptions = []
78 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
79 added_includes):
80 description_with_path = '%s\n %s' % (path, rule_description)
81 if rule_type == Rule.DISALLOW:
82 error_descriptions.append(description_with_path)
83 else:
84 warning_descriptions.append(description_with_path)
85
86 results = []
87 if error_descriptions:
88 results.append(output_api.PresubmitError(
89 'You added one or more #includes that violate checkdeps rules.',
90 error_descriptions))
91 if warning_descriptions:
92 results.append(output_api.PresubmitPromptOrNotify(
93 'You added one or more #includes of files that are temporarily\n'
94 'allowed but being removed. Can you avoid introducing the\n'
95 '#include? See relevant DEPS file(s) for details and contacts.',
96 warning_descriptions))
97 return results
98
99
dsinclair2ca2da52016-09-13 18:10:34 -0700100def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
101 """Checks that the lines in scope occur in the right order.
102
103 1. C system files in alphabetical order
104 2. C++ system files in alphabetical order
105 3. Project's .h files
106 """
107
108 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
109 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
110 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
111
112 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
113
114 state = C_SYSTEM_INCLUDES
115
116 previous_line = ''
117 previous_line_num = 0
118 problem_linenums = []
119 out_of_order = " - line belongs before previous line"
120 for line_num, line in scope:
121 if c_system_include_pattern.match(line):
122 if state != C_SYSTEM_INCLUDES:
123 problem_linenums.append((line_num, previous_line_num,
124 " - C system include file in wrong block"))
125 elif previous_line and previous_line > line:
126 problem_linenums.append((line_num, previous_line_num,
127 out_of_order))
128 elif cpp_system_include_pattern.match(line):
129 if state == C_SYSTEM_INCLUDES:
130 state = CPP_SYSTEM_INCLUDES
131 elif state == CUSTOM_INCLUDES:
132 problem_linenums.append((line_num, previous_line_num,
133 " - c++ system include file in wrong block"))
134 elif previous_line and previous_line > line:
135 problem_linenums.append((line_num, previous_line_num, out_of_order))
136 elif custom_include_pattern.match(line):
137 if state != CUSTOM_INCLUDES:
138 state = CUSTOM_INCLUDES
139 elif previous_line and previous_line > line:
140 problem_linenums.append((line_num, previous_line_num, out_of_order))
141 else:
142 problem_linenums.append((line_num, previous_line_num,
143 "Unknown include type"))
144 previous_line = line
145 previous_line_num = line_num
146
147 warnings = []
148 for (line_num, previous_line_num, failure_type) in problem_linenums:
149 if line_num in changed_linenums or previous_line_num in changed_linenums:
150 warnings.append(' %s:%d:%s' % (file_path, line_num, failure_type))
151 return warnings
152
153
154def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
155 """Checks the #include order for the given file f."""
156
157 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
158 # Exclude the following includes from the check:
159 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
160 # specific order.
161 # 2) <atlbase.h>, "build/build_config.h"
162 excluded_include_pattern = input_api.re.compile(
163 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
164 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
165 # Match the final or penultimate token if it is xxxtest so we can ignore it
166 # when considering the special first include.
167 test_file_tag_pattern = input_api.re.compile(
168 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
169 if_pattern = input_api.re.compile(
170 r'\s*#\s*(if|elif|else|endif|define|undef).*')
171 # Some files need specialized order of includes; exclude such files from this
172 # check.
173 uncheckable_includes_pattern = input_api.re.compile(
174 r'\s*#include '
175 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
176
177 contents = f.NewContents()
178 warnings = []
179 line_num = 0
180
181 # Handle the special first include. If the first include file is
182 # some/path/file.h, the corresponding including file can be some/path/file.cc,
183 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
184 # etc. It's also possible that no special first include exists.
185 # If the included file is some/path/file_platform.h the including file could
186 # also be some/path/file_xxxtest_platform.h.
187 including_file_base_name = test_file_tag_pattern.sub(
188 '', input_api.os_path.basename(f.LocalPath()))
189
190 for line in contents:
191 line_num += 1
192 if system_include_pattern.match(line):
193 # No special first include -> process the line again along with normal
194 # includes.
195 line_num -= 1
196 break
197 match = custom_include_pattern.match(line)
198 if match:
199 match_dict = match.groupdict()
200 header_basename = test_file_tag_pattern.sub(
201 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
202
203 if header_basename not in including_file_base_name:
204 # No special first include -> process the line again along with normal
205 # includes.
206 line_num -= 1
207 break
208
209 # Split into scopes: Each region between #if and #endif is its own scope.
210 scopes = []
211 current_scope = []
212 for line in contents[line_num:]:
213 line_num += 1
214 if uncheckable_includes_pattern.match(line):
215 continue
216 if if_pattern.match(line):
217 scopes.append(current_scope)
218 current_scope = []
219 elif ((system_include_pattern.match(line) or
220 custom_include_pattern.match(line)) and
221 not excluded_include_pattern.match(line)):
222 current_scope.append((line_num, line))
223 scopes.append(current_scope)
224
225 for scope in scopes:
226 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
227 changed_linenums))
228 return warnings
229
230
231def _CheckIncludeOrder(input_api, output_api):
232 """Checks that the #include order is correct.
233
234 1. The corresponding header for source files.
235 2. C system files in alphabetical order
236 3. C++ system files in alphabetical order
237 4. Project's .h files in alphabetical order
238
239 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
240 these rules separately.
241 """
242 def FileFilterIncludeOrder(affected_file):
243 black_list = (input_api.DEFAULT_BLACK_LIST)
244 return input_api.FilterSourceFile(affected_file, black_list=black_list)
245
246 warnings = []
247 for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder):
248 if f.LocalPath().endswith(('.cc', '.h', '.mm')):
249 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
250 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
251
252 results = []
253 if warnings:
254 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
255 warnings))
256 return results
257
258
Nico Weber077f1a32015-08-06 15:08:57 -0700259def CheckChangeOnUpload(input_api, output_api):
260 results = []
Dan Sinclair544bbc62016-03-14 15:07:39 -0400261 results += _CheckUnwantedDependencies(input_api, output_api)
Nico Weber077f1a32015-08-06 15:08:57 -0700262 results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
Dan Sinclair22d66072016-02-22 11:56:05 -0500263 results += input_api.canned_checks.CheckChangeLintsClean(
264 input_api, output_api, None, LINT_FILTERS)
dsinclair2ca2da52016-09-13 18:10:34 -0700265 results += _CheckIncludeOrder(input_api, output_api)
Dan Sinclair544bbc62016-03-14 15:07:39 -0400266
Nico Weber077f1a32015-08-06 15:08:57 -0700267 return results