X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FCommon%2FExpression.py;h=30711cedd784f83922430c39b95a7343f09a0473;hp=edb0a60de6007a367bdf80245fc061d665a3e469;hb=663b9e061ed1b48e562159e51333e996f1efc830;hpb=3be421e98756efc6d355b45e632c5c7b19b35b9e diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py index edb0a60de6..30711cedd7 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -15,7 +15,7 @@ from Common.GlobalData import * from CommonDataClass.Exceptions import BadExpression from CommonDataClass.Exceptions import WrnExpression -from Misc import GuidStringToGuidStructureString, ParseFieldValue +from Misc import GuidStringToGuidStructureString, ParseFieldValue, IsFieldValueAnArray import Common.EdkLogger as EdkLogger import copy @@ -40,20 +40,34 @@ ERR_ARRAY_ELE = 'This must be HEX value for NList or Array: [%s].' ERR_EMPTY_EXPR = 'Empty expression is not allowed.' ERR_IN_OPERAND = 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).' +__ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$') + ## SplitString # Split string to list according double quote # For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn'] # def SplitString(String): - # There might be escaped quote: "abc\"def\\\"ghi" - Str = String.replace('\\\\', '//').replace('\\\"', '\\\'') + # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi' RetList = [] - InQuote = False + InSingleQuote = False + InDoubleQuote = False Item = '' - for i, ch in enumerate(Str): - if ch == '"': - InQuote = not InQuote - if not InQuote: + for i, ch in enumerate(String): + if ch == '"' and not InSingleQuote: + if String[i - 1] != '\\': + InDoubleQuote = not InDoubleQuote + if not InDoubleQuote: + Item += String[i] + RetList.append(Item) + Item = '' + continue + if Item: + RetList.append(Item) + Item = '' + elif ch == "'" and not InDoubleQuote: + if String[i - 1] != '\\': + InSingleQuote = not InSingleQuote + if not InSingleQuote: Item += String[i] RetList.append(Item) Item = '' @@ -62,7 +76,7 @@ def SplitString(String): RetList.append(Item) Item = '' Item += String[i] - if InQuote: + if InSingleQuote or InDoubleQuote: raise BadExpression(ERR_STRING_TOKEN % Item) if Item: RetList.append(Item) @@ -71,27 +85,26 @@ def SplitString(String): def SplitPcdValueString(String): # There might be escaped comma in GUID() or DEVICE_PATH() or " " # or ' ' or L' ' or L" " - Str = String RetList = [] InParenthesis = 0 InSingleQuote = False InDoubleQuote = False Item = '' - for i, ch in enumerate(Str): + for i, ch in enumerate(String): if ch == '(': InParenthesis += 1 - if ch == ')': + elif ch == ')': if InParenthesis: InParenthesis -= 1 else: raise BadExpression(ERR_STRING_TOKEN % Item) - if ch == '"' and not InSingleQuote: + elif ch == '"' and not InSingleQuote: if String[i-1] != '\\': InDoubleQuote = not InDoubleQuote - if ch == "'" and not InDoubleQuote: + elif ch == "'" and not InDoubleQuote: if String[i-1] != '\\': InSingleQuote = not InSingleQuote - if ch == ',': + elif ch == ',': if InParenthesis or InSingleQuote or InDoubleQuote: Item += String[i] continue @@ -106,6 +119,25 @@ def SplitPcdValueString(String): RetList.append(Item) return RetList +def IsValidCName(Str): + return True if __ValidString.match(Str) else False + +def BuildOptionValue(PcdValue, GuidDict): + if PcdValue.startswith('H'): + InputValue = PcdValue[1:] + elif PcdValue.startswith("L'") or PcdValue.startswith("'"): + InputValue = PcdValue + elif PcdValue.startswith('L'): + InputValue = 'L"' + PcdValue[1:] + '"' + else: + InputValue = PcdValue + if IsFieldValueAnArray(InputValue): + try: + PcdValue = ValueExpressionEx(InputValue, 'VOID*', GuidDict)(True) + except: + pass + return PcdValue + ## ReplaceExprMacro # def ReplaceExprMacro(String, Macros, ExceptionList = None): @@ -181,8 +213,6 @@ class ValueExpression(object): NonLetterOpLst = ['+', '-', '*', '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':'] PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$') - HexPattern = re.compile(r'0[xX][0-9a-fA-F]+$') - RegGuidPattern = re.compile(r'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') SymbolPattern = re.compile("(" "\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|" @@ -483,6 +513,8 @@ class ValueExpression(object): Flag = 0 for Index in range(len(self._Token)): if self._Token[Index] in ['"']: + if self._Token[Index - 1] == '\\': + continue Flag += 1 if Flag == 2 and self._Token.endswith('"'): return True @@ -490,6 +522,8 @@ class ValueExpression(object): Flag = 0 for Index in range(len(self._Token)): if self._Token[Index] in ["'"]: + if self._Token[Index - 1] == '\\': + continue Flag += 1 if Flag == 2 and self._Token.endswith("'"): return True @@ -537,16 +571,25 @@ class ValueExpression(object): self._Idx += 1 # Replace escape \\\", \" - Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace('\\\"', '\\\'') - for Ch in Expr: - self._Idx += 1 - if Ch == '"' or Ch == "'": - break - self._Token = self._LiteralToken = self._Expr[Idx:self._Idx] - if self._Token.startswith('"') and not self._Token.endswith('"'): - raise BadExpression(ERR_STRING_TOKEN % self._Token) - if self._Token.startswith("'") and not self._Token.endswith("'"): - raise BadExpression(ERR_STRING_TOKEN % self._Token) + if self._Expr[Idx] == '"': + Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace('\\\"', '\\\'') + for Ch in Expr: + self._Idx += 1 + if Ch == '"': + break + self._Token = self._LiteralToken = self._Expr[Idx:self._Idx] + if not self._Token.endswith('"'): + raise BadExpression(ERR_STRING_TOKEN % self._Token) + #Replace escape \\\', \' + elif self._Expr[Idx] == "'": + Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace("\\\'", "\\\"") + for Ch in Expr: + self._Idx += 1 + if Ch == "'": + break + self._Token = self._LiteralToken = self._Expr[Idx:self._Idx] + if not self._Token.endswith("'"): + raise BadExpression(ERR_STRING_TOKEN % self._Token) self._Token = self._Token[1:-1] return self._Token @@ -621,7 +664,7 @@ class ValueExpression(object): self._LiteralToken.endswith('}'): return True - if self.HexPattern.match(self._LiteralToken): + if gHexPattern.match(self._LiteralToken): Token = self._LiteralToken[2:] if not Token: self._LiteralToken = '0x0' @@ -680,7 +723,7 @@ class ValueExpression(object): self._Token = '' if Expr: Ch = Expr[0] - Match = self.RegGuidPattern.match(Expr) + Match = gGuidPattern.match(Expr) if Match and not Expr[Match.end():Match.end()+1].isalnum() \ and Expr[Match.end():Match.end()+1] != '_': self._Idx += Match.end() @@ -858,14 +901,15 @@ class ValueExpressionEx(ValueExpression): LabelOffset = 0 for Index, Item in enumerate(PcdValueList): # compute byte offset of every LABEL + LabelList = ReLabel.findall(Item) + Item = ReLabel.sub('', Item) Item = Item.strip() - try: - LabelList = ReLabel.findall(Item) + if LabelList: for Label in LabelList: + if not IsValidCName(Label): + raise BadExpression('%s is not a valid c variable name' % Label) if Label not in LabelDict.keys(): LabelDict[Label] = str(LabelOffset) - except: - pass if Item.startswith('UINT8'): LabelOffset = LabelOffset + 1 elif Item.startswith('UINT16'): @@ -947,7 +991,7 @@ class ValueExpressionEx(ValueExpression): Item = '0x%x' % TmpValue if type(TmpValue) != type('') else TmpValue if ItemSize == 0: ItemValue, ItemSize = ParseFieldValue(Item) - if not (Item.startswith('"') or Item.startswith('L') or Item.startswith('{')) and ItemSize > 1: + if Item[0] not in ['"','L','{'] and ItemSize > 1: raise BadExpression("Byte array number %s should less than 0xFF." % Item) else: ItemValue = ParseFieldValue(Item)[0]