qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Benoît Canet" <benoit.canet@irqsave.net>
To: qemu-devel@nongnu.org
Cc: "Benoît Canet" <benoit.canet@irqsave.net>,
	"Benoit Canet" <benoit@irqsave.net>,
	armbru@redhat.com, lcapitulino@redhat.com, vilanova@ac.upc.edu
Subject: [Qemu-devel] [PATCH V2] qapi: Let redundant includes be skipped excepted the first occurrence.
Date: Fri,  9 May 2014 17:48:22 +0200	[thread overview]
Message-ID: <1399650502-25530-2-git-send-email-benoit.canet@irqsave.net> (raw)
In-Reply-To: <1399650502-25530-1-git-send-email-benoit.canet@irqsave.net>

The purpose of this change is to help create a json file containing
common definitions; each bit of generated C code must be spitted
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 <benoit@irqsave.net>
---
 scripts/qapi.py                                  |   14 +++++++++++---
 tests/Makefile                                   |    3 ++-
 tests/qapi-schema/include-only-one-time.exit     |    1 +
 tests/qapi-schema/include-only-one-time.json     |    3 +++
 tests/qapi-schema/include-only-one-time.out      |    3 +++
 tests/qapi-schema/sub-include-only-one-time.json |    2 ++
 6 files changed, 22 insertions(+), 4 deletions(-)
 create mode 100644 tests/qapi-schema/include-only-one-time.err
 create mode 100644 tests/qapi-schema/include-only-one-time.exit
 create mode 100644 tests/qapi-schema/include-only-one-time.json
 create mode 100644 tests/qapi-schema/include-only-one-time.out
 create mode 100644 tests/qapi-schema/sub-include-only-one-time.json

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 a8d45be..a0f12fb 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -182,7 +182,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-only-one-time.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-only-one-time.err b/tests/qapi-schema/include-only-one-time.err
new file mode 100644
index 0000000..e69de29
diff --git a/tests/qapi-schema/include-only-one-time.exit b/tests/qapi-schema/include-only-one-time.exit
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/tests/qapi-schema/include-only-one-time.exit
@@ -0,0 +1 @@
+0
diff --git a/tests/qapi-schema/include-only-one-time.json b/tests/qapi-schema/include-only-one-time.json
new file mode 100644
index 0000000..11772e3
--- /dev/null
+++ b/tests/qapi-schema/include-only-one-time.json
@@ -0,0 +1,3 @@
+{ 'include': 'comments.json' }
+{ 'include': 'sub-include-only-one-time.json' }
+{ 'include': 'comments.json' }
diff --git a/tests/qapi-schema/include-only-one-time.out b/tests/qapi-schema/include-only-one-time.out
new file mode 100644
index 0000000..4ce3dcf
--- /dev/null
+++ b/tests/qapi-schema/include-only-one-time.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/sub-include-only-one-time.json b/tests/qapi-schema/sub-include-only-one-time.json
new file mode 100644
index 0000000..6bfffdf
--- /dev/null
+++ b/tests/qapi-schema/sub-include-only-one-time.json
@@ -0,0 +1,2 @@
+{ 'include': 'comments.json' }
+{ 'include': 'comments.json' }
-- 
1.7.10.4

  reply	other threads:[~2014-05-09 15:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-09 15:48 [Qemu-devel] [PATCH V2] Let the C generated bits of QAPI be generated only once when identical includes are done Benoît Canet
2014-05-09 15:48 ` Benoît Canet [this message]
2014-05-12 17:11   ` [Qemu-devel] [PATCH V2] qapi: Let redundant includes be skipped excepted the first occurrence Eric Blake
2014-05-14 14:04     ` Benoît Canet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1399650502-25530-2-git-send-email-benoit.canet@irqsave.net \
    --to=benoit.canet@irqsave.net \
    --cc=armbru@redhat.com \
    --cc=benoit@irqsave.net \
    --cc=lcapitulino@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=vilanova@ac.upc.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).