PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/util/chkformat

https://bitbucket.org/musleh123/gem5_cetus
Python | 168 lines | 113 code | 22 blank | 33 comment | 31 complexity | a02f5dcbd42bc58eca314530a53777e5 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. #!/usr/bin/env python
  2. # Copyright (c) 2006 The Regents of The University of Michigan
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met: redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer;
  9. # redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution;
  12. # neither the name of the copyright holders nor the names of its
  13. # contributors may be used to endorse or promote products derived from
  14. # this software without specific prior written permission.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #
  28. # Authors: Nathan Binkert
  29. from getopt import getopt, GetoptError
  30. import re
  31. import sys
  32. tabsize = 8
  33. lead = re.compile(r'^([ \t])+')
  34. trail = re.compile(r'[ \t]+$')
  35. any_control = re.compile(r'\b(if|while|for)[ \t]*[(]')
  36. good_control = re.compile(r'\b(if|while|for) [(]')
  37. def linelen(line):
  38. tabs = line.count('\t')
  39. if not tabs:
  40. return len(line)
  41. count = 0
  42. for c in line:
  43. if c == '\t':
  44. count += tabsize - count % tabsize
  45. else:
  46. count += 1
  47. return count
  48. toolong = 0
  49. toolong80 = 0
  50. leadtabs = 0
  51. trailwhite = 0
  52. badcontrol = 0
  53. cret = 0
  54. def validate(filename, verbose, code):
  55. global toolong, toolong80, leadtabs, trailwhite, badcontrol, cret
  56. def msg(lineno, line, message):
  57. print '%s:%d>' % (filename, lineno + 1), message
  58. if verbose > 2:
  59. print line
  60. def bad():
  61. if code is not None:
  62. sys.exit(code)
  63. cpp = filename.endswith('.cc') or filename.endswith('.hh')
  64. py = filename.endswith('.py')
  65. if py + cpp != 1:
  66. raise AttributeError, \
  67. "I don't know how to deal with the file %s" % filename
  68. try:
  69. f = file(filename, 'r')
  70. except OSError:
  71. if verbose > 0:
  72. print 'could not open file %s' % filename
  73. bad()
  74. return
  75. for i,line in enumerate(f):
  76. line = line.rstrip('\n')
  77. # no carriage returns
  78. if line.find('\r') != -1:
  79. cret += 1
  80. if verbose > 1:
  81. msg(i, line, 'carriage return found')
  82. bad()
  83. # lines max out at 79 chars
  84. llen = linelen(line)
  85. if llen > 79:
  86. toolong += 1
  87. if llen == 80:
  88. toolong80 += 1
  89. if verbose > 1:
  90. msg(i, line, 'line too long (%d chars)' % llen)
  91. bad()
  92. # no tabs used to indent
  93. match = lead.search(line)
  94. if match and match.group(1).find('\t') != -1:
  95. leadtabs += 1
  96. if verbose > 1:
  97. msg(i, line, 'using tabs to indent')
  98. bad()
  99. # no trailing whitespace
  100. if trail.search(line):
  101. trailwhite +=1
  102. if verbose > 1:
  103. msg(i, line, 'trailing whitespace')
  104. bad()
  105. # for c++, exactly one space betwen if/while/for and (
  106. if cpp:
  107. match = any_control.search(line)
  108. if match and not good_control.search(line):
  109. badcontrol += 1
  110. if verbose > 1:
  111. msg(i, line, 'improper spacing after %s' % match.group(1))
  112. bad()
  113. if __name__ == '__main__':
  114. progname = sys.argv[0]
  115. def usage(code=None):
  116. print >>sys.stderr, '''%s [-n] [-q] [-v] <filenames>''' % progname
  117. if code is not None:
  118. sys.exit(code)
  119. try:
  120. opts, args = getopt(sys.argv[1:], '-nv')
  121. except GetoptError:
  122. usage(2)
  123. code = 1
  124. verbose = 1
  125. for opt,arg in opts:
  126. if opt == '-n':
  127. code = None
  128. if opt == '-q':
  129. verbose -= 1
  130. if opt == '-v':
  131. verbose += 1
  132. for filename in args:
  133. validate(filename, verbose=verbose, code=code)
  134. if verbose > 0:
  135. print '''\
  136. %d violations of lines over 79 chars. %d of which are 80 chars exactly.
  137. %d cases of whitespace at the end of a line.
  138. %d cases of tabs to indent.
  139. %d bad parens after if/while/for.
  140. %d carriage returns found.
  141. ''' % (toolong, toolong80, trailwhite, leadtabs, badcontrol, cret)