Chris Craik | 908cd99 | 2019-04-03 10:15:52 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 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 | |
| 36 | report = "" |
| 37 | |
| 38 | ## Check a line for any invalid suppressions, and add to 'report' if found |
| 39 | def checkLine(filename, i, line, lines): |
| 40 | global report |
| 41 | for bad, good in MATCHERS.iteritems(): |
| 42 | if bad in line: |
| 43 | context = "".join(lines[i:i+3]) |
| 44 | report += "\n{}:{}:\nError: unsupported comment suppression\n{}Instead, use: {}\n".format( |
| 45 | filename, i, context, good) |
| 46 | |
| 47 | def main(): |
| 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 | |
| 55 | with open(filename, "r") as f: |
| 56 | lines = f.readlines() |
| 57 | |
| 58 | linetuples = map(lambda i: [i, lines[i]], range(len(lines))) |
| 59 | for i, line in linetuples: |
| 60 | if ROOT_MATCH in line: |
| 61 | checkLine(filename, i, line, lines) |
| 62 | |
| 63 | if not report: |
| 64 | sys.exit(0) |
| 65 | |
| 66 | print "Invalid, IDEA-specific warning suppression found. These cause warnings during compilation." |
| 67 | print report |
| 68 | sys.exit(1) |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | main() |