common.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (C) 2010 Chris Jerdonek (cjerdonek@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. """Supports style checking not specific to any one file type."""
  23. # FIXME: Test this list in the same way that the list of CppChecker
  24. # categories is tested, for example by checking that all of its
  25. # elements appear in the unit tests. This should probably be done
  26. # after moving the relevant cpp_unittest.ErrorCollector code
  27. # into a shared location and refactoring appropriately.
  28. categories = set([
  29. "whitespace/carriage_return",
  30. "whitespace/tab"])
  31. class CarriageReturnChecker(object):
  32. """Supports checking for and handling carriage returns."""
  33. def __init__(self, handle_style_error):
  34. self._handle_style_error = handle_style_error
  35. def check(self, lines):
  36. """Check for and strip trailing carriage returns from lines."""
  37. for line_number in range(len(lines)):
  38. if not lines[line_number].endswith("\r"):
  39. continue
  40. self._handle_style_error(line_number + 1, # Correct for offset.
  41. "whitespace/carriage_return",
  42. 1,
  43. "One or more unexpected \\r (^M) found; "
  44. "better to use only a \\n")
  45. lines[line_number] = lines[line_number].rstrip("\r")
  46. return lines
  47. class TabChecker(object):
  48. """Supports checking for and handling tabs."""
  49. def __init__(self, file_path, handle_style_error):
  50. self.file_path = file_path
  51. self.handle_style_error = handle_style_error
  52. def check(self, lines):
  53. # FIXME: share with cpp_style.
  54. for line_number, line in enumerate(lines):
  55. if "\t" in line:
  56. self.handle_style_error(line_number + 1,
  57. "whitespace/tab", 5,
  58. "Line contains tab character.")