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