1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # SPDX-FileCopyrightText: 2010 Martin Decky
|
---|
4 | #
|
---|
5 | # SPDX-License-Identifier: BSD-3-Clause
|
---|
6 | #
|
---|
7 | """
|
---|
8 | Wrapper for Clang static analyzer
|
---|
9 | """
|
---|
10 |
|
---|
11 | import sys
|
---|
12 | import os
|
---|
13 | import subprocess
|
---|
14 | import jobfile
|
---|
15 |
|
---|
16 | jobs = [
|
---|
17 | "kernel/kernel.job"
|
---|
18 | ]
|
---|
19 |
|
---|
20 | def usage(prname):
|
---|
21 | "Print usage syntax"
|
---|
22 | print(prname + " <ROOT>")
|
---|
23 |
|
---|
24 | def clang(root, job):
|
---|
25 | "Run Clang on a jobfile"
|
---|
26 |
|
---|
27 | inname = os.path.join(root, job)
|
---|
28 |
|
---|
29 | if (not os.path.isfile(inname)):
|
---|
30 | print("Unable to open %s" % inname)
|
---|
31 | print("Did you run \"make precheck\" on the source tree?")
|
---|
32 | return False
|
---|
33 |
|
---|
34 | inf = open(inname, "r")
|
---|
35 | records = inf.read().splitlines()
|
---|
36 | inf.close()
|
---|
37 |
|
---|
38 | for record in records:
|
---|
39 | arg = jobfile.parse_arg(record)
|
---|
40 | if (not arg):
|
---|
41 | return False
|
---|
42 |
|
---|
43 | if (len(arg) < 6):
|
---|
44 | print("Not enough jobfile record arguments")
|
---|
45 | return False
|
---|
46 |
|
---|
47 | srcfname = arg[0]
|
---|
48 | tgtfname = arg[1]
|
---|
49 | tool = arg[2]
|
---|
50 | category = arg[3]
|
---|
51 | base = arg[4]
|
---|
52 | options = arg[5].split()
|
---|
53 |
|
---|
54 | srcfqname = os.path.join(base, srcfname)
|
---|
55 | if (not os.path.isfile(srcfqname)):
|
---|
56 | print("Source %s not found" % srcfqname)
|
---|
57 | return False
|
---|
58 |
|
---|
59 | # Only C files are interesting for us
|
---|
60 | if (tool != "cc"):
|
---|
61 | continue
|
---|
62 |
|
---|
63 | args = ['clang', '-Qunused-arguments', '--analyze',
|
---|
64 | '-Xanalyzer', '-analyzer-opt-analyze-headers',
|
---|
65 | '-Xanalyzer', '-checker-cfref']
|
---|
66 | args.extend(options)
|
---|
67 | args.extend(['-o', tgtfname, srcfname])
|
---|
68 |
|
---|
69 | cwd = os.getcwd()
|
---|
70 | os.chdir(base)
|
---|
71 | retval = subprocess.Popen(args).wait()
|
---|
72 | os.chdir(cwd)
|
---|
73 |
|
---|
74 | if (retval != 0):
|
---|
75 | return False
|
---|
76 |
|
---|
77 | return True
|
---|
78 |
|
---|
79 | def main():
|
---|
80 | if (len(sys.argv) < 2):
|
---|
81 | usage(sys.argv[0])
|
---|
82 | return
|
---|
83 |
|
---|
84 | rootdir = os.path.abspath(sys.argv[1])
|
---|
85 | config = os.path.join(rootdir, "HelenOS.config")
|
---|
86 |
|
---|
87 | if (not os.path.isfile(config)):
|
---|
88 | print("%s not found." % config)
|
---|
89 | print("Please specify the path to HelenOS build tree root as the first argument.")
|
---|
90 | return
|
---|
91 |
|
---|
92 | for job in jobs:
|
---|
93 | if (not clang(rootdir, job)):
|
---|
94 | print()
|
---|
95 | print("Failed job: %s" % job)
|
---|
96 | return
|
---|
97 |
|
---|
98 | print
|
---|
99 | print("All jobs passed")
|
---|
100 |
|
---|
101 | if __name__ == '__main__':
|
---|
102 | main()
|
---|