• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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
20from common.archs                     import archs_list
21from 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")
38  parser.add_argument("--arch", dest="arch", choices=archs_list,
39                      help="Run tests for the specified target architecture.")
40  parser.add_argument("--debuggable", action="store_true",
41                      help="Run tests for debuggable code.")
42  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
70      recursively traversed and all '.java' and '.smali' files returned.
71  """
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
88def RunTests(checkPrefix, checkPath, outputFilename, targetArch, debuggableMode):
89  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"))
94    MatchFiles(checkerFile, c1File, targetArch, debuggableMode)
95
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:
108    RunTests(args.check_prefix, args.source_path, args.tested_file, args.arch, args.debuggable)
109