check_c_function_newline.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. """ Checks that there is a newline in function definitions. """
  3. import sys
  4. import style_utils
  5. def handle_file(filename):
  6. """ Parses a .c file and checks that functions are defined with a newline.
  7. Args:
  8. filename (string): the filename to examine."""
  9. func_text = ""
  10. with open(filename, encoding="utf-8") as filep:
  11. for i, line in enumerate(filep):
  12. if line[0] == "{":
  13. func_text += line
  14. func_text_search = func_text.replace("static ", "")
  15. if " " in func_text_search.split("(")[0]:
  16. print("--------- WARNING: possible style violation")
  17. print("\t%s" % filename)
  18. print("\tline %d" % i)
  19. print(func_text)
  20. func_text = ""
  21. if line[0] == "\t" or line[0] == " " or line[0] == "_":
  22. continue
  23. if line[0] == "#" or line[0:2] == "/*" or line[0:3] == " */":
  24. func_text = ""
  25. continue
  26. if ";" in line:
  27. func_text = ""
  28. continue
  29. if "(" not in line:
  30. func_text = ""
  31. continue
  32. func_text += line
  33. def main(filenames):
  34. """ Checks files for a newline in function definitions.
  35. Args:
  36. filenames (list): the filenames (as strings) to check."""
  37. for filename in filenames:
  38. # don't apply to libarchive right now
  39. if style_utils.is_libarchive(filename):
  40. continue
  41. handle_file(filename)
  42. print("---------")
  43. print("This script is an aid, not a definitive guide. It can create")
  44. print("false positives and false negatives. Use your own judgement.")
  45. if __name__ == "__main__":
  46. if len(sys.argv) < 2:
  47. print("Manual usage: specify the filename of a .c file")
  48. print("Automatic usage:")
  49. print(" find . -name \"*.c\" | "
  50. "xargs tools/check_c_function_newline.py")
  51. exit(1)
  52. main(sys.argv[1:])