board_inspector: fix unregisteration of conditionally disabled objects

The current ConditionallyUnregisterSymbolVisitor has the following two
issues.

  1. The visitor will crash when a DefIfElse node is not fully parsed due
     to failed deferred expansion.

  2. Nested DefIfElse of disabled blocks are still checked and one of its
     branch may still take effect.

This patch fixes those issues by checking the predicates of a DefIfElse
block only when conditionally_hidden is False and check existence of
TermList and DefElse clauses.

Tracked-On: #6298
Signed-off-by: Junjie Mao <junjie.mao@intel.com>
This commit is contained in:
Junjie Mao 2021-07-21 13:19:36 +08:00 committed by wenlingz
parent 35edd7804a
commit a5f5ed0865

View File

@ -46,24 +46,26 @@ class ConditionallyUnregisterSymbolVisitor(Visitor):
return f
def visit_topdown(self, tree):
if tree.label == "DefIfElse":
if not self.conditionally_hidden and tree.label == "DefIfElse":
self.context.change_scope(tree.scope)
cond = self.interpreter.interpret(tree.children[1]).get()
self.context.pop_scope()
self.depth += 1
if cond:
self.visit_topdown(tree.children[2])
if len(tree.children) == 4:
if hasattr(tree, "TermList"):
self.visit_topdown(tree.TermList)
if hasattr(tree, "DefElse") and tree.DefElse:
self.conditionally_hidden = True
self.visit_topdown(tree.children[3])
self.visit_topdown(tree.DefElse)
self.conditionally_hidden = False
else:
self.conditionally_hidden = True
self.visit_topdown(tree.children[2])
self.conditionally_hidden = False
if len(tree.children) == 4:
self.visit_topdown(tree.children[3])
if hasattr(tree, "TermList"):
self.conditionally_hidden = True
self.visit_topdown(tree.TermList)
self.conditionally_hidden = False
if hasattr(tree, "DefElse") and tree.DefElse:
self.visit_topdown(tree.DefElse)
self.depth -= 1
elif tree.label not in ["DefMethod"]:
super().visit_topdown(tree)