From nobody Tue Feb 10 00:39:45 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1597089266; cv=none; d=zohomail.com; s=zohoarc; b=Tb8+rHYMhLkZpBtY3vXo3UtHR8IsTms95k5fJ0NYPUCfo2A4A0coMo95D1JGQgQzr/0kdWxbCCx0beghG413kntUiu+0cOPLVsunOwbFx1N9FAeC6SyRDPI0imXzn5gNbm5OqiGHy3Eij4ZjDk3zVdTKzdT0LYKQ0ShqpfhQkHU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597089266; 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=in8nHFzKOQegCgF8vb8hOhMAaiGbmURtCJ4Hj4NuXzg=; b=XbaomuWAWdCBhkg5FAiP1pfnrxeNMXXnacfR8Iaet4j1jaIHNtYp5HtN6ZOr9IR/yfR/s35JxL4oUuoAxoQsa+ruIzHPjOhp2P8iQ495PS8M3iURg9aJvsqflmcUz6SmHqkDs98CJwJuD0TZPXOPmvnQkOJQVJkj0dlPOa9WLbg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass 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 1597089266721699.6510815367701; Mon, 10 Aug 2020 12:54:26 -0700 (PDT) Received: from localhost ([::1]:49698 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k5DsH-0008U1-CC for importer@patchew.org; Mon, 10 Aug 2020 15:54:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51126) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k5Dog-0000VF-Am for qemu-devel@nongnu.org; Mon, 10 Aug 2020 15:50:42 -0400 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]:36425) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k5Dob-0004l5-OB for qemu-devel@nongnu.org; Mon, 10 Aug 2020 15:50:41 -0400 Received: by mail-wm1-x32e.google.com with SMTP id 3so692692wmi.1 for ; Mon, 10 Aug 2020 12:50:37 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id b129sm976087wmb.29.2020.08.10.12.50.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2020 12:50:34 -0700 (PDT) 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=in8nHFzKOQegCgF8vb8hOhMAaiGbmURtCJ4Hj4NuXzg=; b=oEINfCMrjNcCDNfoGaT77zm+BZJrp0tyYJNIvjAA06OSfwbf5+4gKdoKyPgiahXWTp pfMyQC/iMt1nVN4IJYZyKqRM4/cdzhH76cYT+MiQXvmwhbs5bZUyR5vYzH/oIiqreEEC DeTLiaM8EF6zQrQ/4J59qYPsHyrCEUQem7Awb7AIPzp7EB/ib7SYwIiNfC3xRumLY4QN yt6PaA8qQWKwMUrJ50fBhW3Qh/zmONMuAOnCAnXOJcBTH/iOfAFzCVQ4El1AI/6+joS8 QPzQUq/kERTMt0jSTkQR7nqCeoIiQnDxWEndvu2tEyiJg1GwqQNzsi6V//NSmtTy6Ygg S4tg== 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=in8nHFzKOQegCgF8vb8hOhMAaiGbmURtCJ4Hj4NuXzg=; b=tbAP0HSd9hthXBsNozdC3RUPjsyB7Ew5SnrqHI/2jf4OZzEy+QR31tQGROI3TqceKf ZKHan6ASIlDhU3VIQFLfQDaSs4UvjhmZtRij3ahf5pqTUrijNeabgnndxJRThigLFk4p pVh38gvAyyavDizm1BX595sIadP4ouANVWygSbmRowMO80vLCHicxNpAnZo7sMmKyTps c4RaSJvgWu8EqVYy/zLM3q+BDNhJsbBBkSWZ9K4HwDpXR3KlR/LMPgVPIrmg649tYDZS bSOZs7pBirvrOT3Ue5fFuSP0J1E6BVwE8YBfQzZCUZbhezPBKS6lFDoZ/2R1xqcWBjVE AlaA== X-Gm-Message-State: AOAM532cVzYj9g0cb6Xp+rqGsnxPDgDKKfn+/L7a+vCzshgbD1WJta9m dMMV96DvXwxOFI3jbBiX6D41lP5pVDtyMg== X-Google-Smtp-Source: ABdhPJzLPjoKYwq19yk3SwarDfhuwmOzgWFfI2kjgugOvqyQUN5XOAqcGI0j609qI9Y/Z7dLjWXztA== X-Received: by 2002:a1c:964b:: with SMTP id y72mr745852wmd.69.1597089035390; Mon, 10 Aug 2020 12:50:35 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH v5 08/20] scripts/qapi/parser.py: improve doc comment indent handling Date: Mon, 10 Aug 2020 20:50:07 +0100 Message-Id: <20200810195019.25427-9-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200810195019.25427-1-peter.maydell@linaro.org> References: <20200810195019.25427-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=2a00:1450:4864:20::32e; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x32e.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: Markus Armbruster Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) 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. Reviewed-by: Richard Henderson Signed-off-by: Peter Maydell --- v1->v2: Update doc-good.out as per final para. --- scripts/qapi/parser.py | 81 +++++++++++++++++++++++++++------- tests/qapi-schema/doc-good.out | 4 +- 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py index 7fae4478d34..d9f11eadd96 100644 --- a/scripts/qapi/parser.py +++ b/scripts/qapi/parser.py @@ -308,18 +308,32 @@ class QAPIDoc: """ =20 class Section: - 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): - super().__init__(name) + def __init__(self, parser, name, indent=3D0): + super().__init__(parser, name, indent) self.member =3D None =20 def connect(self, member): @@ -333,7 +347,7 @@ class QAPIDoc: 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() @@ -438,7 +452,18 @@ class QAPIDoc: =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) @@ -460,7 +485,17 @@ class QAPIDoc: =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) @@ -493,11 +528,23 @@ class QAPIDoc: % (name, self.sections[0].name)) if 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") @@ -506,21 +553,21 @@ class QAPIDoc: "'%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): @@ -543,7 +590,7 @@ class QAPIDoc: 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): @@ -551,6 +598,8 @@ class QAPIDoc: raise QAPISemError(feature.info, "feature '%s' lacks documentation" % feature.name) + 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 0ef85d959ac..bbf77b08dc3 100644 --- a/tests/qapi-schema/doc-good.out +++ b/tests/qapi-schema/doc-good.out @@ -158,7 +158,7 @@ doc symbol=3DAlternate =20 arg=3Di an integer - @b is undocumented +@b is undocumented arg=3Db =20 feature=3Dalt-feat @@ -173,7 +173,7 @@ doc symbol=3Dcmd the first argument arg=3Darg2 the second - argument +argument arg=3Darg3 =20 feature=3Dcmd-feat1 --=20 2.20.1