Skip to content

Commit a798779

Browse files
author
Markus Armbruster
committed
qapi: Avoid redundant parens in code generated for conditionals
Commit 6cc2e48 "qapi: introduce QAPISchemaIfCond.cgen()" caused a minor regression: redundant parenthesis. Subsequent commits eliminated of many of them, but not all. Get rid of the rest now. Signed-off-by: Markus Armbruster <[email protected]> Message-Id: <[email protected]> Reviewed-by: Marc-André Lureau <[email protected]>
1 parent ccea6a8 commit a798779

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

scripts/qapi/common.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,26 @@ def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]],
205205
cond_fmt: str, not_fmt: str,
206206
all_operator: str, any_operator: str) -> str:
207207

208-
def do_gen(ifcond: Union[str, Dict[str, Any]]):
208+
def do_gen(ifcond: Union[str, Dict[str, Any]], need_parens: bool):
209209
if isinstance(ifcond, str):
210210
return cond_fmt % ifcond
211211
assert isinstance(ifcond, dict) and len(ifcond) == 1
212212
if 'not' in ifcond:
213-
return not_fmt % do_gen(ifcond['not'])
213+
return not_fmt % do_gen(ifcond['not'], True)
214214
if 'all' in ifcond:
215215
gen = gen_infix(all_operator, ifcond['all'])
216216
else:
217217
gen = gen_infix(any_operator, ifcond['any'])
218+
if need_parens:
219+
gen = '(' + gen + ')'
218220
return gen
219221

220222
def gen_infix(operator: str, operands: Sequence[Any]) -> str:
221-
return '(' + operator.join([do_gen(o) for o in operands]) + ')'
223+
return operator.join([do_gen(o, True) for o in operands])
222224

223225
if not ifcond:
224226
return ''
225-
return do_gen(ifcond)
227+
return do_gen(ifcond, False)
226228

227229

228230
def cgen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]]) -> str:

tests/qapi-schema/doc-good.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Members
7979
If
8080
~~
8181

82-
"(IFALL1 and IFALL2)"
82+
"IFALL1 and IFALL2"
8383

8484

8585
"Variant1" (Object)
@@ -120,8 +120,8 @@ Members
120120

121121
The members of "Base"
122122
The members of "Variant1" when "base1" is ""one""
123-
The members of "Variant2" when "base1" is ""two"" (**If: **"(IFONE or
124-
IFTWO)")
123+
The members of "Variant2" when "base1" is ""two"" (**If: **"IFONE or
124+
IFTWO")
125125

126126
Features
127127
~~~~~~~~

0 commit comments

Comments
 (0)