Skip to content

Commit

Permalink
base_parser.py: support conditions checking for empty string (#653)
Browse files Browse the repository at this point in the history
base_parser.py did not support conditional directives that were comparing to empty strings (e.g. `!if $(MY_VAL) == ""`, `!if $(MY_VAL) != "". The logic in `_TokenizeConditional`, was setting `mode` to QUOTE_MODE twice because the token length was zero for an empty quote (`len(token) == 0`).

This PR Updates the logic to not enter QUOTE_MODE when its already in QUOTE_MODE, and to fall over to end of QUOTE_MODE.
  • Loading branch information
apop5 authored Oct 18, 2024
1 parent 7531944 commit db0565d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion edk2toollib/uefi/edk2/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,14 @@ def _TokenizeConditional(cls: "BaseParser", text: str) -> str:
mode = 0
tokens = []
for character in text:
if character == '"' and len(token) == 0:
if character == '"' and len(token) == 0 and mode != QUOTE_MODE:
mode = QUOTE_MODE
elif character == '"' and mode == QUOTE_MODE:
if len(token) > 0:
tokens.append(f'"{token}"')
token = ""
else:
tokens.append('""')
mode = TEXT_MODE
elif character == "$" and len(token) == 0:
token += character
Expand Down
35 changes: 35 additions & 0 deletions tests.unit/parsers/test_dsc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import unittest
import tempfile
import os
import textwrap
from edk2toollib.uefi.edk2.parsers.dsc_parser import DscParser
from edk2toollib.uefi.edk2.path_utilities import Edk2Path

Expand Down Expand Up @@ -155,3 +156,37 @@ def test_dsc_include_relative_path(self):
self.assertEqual(parser.LocalVars["INCLUDED"], "TRUE") # make sure we got the defines
finally:
os.chdir(cwd)

def test_dsc_define_statements(self):
"""This test some dsc define statements"""
SAMPLE_DSC_FILE1 = textwrap.dedent("""\
[Defines]
PLATFORM_NAME = SomePlatformPkg
PLATFORM_GUID = aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
PLATFORM_VERSION = 0.1
DSC_SPECIFICATION = 0x00010005
OUTPUT_DIRECTORY = Build/$(PLATFORM_NAME)
DEFINE FLAG1 =
!if FLAG1 != ""
FLAG2 = "hi"
!endif
!if $(FLAG2) == "hi"
[Components.IA32]
FakePath/FakePath2/FakeInf.inf
!endif
""")
workspace = tempfile.mkdtemp()

file1_name = "file1.dsc"
file1_path = os.path.join(workspace, file1_name)
TestDscParserIncludes.write_to_file(file1_path, SAMPLE_DSC_FILE1)
try:
parser = DscParser()
parser.SetEdk2Path(Edk2Path(workspace, []))
parser.ParseFile(file1_path)
finally:
os.remove(file1_path)
assert any("FakePath/FakePath2/FakeInf.inf" in value for value in parser.Components)

0 comments on commit db0565d

Please sign in to comment.