Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: PRESUBMIT.py

Issue 2957353002: Stop checking include order in PRESUBMIT.py (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | PRESUBMIT_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Top-level presubmit script for Chromium. 5 """Top-level presubmit script for Chromium.
6 6
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into depot_tools. 8 for more details about the presubmit API built into depot_tools.
9 """ 9 """
10 10
(...skipping 874 matching lines...) Expand 10 before | Expand all | Expand 10 after
885 if pattern.match(line): 885 if pattern.match(line):
886 errors.append(' %s:%d' % (f.LocalPath(), line_num)) 886 errors.append(' %s:%d' % (f.LocalPath(), line_num))
887 887
888 results = [] 888 results = []
889 if errors: 889 if errors:
890 results.append(output_api.PresubmitError( 890 results.append(output_api.PresubmitError(
891 'Header files should not include ui/aura/window_property.h', errors)) 891 'Header files should not include ui/aura/window_property.h', errors))
892 return results 892 return results
893 893
894 894
895 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
896 """Checks that the lines in scope occur in the right order.
897
898 1. C system files in alphabetical order
899 2. C++ system files in alphabetical order
900 3. Project's .h files
901 """
902
903 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
904 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
905 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
906
907 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
908
909 state = C_SYSTEM_INCLUDES
910
911 previous_line = ''
912 previous_line_num = 0
913 problem_linenums = []
914 out_of_order = " - line belongs before previous line"
915 for line_num, line in scope:
916 if c_system_include_pattern.match(line):
917 if state != C_SYSTEM_INCLUDES:
918 problem_linenums.append((line_num, previous_line_num,
919 " - C system include file in wrong block"))
920 elif previous_line and previous_line > line:
921 problem_linenums.append((line_num, previous_line_num,
922 out_of_order))
923 elif cpp_system_include_pattern.match(line):
924 if state == C_SYSTEM_INCLUDES:
925 state = CPP_SYSTEM_INCLUDES
926 elif state == CUSTOM_INCLUDES:
927 problem_linenums.append((line_num, previous_line_num,
928 " - c++ system include file in wrong block"))
929 elif previous_line and previous_line > line:
930 problem_linenums.append((line_num, previous_line_num, out_of_order))
931 elif custom_include_pattern.match(line):
932 if state != CUSTOM_INCLUDES:
933 state = CUSTOM_INCLUDES
934 elif previous_line and previous_line > line:
935 problem_linenums.append((line_num, previous_line_num, out_of_order))
936 else:
937 problem_linenums.append((line_num, previous_line_num,
938 "Unknown include type"))
939 previous_line = line
940 previous_line_num = line_num
941
942 warnings = []
943 for (line_num, previous_line_num, failure_type) in problem_linenums:
944 if line_num in changed_linenums or previous_line_num in changed_linenums:
945 warnings.append(' %s:%d:%s' % (file_path, line_num, failure_type))
946 return warnings
947
948
949 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
950 """Checks the #include order for the given file f."""
951
952 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
953 # Exclude the following includes from the check:
954 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
955 # specific order.
956 # 2) <atlbase.h>, "build/build_config.h"
957 excluded_include_pattern = input_api.re.compile(
958 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
959 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
960 # Match the final or penultimate token if it is xxxtest so we can ignore it
961 # when considering the special first include.
962 test_file_tag_pattern = input_api.re.compile(
963 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
964 if_pattern = input_api.re.compile(
965 r'\s*#\s*(if|elif|else|endif|define|undef).*')
966 # Some files need specialized order of includes; exclude such files from this
967 # check.
968 uncheckable_includes_pattern = input_api.re.compile(
969 r'\s*#include '
970 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
971
972 contents = f.NewContents()
973 warnings = []
974 line_num = 0
975
976 # Handle the special first include. If the first include file is
977 # some/path/file.h, the corresponding including file can be some/path/file.cc,
978 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
979 # etc. It's also possible that no special first include exists.
980 # If the included file is some/path/file_platform.h the including file could
981 # also be some/path/file_xxxtest_platform.h.
982 including_file_base_name = test_file_tag_pattern.sub(
983 '', input_api.os_path.basename(f.LocalPath()))
984
985 for line in contents:
986 line_num += 1
987 if system_include_pattern.match(line):
988 # No special first include -> process the line again along with normal
989 # includes.
990 line_num -= 1
991 break
992 match = custom_include_pattern.match(line)
993 if match:
994 match_dict = match.groupdict()
995 header_basename = test_file_tag_pattern.sub(
996 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
997
998 if header_basename not in including_file_base_name:
999 # No special first include -> process the line again along with normal
1000 # includes.
1001 line_num -= 1
1002 break
1003
1004 # Split into scopes: Each region between #if and #endif is its own scope.
1005 scopes = []
1006 current_scope = []
1007 for line in contents[line_num:]:
1008 line_num += 1
1009 if uncheckable_includes_pattern.match(line):
1010 continue
1011 if if_pattern.match(line):
1012 scopes.append(current_scope)
1013 current_scope = []
1014 elif ((system_include_pattern.match(line) or
1015 custom_include_pattern.match(line)) and
1016 not excluded_include_pattern.match(line)):
1017 current_scope.append((line_num, line))
1018 scopes.append(current_scope)
1019
1020 for scope in scopes:
1021 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
1022 changed_linenums))
1023 return warnings
1024
1025
1026 def _CheckIncludeOrder(input_api, output_api):
1027 """Checks that the #include order is correct.
1028
1029 1. The corresponding header for source files.
1030 2. C system files in alphabetical order
1031 3. C++ system files in alphabetical order
1032 4. Project's .h files in alphabetical order
1033
1034 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
1035 these rules separately.
1036 """
1037 def FileFilterIncludeOrder(affected_file):
1038 black_list = (_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
1039 return input_api.FilterSourceFile(affected_file, black_list=black_list)
1040
1041 warnings = []
1042 for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder):
1043 if f.LocalPath().endswith(('.cc', '.h', '.mm')):
1044 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
1045 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
1046
1047 results = []
1048 if warnings:
1049 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
1050 warnings))
1051 return results
1052
1053
1054 def _CheckForVersionControlConflictsInFile(input_api, f): 895 def _CheckForVersionControlConflictsInFile(input_api, f):
1055 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') 896 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
1056 errors = [] 897 errors = []
1057 for line_num, line in f.ChangedContents(): 898 for line_num, line in f.ChangedContents():
1058 if f.LocalPath().endswith('.md'): 899 if f.LocalPath().endswith('.md'):
1059 # First-level headers in markdown look a lot like version control 900 # First-level headers in markdown look a lot like version control
1060 # conflict markers. http://daringfireball.net/projects/markdown/basics 901 # conflict markers. http://daringfireball.net/projects/markdown/basics
1061 continue 902 continue
1062 if pattern.match(line): 903 if pattern.match(line):
1063 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line)) 904 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
(...skipping 1191 matching lines...) Expand 10 before | Expand all | Expand 10 after
2255 results.extend(_CheckDCHECK_IS_ONHasBraces(input_api, output_api)) 2096 results.extend(_CheckDCHECK_IS_ONHasBraces(input_api, output_api))
2256 results.extend(_CheckNoNewWStrings(input_api, output_api)) 2097 results.extend(_CheckNoNewWStrings(input_api, output_api))
2257 results.extend(_CheckNoDEPSGIT(input_api, output_api)) 2098 results.extend(_CheckNoDEPSGIT(input_api, output_api))
2258 results.extend(_CheckNoBannedFunctions(input_api, output_api)) 2099 results.extend(_CheckNoBannedFunctions(input_api, output_api))
2259 results.extend(_CheckNoPragmaOnce(input_api, output_api)) 2100 results.extend(_CheckNoPragmaOnce(input_api, output_api))
2260 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api)) 2101 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
2261 results.extend(_CheckUnwantedDependencies(input_api, output_api)) 2102 results.extend(_CheckUnwantedDependencies(input_api, output_api))
2262 results.extend(_CheckFilePermissions(input_api, output_api)) 2103 results.extend(_CheckFilePermissions(input_api, output_api))
2263 results.extend(_CheckTeamTags(input_api, output_api)) 2104 results.extend(_CheckTeamTags(input_api, output_api))
2264 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api)) 2105 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
2265 results.extend(_CheckIncludeOrder(input_api, output_api))
2266 results.extend(_CheckForVersionControlConflicts(input_api, output_api)) 2106 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
2267 results.extend(_CheckPatchFiles(input_api, output_api)) 2107 results.extend(_CheckPatchFiles(input_api, output_api))
2268 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api)) 2108 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
2269 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api)) 2109 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
2270 results.extend(_CheckForInvalidOSMacros(input_api, output_api)) 2110 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
2271 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api)) 2111 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api))
2272 results.extend(_CheckFlakyTestUsage(input_api, output_api)) 2112 results.extend(_CheckFlakyTestUsage(input_api, output_api))
2273 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api)) 2113 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
2274 results.extend( 2114 results.extend(
2275 input_api.canned_checks.CheckChangeHasNoTabs( 2115 input_api.canned_checks.CheckChangeHasNoTabs(
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
2545 output_api, 2385 output_api,
2546 json_url='http://chromium-status.appspot.com/current?format=json')) 2386 json_url='http://chromium-status.appspot.com/current?format=json'))
2547 2387
2548 results.extend( 2388 results.extend(
2549 input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) 2389 input_api.canned_checks.CheckPatchFormatted(input_api, output_api))
2550 results.extend(input_api.canned_checks.CheckChangeHasBugField( 2390 results.extend(input_api.canned_checks.CheckChangeHasBugField(
2551 input_api, output_api)) 2391 input_api, output_api))
2552 results.extend(input_api.canned_checks.CheckChangeHasDescription( 2392 results.extend(input_api.canned_checks.CheckChangeHasDescription(
2553 input_api, output_api)) 2393 input_api, output_api))
2554 return results 2394 return results
OLDNEW
« no previous file with comments | « no previous file | PRESUBMIT_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698