cpp_check.py 2.2 KB

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