Skip to content

Commit

Permalink
base_parser.py: conditional evaluation bugfix (#425)
Browse files Browse the repository at this point in the history
recent changes to conditional evaluations to support evaluating non
decimal integers such as hex strings broke the ability to evaluate
define macro replacements that are integers. This is a bugfix to keep
that support along with additional tests to ensure it does not break
in the future.
  • Loading branch information
Javagedes authored Oct 6, 2023
1 parent abe26c3 commit b62e1c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion edk2toollib/uefi/edk2/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ def ComputeResult(self, value, cond, value2):
return (ivalue != ivalue2) and (value != value2)

# check to make sure we only have digits from here on out
if value.upper() in ["TRUE", "FALSE"] or value2.upper() in ["TRUE", "FALSE"]:
if (isinstance(value, str) and value.upper() in ["TRUE", "FALSE"]) \
or (isinstance(value, str) and value2.upper() in ["TRUE", "FALSE"]):
self.Logger.error(f"Invalid comparison: {value} {cond} {value2}")
self.Logger.debug(f"Invalid comparison: {value} {cond} {value2}")
raise ValueError("Invalid comparison")
Expand Down
21 changes: 21 additions & 0 deletions tests.unit/parsers/test_base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,27 @@ def test_emulator_conditional_not_it_all(self):

parser.ResetParserState()

def test_conditional_with_variable(self):
''' Makes sure conversions are correct when using variables '''
parser = BaseParser("")
parser.LocalVars = {"MAX_SOCKET": '4', 'TARGET': 'DEBUG'}

self.assertTrue(parser.ProcessConditional('!if $(MAX_SOCKET) <= 4'))
self.assertTrue(parser.InActiveCode())
self.assertTrue(parser.ProcessConditional('!endif'))

self.assertTrue(parser.ProcessConditional('!if $(TARGET) == "DEBUG" && $(MAX_SOCKET) <= 4'))
self.assertTrue(parser.InActiveCode())
self.assertTrue(parser.ProcessConditional('!endif'))

self.assertTrue(parser.ProcessConditional('!if $(MAX_SOCKET) <= 3'))
self.assertFalse(parser.InActiveCode())
self.assertTrue(parser.ProcessConditional('!endif'))

self.assertTrue(parser.ProcessConditional('!if ($(TARGET) == "RELEASE") && ($(MAX_SOCKET) <= 4)'))
self.assertFalse(parser.InActiveCode())
self.assertTrue(parser.ProcessConditional('!endif'))


class TestBaseParserGuids(unittest.TestCase):

Expand Down

0 comments on commit b62e1c1

Please sign in to comment.