cpp_check.py 2.2 KB

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