cpp_check.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(
  24. [
  25. 'cppcheck',
  26. '-DRT_ASSERT(x)=',
  27. '-Drt_list_for_each_entry(a,b,c)=a=(void*)b',
  28. '-I include',
  29. '-I thread/components/finsh',
  30. '--suppress=syntaxError',
  31. '--enable=warning',
  32. 'performance',
  33. 'portability',
  34. '--inline-suppr',
  35. '--error-exitcode=1',
  36. '--force',
  37. file
  38. ],
  39. stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  40. logging.info(result.stdout.decode())
  41. logging.info(result.stderr.decode())
  42. if result.stderr:
  43. check_result = False
  44. return check_result
  45. @click.group()
  46. @click.pass_context
  47. def cli(ctx):
  48. pass
  49. @cli.command()
  50. def check():
  51. """
  52. static code analysis(cppcheck).
  53. """
  54. format_ignore.init_logger()
  55. # get modified files list
  56. checkout = format_ignore.CheckOut()
  57. file_list = checkout.get_new_file()
  58. if file_list is None:
  59. logging.error("checkout files fail")
  60. sys.exit(1)
  61. # use cppcheck
  62. cpp_check = CPPCheck(file_list)
  63. cpp_check_result = cpp_check.check()
  64. if not cpp_check_result:
  65. logging.error("static code analysis(cppcheck) fail.")
  66. sys.exit(1)
  67. logging.info("check success.")
  68. sys.exit(0)
  69. if __name__ == '__main__':
  70. cli()