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