check_sparse.py 695 B

12345678910111213141516171819202122232425
  1. #! /usr/bin/env python3
  2. # Invoke sparse based on the contents of compile_commands.json
  3. import json
  4. import subprocess
  5. import sys
  6. import shlex
  7. def extract_cflags(shcmd):
  8. cflags = shlex.split(shcmd)
  9. return [x for x in cflags
  10. if x.startswith('-D') or x.startswith('-I') or x.startswith('-W')
  11. or x.startswith('-std=')]
  12. cflags = sys.argv[1:-1]
  13. with open(sys.argv[-1], 'r') as fd:
  14. compile_commands = json.load(fd)
  15. for cmd in compile_commands:
  16. cmd = ['sparse'] + cflags + extract_cflags(cmd['command']) + [cmd['file']]
  17. print(' '.join((shlex.quote(x) for x in cmd)))
  18. r = subprocess.run(cmd)
  19. if r.returncode != 0:
  20. sys.exit(r.returncode)