Received: from eggs.gnu.org ([2001:4830:134:3::10]:40580) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ba4AM-00055m-Ox for qemu-devel@nongnu.org; Wed, 17 Aug 2016 12:58:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ba4AK-0002MX-OX for qemu-devel@nongnu.org; Wed, 17 Aug 2016 12:58:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33642) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ba4AK-0002MT-Gi for qemu-devel@nongnu.org; Wed, 17 Aug 2016 12:58:08 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0AD5885547 for ; Wed, 17 Aug 2016 16:58:08 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Wed, 17 Aug 2016 20:57:39 +0400 Message-Id: <20160817165757.23486-3-marcandre.lureau@redhat.com> In-Reply-To: <20160817165757.23486-1-marcandre.lureau@redhat.com> References: <20160817165757.23486-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v5 02/20] qapi.py: add a simple #ifdef conditional List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: eblake@redhat.com, armbru@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Learn to parse #define files provided with -f option, and skip undefined #ifdef blocks in the schema. This is a very simple pre-processing, without stacking support or evaluation (it could be implemented if needed). Signed-off-by: Marc-Andr=C3=A9 Lureau --- scripts/qapi.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/scripts/qapi.py b/scripts/qapi.py index 21bc32f..d0b8a66 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -76,6 +76,7 @@ struct_types =3D [] union_types =3D [] events =3D [] all_names =3D {} +defs =3D [] =20 # # Parsing the schema into expressions @@ -177,6 +178,7 @@ class QAPISchemaParser(object): self.exprs.append(expr_elem) =20 def accept(self): + ok =3D True while True: self.tok =3D self.src[self.cursor] self.pos =3D self.cursor @@ -184,7 +186,19 @@ class QAPISchemaParser(object): self.val =3D None =20 if self.tok =3D=3D '#': - self.cursor =3D self.src.find('\n', self.cursor) + end =3D self.src.find('\n', self.cursor) + line =3D self.src[self.cursor:end+1] + self.cursor =3D end + sline =3D line.split() + if len(defs) and len(sline) >=3D 1 \ + and sline[0] in ['ifdef', 'endif']: + if sline[0] =3D=3D 'ifdef': + ok =3D sline[1] in defs + elif sline[0] =3D=3D 'endif': + ok =3D True + continue + elif not ok: + continue elif self.tok in "{}:,[]": return elif self.tok =3D=3D "'": @@ -1707,15 +1721,36 @@ def gen_params(arg_type, boxed, extra): # # Common command line parsing # +def defile(filename): + f =3D open(filename, 'r') + while 1: + line =3D f.readline() + if not line: + break + while line[-2:] =3D=3D '\\\n': + nextline =3D f.readline() + if not nextline: + break + line =3D line + nextline + tmp =3D line.strip() + if tmp[:1] !=3D '#': + continue + tmp =3D tmp[1:] + words =3D tmp.split() + if words[0] !=3D "define": + continue + defs.append(words[1]) + f.close() =20 =20 def parse_command_line(extra_options=3D"", extra_long_options=3D[]): =20 try: opts, args =3D getopt.gnu_getopt(sys.argv[1:], - "chp:o:" + extra_options, + "chp:o:f:" + extra_options, ["source", "header", "prefix=3D", - "output-dir=3D"] + extra_long_op= tions) + "output-dir=3D", "--defile=3D"] = + + extra_long_options) except getopt.GetoptError as err: print >>sys.stderr, "%s: %s" % (sys.argv[0], str(err)) sys.exit(1) @@ -1742,6 +1777,8 @@ def parse_command_line(extra_options=3D"", extra_lo= ng_options=3D[]): do_c =3D True elif o in ("-h", "--header"): do_h =3D True + elif o in ("-f", "--defile"): + defile(a) else: extra_opts.append(oa) =20 --=20 2.9.0