cpp_check.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #
  2. # Copyright (c) 2006-2023, RT-Thread Development Team
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Change Logs:
  7. # Date Author Notes
  8. # 2023-05-16 dejavudwh the first version
  9. #
  10. import click
  11. import logging
  12. import subprocess
  13. import sys
  14. import format_ignore
  15. class CPPCheck:
  16. def __init__(self, file_list):
  17. self.file_list = file_list
  18. def check(self):
  19. file_list_filtered = [file for file in self.file_list if file.endswith(('.c', '.cpp', '.cc', '.cxx'))]
  20. logging.info("Start to static code analysis.")
  21. check_result = True
  22. for file in file_list_filtered:
  23. result = subprocess.run(['cppcheck', '--enable=warning', 'performance', 'portability', '--inline-suppr', '--error-exitcode=1', '--force', file], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  24. logging.info(result.stdout.decode())
  25. logging.info(result.stderr.decode())
  26. if result.stderr:
  27. check_result = False
  28. return check_result
  29. @click.group()
  30. @click.pass_context
  31. def cli(ctx):
  32. pass
  33. @cli.command()
  34. def check():
  35. """
  36. static code analysis(cppcheck).
  37. """
  38. format_ignore.init_logger()
  39. # get modified files list
  40. checkout = format_ignore.CheckOut()
  41. file_list = checkout.get_new_file()
  42. if file_list is None:
  43. logging.error("checkout files fail")
  44. sys.exit(1)
  45. # use cppcheck
  46. cpp_check = CPPCheck(file_list)
  47. cpp_check_result = cpp_check.check()
  48. if not cpp_check_result:
  49. logging.error("static code analysis(cppcheck) fail.")
  50. sys.exit(1)
  51. logging.info("check success.")
  52. sys.exit(0)
  53. if __name__ == '__main__':
  54. cli()