Skip to content

Commit

Permalink
Updates per external conversation to handle scenarios where we call G…
Browse files Browse the repository at this point in the history
…etNextLine() recursively
  • Loading branch information
Javagedes committed Aug 28, 2023
1 parent b1b7262 commit 9e33b51
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 4 additions & 4 deletions edk2toollib/uefi/edk2/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, log="BaseParser"):
self.PPs = []
self._Edk2PathUtil = None
self.TargetFilePath = None # the abs path of the target file
self.FilePathStack = [] # a stack containing a tuple of a file, and lines left to parse in that file
self.FilePathStack = [] # a stack containing a list of size 2: [filepath, line_count]
self.ParsedFiles = set()
self.CurrentLine = -1
self._MacroNotDefinedValue = "0" # value to used for undefined macro
Expand Down Expand Up @@ -137,7 +137,7 @@ def SetInputVars(self, inputdict):

def PushTargetFile(self, abs_path, line_count):
"""Adds a target file to the stack."""
self.FilePathStack.append((abs_path, line_count))
self.FilePathStack.append([abs_path, line_count])
self.ParsedFiles.add(abs_path)

def DecrementLinesParsed(self) -> bool:
Expand All @@ -148,9 +148,9 @@ def DecrementLinesParsed(self) -> bool:
"""
if not self.FilePathStack:
return False

Check warning on line 150 in edk2toollib/uefi/edk2/parsers/base_parser.py

View check run for this annotation

Codecov / codecov/patch

edk2toollib/uefi/edk2/parsers/base_parser.py#L150

Added line #L150 was not covered by tests
(abs_path, line_count) = self.FilePathStack[-1]
line_count = self.FilePathStack[-1][1]
if line_count - 1 > 0:
self.FilePathStack[-1] = (abs_path, line_count - 1)
self.FilePathStack[-1][1] -= 1
return True

self.FilePathStack.pop()
Expand Down
3 changes: 3 additions & 0 deletions edk2toollib/uefi/edk2/parsers/fdf_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ def GetNextLine(self):
sline = self.StripComment(line)

if (sline is None or len(sline) < 1):
self.DecrementLinesParsed()
return self.GetNextLine()

sline = self.ReplaceVariables(sline)
if self.ProcessConditional(sline):
# was a conditional so skip
self.DecrementLinesParsed()
return self.GetNextLine()
if not self.InActiveCode():
self.DecrementLinesParsed()
return self.GetNextLine()

self._BracketCount += sline.count("{")
Expand Down
13 changes: 12 additions & 1 deletion tests.unit/parsers/test_fdf_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ def test_section_guided():
def test_fdf_include_relative_paths(tmp_path):
"""This tests whether includes work properly with a relative path.
Tests the following scenarios:
1. include multiple files from the same directory
2. Properly decrement when a comment exists in the file
3. Properly decrement when a macro exists in the file
4. Properly decrement when in an inactive portion of a conditional macro
Directory setup
src
└─ Platforms
Expand All @@ -129,7 +135,12 @@ def test_fdf_include_relative_paths(tmp_path):
inc_dir.mkdir(parents=True)

with open(inc_dir / "Libs1.dsc.inc", "w") as f:
f.write("Lib1|BaseLib1.inf\n")
f.write("# A Comment here.\n")
f.write("!if TRUE == TRUE\n")
f.write(" Lib1|BaseLib1.inf\n")
f.write("!else\n")
f.write(" Lib1|BaseLib3.inf\n")
f.write("!endif\n")

with open(inc_dir / "Libs2.dsc.inc", "w") as f:
f.write("Lib2|BaseLib2.inf\n")
Expand Down

0 comments on commit 9e33b51

Please sign in to comment.