Received: from eggs.gnu.org ([2001:4830:134:3::10]:51333) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WUzPE-0000PJ-Re for qemu-devel@nongnu.org; Tue, 01 Apr 2014 10:11:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WUzP9-0000an-0P for qemu-devel@nongnu.org; Tue, 01 Apr 2014 10:11:12 -0400 Received: from roura.ac.upc.es ([147.83.33.10]:39630) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WUzP8-0000Za-BL for qemu-devel@nongnu.org; Tue, 01 Apr 2014 10:11:06 -0400 From: =?utf-8?b?TGx1w61z?= Vilanova Date: Tue, 1 Apr 2014 16:11:04 +0200 Message-Id: <20140401141104.15554.33382.stgit@fimbulvetr.bsc.es> In-Reply-To: <20140401141041.15554.76350.stgit@fimbulvetr.bsc.es> References: <20140401141041.15554.76350.stgit@fimbulvetr.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v7 4/4] qapi: Add a primitive to include other files from a QAPI schema file List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Beno=C3=AEt=20Canet?= , Markus Armbruster , Luiz Capitulino Signed-off-by: Llu=C3=ADs Vilanova --- docs/qapi-code-gen.txt | 11 +++++ scripts/qapi.py | 59 ++++++++++++++++++++++= +----- tests/Makefile | 5 ++ tests/qapi-schema/include-after-err.err | 1=20 tests/qapi-schema/include-after-err.exit | 1=20 tests/qapi-schema/include-after-err.json | 2 + tests/qapi-schema/include-after-err.out | 0=20 tests/qapi-schema/include-cycle-b.json | 1=20 tests/qapi-schema/include-cycle-c.json | 1=20 tests/qapi-schema/include-cycle.err | 3 + tests/qapi-schema/include-cycle.exit | 1=20 tests/qapi-schema/include-cycle.json | 1=20 tests/qapi-schema/include-cycle.out | 0=20 tests/qapi-schema/include-format-err.err | 1=20 tests/qapi-schema/include-format-err.exit | 1=20 tests/qapi-schema/include-format-err.json | 2 + tests/qapi-schema/include-format-err.out | 0=20 tests/qapi-schema/include-nested-err.err | 2 + tests/qapi-schema/include-nested-err.exit | 1=20 tests/qapi-schema/include-nested-err.json | 1=20 tests/qapi-schema/include-nested-err.out | 0=20 tests/qapi-schema/include-no-file.err | 1=20 tests/qapi-schema/include-no-file.exit | 1=20 tests/qapi-schema/include-no-file.json | 1=20 tests/qapi-schema/include-no-file.out | 0=20 tests/qapi-schema/include-non-file.err | 1=20 tests/qapi-schema/include-non-file.exit | 1=20 tests/qapi-schema/include-non-file.json | 1=20 tests/qapi-schema/include-non-file.out | 0=20 tests/qapi-schema/include-relpath-sub.json | 2 + tests/qapi-schema/include-relpath.err | 0=20 tests/qapi-schema/include-relpath.exit | 1=20 tests/qapi-schema/include-relpath.json | 1=20 tests/qapi-schema/include-relpath.out | 3 + tests/qapi-schema/include-self-cycle.err | 1=20 tests/qapi-schema/include-self-cycle.exit | 1=20 tests/qapi-schema/include-self-cycle.json | 1=20 tests/qapi-schema/include-self-cycle.out | 0=20 tests/qapi-schema/include-simple-sub.json | 2 + tests/qapi-schema/include-simple.err | 0=20 tests/qapi-schema/include-simple.exit | 1=20 tests/qapi-schema/include-simple.json | 1=20 tests/qapi-schema/include-simple.out | 3 + tests/qapi-schema/include/relpath.json | 1=20 44 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 tests/qapi-schema/include-after-err.err create mode 100644 tests/qapi-schema/include-after-err.exit create mode 100644 tests/qapi-schema/include-after-err.json create mode 100644 tests/qapi-schema/include-after-err.out create mode 100644 tests/qapi-schema/include-cycle-b.json create mode 100644 tests/qapi-schema/include-cycle-c.json create mode 100644 tests/qapi-schema/include-cycle.err create mode 100644 tests/qapi-schema/include-cycle.exit create mode 100644 tests/qapi-schema/include-cycle.json create mode 100644 tests/qapi-schema/include-cycle.out create mode 100644 tests/qapi-schema/include-format-err.err create mode 100644 tests/qapi-schema/include-format-err.exit create mode 100644 tests/qapi-schema/include-format-err.json create mode 100644 tests/qapi-schema/include-format-err.out create mode 100644 tests/qapi-schema/include-nested-err.err create mode 100644 tests/qapi-schema/include-nested-err.exit create mode 100644 tests/qapi-schema/include-nested-err.json create mode 100644 tests/qapi-schema/include-nested-err.out create mode 100644 tests/qapi-schema/include-no-file.err create mode 100644 tests/qapi-schema/include-no-file.exit create mode 100644 tests/qapi-schema/include-no-file.json create mode 100644 tests/qapi-schema/include-no-file.out create mode 100644 tests/qapi-schema/include-non-file.err create mode 100644 tests/qapi-schema/include-non-file.exit create mode 100644 tests/qapi-schema/include-non-file.json create mode 100644 tests/qapi-schema/include-non-file.out create mode 100644 tests/qapi-schema/include-relpath-sub.json create mode 100644 tests/qapi-schema/include-relpath.err create mode 100644 tests/qapi-schema/include-relpath.exit create mode 100644 tests/qapi-schema/include-relpath.json create mode 100644 tests/qapi-schema/include-relpath.out create mode 100644 tests/qapi-schema/include-self-cycle.err create mode 100644 tests/qapi-schema/include-self-cycle.exit create mode 100644 tests/qapi-schema/include-self-cycle.json create mode 100644 tests/qapi-schema/include-self-cycle.out create mode 100644 tests/qapi-schema/include-simple-sub.json create mode 100644 tests/qapi-schema/include-simple.err create mode 100644 tests/qapi-schema/include-simple.exit create mode 100644 tests/qapi-schema/include-simple.json create mode 100644 tests/qapi-schema/include-simple.out create mode 100644 tests/qapi-schema/include/relpath.json diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt index 63b03cf..051d109 100644 --- a/docs/qapi-code-gen.txt +++ b/docs/qapi-code-gen.txt @@ -40,6 +40,17 @@ enumeration types and union types. Generally speaking, types definitions should always use CamelCase for th= e type names. Command names should be all lower case with words separated by a = hyphen. =20 + +=3D=3D=3D Includes =3D=3D=3D + +The QAPI schema definitions can be modularized using the 'include' direc= tive: + + { 'include': 'path/to/file.json'} + +The directive is evaluated recursively, and include paths are relative t= o the +file using the directive. + + =3D=3D=3D Complex types =3D=3D=3D =20 A complex type is a dictionary containing a single key whose value is a diff --git a/scripts/qapi.py b/scripts/qapi.py index 3a38e27..2585792 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -11,6 +11,8 @@ # This work is licensed under the terms of the GNU GPL, version 2. # See the COPYING file in the top-level directory. =20 +import os +import re from ordereddict import OrderedDict import os import sys @@ -36,6 +38,14 @@ builtin_type_qtypes =3D { 'uint64': 'QTYPE_QINT', } =20 +def error_path(parent): + res =3D "" + while parent: + res =3D ("In file included from %s:%d:\n" % (parent['fp'].name, + parent['line'])) + re= s + parent =3D parent['parent'] + return res + class QAPISchemaError(Exception): def __init__(self, schema, msg): self.fp =3D schema.fp @@ -47,23 +57,29 @@ class QAPISchemaError(Exception): self.col =3D (self.col + 7) % 8 + 1 else: self.col +=3D 1 + self.info =3D schema.parent_info =20 def __str__(self): - return "%s:%s:%s: %s" % (self.fp.name, self.line, self.col, self= .msg) + return error_path(self.info) + \ + "%s:%d:%d: %s" % (self.fp.name, self.line, self.col, self.ms= g) =20 class QAPIExprError(Exception): def __init__(self, expr_info, msg): - self.fp =3D expr_info['fp'] - self.line =3D expr_info['line'] + self.info =3D expr_info self.msg =3D msg =20 def __str__(self): - return "%s:%s: %s" % (self.fp.name, self.line, self.msg) + return error_path(self.info['parent']) + \ + "%s:%d: %s" % (self.info['fp'].name, self.info['line'], self= .msg) =20 class QAPISchema: =20 - def __init__(self, fp): + def __init__(self, fp, included=3D[], parent_info=3DNone): self.fp =3D fp + input_path =3D os.path.abspath(fp.name) + self.included =3D included + [input_path] + self.input_dir =3D os.path.dirname(input_path) + self.parent_info =3D parent_info self.src =3D fp.read() if self.src =3D=3D '' or self.src[-1] !=3D '\n': self.src +=3D '\n' @@ -74,10 +90,33 @@ class QAPISchema: self.accept() =20 while self.tok !=3D None: - expr_info =3D {'fp': fp, 'line': self.line} - expr_elem =3D {'expr': self.get_expr(False), - 'info': expr_info} - self.exprs.append(expr_elem) + expr_info =3D {'fp': fp, 'line': self.line, 'parent': self.p= arent_info} + expr =3D self.get_expr(False) + if isinstance(expr, dict) and "include" in expr: + if len(expr) !=3D 1: + raise QAPIExprError(expr_info, "Invalid 'include' di= rective") + include_path =3D expr["include"] + if not isinstance(include_path, str): + raise QAPIExprError(expr_info, + 'Expected a file name (string), = got: %s' + % include_path) + if not os.path.isabs(include_path): + include_path =3D os.path.join(self.input_dir, includ= e_path) + if not os.path.isfile(include_path): + raise QAPIExprError( + expr_info, + 'Non-existing include file "%s"' % + include_path) + if include_path in self.included: + raise QAPIExprError(expr_info, "Inclusion loop for %= s" + % include_path) + exprs_include =3D QAPISchema(open(include_path, "r"), + self.included, expr_info) + self.exprs.extend(exprs_include.exprs) + else: + expr_elem =3D {'expr': expr, + 'info': expr_info} + self.exprs.append(expr_elem) =20 def accept(self): while True: @@ -267,7 +306,7 @@ def check_exprs(schema): def parse_schema(input_path): try: schema =3D QAPISchema(open(input_path, "r")) - except QAPISchemaError, e: + except (QAPISchemaError, QAPIExprError), e: print >>sys.stderr, e exit(1) =20 diff --git a/tests/Makefile b/tests/Makefile index 5dc2606..f6807ac 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -164,7 +164,10 @@ check-qapi-schema-y :=3D $(addprefix tests/qapi-sche= ma/, \ duplicate-key.json union-invalid-base.json flat-union-no-base.js= on \ flat-union-invalid-discriminator.json \ flat-union-invalid-branch-key.json flat-union-reverse-define.jso= n \ - flat-union-string-discriminator.json) + flat-union-string-discriminator.json \ + include-simple.json include-relpath.json include-format-err.json= \ + include-non-file.json include-no-file.json include-after-err.jso= n \ + include-nested-err.json include-self-cycle.json include-cycle.js= on) =20 GENERATED_HEADERS +=3D tests/test-qapi-types.h tests/test-qapi-visit.h t= ests/test-qmp-commands.h =20 diff --git a/tests/qapi-schema/include-after-err.err b/tests/qapi-schema/= include-after-err.err new file mode 100644 index 0000000..eaf632a --- /dev/null +++ b/tests/qapi-schema/include-after-err.err @@ -0,0 +1 @@ +tests/qapi-schema/include-after-err.json:2:13: Expected ":" diff --git a/tests/qapi-schema/include-after-err.exit b/tests/qapi-schema= /include-after-err.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/qapi-schema/include-after-err.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/include-after-err.json b/tests/qapi-schema= /include-after-err.json new file mode 100644 index 0000000..afb6cb6 --- /dev/null +++ b/tests/qapi-schema/include-after-err.json @@ -0,0 +1,2 @@ +{ 'include': 'include-simple-sub.json' } +{ 'command' 'missing-colon' } diff --git a/tests/qapi-schema/include-after-err.out b/tests/qapi-schema/= include-after-err.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/qapi-schema/include-cycle-b.json b/tests/qapi-schema/i= nclude-cycle-b.json new file mode 100644 index 0000000..4fa985d --- /dev/null +++ b/tests/qapi-schema/include-cycle-b.json @@ -0,0 +1 @@ +{ 'include': 'include-cycle-c.json' } diff --git a/tests/qapi-schema/include-cycle-c.json b/tests/qapi-schema/i= nclude-cycle-c.json new file mode 100644 index 0000000..d12b592 --- /dev/null +++ b/tests/qapi-schema/include-cycle-c.json @@ -0,0 +1 @@ +{ 'include': 'include-cycle.json' } diff --git a/tests/qapi-schema/include-cycle.err b/tests/qapi-schema/incl= ude-cycle.err new file mode 100644 index 0000000..1f7cc09 --- /dev/null +++ b/tests/qapi-schema/include-cycle.err @@ -0,0 +1,3 @@ +In file included from tests/qapi-schema/include-cycle.json:1: +In file included from tests/qapi-schema/include-cycle-b.json:1: +tests/qapi-schema/include-cycle-c.json:1: Inclusion loop for tests/qapi-= schema/include-cycle.json diff --git a/tests/qapi-schema/include-cycle.exit b/tests/qapi-schema/inc= lude-cycle.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/qapi-schema/include-cycle.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/include-cycle.json b/tests/qapi-schema/inc= lude-cycle.json new file mode 100644 index 0000000..6fcf1eb --- /dev/null +++ b/tests/qapi-schema/include-cycle.json @@ -0,0 +1 @@ +{ 'include': 'include-cycle-b.json' } diff --git a/tests/qapi-schema/include-cycle.out b/tests/qapi-schema/incl= ude-cycle.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/qapi-schema/include-format-err.err b/tests/qapi-schema= /include-format-err.err new file mode 100644 index 0000000..721ff4e --- /dev/null +++ b/tests/qapi-schema/include-format-err.err @@ -0,0 +1 @@ +tests/qapi-schema/include-format-err.json:1: Invalid 'include' directive diff --git a/tests/qapi-schema/include-format-err.exit b/tests/qapi-schem= a/include-format-err.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/qapi-schema/include-format-err.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/include-format-err.json b/tests/qapi-schem= a/include-format-err.json new file mode 100644 index 0000000..44980f0 --- /dev/null +++ b/tests/qapi-schema/include-format-err.json @@ -0,0 +1,2 @@ +{ 'include': 'include-simple-sub.json', + 'foo': 'bar' } diff --git a/tests/qapi-schema/include-format-err.out b/tests/qapi-schema= /include-format-err.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/qapi-schema/include-nested-err.err b/tests/qapi-schema= /include-nested-err.err new file mode 100644 index 0000000..1b7b227 --- /dev/null +++ b/tests/qapi-schema/include-nested-err.err @@ -0,0 +1,2 @@ +In file included from tests/qapi-schema/include-nested-err.json:1: +tests/qapi-schema/missing-colon.json:1:10: Expected ":" diff --git a/tests/qapi-schema/include-nested-err.exit b/tests/qapi-schem= a/include-nested-err.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/qapi-schema/include-nested-err.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/include-nested-err.json b/tests/qapi-schem= a/include-nested-err.json new file mode 100644 index 0000000..5631e56 --- /dev/null +++ b/tests/qapi-schema/include-nested-err.json @@ -0,0 +1 @@ +{ 'include': 'missing-colon.json' } diff --git a/tests/qapi-schema/include-nested-err.out b/tests/qapi-schema= /include-nested-err.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/qapi-schema/include-no-file.err b/tests/qapi-schema/in= clude-no-file.err new file mode 100644 index 0000000..0e70e3b --- /dev/null +++ b/tests/qapi-schema/include-no-file.err @@ -0,0 +1 @@ +tests/qapi-schema/include-no-file.json:1: Non-existing include file "tes= ts/qapi-schema/include-no-file-sub.json" diff --git a/tests/qapi-schema/include-no-file.exit b/tests/qapi-schema/i= nclude-no-file.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/qapi-schema/include-no-file.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/include-no-file.json b/tests/qapi-schema/i= nclude-no-file.json new file mode 100644 index 0000000..9249ebd --- /dev/null +++ b/tests/qapi-schema/include-no-file.json @@ -0,0 +1 @@ +{ 'include': 'include-no-file-sub.json' } diff --git a/tests/qapi-schema/include-no-file.out b/tests/qapi-schema/in= clude-no-file.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/qapi-schema/include-non-file.err b/tests/qapi-schema/i= nclude-non-file.err new file mode 100644 index 0000000..9658c78 --- /dev/null +++ b/tests/qapi-schema/include-non-file.err @@ -0,0 +1 @@ +tests/qapi-schema/include-non-file.json:1: Expected a file name (string)= , got: ['foo', 'bar'] diff --git a/tests/qapi-schema/include-non-file.exit b/tests/qapi-schema/= include-non-file.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/qapi-schema/include-non-file.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/include-non-file.json b/tests/qapi-schema/= include-non-file.json new file mode 100644 index 0000000..cd43c3f --- /dev/null +++ b/tests/qapi-schema/include-non-file.json @@ -0,0 +1 @@ +{ 'include': [ 'foo', 'bar' ] } diff --git a/tests/qapi-schema/include-non-file.out b/tests/qapi-schema/i= nclude-non-file.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/qapi-schema/include-relpath-sub.json b/tests/qapi-sche= ma/include-relpath-sub.json new file mode 100644 index 0000000..4bd4af4 --- /dev/null +++ b/tests/qapi-schema/include-relpath-sub.json @@ -0,0 +1,2 @@ +{ 'enum': 'Status', + 'data': [ 'good', 'bad', 'ugly' ] } diff --git a/tests/qapi-schema/include-relpath.err b/tests/qapi-schema/in= clude-relpath.err new file mode 100644 index 0000000..e69de29 diff --git a/tests/qapi-schema/include-relpath.exit b/tests/qapi-schema/i= nclude-relpath.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/tests/qapi-schema/include-relpath.exit @@ -0,0 +1 @@ +0 diff --git a/tests/qapi-schema/include-relpath.json b/tests/qapi-schema/i= nclude-relpath.json new file mode 100644 index 0000000..05018f3 --- /dev/null +++ b/tests/qapi-schema/include-relpath.json @@ -0,0 +1 @@ +{ 'include': 'include/relpath.json' } diff --git a/tests/qapi-schema/include-relpath.out b/tests/qapi-schema/in= clude-relpath.out new file mode 100644 index 0000000..4ce3dcf --- /dev/null +++ b/tests/qapi-schema/include-relpath.out @@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}] +[] diff --git a/tests/qapi-schema/include-self-cycle.err b/tests/qapi-schema= /include-self-cycle.err new file mode 100644 index 0000000..f4df280 --- /dev/null +++ b/tests/qapi-schema/include-self-cycle.err @@ -0,0 +1 @@ +tests/qapi-schema/include-self-cycle.json:1: Inclusion loop for tests/qa= pi-schema/include-self-cycle.json diff --git a/tests/qapi-schema/include-self-cycle.exit b/tests/qapi-schem= a/include-self-cycle.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/qapi-schema/include-self-cycle.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/include-self-cycle.json b/tests/qapi-schem= a/include-self-cycle.json new file mode 100644 index 0000000..55fb1b5 --- /dev/null +++ b/tests/qapi-schema/include-self-cycle.json @@ -0,0 +1 @@ +{ 'include': 'include-self-cycle.json' } diff --git a/tests/qapi-schema/include-self-cycle.out b/tests/qapi-schema= /include-self-cycle.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/qapi-schema/include-simple-sub.json b/tests/qapi-schem= a/include-simple-sub.json new file mode 100644 index 0000000..4bd4af4 --- /dev/null +++ b/tests/qapi-schema/include-simple-sub.json @@ -0,0 +1,2 @@ +{ 'enum': 'Status', + 'data': [ 'good', 'bad', 'ugly' ] } diff --git a/tests/qapi-schema/include-simple.err b/tests/qapi-schema/inc= lude-simple.err new file mode 100644 index 0000000..e69de29 diff --git a/tests/qapi-schema/include-simple.exit b/tests/qapi-schema/in= clude-simple.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/tests/qapi-schema/include-simple.exit @@ -0,0 +1 @@ +0 diff --git a/tests/qapi-schema/include-simple.json b/tests/qapi-schema/in= clude-simple.json new file mode 100644 index 0000000..1dd391a --- /dev/null +++ b/tests/qapi-schema/include-simple.json @@ -0,0 +1 @@ +{ 'include': 'include-simple-sub.json' } diff --git a/tests/qapi-schema/include-simple.out b/tests/qapi-schema/inc= lude-simple.out new file mode 100644 index 0000000..4ce3dcf --- /dev/null +++ b/tests/qapi-schema/include-simple.out @@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}] +[] diff --git a/tests/qapi-schema/include/relpath.json b/tests/qapi-schema/i= nclude/relpath.json new file mode 100644 index 0000000..45dee24 --- /dev/null +++ b/tests/qapi-schema/include/relpath.json @@ -0,0 +1 @@ +{ 'include': '../include-relpath-sub.json' }