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([ |
Tao Huang | a1fa6e5 | 2019-11-25 16:36:07 +0800 | [diff] [blame] | 41 | "atags_to_fdt.c:109", # arch/arm/boot/compressed/atags_to_fdt.c:109:5: warning: stack frame size of 4416 bytes in function 'atags_to_fdt' [-Wframe-larger-than=] |
Tao Huang | ad61dfe | 2020-02-17 15:47:12 +0800 | [diff] [blame^] | 42 | "file.c:3010", # fs/f2fs/file.c:3010:12: warning: unused function 'f2fs_ioctl_check_project' |
| 43 | "configfs.c:1488", # drivers/usb/gadget/configfs.c:1488:12: warning: unused function 'configfs_composite_setup' |
| 44 | "configfs.c:1513", # drivers/usb/gadget/configfs.c:1513:13: warning: unused function 'configfs_composite_disconnect' |
Tao Huang | b8f2e67 | 2019-11-22 11:05:13 +0800 | [diff] [blame] | 45 | ]) |
| 46 | |
| 47 | # Capture the name of the object file, can find it. |
| 48 | ofile = None |
| 49 | |
| 50 | do_exit = False; |
| 51 | |
| 52 | warning_re = re.compile(r'''(.*/|)([^/]+\.[a-z]+:\d+):(\d+:)? warning:''') |
| 53 | def interpret_warning(line): |
| 54 | """Decode the message from clang. The messages we care about have a filename, and a warning""" |
| 55 | line = line.rstrip('\n') |
| 56 | m = warning_re.match(line) |
| 57 | if m and m.group(2) not in allowed_warnings: |
| 58 | print ("error, forbidden warning:" + m.group(2)) |
| 59 | |
| 60 | # If there is a warning, remove any object if it exists. |
| 61 | if ofile: |
| 62 | try: |
| 63 | os.remove(ofile) |
| 64 | except OSError: |
| 65 | pass |
| 66 | global do_exit |
| 67 | do_exit = True; |
| 68 | |
| 69 | def run_clang(): |
| 70 | args = sys.argv[1:] |
| 71 | # Look for -o |
| 72 | try: |
| 73 | i = args.index('-o') |
| 74 | global ofile |
| 75 | ofile = args[i+1] |
| 76 | except (ValueError, IndexError): |
| 77 | pass |
| 78 | |
| 79 | try: |
| 80 | env = os.environ.copy() |
| 81 | env['LC_ALL'] = 'C' |
| 82 | proc = subprocess.Popen(args, stderr=subprocess.PIPE, env=env) |
| 83 | for line in proc.stderr: |
| 84 | print (line.decode("utf-8"), end="") |
| 85 | interpret_warning(line.decode("utf-8")) |
| 86 | if do_exit: |
| 87 | sys.exit(1) |
| 88 | |
| 89 | result = proc.wait() |
| 90 | except OSError as e: |
| 91 | result = e.errno |
| 92 | if result == errno.ENOENT: |
| 93 | print (args[0] + ':' + e.strerror) |
| 94 | print ('Is your PATH set correctly?') |
| 95 | else: |
| 96 | print (' '.join(args) + str(e)) |
| 97 | |
| 98 | return result |
| 99 | |
| 100 | if __name__ == '__main__': |
| 101 | status = run_clang() |
| 102 | sys.exit(status) |