blob: 871aa5080baddb62cc19eba2b3dd18a7e6ba717e [file] [log] [blame]
Tao Huangb8f2e672019-11-22 11:05:13 +08001#! /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
33from __future__ import print_function
34import errno
35import re
36import os
37import sys
38import subprocess
39
40allowed_warnings = set([
Tao Huange9753da2022-07-25 14:31:20 +080041 "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 Huang53bbaaa2021-07-27 17:19:42 +080042 "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 Huangad61dfe2020-02-17 15:47:12 +080043 "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 Huangb8f2e672019-11-22 11:05:13 +080046 ])
47
48# Capture the name of the object file, can find it.
49ofile = None
50
51do_exit = False;
52
53warning_re = re.compile(r'''(.*/|)([^/]+\.[a-z]+:\d+):(\d+:)? warning:''')
54def 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
70def 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
101if __name__ == '__main__':
102 status = run_clang()
103 sys.exit(status)