| OLD | NEW |
| 1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
| 2 # | 2 # |
| 3 # Copyright (c) 2009 Google Inc. All rights reserved. | 3 # Copyright (c) 2009 Google Inc. All rights reserved. |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
| 7 # met: | 7 # met: |
| 8 # | 8 # |
| 9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
| (...skipping 2880 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2891 if [True for header in headers if header in include_state]: | 2891 if [True for header in headers if header in include_state]: |
| 2892 continue | 2892 continue |
| 2893 if required_header_unstripped.strip('<>"') not in include_state: | 2893 if required_header_unstripped.strip('<>"') not in include_state: |
| 2894 error(filename, required[required_header_unstripped][0], | 2894 error(filename, required[required_header_unstripped][0], |
| 2895 'build/include_what_you_use', 4, | 2895 'build/include_what_you_use', 4, |
| 2896 'Add #include ' + required_header_unstripped + ' for ' + template) | 2896 'Add #include ' + required_header_unstripped + ' for ' + template) |
| 2897 | 2897 |
| 2898 | 2898 |
| 2899 def ProcessLine(filename, file_extension, | 2899 def ProcessLine(filename, file_extension, |
| 2900 clean_lines, line, include_state, function_state, | 2900 clean_lines, line, include_state, function_state, |
| 2901 class_state, error): | 2901 class_state, error, extra_check_functions=[]): |
| 2902 """Processes a single line in the file. | 2902 """Processes a single line in the file. |
| 2903 | 2903 |
| 2904 Args: | 2904 Args: |
| 2905 filename: Filename of the file that is being processed. | 2905 filename: Filename of the file that is being processed. |
| 2906 file_extension: The extension (dot not included) of the file. | 2906 file_extension: The extension (dot not included) of the file. |
| 2907 clean_lines: An array of strings, each representing a line of the file, | 2907 clean_lines: An array of strings, each representing a line of the file, |
| 2908 with comments stripped. | 2908 with comments stripped. |
| 2909 line: Number of line being processed. | 2909 line: Number of line being processed. |
| 2910 include_state: An _IncludeState instance in which the headers are inserted. | 2910 include_state: An _IncludeState instance in which the headers are inserted. |
| 2911 function_state: A _FunctionState instance which counts function lines, etc. | 2911 function_state: A _FunctionState instance which counts function lines, etc. |
| 2912 class_state: A _ClassState instance which maintains information about | 2912 class_state: A _ClassState instance which maintains information about |
| 2913 the current stack of nested class declarations being parsed. | 2913 the current stack of nested class declarations being parsed. |
| 2914 error: A callable to which errors are reported, which takes 4 arguments: | 2914 error: A callable to which errors are reported, which takes 4 arguments: |
| 2915 filename, line number, error level, and message | 2915 filename, line number, error level, and message |
| 2916 | 2916 |
| 2917 """ | 2917 """ |
| 2918 raw_lines = clean_lines.raw_lines | 2918 raw_lines = clean_lines.raw_lines |
| 2919 ParseNolintSuppressions(filename, raw_lines[line], line, error) | 2919 ParseNolintSuppressions(filename, raw_lines[line], line, error) |
| 2920 CheckForFunctionLengths(filename, clean_lines, line, function_state, error) | 2920 CheckForFunctionLengths(filename, clean_lines, line, function_state, error) |
| 2921 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error) | 2921 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error) |
| 2922 CheckStyle(filename, clean_lines, line, file_extension, error) | 2922 CheckStyle(filename, clean_lines, line, file_extension, error) |
| 2923 CheckLanguage(filename, clean_lines, line, file_extension, include_state, | 2923 CheckLanguage(filename, clean_lines, line, file_extension, include_state, |
| 2924 error) | 2924 error) |
| 2925 CheckForNonStandardConstructs(filename, clean_lines, line, | 2925 CheckForNonStandardConstructs(filename, clean_lines, line, |
| 2926 class_state, error) | 2926 class_state, error) |
| 2927 CheckPosixThreading(filename, clean_lines, line, error) | 2927 CheckPosixThreading(filename, clean_lines, line, error) |
| 2928 CheckInvalidIncrement(filename, clean_lines, line, error) | 2928 CheckInvalidIncrement(filename, clean_lines, line, error) |
| 2929 for check_fn in extra_check_functions: |
| 2930 check_fn(filename, clean_lines, line, error) |
| 2929 | 2931 |
| 2930 | 2932 def ProcessFileData(filename, file_extension, lines, error, |
| 2931 def ProcessFileData(filename, file_extension, lines, error): | 2933 extra_check_functions=[]): |
| 2932 """Performs lint checks and reports any errors to the given error function. | 2934 """Performs lint checks and reports any errors to the given error function. |
| 2933 | 2935 |
| 2934 Args: | 2936 Args: |
| 2935 filename: Filename of the file that is being processed. | 2937 filename: Filename of the file that is being processed. |
| 2936 file_extension: The extension (dot not included) of the file. | 2938 file_extension: The extension (dot not included) of the file. |
| 2937 lines: An array of strings, each representing a line of the file, with the | 2939 lines: An array of strings, each representing a line of the file, with the |
| 2938 last element being empty if the file is termined with a newline. | 2940 last element being empty if the file is termined with a newline. |
| 2939 error: A callable to which errors are reported, which takes 4 arguments: | 2941 error: A callable to which errors are reported, which takes 4 arguments: |
| 2940 """ | 2942 """ |
| 2941 lines = (['// marker so line numbers and indices both start at 1'] + lines + | 2943 lines = (['// marker so line numbers and indices both start at 1'] + lines + |
| 2942 ['// marker so line numbers end in a known way']) | 2944 ['// marker so line numbers end in a known way']) |
| 2943 | 2945 |
| 2944 include_state = _IncludeState() | 2946 include_state = _IncludeState() |
| 2945 function_state = _FunctionState() | 2947 function_state = _FunctionState() |
| 2946 class_state = _ClassState() | 2948 class_state = _ClassState() |
| 2947 | 2949 |
| 2948 ResetNolintSuppressions() | 2950 ResetNolintSuppressions() |
| 2949 | 2951 |
| 2950 CheckForCopyright(filename, lines, error) | 2952 CheckForCopyright(filename, lines, error) |
| 2951 | 2953 |
| 2952 if file_extension == 'h': | 2954 if file_extension == 'h': |
| 2953 CheckForHeaderGuard(filename, lines, error) | 2955 CheckForHeaderGuard(filename, lines, error) |
| 2954 | 2956 |
| 2955 RemoveMultiLineComments(filename, lines, error) | 2957 RemoveMultiLineComments(filename, lines, error) |
| 2956 clean_lines = CleansedLines(lines) | 2958 clean_lines = CleansedLines(lines) |
| 2957 for line in xrange(clean_lines.NumLines()): | 2959 for line in xrange(clean_lines.NumLines()): |
| 2958 ProcessLine(filename, file_extension, clean_lines, line, | 2960 ProcessLine(filename, file_extension, clean_lines, line, |
| 2959 include_state, function_state, class_state, error) | 2961 include_state, function_state, class_state, error, |
| 2962 extra_check_functions) |
| 2960 class_state.CheckFinished(filename, error) | 2963 class_state.CheckFinished(filename, error) |
| 2961 | 2964 |
| 2962 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error) | 2965 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error) |
| 2963 | 2966 |
| 2964 # We check here rather than inside ProcessLine so that we see raw | 2967 # We check here rather than inside ProcessLine so that we see raw |
| 2965 # lines rather than "cleaned" lines. | 2968 # lines rather than "cleaned" lines. |
| 2966 CheckForUnicodeReplacementCharacters(filename, lines, error) | 2969 CheckForUnicodeReplacementCharacters(filename, lines, error) |
| 2967 | 2970 |
| 2968 CheckForNewlineAtEOF(filename, lines, error) | 2971 CheckForNewlineAtEOF(filename, lines, error) |
| 2969 | 2972 |
| 2970 def ProcessFile(filename, vlevel): | 2973 def ProcessFile(filename, vlevel, extra_check_functions=[]): |
| 2971 """Does google-lint on a single file. | 2974 """Does google-lint on a single file. |
| 2972 | 2975 |
| 2973 Args: | 2976 Args: |
| 2974 filename: The name of the file to parse. | 2977 filename: The name of the file to parse. |
| 2975 | 2978 |
| 2976 vlevel: The level of errors to report. Every error of confidence | 2979 vlevel: The level of errors to report. Every error of confidence |
| 2977 >= verbose_level will be reported. 0 is a good default. | 2980 >= verbose_level will be reported. 0 is a good default. |
| 2978 """ | 2981 """ |
| 2979 | 2982 |
| 2980 _SetVerboseLevel(vlevel) | 2983 _SetVerboseLevel(vlevel) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3012 | 3015 |
| 3013 # Note, if no dot is found, this will give the entire filename as the ext. | 3016 # Note, if no dot is found, this will give the entire filename as the ext. |
| 3014 file_extension = filename[filename.rfind('.') + 1:] | 3017 file_extension = filename[filename.rfind('.') + 1:] |
| 3015 | 3018 |
| 3016 # When reading from stdin, the extension is unknown, so no cpplint tests | 3019 # When reading from stdin, the extension is unknown, so no cpplint tests |
| 3017 # should rely on the extension. | 3020 # should rely on the extension. |
| 3018 if (filename != '-' and file_extension != 'cc' and file_extension != 'h' | 3021 if (filename != '-' and file_extension != 'cc' and file_extension != 'h' |
| 3019 and file_extension != 'cpp'): | 3022 and file_extension != 'cpp'): |
| 3020 sys.stderr.write('Ignoring %s; not a .cc or .h file\n' % filename) | 3023 sys.stderr.write('Ignoring %s; not a .cc or .h file\n' % filename) |
| 3021 else: | 3024 else: |
| 3022 ProcessFileData(filename, file_extension, lines, Error) | 3025 ProcessFileData(filename, file_extension, lines, Error, |
| 3026 extra_check_functions) |
| 3023 if carriage_return_found and os.linesep != '\r\n': | 3027 if carriage_return_found and os.linesep != '\r\n': |
| 3024 # Use 0 for linenum since outputing only one error for potentially | 3028 # Use 0 for linenum since outputing only one error for potentially |
| 3025 # several lines. | 3029 # several lines. |
| 3026 Error(filename, 0, 'whitespace/newline', 1, | 3030 Error(filename, 0, 'whitespace/newline', 1, |
| 3027 'One or more unexpected \\r (^M) found;' | 3031 'One or more unexpected \\r (^M) found;' |
| 3028 'better to use only a \\n') | 3032 'better to use only a \\n') |
| 3029 | 3033 |
| 3030 sys.stderr.write('Done processing %s\n' % filename) | 3034 sys.stderr.write('Done processing %s\n' % filename) |
| 3031 | 3035 |
| 3032 | 3036 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3117 _cpplint_state.ResetErrorCounts() | 3121 _cpplint_state.ResetErrorCounts() |
| 3118 for filename in filenames: | 3122 for filename in filenames: |
| 3119 ProcessFile(filename, _cpplint_state.verbose_level) | 3123 ProcessFile(filename, _cpplint_state.verbose_level) |
| 3120 _cpplint_state.PrintErrorCounts() | 3124 _cpplint_state.PrintErrorCounts() |
| 3121 | 3125 |
| 3122 sys.exit(_cpplint_state.error_count > 0) | 3126 sys.exit(_cpplint_state.error_count > 0) |
| 3123 | 3127 |
| 3124 | 3128 |
| 3125 if __name__ == '__main__': | 3129 if __name__ == '__main__': |
| 3126 main() | 3130 main() |
| OLD | NEW |