BaseTools: Update Expression.py for string comparison and MACRO replace issue

1. Fix string comparison incorrect issue, we expected "ABC" is greater than
"AAD" since the second char 'B' is greater than 'A'.
2. fix MACRO not replace issue.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
This commit is contained in:
Yunhua Feng 2018-02-07 21:37:26 +08:00 committed by Yonghong Zhu
parent 2052cb675f
commit 9efe8d6040
3 changed files with 13 additions and 31 deletions

View File

@ -157,18 +157,8 @@ class ValueExpression(object):
def Eval(Operator, Oprand1, Oprand2 = None): def Eval(Operator, Oprand1, Oprand2 = None):
WrnExp = None WrnExp = None
if Operator not in ["in", "not in"] and (type(Oprand1) == type('') or type(Oprand2) == type('')): if Operator not in ["==", "!=", ">=", "<=", ">", "<", "in", "not in"] and \
if type(Oprand1) == type(''): (type(Oprand1) == type('') or type(Oprand2) == type('')):
if Oprand1[0] in ['"', "'"] or Oprand1.startswith('L"') or Oprand1.startswith("L'")or Oprand1.startswith('UINT'):
Oprand1, Size = ParseFieldValue(Oprand1)
else:
Oprand1,Size = ParseFieldValue('"' + Oprand1 + '"')
if type(Oprand2) == type(''):
if Oprand2[0] in ['"', "'"] or Oprand2.startswith('L"') or Oprand2.startswith("L'") or Oprand2.startswith('UINT'):
Oprand2, Size = ParseFieldValue(Oprand2)
else:
Oprand2, Size = ParseFieldValue('"' + Oprand2 + '"')
if type(Oprand1) == type('') or type(Oprand2) == type(''):
raise BadExpression(ERR_STRING_EXPR % Operator) raise BadExpression(ERR_STRING_EXPR % Operator)
if Operator in ['in', 'not in']: if Operator in ['in', 'not in']:
if type(Oprand1) != type(''): if type(Oprand1) != type(''):
@ -296,8 +286,6 @@ class ValueExpression(object):
except BadExpression: except BadExpression:
pass pass
if type(Token) == type('') and Token.startswith('{') and Token.endswith('}') and self._Idx >= self._Len: if type(Token) == type('') and Token.startswith('{') and Token.endswith('}') and self._Idx >= self._Len:
if len(Token) != len(self._Expr.replace(' ', '')):
raise BadExpression
return self._Expr return self._Expr
self._Idx = 0 self._Idx = 0
@ -459,7 +447,6 @@ class ValueExpression(object):
if self._Token[Index] in ['"']: if self._Token[Index] in ['"']:
Flag += 1 Flag += 1
if Flag == 2 and self._Token.endswith('"'): if Flag == 2 and self._Token.endswith('"'):
self._Token = ParseFieldValue(self._Token)[0]
return True return True
if self._Token.startswith("'") or self._Token.startswith("L'"): if self._Token.startswith("'") or self._Token.startswith("L'"):
Flag = 0 Flag = 0
@ -467,7 +454,6 @@ class ValueExpression(object):
if self._Token[Index] in ["'"]: if self._Token[Index] in ["'"]:
Flag += 1 Flag += 1
if Flag == 2 and self._Token.endswith("'"): if Flag == 2 and self._Token.endswith("'"):
self._Token = ParseFieldValue(self._Token)[0]
return True return True
try: try:
self._Token = int(self._Token, Radix) self._Token = int(self._Token, Radix)
@ -622,24 +608,16 @@ class ValueExpression(object):
self._Idx += 1 self._Idx += 1
UStr = self.__GetString() UStr = self.__GetString()
self._Token = 'L"' + UStr + '"' self._Token = 'L"' + UStr + '"'
self._Token, Size = ParseFieldValue(self._Token)
return self._Token return self._Token
elif Expr.startswith("L'"): elif Expr.startswith("L'"):
# Skip L # Skip L
self._Idx += 1 self._Idx += 1
UStr = self.__GetString() UStr = self.__GetString()
self._Token = "L'" + UStr + "'" self._Token = "L'" + UStr + "'"
self._Token, Size = ParseFieldValue(self._Token)
return self._Token
elif Expr.startswith('"'):
UStr = self.__GetString()
self._Token = '"' + UStr + '"'
self._Token, Size = ParseFieldValue(self._Token)
return self._Token return self._Token
elif Expr.startswith("'"): elif Expr.startswith("'"):
UStr = self.__GetString() UStr = self.__GetString()
self._Token = "'" + UStr + "'" self._Token = "'" + UStr + "'"
self._Token, Size = ParseFieldValue(self._Token)
return self._Token return self._Token
elif Expr.startswith('UINT'): elif Expr.startswith('UINT'):
Re = re.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)') Re = re.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)')
@ -751,7 +729,7 @@ class ValueExpressionEx(ValueExpression):
raise BadExpression raise BadExpression
except WrnExpression, Value: except WrnExpression, Value:
PcdValue = Value.result PcdValue = Value.result
except BadExpression: except BadExpression, Value:
if self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN']: if self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN']:
PcdValue = PcdValue.strip() PcdValue = PcdValue.strip()
if type(PcdValue) == type('') and PcdValue.startswith('{') and PcdValue.endswith('}'): if type(PcdValue) == type('') and PcdValue.startswith('{') and PcdValue.endswith('}'):
@ -785,10 +763,13 @@ class ValueExpressionEx(ValueExpression):
else: else:
try: try:
TmpValue, Size = ParseFieldValue(PcdValue) TmpValue, Size = ParseFieldValue(PcdValue)
except BadExpression: except BadExpression, Value:
raise BadExpression("Type: %s, Value: %s, format or value error" % (self.PcdType, PcdValue)) raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value))
if type(TmpValue) == type(''): if type(TmpValue) == type(''):
try:
TmpValue = int(TmpValue) TmpValue = int(TmpValue)
except:
raise BadExpression(Value)
else: else:
PcdValue = '0x%0{}X'.format(Size) % (TmpValue) PcdValue = '0x%0{}X'.format(Size) % (TmpValue)
if TmpValue < 0: if TmpValue < 0:
@ -898,7 +879,7 @@ class ValueExpressionEx(ValueExpression):
if Size > 0: if Size > 0:
PcdValue = '{' + ValueStr[:-2] + '}' PcdValue = '{' + ValueStr[:-2] + '}'
else: else:
raise BadExpression("Type: %s, Value: %s, format or value error"%(self.PcdType, PcdValue)) raise BadExpression("Type: %s, Value: %s, %s"%(self.PcdType, PcdValue, Value))
if PcdValue == 'True': if PcdValue == 'True':
PcdValue = '1' PcdValue = '1'

View File

@ -808,7 +808,7 @@ class DscBuildData(PlatformBuildClassObject):
PkgSet.update(ModuleData.Packages) PkgSet.update(ModuleData.Packages)
self._DecPcds, self._GuidDict = GetDeclaredPcd(self, self._Bdb, self._Arch, self._Target, self._Toolchain,PkgSet) self._DecPcds, self._GuidDict = GetDeclaredPcd(self, self._Bdb, self._Arch, self._Target, self._Toolchain,PkgSet)
self._GuidDict.update(GlobalData.gPlatformPcds)
if (PcdCName, TokenSpaceGuid) not in self._DecPcds: if (PcdCName, TokenSpaceGuid) not in self._DecPcds:
EdkLogger.error('build', PARSER_ERROR, EdkLogger.error('build', PARSER_ERROR,

View File

@ -1994,6 +1994,7 @@ class DecParser(MetaFileParser):
PcdValue = ValueList[0] PcdValue = ValueList[0]
if PcdValue: if PcdValue:
try: try:
self._GuidDict.update(self._AllPcdDict)
ValueList[0] = ValueExpressionEx(ValueList[0], ValueList[1], self._GuidDict)(True) ValueList[0] = ValueExpressionEx(ValueList[0], ValueList[1], self._GuidDict)(True)
except BadExpression, Value: except BadExpression, Value:
EdkLogger.error('Parser', FORMAT_INVALID, Value, ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1) EdkLogger.error('Parser', FORMAT_INVALID, Value, ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)