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