1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-23 04:52:54 +02:00
llvm-mirror/test/TableGen/JSON-check.py

52 lines
1.4 KiB
Python
Raw Normal View History

[TableGen] Add a general-purpose JSON backend. The aim of this backend is to output everything TableGen knows about the record set, similarly to the default -print-records backend. But where -print-records produces output in TableGen's input syntax (convenient for humans to read), this backend produces it as structured JSON data, which is convenient for loading into standard scripting languages such as Python, in order to extract information from the data set in an automated way. The output data contains a JSON representation of the variable definitions in output 'def' records, and a few pieces of metadata such as which of those definitions are tagged with the 'field' prefix and which defs are derived from which classes. It doesn't dump out absolutely every piece of knowledge it _could_ produce, such as type information and complicated arithmetic operator nodes in abstract superclasses; the main aim is to allow consumers of this JSON dump to essentially act as new backends, and backends don't generally need to depend on that kind of data. The new backend is implemented as an EmitJSON() function similar to all of llvm-tblgen's other EmitFoo functions, except that it lives in lib/TableGen instead of utils/TableGen on the basis that I'm expecting to add it to clang-tblgen too in a future patch. To test it, I've written a Python script that loads the JSON output and tests properties of it based on comments in the .td source - more or less like FileCheck, except that the CHECK: lines have Python expressions after them instead of textual pattern matches. Reviewers: nhaehnle Reviewed By: nhaehnle Subscribers: arichardson, labath, mgorny, llvm-commits Differential Revision: https://reviews.llvm.org/D46054 llvm-svn: 336771
2018-07-11 10:40:19 +02:00
#!/usr/bin/env python
import sys
import subprocess
import traceback
import json
data = json.load(sys.stdin)
testfile = sys.argv[1]
prefix = "CHECK: "
fails = 0
passes = 0
with open(testfile) as testfh:
lineno = 0
for line in iter(testfh.readline, ""):
lineno += 1
line = line.rstrip("\r\n")
try:
prefix_pos = line.index(prefix)
except ValueError:
continue
check_expr = line[prefix_pos + len(prefix):]
try:
exception = None
result = eval(check_expr, {"data":data})
except Exception:
result = False
exception = traceback.format_exc().splitlines()[-1]
if exception is not None:
sys.stderr.write(
"{file}:{line:d}: check threw exception: {expr}\n"
"{file}:{line:d}: exception was: {exception}\n".format(
file=testfile, line=lineno,
expr=check_expr, exception=exception))
fails += 1
elif not result:
sys.stderr.write(
"{file}:{line:d}: check returned False: {expr}\n".format(
file=testfile, line=lineno, expr=check_expr))
fails += 1
else:
passes += 1
if fails != 0:
sys.exit("{} checks failed".format(fails))
else:
sys.stdout.write("{} checks passed\n".format(passes))