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