[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 20/20] qapi: skip redundant includes
From: |
Luiz Capitulino |
Subject: |
[Qemu-devel] [PULL 20/20] qapi: skip redundant includes |
Date: |
Fri, 16 May 2014 11:30:35 -0400 |
From: BenoƮt Canet <address@hidden>
The purpose of this change is to help create a json file containing
common definitions; each bit of generated C code must be emitted
only one time.
A second history global to all QAPISchema instances has been added
to detect when a file is included more than one time and skip these
includes.
It does not act as a stack and the changes made to it by the
__init__ function are propagated back to the caller so it's really
a global state.
Signed-off-by: Benoit Canet <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
Signed-off-by: Luiz Capitulino <address@hidden>
---
docs/qapi-code-gen.txt | 2 +-
scripts/qapi.py | 14 +++++++++++---
tests/Makefile | 3 ++-
tests/qapi-schema/include-repetition-sub.json | 2 ++
tests/qapi-schema/include-repetition.err | 0
tests/qapi-schema/include-repetition.exit | 1 +
tests/qapi-schema/include-repetition.json | 3 +++
tests/qapi-schema/include-repetition.out | 3 +++
8 files changed, 23 insertions(+), 5 deletions(-)
create mode 100644 tests/qapi-schema/include-repetition-sub.json
create mode 100644 tests/qapi-schema/include-repetition.err
create mode 100644 tests/qapi-schema/include-repetition.exit
create mode 100644 tests/qapi-schema/include-repetition.json
create mode 100644 tests/qapi-schema/include-repetition.out
diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt
index 867d124..dea0d50 100644
--- a/docs/qapi-code-gen.txt
+++ b/docs/qapi-code-gen.txt
@@ -48,7 +48,7 @@ The QAPI schema definitions can be modularized using the
'include' directive:
{ 'include': 'path/to/file.json'}
The directive is evaluated recursively, and include paths are relative to the
-file using the directive.
+file using the directive. Multiple includes of the same file are safe.
=== Complex types ===
diff --git a/scripts/qapi.py b/scripts/qapi.py
index ec806aa..0265b40 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -73,13 +73,18 @@ class QAPIExprError(Exception):
class QAPISchema:
- def __init__(self, fp, input_relname=None, include_hist=[],
parent_info=None):
+ def __init__(self, fp, input_relname=None, include_hist=[],
+ previously_included=[], parent_info=None):
+ """ include_hist is a stack used to detect inclusion cycles
+ previously_included is a global state used to avoid multiple
+ inclusions of the same file"""
input_fname = os.path.abspath(fp.name)
if input_relname is None:
input_relname = fp.name
self.input_dir = os.path.dirname(input_fname)
self.input_file = input_relname
self.include_hist = include_hist + [(input_relname, input_fname)]
+ previously_included.append(input_fname)
self.parent_info = parent_info
self.src = fp.read()
if self.src == '' or self.src[-1] != '\n':
@@ -106,13 +111,16 @@ class QAPISchema:
for elem in self.include_hist):
raise QAPIExprError(expr_info, "Inclusion loop for %s"
% include)
+ # skip multiple include of the same file
+ if include_path in previously_included:
+ continue
try:
fobj = open(include_path, 'r')
except IOError as e:
raise QAPIExprError(expr_info,
'%s: %s' % (e.strerror, include))
- exprs_include = QAPISchema(fobj, include,
- self.include_hist, expr_info)
+ exprs_include = QAPISchema(fobj, include, self.include_hist,
+ previously_included, expr_info)
self.exprs.extend(exprs_include.exprs)
else:
expr_elem = {'expr': expr,
diff --git a/tests/Makefile b/tests/Makefile
index 6b8b6f2..9f7ca61 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -193,7 +193,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
flat-union-string-discriminator.json \
include-simple.json include-relpath.json include-format-err.json \
include-non-file.json include-no-file.json include-before-err.json \
- include-nested-err.json include-self-cycle.json include-cycle.json)
+ include-nested-err.json include-self-cycle.json include-cycle.json \
+ include-repetition.json)
GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h
tests/test-qmp-commands.h
diff --git a/tests/qapi-schema/include-repetition-sub.json
b/tests/qapi-schema/include-repetition-sub.json
new file mode 100644
index 0000000..6bfffdf
--- /dev/null
+++ b/tests/qapi-schema/include-repetition-sub.json
@@ -0,0 +1,2 @@
+{ 'include': 'comments.json' }
+{ 'include': 'comments.json' }
diff --git a/tests/qapi-schema/include-repetition.err
b/tests/qapi-schema/include-repetition.err
new file mode 100644
index 0000000..e69de29
diff --git a/tests/qapi-schema/include-repetition.exit
b/tests/qapi-schema/include-repetition.exit
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/tests/qapi-schema/include-repetition.exit
@@ -0,0 +1 @@
+0
diff --git a/tests/qapi-schema/include-repetition.json
b/tests/qapi-schema/include-repetition.json
new file mode 100644
index 0000000..ec329dd
--- /dev/null
+++ b/tests/qapi-schema/include-repetition.json
@@ -0,0 +1,3 @@
+{ 'include': 'comments.json' }
+{ 'include': 'include-repetition-sub.json' }
+{ 'include': 'comments.json' }
diff --git a/tests/qapi-schema/include-repetition.out
b/tests/qapi-schema/include-repetition.out
new file mode 100644
index 0000000..4ce3dcf
--- /dev/null
+++ b/tests/qapi-schema/include-repetition.out
@@ -0,0 +1,3 @@
+[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
+[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}]
+[]
--
1.9.0
- [Qemu-devel] [PULL 00/20] QMP queue, Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 01/20] qapi: Update qapi-code-gen.txt example to match current code, Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 14/20] monitor: Convert sendkey to use command_completion., Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 13/20] qapi: Show qapi-commands.py invocation in qapi-code-gen.txt, Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 20/20] qapi: skip redundant includes,
Luiz Capitulino <=
- [Qemu-devel] [PULL 11/20] tests: Don't call visit_end_struct() after visit_start_struct() fails, Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 08/20] qapi: Un-inline visit of implicit struct, Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 09/20] hmp: Call visit_end_struct() after visit_start_struct() succeeds, Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 17/20] monitor: Add set_link arguments completion., Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 16/20] monitor: Add chardev-add backend argument completion., Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 15/20] monitor: Add chardev-remove command completion., Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 19/20] monitor: Add netdev_del id argument completion., Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 06/20] qapi: Clean up shadowing of parameters and locals in inner scopes, Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 18/20] monitor: Add netdev_add type argument completion., Luiz Capitulino, 2014/05/16
- [Qemu-devel] [PULL 12/20] qapi: Replace uncommon use of the error API by the common one, Luiz Capitulino, 2014/05/16