1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek (at] webkit.org) 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions 5 # are met: 6 # 1. Redistributions of source code must retain the above copyright 7 # notice, this list of conditions and the following disclaimer. 8 # 2. Redistributions in binary form must reproduce the above copyright 9 # notice, this list of conditions and the following disclaimer in the 10 # documentation and/or other materials provided with the distribution. 11 # 12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 13 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 16 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 19 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 23 """Supports style checking not specific to any one file type.""" 24 25 26 # FIXME: Test this list in the same way that the list of CppChecker 27 # categories is tested, for example by checking that all of its 28 # elements appear in the unit tests. This should probably be done 29 # after moving the relevant cpp_unittest.ErrorCollector code 30 # into a shared location and refactoring appropriately. 31 categories = set([ 32 "whitespace/carriage_return", 33 "whitespace/tab"]) 34 35 36 class CarriageReturnChecker(object): 37 38 """Supports checking for and handling carriage returns.""" 39 40 def __init__(self, handle_style_error): 41 self._handle_style_error = handle_style_error 42 43 def check(self, lines): 44 """Check for and strip trailing carriage returns from lines.""" 45 for line_number in range(len(lines)): 46 if not lines[line_number].endswith("\r"): 47 continue 48 49 self._handle_style_error(line_number + 1, # Correct for offset. 50 "whitespace/carriage_return", 51 1, 52 "One or more unexpected \\r (^M) found; " 53 "better to use only a \\n") 54 55 lines[line_number] = lines[line_number].rstrip("\r") 56 57 return lines 58 59 60 class TabChecker(object): 61 62 """Supports checking for and handling tabs.""" 63 64 def __init__(self, file_path, handle_style_error): 65 self.file_path = file_path 66 self.handle_style_error = handle_style_error 67 68 def check(self, lines): 69 # FIXME: share with cpp_style. 70 for line_number, line in enumerate(lines): 71 if "\t" in line: 72 self.handle_style_error(line_number + 1, 73 "whitespace/tab", 5, 74 "Line contains tab character.") 75