From nobody Thu Nov 13 20:40:35 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1582640402; cv=none; d=zohomail.com; s=zohoarc; b=XWrYYkWP8TsypIIPGDwAf0ZVhAvNkTCZWSRQyLGAbWrOFxAlU3T1bVwrzNgqq38eR8ew8ooNUqPZxh2CVj2+TKMBgo2yKSfuGnlV6y+i1qdM08O1n6o2dVDIqKoXe9G5kWknTcmbPNZ+mgbgKPmXbnfkIPcfM1OVdP02rL4xHUk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582640402; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=yx82A80BgFchuY4arBJB9y5p01nca9y/eUUsgMiGXgw=; b=BNkSqS9UM6/JNCFVcJv9rJb/g+HEZgkG1idTI8o4DrpohcEJ73kyDHV5V7fr5yABIcAPk/Pz8S4/WkMoCtdhqQ2/xwiLGAgh7Cftoy+YpbVYClAM7HjKxecBXuTDHrn7IJ0hsFUoII2zB63Rx7bQPmqqGT/ngyqjbsSk0hNqUr4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582640402032891.3441490626432; Tue, 25 Feb 2020 06:20:02 -0800 (PST) Received: from localhost ([::1]:57682 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6b44-0004WU-OB for importer@patchew.org; Tue, 25 Feb 2020 09:20:00 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:44778) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6apT-0006ga-TB for qemu-devel@nongnu.org; Tue, 25 Feb 2020 09:05:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6apO-0005IR-Qt for qemu-devel@nongnu.org; Tue, 25 Feb 2020 09:04:55 -0500 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]:50909) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j6apO-0005IC-KD for qemu-devel@nongnu.org; Tue, 25 Feb 2020 09:04:50 -0500 Received: by mail-wm1-x32f.google.com with SMTP id a5so3133530wmb.0 for ; Tue, 25 Feb 2020 06:04:50 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id f127sm4322136wma.4.2020.02.25.06.04.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2020 06:04:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yx82A80BgFchuY4arBJB9y5p01nca9y/eUUsgMiGXgw=; b=aL0IevAW73/+IgMxhYgXFw0v2Vjx2sx5oRUNPfkyQUqmGgHl6fo7D7eKK+wS2w9lVL ASnIbXoEyR7jbAAImZotxCXC9xV1TQ3IUANkbtzkStmR+PecdGh09X/9aU3XXILjaRUr o7kDxVcYsAJJc31u7Ck8S+C72p16dBmHz/fHFo2JQ72r1aiY6G14otq3bFTM02jRN6hc qzHClxCoMR1jmkvnxWnDxN5/vrvRxvxIuMOysI4XoGMQXTOVHHSSvZqoIhIzLRO+lqYA 6+0lJbZ2fo8Dsev+Un8NhaDO0XKjtr3KZXlEe5+T4QylY+fHQGuBArgUHOjQdk6u8eb1 kcmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yx82A80BgFchuY4arBJB9y5p01nca9y/eUUsgMiGXgw=; b=PjN720ppxQfSR5nAIpNNiMrzMFmxmiFDIW8WgkXw59uteTrLWLDpG2T/Gg+MAlYAzx Cl5mRFBITDfnMyLGLOZclRpMbwWfECmJafkOUf437L7kXeZyhWL1KkaLl0L3w/fGt/rx OvFDznFljR0YVWrOU42KKEkpEgZPVJjktCVjoS5V5c8lPW2cBre3EtLI/8s325xpg4iJ hqyEe9ntoM51vl9/RsGJ4kZwMLI3IGvJCujpohM9Js2oGGmRiW5NlAlzg11JOXW3lvbN dsZYxtU1gJx2kaqf2KrAX2BpaFsdD7wz1CbaVNMDKZEam/DlY9F3DSKEkZMaZnweMQv4 TffQ== X-Gm-Message-State: APjAAAVvGTdqSFE/SyocYbIYVK1u25fMdeKWd6R+rb0W6L8KgoQR3C5U 6BwcHgQt1HvbW+pHibAEK9R6C5GIcNxJUA== X-Google-Smtp-Source: APXvYqwR2DqYZdTbKJJ8oF2NeK/aHwTnYWK9g7ToPHH7pQus/W8ZMTPWYkUoA0RY+up52JrQ9J73vg== X-Received: by 2002:a7b:cbcf:: with SMTP id n15mr5477846wmi.21.1582639489044; Tue, 25 Feb 2020 06:04:49 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH v3 05/12] scripts/qapi/parser.py: improve doc comment indent handling Date: Tue, 25 Feb 2020 14:04:30 +0000 Message-Id: <20200225140437.20609-6-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200225140437.20609-1-peter.maydell@linaro.org> References: <20200225140437.20609-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::32f X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Markus Armbruster , Michael Roth , Stefan Hajnoczi , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Make the handling of indentation in doc comments more sophisticated, so that when we see a section like: Notes: some text some more text indented line 3 we save it for the doc-comment processing code as: some text some more text indented line 3 and when we see a section with the heading on its own line: Notes: some text some more text indented text we also accept that and save it in the same form. The exception is that we always retain indentation as-is for Examples sections, because these are literal text. If we detect that the comment document text is not indented as much as we expect it to be, we throw a parse error. (We don't complain about over-indented sections, because for rST this can be legitimate markup.) The golden reference for the doc comment text is updated to remove the two 'wrong' indents; these now form a test case that we correctly stripped leading whitespace from an indented multi-line argument definition. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson --- v1->v2: Update doc-good.out as per final para. --- scripts/qapi/parser.py | 82 +++++++++++++++++++++++++++------- tests/qapi-schema/doc-good.out | 4 +- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py index 2196ec5de1e..66f802641c9 100644 --- a/scripts/qapi/parser.py +++ b/scripts/qapi/parser.py @@ -313,18 +313,32 @@ class QAPIDoc(object): """ =20 class Section(object): - def __init__(self, name=3DNone): + def __init__(self, parser, name=3DNone, indent=3D0): + # parser, for error messages about indentation + self._parser =3D parser # optional section name (argument/member or section name) self.name =3D name # the list of lines for this section self.text =3D '' + # the expected indent level of the text of this section + self._indent =3D indent =20 def append(self, line): + # Strip leading spaces corresponding to the expected indent le= vel + # Blank lines are always OK. + if line: + spacecount =3D len(line) - len(line.lstrip(" ")) + if spacecount > self._indent: + spacecount =3D self._indent + if spacecount < self._indent: + raise QAPIParseError(self._parser, "unexpected de-inde= nt") + line =3D line[spacecount:] + self.text +=3D line.rstrip() + '\n' =20 class ArgSection(Section): - def __init__(self, name): - QAPIDoc.Section.__init__(self, name) + def __init__(self, parser, name, indent=3D0): + QAPIDoc.Section.__init__(self, parser, name, indent) self.member =3D None =20 def connect(self, member): @@ -338,7 +352,7 @@ class QAPIDoc(object): self._parser =3D parser self.info =3D info self.symbol =3D None - self.body =3D QAPIDoc.Section() + self.body =3D QAPIDoc.Section(parser) # dict mapping parameter name to ArgSection self.args =3D OrderedDict() self.features =3D OrderedDict() @@ -443,7 +457,18 @@ class QAPIDoc(object): =20 if name.startswith('@') and name.endswith(':'): line =3D line[len(name)+1:] - self._start_args_section(name[1:-1]) + if not line or line.isspace(): + # Line was just the "@arg:" header; following lines + # are not indented + indent =3D 0 + line =3D '' + else: + # Line is "@arg: first line of description"; following + # lines should be indented by len(name) + 1, and we + # pad out this first line so it is handled the same way + indent =3D len(name) + 1 + line =3D ' ' * indent + line + self._start_args_section(name[1:-1], indent) elif self._is_section_tag(name): self._append_line =3D self._append_various_line self._append_various_line(line) @@ -465,7 +490,17 @@ class QAPIDoc(object): =20 if name.startswith('@') and name.endswith(':'): line =3D line[len(name)+1:] - self._start_features_section(name[1:-1]) + if not line or line.isspace(): + # Line is just the "@name:" header, no ident for following= lines + indent =3D 0 + line =3D '' + else: + # Line is "@arg: first line of description"; following + # lines should be indented by len(name) + 3, and we + # pad out this first line so it is handled the same way + indent =3D len(name) + 1 + line =3D ' ' * indent + line + self._start_features_section(name[1:-1], indent) elif self._is_section_tag(name): self._append_line =3D self._append_various_line self._append_various_line(line) @@ -498,11 +533,23 @@ class QAPIDoc(object): % (name, self.sections[0].name)) elif self._is_section_tag(name): line =3D line[len(name)+1:] - self._start_section(name[:-1]) + if not line or line.isspace(): + # Line is just "SectionName:", no indent for following lin= es + indent =3D 0 + line =3D '' + elif name.startswith("Example"): + # The "Examples" section is literal-text, so preserve + # all the indentation as-is + indent =3D 0 + else: + # Line is "SectionName: some text", indent required + indent =3D len(name) + 1 + line =3D ' ' * indent + line + self._start_section(name[:-1], indent) =20 self._append_freeform(line) =20 - def _start_symbol_section(self, symbols_dict, name): + def _start_symbol_section(self, symbols_dict, name, indent): # FIXME invalid names other than the empty string aren't flagged if not name: raise QAPIParseError(self._parser, "invalid parameter name") @@ -511,21 +558,21 @@ class QAPIDoc(object): "'%s' parameter name duplicated" % name) assert not self.sections self._end_section() - self._section =3D QAPIDoc.ArgSection(name) + self._section =3D QAPIDoc.ArgSection(self._parser, name, indent) symbols_dict[name] =3D self._section =20 - def _start_args_section(self, name): - self._start_symbol_section(self.args, name) + def _start_args_section(self, name, indent): + self._start_symbol_section(self.args, name, indent) =20 - def _start_features_section(self, name): - self._start_symbol_section(self.features, name) + def _start_features_section(self, name, indent): + self._start_symbol_section(self.features, name, indent) =20 - def _start_section(self, name=3DNone): + def _start_section(self, name=3DNone, indent=3D0): if name in ('Returns', 'Since') and self.has_section(name): raise QAPIParseError(self._parser, "duplicated '%s' section" % name) self._end_section() - self._section =3D QAPIDoc.Section(name) + self._section =3D QAPIDoc.Section(self._parser, name, indent) self.sections.append(self._section) =20 def _end_section(self): @@ -548,7 +595,7 @@ class QAPIDoc(object): def connect_member(self, member): if member.name not in self.args: # Undocumented TODO outlaw - self.args[member.name] =3D QAPIDoc.ArgSection(member.name) + self.args[member.name] =3D QAPIDoc.ArgSection(self._parser, me= mber.name) self.args[member.name].connect(member) =20 def connect_feature(self, feature): @@ -556,7 +603,8 @@ class QAPIDoc(object): raise QAPISemError(feature.info, "feature '%s' lacks documentation" % feature.name) - self.features[feature.name] =3D QAPIDoc.ArgSection(feature.nam= e) + self.features[feature.name] =3D QAPIDoc.ArgSection(self._parse= r, + feature.name) self.features[feature.name].connect(feature) =20 def check_expr(self, expr): diff --git a/tests/qapi-schema/doc-good.out b/tests/qapi-schema/doc-good.out index a65bce639ff..0302ce0bde1 100644 --- a/tests/qapi-schema/doc-good.out +++ b/tests/qapi-schema/doc-good.out @@ -144,7 +144,7 @@ doc symbol=3DAlternate =20 arg=3Di an integer - @b is undocumented +@b is undocumented arg=3Db =20 doc freeform @@ -157,7 +157,7 @@ doc symbol=3Dcmd the first argument arg=3Darg2 the second - argument +argument arg=3Darg3 =20 feature=3Dcmd-feat1 --=20 2.20.1