blob: bc5e17da6a5a95cbf32e0671455e1aec452eea8e [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,
39 help="Run the tests for the specified target architecture.")
David Brazdil2c27f2c2015-05-12 18:06:38 +010040 parser.add_argument("-q", "--quiet", action="store_true",
41 help="print only errors")
42 return parser.parse_args()
43
44
45def ListPasses(outputFilename):
46 c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r"))
47 for compiler_pass in c1File.passes:
48 Logger.log(compiler_pass.name)
49
50
51def DumpPass(outputFilename, passName):
52 c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r"))
53 compiler_pass = c1File.findPass(passName)
54 if compiler_pass:
55 maxLineNo = compiler_pass.startLineNo + len(compiler_pass.body)
56 lenLineNo = len(str(maxLineNo)) + 2
57 curLineNo = compiler_pass.startLineNo
58 for line in compiler_pass.body:
59 Logger.log((str(curLineNo) + ":").ljust(lenLineNo) + line)
60 curLineNo += 1
61 else:
62 Logger.fail("Pass \"" + passName + "\" not found in the output")
63
64
65def FindCheckerFiles(path):
66 """ Returns a list of files to scan for check annotations in the given path.
67 Path to a file is returned as a single-element list, directories are
Roland Levillain74e1cc02015-07-22 13:37:27 +010068 recursively traversed and all '.java' and '.smali' files returned.
David Brazdil2c27f2c2015-05-12 18:06:38 +010069 """
70 if not path:
71 Logger.fail("No source path provided")
72 elif os.path.isfile(path):
73 return [ path ]
74 elif os.path.isdir(path):
75 foundFiles = []
76 for root, dirs, files in os.walk(path):
77 for file in files:
78 extension = os.path.splitext(file)[1]
79 if extension in [".java", ".smali"]:
80 foundFiles.append(os.path.join(root, file))
81 return foundFiles
82 else:
83 Logger.fail("Source path \"" + path + "\" not found")
84
85
Alexandre Rames5e2c8d32015-08-06 14:49:28 +010086def RunTests(checkPrefix, checkPath, outputFilename, targetArch):
David Brazdil2c27f2c2015-05-12 18:06:38 +010087 c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r"))
88 for checkFilename in FindCheckerFiles(checkPath):
89 checkerFile = ParseCheckerStream(os.path.basename(checkFilename),
90 checkPrefix,
91 open(checkFilename, "r"))
Alexandre Rames5e2c8d32015-08-06 14:49:28 +010092 MatchFiles(checkerFile, c1File, targetArch)
David Brazdil2c27f2c2015-05-12 18:06:38 +010093
94
95if __name__ == "__main__":
96 args = ParseArguments()
97
98 if args.quiet:
99 Logger.Verbosity = Logger.Level.Error
100
101 if args.list_passes:
102 ListPasses(args.tested_file)
103 elif args.dump_pass:
104 DumpPass(args.tested_file, args.dump_pass)
105 else:
Alexandre Rames5e2c8d32015-08-06 14:49:28 +0100106 RunTests(args.check_prefix, args.source_path, args.tested_file, args.arch)