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

Side by Side Diff: PRESUBMIT.py

Issue 2598173002: Add a Presubmit to check if a file must be included or imported.
Patch Set: Created 3 years, 12 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 | no next file » | 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 ) 51 )
52 52
53 53
54 _TEST_ONLY_WARNING = ( 54 _TEST_ONLY_WARNING = (
55 'You might be calling functions intended only for testing from\n' 55 'You might be calling functions intended only for testing from\n'
56 'production code. It is OK to ignore this warning if you know what\n' 56 'production code. It is OK to ignore this warning if you know what\n'
57 'you are doing, as the heuristics used to detect the situation are\n' 57 'you are doing, as the heuristics used to detect the situation are\n'
58 'not perfect. The commit queue will not block on this warning.') 58 'not perfect. The commit queue will not block on this warning.')
59 59
60 60
61 _INCLUDE_IMPORT_WARNING = (
62 'You are including ObjC headers or importing C++ headers. Remember to use '
63 'the right #include/#import directive.')
64
65
61 _INCLUDE_ORDER_WARNING = ( 66 _INCLUDE_ORDER_WARNING = (
62 'Your #include order seems to be broken. Remember to use the right ' 67 'Your #include order seems to be broken. Remember to use the right '
63 'collation (LC_COLLATE=C) and check\nhttps://google.github.io/styleguide/' 68 'collation (LC_COLLATE=C) and check\nhttps://google.github.io/styleguide/'
64 'cppguide.html#Names_and_Order_of_Includes') 69 'cppguide.html#Names_and_Order_of_Includes')
65 70
66 71
67 _BANNED_OBJC_FUNCTIONS = ( 72 _BANNED_OBJC_FUNCTIONS = (
68 ( 73 (
69 'addTrackingRect:', 74 'addTrackingRect:',
70 ( 75 (
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 if pattern.match(line): 769 if pattern.match(line):
765 errors.append(' %s:%d' % (f.LocalPath(), line_num)) 770 errors.append(' %s:%d' % (f.LocalPath(), line_num))
766 771
767 results = [] 772 results = []
768 if errors: 773 if errors:
769 results.append(output_api.PresubmitError( 774 results.append(output_api.PresubmitError(
770 'Header files should not include ui/aura/window_property.h', errors)) 775 'Header files should not include ui/aura/window_property.h', errors))
771 return results 776 return results
772 777
773 778
779 def _CheckIncludeImportInFile(input_api, f, changed_linenums):
780 """Checks the include/import coherence in file f."""
Eugene But (OOO till 7-30) 2016/12/23 16:25:08 Could you please document the rules, about @end/@c
Olivier 2016/12/26 14:49:29 Done.
781 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
782 custom_import_pattern = input_api.re.compile(r'\s*#import "(?P<FILE>.*)"')
783 guard_pattern = input_api.re.compile(
784 r'\s*#if !?defined\(__OBJC__\).*')
785 objc_pattern = input_api.re.compile(
786 r'\s*(@interface|@end|@implementation|@protocol|#import).*')
787 contents = f.NewContents()
788 warnings = []
789 line_num = 0
790 for line in contents:
Eugene But (OOO till 7-30) 2016/12/23 16:25:08 Could you please add implementation comments. The
Olivier 2016/12/26 14:49:29 Done.
791 line_num += 1
792 is_imported = False
793 included_file = ""
794 match_cpp = custom_include_pattern.match(line)
795 if match_cpp:
796 match_dict = match_cpp.groupdict()
797 included_file = match_dict['FILE']
798 match_objc = custom_import_pattern.match(line)
799 if match_objc:
800 match_dict = match_objc.groupdict()
801 included_file = match_dict['FILE']
802 is_imported = True
803 if included_file == "":
804 continue
805 included_absolute_path = input_api.os_path.join(
806 input_api.change.RepositoryRoot(), included_file)
807 if not input_api.os_path.isfile(included_absolute_path):
808 # Included file may need a custom -I flag.
809 # Ignore it.
810 continue
811 has_objc = False
812 ignore_objc = False
813 for included_line in open(included_absolute_path, 'r'):
814 match_guard = guard_pattern.match(included_line)
815 if match_guard:
816 ignore_objc = True
817 break
818 match_objc = objc_pattern.match(included_line)
819 if match_objc:
820 has_objc = True
821 break
Eugene But (OOO till 7-30) 2016/12/23 16:25:09 This is a complex body with multiple loops continu
Olivier 2016/12/26 14:49:29 Done.
822 if ignore_objc:
823 # There is an Objective C guard, the file is internded to be included or
824 # imported.
825 continue
826 if has_objc and not is_imported:
827 warnings.append('%s:%d %s' % (f.LocalPath(), line_num, line))
828 warnings.append('Header is included but has Objective C instructions.')
829 if not has_objc and is_imported:
830 warnings.append('%s:%d %s' % (f.LocalPath(), line_num, line))
831 warnings.append('Header is imported but has no Objective C instructions.')
832 return warnings
833
834
835 def _CheckIncludeImport(input_api, output_api):
836 """Checks that C++ files are included, ObjC files imported
Eugene But (OOO till 7-30) 2016/12/23 16:25:08 nit: s/files/headers
Olivier 2016/12/26 14:49:29 Done.
837 """
838 def FileFilterIncludeImport(affected_file):
839 black_list = (_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
840 return input_api.FilterSourceFile(affected_file, black_list=black_list)
841
842 warnings = []
843 for f in input_api.AffectedFiles(file_filter=FileFilterIncludeImport):
Eugene But (OOO till 7-30) 2016/12/23 16:25:08 Usage of filter is discouraged by Python Style Gui
Olivier 2016/12/26 14:49:29 All other tests use this structure and I cannot ch
844 if f.LocalPath().endswith(('.cc', '.h', '.mm')):
845 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
846 warnings.extend(_CheckIncludeImportInFile(input_api, f, changed_linenums))
847
848 results = []
849 if warnings:
850 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_IMPORT_WARNING,
851 warnings))
852 return results
853
854
774 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums): 855 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
775 """Checks that the lines in scope occur in the right order. 856 """Checks that the lines in scope occur in the right order.
776 857
777 1. C system files in alphabetical order 858 1. C system files in alphabetical order
778 2. C++ system files in alphabetical order 859 2. C++ system files in alphabetical order
779 3. Project's .h files 860 3. Project's .h files
780 """ 861 """
781 862
782 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>') 863 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
783 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>') 864 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
(...skipping 1255 matching lines...) Expand 10 before | Expand all | Expand 10 after
2039 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api)) 2120 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
2040 results.extend(_CheckDCHECK_IS_ONHasBraces(input_api, output_api)) 2121 results.extend(_CheckDCHECK_IS_ONHasBraces(input_api, output_api))
2041 results.extend(_CheckNoNewWStrings(input_api, output_api)) 2122 results.extend(_CheckNoNewWStrings(input_api, output_api))
2042 results.extend(_CheckNoDEPSGIT(input_api, output_api)) 2123 results.extend(_CheckNoDEPSGIT(input_api, output_api))
2043 results.extend(_CheckNoBannedFunctions(input_api, output_api)) 2124 results.extend(_CheckNoBannedFunctions(input_api, output_api))
2044 results.extend(_CheckNoPragmaOnce(input_api, output_api)) 2125 results.extend(_CheckNoPragmaOnce(input_api, output_api))
2045 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api)) 2126 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
2046 results.extend(_CheckUnwantedDependencies(input_api, output_api)) 2127 results.extend(_CheckUnwantedDependencies(input_api, output_api))
2047 results.extend(_CheckFilePermissions(input_api, output_api)) 2128 results.extend(_CheckFilePermissions(input_api, output_api))
2048 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api)) 2129 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
2130 results.extend(_CheckIncludeImport(input_api, output_api))
2049 results.extend(_CheckIncludeOrder(input_api, output_api)) 2131 results.extend(_CheckIncludeOrder(input_api, output_api))
2050 results.extend(_CheckForVersionControlConflicts(input_api, output_api)) 2132 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
2051 results.extend(_CheckPatchFiles(input_api, output_api)) 2133 results.extend(_CheckPatchFiles(input_api, output_api))
2052 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api)) 2134 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
2053 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api)) 2135 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
2054 results.extend(_CheckForInvalidOSMacros(input_api, output_api)) 2136 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
2055 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api)) 2137 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api))
2056 results.extend(_CheckFlakyTestUsage(input_api, output_api)) 2138 results.extend(_CheckFlakyTestUsage(input_api, output_api))
2057 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api)) 2139 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
2058 results.extend( 2140 results.extend(
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
2322 results.extend(input_api.canned_checks.CheckTreeIsOpen( 2404 results.extend(input_api.canned_checks.CheckTreeIsOpen(
2323 input_api, 2405 input_api,
2324 output_api, 2406 output_api,
2325 json_url='http://chromium-status.appspot.com/current?format=json')) 2407 json_url='http://chromium-status.appspot.com/current?format=json'))
2326 2408
2327 results.extend(input_api.canned_checks.CheckChangeHasBugField( 2409 results.extend(input_api.canned_checks.CheckChangeHasBugField(
2328 input_api, output_api)) 2410 input_api, output_api))
2329 results.extend(input_api.canned_checks.CheckChangeHasDescription( 2411 results.extend(input_api.canned_checks.CheckChangeHasDescription(
2330 input_api, output_api)) 2412 input_api, output_api))
2331 return results 2413 return results
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698