blob: 2e9faba9fb1127637a0826b6b96a6c572567adb0 [file] [log] [blame]
David Brazdil2c27f2c2015-05-12 18:06:38 +01001#!/usr/bin/env python2
2#
3# Copyright (C) 2014 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import argparse
18import os
19
Alexandre Rames5e2c8d32015-08-06 14:49:28 +010020from common.archs import archs_list
David Brazdil2c27f2c2015-05-12 18:06:38 +010021from common.logger import Logger
22from file_format.c1visualizer.parser import ParseC1visualizerStream
23from file_format.checker.parser import ParseCheckerStream
24from match.file import MatchFiles
25
26def ParseArguments():
27 parser = argparse.ArgumentParser()
28 parser.add_argument("tested_file",
29 help="text file the checks should be verified against")
30 parser.add_argument("source_path", nargs="?",
31 help="path to file/folder with checking annotations")
32 parser.add_argument("--check-prefix", dest="check_prefix", default="CHECK", metavar="PREFIX",
33 help="prefix of checks in the test files (default: CHECK)")
34 parser.add_argument("--list-passes", dest="list_passes", action="store_true",
35 help="print a list of all passes found in the tested file")
36 parser.add_argument("--dump-pass", dest="dump_pass", metavar="PASS",
37 help="print a compiler pass dump")
Alexandre Rames5e2c8d32015-08-06 14:49:28 +010038 parser.add_argument("--arch", dest="arch", choices=archs_list,
David Brazdil5cc343d2015-10-08 11:35:32 +010039 help="Run tests for the specified target architecture.")
40 parser.add_argument("--debuggable", action="store_true",
41 help="Run tests for debuggable code.")
David Brazdil2c27f2c2015-05-12 18:06:38 +010042 parser.add_argument("-q", "--quiet", action="store_true",
43 help="print only errors")
44 return parser.parse_args()
45
46
47def ListPasses(outputFilename):
48 c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r"))
49 for compiler_pass in c1File.passes:
50 Logger.log(compiler_pass.name)
51
52
53def DumpPass(outputFilename, passName):
54 c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r"))
55 compiler_pass = c1File.findPass(passName)
56 if compiler_pass:
57 maxLineNo = compiler_pass.startLineNo + len(compiler_pass.body)
58 lenLineNo = len(str(maxLineNo)) + 2
59 curLineNo = compiler_pass.startLineNo
60 for line in compiler_pass.body:
61 Logger.log((str(curLineNo) + ":").ljust(lenLineNo) + line)
62 curLineNo += 1
63 else:
64 Logger.fail("Pass \"" + passName + "\" not found in the output")
65
66
67def FindCheckerFiles(path):
68 """ Returns a list of files to scan for check annotations in the given path.
69 Path to a file is returned as a single-element list, directories are
Roland Levillain74e1cc02015-07-22 13:37:27 +010070 recursively traversed and all '.java' and '.smali' files returned.
David Brazdil2c27f2c2015-05-12 18:06:38 +010071 """
72 if not path:
73 Logger.fail("No source path provided")
74 elif os.path.isfile(path):
75 return [ path ]
76 elif os.path.isdir(path):
77 foundFiles = []
78 for root, dirs, files in os.walk(path):
79 for file in files:
80 extension = os.path.splitext(file)[1]
81 if extension in [".java", ".smali"]:
82 foundFiles.append(os.path.join(root, file))
83 return foundFiles
84 else:
85 Logger.fail("Source path \"" + path + "\" not found")
86
87
David Brazdil5cc343d2015-10-08 11:35:32 +010088def RunTests(checkPrefix, checkPath, outputFilename, targetArch, debuggableMode):
David Brazdil2c27f2c2015-05-12 18:06:38 +010089 c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r"))
90 for checkFilename in FindCheckerFiles(checkPath):
91 checkerFile = ParseCheckerStream(os.path.basename(checkFilename),
92 checkPrefix,
93 open(checkFilename, "r"))
David Brazdil5cc343d2015-10-08 11:35:32 +010094 MatchFiles(checkerFile, c1File, targetArch, debuggableMode)
David Brazdil2c27f2c2015-05-12 18:06:38 +010095
96
97if __name__ == "__main__":
98 args = ParseArguments()
99
100 if args.quiet:
101 Logger.Verbosity = Logger.Level.Error
102
103 if args.list_passes:
104 ListPasses(args.tested_file)
105 elif args.dump_pass:
106 DumpPass(args.tested_file, args.dump_pass)
107 else:
David Brazdil5cc343d2015-10-08 11:35:32 +0100108 RunTests(args.check_prefix, args.source_path, args.tested_file, args.arch, args.debuggable)