Prathyush Katukojwala | eec0cc6 | 2017-08-23 12:00:14 -0700 | [diff] [blame] | 1 | #! /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 Shriram | 73a5708 | 2017-09-21 17:15:34 -0700 | [diff] [blame] | 7 | # 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 Katukojwala | eec0cc6 | 2017-08-23 12:00:14 -0700 | [diff] [blame] | 18 | # |
Jeevan Shriram | 73a5708 | 2017-09-21 17:15:34 -0700 | [diff] [blame] | 19 | # 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 Katukojwala | eec0cc6 | 2017-08-23 12:00:14 -0700 | [diff] [blame] | 30 | |
| 31 | # Invoke clang, look for static analyzer warnings, and cause a failure |
| 32 | # if there are non-whitelisted warnings. |
| 33 | |
| 34 | import errno |
| 35 | import re |
| 36 | import os |
| 37 | import sys |
| 38 | import subprocess |
| 39 | |
| 40 | allowed_warnings = set([ |
| 41 | "list.h:80", |
Prathyush Katukojwala | eec0cc6 | 2017-08-23 12:00:14 -0700 | [diff] [blame] | 42 | ]) |
| 43 | |
| 44 | # Capture the name of the object file, if can find it. |
| 45 | ofile = None |
| 46 | |
| 47 | warning_re = re.compile(r'''(.*/|)([^/]+\.[a-z]+:\d+):(\d+:)? clang_sa_warning:''') |
| 48 | def 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 | |
| 63 | def 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 | |
| 92 | if __name__ == '__main__': |
| 93 | status = run_clang() |
| 94 | sys.exit(status) |