blob: 531da4833a82f7d6686894db9dcac9434b380a2e [file] [log] [blame]
Prathyush Katukojwalaeec0cc62017-08-23 12:00:14 -07001#! /usr/bin/env python2
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
Jeevan Shriram73a57082017-09-21 17:15:34 -07007# modification, are permitted provided that the following conditions are
8# met:
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following
13# disclaimer in the documentation and/or other materials provided
14# with the distribution.
15# * Neither the name of The Linux Foundation nor the names of its
16# contributors may be used to endorse or promote products derived
17# from this software without specific prior written permission.
Prathyush Katukojwalaeec0cc62017-08-23 12:00:14 -070018#
Jeevan Shriram73a57082017-09-21 17:15:34 -070019# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
20# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
22# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Prathyush Katukojwalaeec0cc62017-08-23 12:00:14 -070030
31# Invoke clang, look for static analyzer warnings, and cause a failure
32# if there are non-whitelisted warnings.
33
34import errno
35import re
36import os
37import sys
38import subprocess
39
40allowed_warnings = set([
41 "list.h:80",
Prathyush Katukojwalaeec0cc62017-08-23 12:00:14 -070042 ])
43
44# Capture the name of the object file, if can find it.
45ofile = None
46
47warning_re = re.compile(r'''(.*/|)([^/]+\.[a-z]+:\d+):(\d+:)? clang_sa_warning:''')
48def interpret_warning(line):
49 """Decode the message from clang. The messages we care about have a filename, and a warning"""
50 line = line.rstrip('\n')
51 m = warning_re.match(line)
52 if m and m.group(2) not in allowed_warnings:
53 print "error, forbidden warning:", m.group(2)
54
55 # If there is a warning, remove any object if it exists.
56 if ofile:
57 try:
58 os.remove(ofile)
59 except OSError:
60 pass
61 sys.exit(1)
62
63def run_clang():
64 args = sys.argv[1:]
65 # Look for -o
66 try:
67 i = args.index('-o')
68 global ofile
69 ofile = args[i+1]
70 except (ValueError, IndexError):
71 pass
72
73 compiler = sys.argv[0]
74
75 try:
76 proc = subprocess.Popen(args, stderr=subprocess.PIPE)
77 for line in proc.stderr:
78 print line,
79 interpret_warning(line)
80
81 result = proc.wait()
82 except OSError as e:
83 result = e.errno
84 if result == errno.ENOENT:
85 print args[0] + ':',e.strerror
86 print 'Is your PATH set correctly?'
87 else:
88 print ' '.join(args), str(e)
89
90 return result
91
92if __name__ == '__main__':
93 status = run_clang()
94 sys.exit(status)