From 9e33b51e018bbee371c4e950df04c1d1333cc04b Mon Sep 17 00:00:00 2001 From: Joey Vagedes Date: Mon, 28 Aug 2023 16:29:48 -0700 Subject: [PATCH] Updates per external conversation to handle scenarios where we call GetNextLine() recursively --- edk2toollib/uefi/edk2/parsers/base_parser.py | 8 ++++---- edk2toollib/uefi/edk2/parsers/fdf_parser.py | 3 +++ tests.unit/parsers/test_fdf_parser.py | 13 ++++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/edk2toollib/uefi/edk2/parsers/base_parser.py b/edk2toollib/uefi/edk2/parsers/base_parser.py index 3fba7c85b..1cb36b9cd 100644 --- a/edk2toollib/uefi/edk2/parsers/base_parser.py +++ b/edk2toollib/uefi/edk2/parsers/base_parser.py @@ -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 @@ -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: @@ -148,9 +148,9 @@ def DecrementLinesParsed(self) -> bool: """ if not self.FilePathStack: return False - (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() diff --git a/edk2toollib/uefi/edk2/parsers/fdf_parser.py b/edk2toollib/uefi/edk2/parsers/fdf_parser.py index 0e4b22d6c..96aca4bf2 100644 --- a/edk2toollib/uefi/edk2/parsers/fdf_parser.py +++ b/edk2toollib/uefi/edk2/parsers/fdf_parser.py @@ -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("{") diff --git a/tests.unit/parsers/test_fdf_parser.py b/tests.unit/parsers/test_fdf_parser.py index 6baff3654..2d82baacb 100644 --- a/tests.unit/parsers/test_fdf_parser.py +++ b/tests.unit/parsers/test_fdf_parser.py @@ -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 @@ -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")