xref: /OK3568_Linux_fs/kernel/scripts/clang-wrapper.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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
33from __future__ import print_function
34import errno
35import re
36import os
37import sys
38import subprocess
39
40allowed_warnings = set([
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
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=]
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'
46 ])
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)
104