Skip to content

Commit 2178650

Browse files
authored
edk2toollib/uefi/edk2/parsers: enhancements (#419)
Improves the following parsers in the following ways: `base_parser.py`: Properly uses ivalue (int value) to check if the string representation of a value was correctly converted to an integer ('1' to 1, '0x0' to 0, etc) when evaluating conditionals `dsc_parser.py`: register pcds when performing the initial parse responsible for finding define statements. This is necessary to evaluate conditionals that use pcds.
1 parent 2536e7f commit 2178650

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

edk2toollib/uefi/edk2/parsers/base_parser.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,17 @@ def ComputeResult(self, value, cond, value2):
240240
return (ivalue != ivalue2) and (value != value2)
241241

242242
# check to make sure we only have digits from here on out
243-
if not isinstance(value, int) and not str.isdigit(value):
243+
if value.upper() in ["TRUE", "FALSE"] or value2.upper() in ["TRUE", "FALSE"]:
244+
self.Logger.error(f"Invalid comparison: {value} {cond} {value2}")
245+
self.Logger.debug(f"Invalid comparison: {value} {cond} {value2}")
246+
raise ValueError("Invalid comparison")
247+
248+
if not isinstance(ivalue, int) and not str.isdigit(value):
244249
self.Logger.error(f"{self.__class__}: Unknown value: {value} {ivalue.__class__}")
245250
self.Logger.debug(f"{self.__class__}: Conditional: {value} {cond}{value2}")
246251
raise ValueError("Unknown value")
247252

248-
if not isinstance(value2, int) and not str.isdigit(value2):
253+
if not isinstance(ivalue2, int) and not str.isdigit(value2):
249254
self.Logger.error(f"{self.__class__}: Unknown value: {value2} {ivalue2}")
250255
self.Logger.debug(f"{self.__class__}: Conditional: {value} {cond} {value2}")
251256
raise ValueError("Unknown value")

edk2toollib/uefi/edk2/parsers/dsc_parser.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,8 @@ def __ParseLine(self, Line, file_name=None, lineno=None):
113113
p = self.ParseInfPathLib(line_resolved)
114114
self.Libs.append(p)
115115
self.Logger.debug("Found Library in a 64bit BuildOptions Section: %s" % p)
116-
elif "tokenspaceguid" in line_resolved.lower() and \
117-
line_resolved.count('|') > 0 and line_resolved.count('.') > 0:
118-
# should be a pcd statement
119-
p = line_resolved.partition('|')
120-
self.Pcds.append(p[0].strip())
121-
self.PcdValueDict[p[0].strip()] = p[2].strip()
122-
self.Logger.debug("Found a Pcd in a 64bit Module Override section: %s" % p[0].strip())
116+
elif self.RegisterPcds(line_resolved):
117+
self.Logger.debug("Found a Pcd in a 64bit Module Override section")
123118
else:
124119
if (".inf" in line_resolved.lower()):
125120
p = self.ParseInfPathMod(line_resolved)
@@ -141,13 +136,8 @@ def __ParseLine(self, Line, file_name=None, lineno=None):
141136
if file_name is not None and lineno is not None:
142137
self.LibsEnhanced.append({'file': os.path.normpath(file_name), 'lineno': lineno, 'data': p})
143138
self.Logger.debug("Found Library in a 32bit BuildOptions Section: %s" % p)
144-
elif "tokenspaceguid" in line_resolved.lower() and \
145-
line_resolved.count('|') > 0 and line_resolved.count('.') > 0:
146-
# should be a pcd statement
147-
p = line_resolved.partition('|')
148-
self.Pcds.append(p[0].strip())
149-
self.PcdValueDict[p[0].strip()] = p[2].strip()
150-
self.Logger.debug("Found a Pcd in a 32bit Module Override section: %s" % p[0].strip())
139+
elif self.RegisterPcds(line_resolved):
140+
self.Logger.debug("Found a Pcd in a 32bit Module Override section")
151141

152142
else:
153143
if (".inf" in line_resolved.lower()):
@@ -169,13 +159,8 @@ def __ParseLine(self, Line, file_name=None, lineno=None):
169159
p = self.ParseInfPathLib(line_resolved)
170160
self.Libs.append(p)
171161
self.Logger.debug("Found Library in a BuildOptions Section: %s" % p)
172-
elif "tokenspaceguid" in line_resolved.lower() and \
173-
line_resolved.count('|') > 0 and line_resolved.count('.') > 0:
174-
# should be a pcd statement
175-
p = line_resolved.partition('|')
176-
self.Pcds.append(p[0].strip())
177-
self.PcdValueDict[p[0].strip()] = p[2].strip()
178-
self.Logger.debug("Found a Pcd in a Module Override section: %s" % p[0].strip())
162+
elif self.RegisterPcds(line_resolved):
163+
self.Logger.debug("Found a Pcd in a Module Override section")
179164

180165
else:
181166
if (".inf" in line_resolved.lower()):
@@ -196,13 +181,8 @@ def __ParseLine(self, Line, file_name=None, lineno=None):
196181
return (line_resolved, [], None)
197182
# process line in PCD section
198183
elif (self.CurrentSection.upper().startswith("PCDS")):
199-
if "tokenspaceguid" in line_resolved.lower() and \
200-
line_resolved.count('|') > 0 and line_resolved.count('.') > 0:
201-
# should be a pcd statement
202-
p = line_resolved.partition('|')
203-
self.Pcds.append(p[0].strip())
204-
self.PcdValueDict[p[0].strip()] = p[2].strip()
205-
self.Logger.debug("Found a Pcd in a PCD section: %s" % p[0].strip())
184+
if self.RegisterPcds(line_resolved):
185+
self.Logger.debug("Found a Pcd in a PCD section")
206186
return (line_resolved, [], None)
207187
else:
208188
return (line_resolved, [], None)
@@ -213,6 +193,8 @@ def __ParseDefineLine(self, Line):
213193
return ("", [])
214194

215195
# this line needs to be here to resolve any symbols inside the !include lines, if any
196+
self.RegisterPcds(line_stripped)
197+
line_stripped = self.ReplacePcds(line_stripped)
216198
line_resolved = self.ReplaceVariables(line_stripped)
217199
if (self.ProcessConditional(line_resolved)):
218200
# was a conditional
@@ -333,6 +315,8 @@ def __ProcessDefines(self, lines):
333315
# otherwise, raise the exception and act normally
334316
if not self._no_fail_mode:
335317
raise
318+
# Reset the PcdValueDict as this was just to find any Defines.
319+
self.PcdValueDict = {}
336320

337321
def _parse_libraries(self):
338322
"""Builds a lookup table of all possible library instances depending on scope.
@@ -535,3 +519,17 @@ def GetAllDscPaths(self):
535519
They are not all guaranteed to be DSC files
536520
"""
537521
return self._dsc_file_paths
522+
523+
def RegisterPcds(self, line):
524+
"""Reads the line and registers any PCDs found."""
525+
if ("tokenspaceguid" in line.lower() and
526+
line.count('|') > 0 and
527+
line.count('.') > 0):
528+
529+
# should be a pcd statement
530+
p = line.partition('|')
531+
self.Pcds.append(p[0].strip())
532+
self.PcdValueDict[p[0].strip()] = p[2].strip()
533+
self.Logger.debug("Found a Pcd: %s" % p[0].strip())
534+
return True
535+
return False

tests.unit/parsers/test_base_parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ def test_process_conditional_hex_number(self):
229229
self.assertTrue(parser.ProcessConditional("!IF 0x20 == 32"))
230230
self.assertTrue(parser.InActiveCode())
231231
self.assertTrue(parser.ProcessConditional("!endif"))
232+
# check that hex comparisons work
233+
self.assertTrue(parser.ProcessConditional("!IF 0x20 > 0x20"))
234+
self.assertFalse(parser.InActiveCode())
235+
self.assertTrue(parser.ProcessConditional("!endif"))
236+
self.assertTrue(parser.ProcessConditional("!IF 0x20 >= 0x20"))
237+
self.assertTrue(parser.InActiveCode())
238+
self.assertTrue(parser.ProcessConditional("!endif"))
232239

233240
def test_process_conditional_greater_than(self):
234241
parser = BaseParser("")

0 commit comments

Comments
 (0)