Skip to contentSkip to conversation

Let the C generated bits of QAPI be generated only once when identical includes are done

PATCH Superseded Benoît Canet 2 participants 4 messages May 9, 2014 – May 14, 2014

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Let the C generated bits of QAPI be generated only once when identical includes are done

in V2:
Keep the cycle detection [Markus]
Don't talk about idempottent [Markus]

Benoît Canet (1):
qapi: Let redundant includes be skipped excepted the first
occurrence.

 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
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v2 1/1 qapi: Let redundant includes be skipped excepted the first occurrence.

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.

---
 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
scripts/qapi.py +11 -3
index ec806aa..0265b40 100644
@@ -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,
tests/Makefile +2 -1
index a8d45be..a0f12fb 100644
@@ -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
 
tests/qapi-schema/include-only-one-time.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/include-only-one-time.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/include-only-one-time.json new file +3 -0
new file mode 100644
index 0000000..11772e3
@@ -0,0 +1,3 @@
+{ 'include': 'comments.json' }
+{ 'include': 'sub-include-only-one-time.json' }
+{ 'include': 'comments.json' }
tests/qapi-schema/include-only-one-time.out new file +3 -0
new file mode 100644
index 0000000..4ce3dcf
@@ -0,0 +1,3 @@
+[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
+[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}]
+[]
tests/qapi-schema/sub-include-only-one-time.json new file +2 -0
new file mode 100644
index 0000000..6bfffdf
@@ -0,0 +1,2 @@
+{ 'include': 'comments.json' }
+{ 'include': 'comments.json' }
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [PATCH V2] qapi: Let redundant includes be skipped excepted the first occurrence.

On 05/09/2014 09:48 AM, Benoît Canet wrote:

Subject line has problems. 'excepted' is not a word, and you are already asked to avoid trailing '.'. I suggest a much simpler:

qapi: skip redundant includes

Quoted 2 lines

The purpose of this change is to help create a json file containing common definitions; each bit of generated C code must be spitted

s/spitted/emitted/

tests/qapi-schema/include-only-one-time.json

Quoted 22 lines

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.

---

index 0000000..573541a

tests/qapi-schema/include-only-one-time.exit new file +1 -0
@@ -0,0 +1 @@
+0
tests/qapi-schema/include-only-one-time.json new file +0 -0
new file mode 100644
index 0000000..11772e3

Long name; you could get by with:

tests/qapi-schema/include-repetition.json

tests/qapi-schema/include-only-one-time.json line 2

Quoted 3 lines
tests/qapi-schema/include-only-one-time.json new file +2 -0
@@ -0,0 +1,3 @@
+{ 'include': 'comments.json' }
+{ 'include': 'sub-include-only-one-time.json' }

This naming doesn't preserve the 'include-' prefix used by all other files related to include. Maybe:

{ 'include': 'include-repetition-sub.json' }

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH V2] qapi: Let redundant includes be skipped excepted the first occurrence.

Quoted 39 lines · Eric Blake

On 05/09/2014 09:48 AM, Benoît Canet wrote:

Subject line has problems. 'excepted' is not a word, and you are already asked to avoid trailing '.'. I suggest a much simpler:

qapi: skip redundant includes

Quoted 2 lines

The purpose of this change is to help create a json file containing common definitions; each bit of generated C code must be spitted

s/spitted/emitted/

tests/qapi-schema/include-only-one-time.json

Quoted 22 lines

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.

---

index 0000000..573541a

tests/qapi-schema/include-only-one-time.exit new file +1 -0
@@ -0,0 +1 @@
+0
tests/qapi-schema/include-only-one-time.json new file +0 -0
new file mode 100644
index 0000000..11772e3

Long name; you could get by with:

tests/qapi-schema/include-repetition.json

Quoted 13 lines

tests/qapi-schema/include-only-one-time.json line 2

Quoted 3 lines
tests/qapi-schema/include-only-one-time.json new file +2 -0
@@ -0,0 +1,3 @@
+{ 'include': 'comments.json' }
+{ 'include': 'sub-include-only-one-time.json' }

This naming doesn't preserve the 'include-' prefix used by all other files related to include. Maybe:

{ 'include': 'include-repetition-sub.json' }

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Thanks for the review I'll iterate.