blob: e0154835669ef604dace8013db1d66ba2b15842e [file] [log] [blame]
Jeff Gaston87ad8c62020-03-06 15:50:17 -05001#!/usr/bin/python
Chris Craik908cd992019-04-03 10:15:52 -07002
3#
4# Copyright 2019, The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19"""Script used to find IDEA warning suppression that doesn't work for command line builds."""
20
21import os
22import sys
23
24MAIN_DIRECTORY = os.path.normpath(os.path.join(os.path.dirname(__file__), ".."))
25
26# first level line filter - we ignore lines that don't contain the following for speed
27ROOT_MATCH = "noinspection"
28
29# Map of invalid pattern to correct replacement
30# These could be regex for fancy whitespace matching, but don't seem to need in practice
31MATCHERS = {
32 '//noinspection deprecation' : '@SuppressWarnings("deprecation")',
33 '//noinspection unchecked' : '@SuppressWarnings("unchecked")',
34}
35
Chris Craikfc7b7432019-04-04 12:24:40 -070036## Check a line for any invalid suppressions, and return it if found
37def getReportForLine(filename, i, line, lines):
Chris Craik908cd992019-04-03 10:15:52 -070038 for bad, good in MATCHERS.iteritems():
39 if bad in line:
40 context = "".join(lines[i:i+3])
Chris Craikfc7b7432019-04-04 12:24:40 -070041 return "\n{}:{}:\nError: unsupported comment suppression\n{}Instead, use: {}\n".format(
Chris Craik908cd992019-04-03 10:15:52 -070042 filename, i, context, good)
Chris Craikfc7b7432019-04-04 12:24:40 -070043 return ""
Chris Craik908cd992019-04-03 10:15:52 -070044
45def main():
Chris Craikfc7b7432019-04-04 12:24:40 -070046 report = ""
47
Chris Craik908cd992019-04-03 10:15:52 -070048 for filename in sys.argv[1:]:
49 # suppress comments ignored in kotlin, but may as well block there too
50 if not filename.endswith(".java") and not filename.endswith(".kt"):
51 continue
52
53 filename = os.path.join(MAIN_DIRECTORY, filename)
54
Chris Craikfc7b7432019-04-04 12:24:40 -070055 # check file exists
56 if not os.path.isfile(filename):
57 continue
58
Chris Craik908cd992019-04-03 10:15:52 -070059 with open(filename, "r") as f:
60 lines = f.readlines()
61
62 linetuples = map(lambda i: [i, lines[i]], range(len(lines)))
63 for i, line in linetuples:
64 if ROOT_MATCH in line:
Chris Craikfc7b7432019-04-04 12:24:40 -070065 report += getReportForLine(filename, i, line, lines)
Chris Craik908cd992019-04-03 10:15:52 -070066
67 if not report:
68 sys.exit(0)
69
Jeff Gaston103b77f2020-03-04 14:31:41 -050070 print("Invalid, IDEA-specific warning suppression found. These cause warnings during compilation.")
71 print(report)
Chris Craik908cd992019-04-03 10:15:52 -070072 sys.exit(1)
73
74if __name__ == '__main__':
75 main()