Jeff Gaston | 87ad8c6 | 2020-03-06 15:50:17 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
Chris Craik | 908cd99 | 2019-04-03 10:15:52 -0700 | [diff] [blame] | 2 | |
| 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 | |
| 21 | import os |
| 22 | import sys |
| 23 | |
| 24 | MAIN_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 |
| 27 | ROOT_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 |
| 31 | MATCHERS = { |
| 32 | '//noinspection deprecation' : '@SuppressWarnings("deprecation")', |
| 33 | '//noinspection unchecked' : '@SuppressWarnings("unchecked")', |
| 34 | } |
| 35 | |
Chris Craik | fc7b743 | 2019-04-04 12:24:40 -0700 | [diff] [blame] | 36 | ## Check a line for any invalid suppressions, and return it if found |
| 37 | def getReportForLine(filename, i, line, lines): |
Chris Craik | 908cd99 | 2019-04-03 10:15:52 -0700 | [diff] [blame] | 38 | for bad, good in MATCHERS.iteritems(): |
| 39 | if bad in line: |
| 40 | context = "".join(lines[i:i+3]) |
Chris Craik | fc7b743 | 2019-04-04 12:24:40 -0700 | [diff] [blame] | 41 | return "\n{}:{}:\nError: unsupported comment suppression\n{}Instead, use: {}\n".format( |
Chris Craik | 908cd99 | 2019-04-03 10:15:52 -0700 | [diff] [blame] | 42 | filename, i, context, good) |
Chris Craik | fc7b743 | 2019-04-04 12:24:40 -0700 | [diff] [blame] | 43 | return "" |
Chris Craik | 908cd99 | 2019-04-03 10:15:52 -0700 | [diff] [blame] | 44 | |
| 45 | def main(): |
Chris Craik | fc7b743 | 2019-04-04 12:24:40 -0700 | [diff] [blame] | 46 | report = "" |
| 47 | |
Chris Craik | 908cd99 | 2019-04-03 10:15:52 -0700 | [diff] [blame] | 48 | 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 Craik | fc7b743 | 2019-04-04 12:24:40 -0700 | [diff] [blame] | 55 | # check file exists |
| 56 | if not os.path.isfile(filename): |
| 57 | continue |
| 58 | |
Chris Craik | 908cd99 | 2019-04-03 10:15:52 -0700 | [diff] [blame] | 59 | 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 Craik | fc7b743 | 2019-04-04 12:24:40 -0700 | [diff] [blame] | 65 | report += getReportForLine(filename, i, line, lines) |
Chris Craik | 908cd99 | 2019-04-03 10:15:52 -0700 | [diff] [blame] | 66 | |
| 67 | if not report: |
| 68 | sys.exit(0) |
| 69 | |
Jeff Gaston | 103b77f | 2020-03-04 14:31:41 -0500 | [diff] [blame] | 70 | print("Invalid, IDEA-specific warning suppression found. These cause warnings during compilation.") |
| 71 | print(report) |
Chris Craik | 908cd99 | 2019-04-03 10:15:52 -0700 | [diff] [blame] | 72 | sys.exit(1) |
| 73 | |
| 74 | if __name__ == '__main__': |
| 75 | main() |