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    "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=]
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'
45 ])
46
47# Capture the name of the object file, can find it.
48ofile = None
49
50do_exit = False;
51
52warning_re = re.compile(r'''(.*/|)([^/]+\.[a-z]+:\d+):(\d+:)? warning:''')
53def 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
69def 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
100if __name__ == '__main__':
101    status = run_clang()
102    sys.exit(status)
103