ECC: add TPMINTERNALAPI to modifier list in config.ini; add StripComments procedure...
authorjlin16 <jlin16@7335b38e-4728-0410-8992-fb3ffe349368>
Tue, 17 Jun 2008 06:29:17 +0000 (06:29 +0000)
committerjlin16 <jlin16@7335b38e-4728-0410-8992-fb3ffe349368>
Tue, 17 Jun 2008 06:29:17 +0000 (06:29 +0000)
git-svn-id: https://buildtools.tianocore.org/svn/buildtools/trunk/BaseTools@1258 7335b38e-4728-0410-8992-fb3ffe349368

Source/Python/Ecc/c.py
Source/Python/Ecc/config.ini

index 8c2fff7..7e0a14c 100644 (file)
@@ -648,11 +648,55 @@ def GetCNameList(Lvalue):
     \r
     return VarList    \r
 \r
-def SplitPredicateByOp(Str, Op):\r
+def SplitPredicateByOp(Str, Op, IsFuncCalling = False):\r
 \r
     Name = Str.strip()\r
     Value = None\r
     \r
+    if IsFuncCalling:\r
+        Index = 0\r
+        LBFound = False\r
+        UnmatchedLBCount = 0\r
+        while Index < len(Str):\r
+            while not LBFound and Str[Index] != '_' and not Str[Index].isalnum():\r
+                Index += 1\r
+            \r
+            while not LBFound and (Str[Index].isalnum() or Str[Index] == '_'):\r
+                Index += 1\r
+            # maybe type-cast at the begining, skip it.\r
+            RemainingStr = Str[Index:].lstrip()\r
+            if RemainingStr.startswith(')') and not LBFound:\r
+                Index += 1\r
+                continue\r
+            \r
+            if RemainingStr.startswith('(') and not LBFound:\r
+                LBFound = True\r
+                \r
+            if Str[Index] == '(':\r
+                UnmatchedLBCount += 1\r
+                Index += 1\r
+                continue\r
+                \r
+            if Str[Index] == ')':\r
+                UnmatchedLBCount -= 1\r
+                Index += 1\r
+                if UnmatchedLBCount == 0:\r
+                    break\r
+                continue\r
+            \r
+            Index += 1\r
+                \r
+        if UnmatchedLBCount > 0:\r
+            return [Name]\r
+            \r
+        IndexInRemainingStr = Str[Index:].find(Op)\r
+        if IndexInRemainingStr == -1:\r
+            return [Name]\r
+        \r
+        Name = Str[0:Index + IndexInRemainingStr].strip()\r
+        Value = Str[Index+IndexInRemainingStr+len(Op):].strip()\r
+        return [Name, Value]\r
+    \r
     TmpStr = Str.rstrip(';').rstrip(')')\r
     while True:\r
         Index = TmpStr.rfind(Op)\r
@@ -667,32 +711,33 @@ def SplitPredicateByOp(Str, Op):
         TmpStr = Str[0:Index - 1]\r
 \r
 def SplitPredicateStr(Str):\r
+    IsFuncCalling = False\r
     p = GetFuncDeclPattern()\r
     TmpStr = Str.replace('.', '').replace('->', '')\r
     if p.match(TmpStr):\r
-        return [[Str, None], None]\r
+        IsFuncCalling = True\r
     \r
-    PredPartList = SplitPredicateByOp(Str, '==')\r
+    PredPartList = SplitPredicateByOp(Str, '==', IsFuncCalling)\r
     if len(PredPartList) > 1:\r
         return [PredPartList, '==']\r
     \r
-    PredPartList = SplitPredicateByOp(Str, '!=')\r
+    PredPartList = SplitPredicateByOp(Str, '!=', IsFuncCalling)\r
     if len(PredPartList) > 1:\r
         return [PredPartList, '!=']\r
     \r
-    PredPartList = SplitPredicateByOp(Str, '>=')\r
+    PredPartList = SplitPredicateByOp(Str, '>=', IsFuncCalling)\r
     if len(PredPartList) > 1:\r
         return [PredPartList, '>=']\r
         \r
-    PredPartList = SplitPredicateByOp(Str, '<=')\r
+    PredPartList = SplitPredicateByOp(Str, '<=', IsFuncCalling)\r
     if len(PredPartList) > 1:\r
         return [PredPartList, '<=']\r
         \r
-    PredPartList = SplitPredicateByOp(Str, '>')\r
+    PredPartList = SplitPredicateByOp(Str, '>', IsFuncCalling)\r
     if len(PredPartList) > 1:\r
         return [PredPartList, '>']\r
         \r
-    PredPartList = SplitPredicateByOp(Str, '<')\r
+    PredPartList = SplitPredicateByOp(Str, '<', IsFuncCalling)\r
     if len(PredPartList) > 1:\r
         return [PredPartList, '<']\r
         \r
@@ -819,6 +864,29 @@ def GetSUDict(FullFileName):
     SUDict[FullFileName] = Dict\r
     return Dict\r
 \r
+def StripComments(Str):\r
+    StrippedStr = ''\r
+    List = Str.splitlines()\r
+    InComment = False\r
+    for StrPart in List:\r
+        if StrPart.lstrip().startswith('//'):\r
+            continue\r
+        Index = StrPart.find('/*')\r
+        if Index != -1:\r
+            InComment = True\r
+            StrippedStr += StrPart[0:Index]\r
+        \r
+        Index = StrPart.find('*/')\r
+        if Index != -1:\r
+            StrippedStr += StrPart[Index+2:]\r
+            InComment = False\r
+            continue\r
+        \r
+        if not InComment:\r
+            StrippedStr += StrPart\r
+             \r
+    return StrippedStr\r
+\r
 def GetFinalTypeValue(Type, FieldName, TypedefDict, SUDict):\r
     Value = TypedefDict.get(Type)\r
     if Value == None:\r
@@ -843,22 +911,23 @@ def GetFinalTypeValue(Type, FieldName, TypedefDict, SUDict):
      \r
 #    RBPos = Value.find('}')\r
     Fields = Value[LBPos + 1:]\r
+    Fields = StripComments(Fields)\r
     FieldsList = Fields.split(';')\r
     for Field in FieldsList:\r
         Field = Field.strip()\r
-        Index = Field.find(FieldName)\r
+        Index = Field.rfind(FieldName)\r
         if Index < 1:\r
             continue\r
         if not Field[Index - 1].isalnum():\r
             if Index + len(FieldName) == len(Field):\r
                 Type = GetDataTypeFromModifier(Field[0:Index])\r
                 return Type.split()[-1]\r
-#            else:\r
-#                if not Field[Index + len(FieldName) + 1].isalnum():\r
-#                    Type = GetCNameList(Field[0:Index])\r
-#                    if len(Type) == 0:\r
-#                        return Field[0:Index]\r
-#                    return Type[0]\r
+            else:\r
+            # For the condition that the field in struct is an array with [] sufixes...               \r
+                if not Field[Index + len(FieldName)].isalnum():\r
+                    Type = GetDataTypeFromModifier(Field[0:Index])\r
+                    return Type.split()[-1]\r
+                \r
     return None\r
     \r
 def GetRealType(Type, TypedefDict, TargetType = None):\r
@@ -1450,6 +1519,9 @@ def CheckPointerNullComparison(FullFileName):
     if FileID < 0:\r
         return ErrorMsgList\r
     \r
+    # cache the found function return type to accelerate later checking in this file.\r
+    FuncReturnTypeDict = {}\r
+    \r
     Db = GetDB()\r
     FileTable = 'Identifier' + str(FileID)\r
     SqlStatement = """ select Value, StartLine, ID\r
@@ -1481,15 +1553,37 @@ def CheckPointerNullComparison(FullFileName):
         for Exp in GetPredicateListFromPredicateExpStr(Str[0]):\r
             PredInfo = SplitPredicateStr(Exp)\r
             if PredInfo[1] == None:\r
-                PredVarList = GetCNameList(PredInfo[0][0])\r
+                PredVarStr = PredInfo[0][0].strip()\r
+                IsFuncCall = False\r
+                SearchInCache = False\r
+                # PredVarStr may contain '.' or '->'\r
+                TmpStr = PredVarStr.replace('.', '').replace('->', '')\r
+                if p.match(TmpStr):\r
+                    PredVarStr = PredVarStr[0:PredVarStr.find('(')]\r
+                    SearchInCache = True\r
+                    # Only direct function call using IsFuncCall branch. Multi-level ref. function call is considered a variable.\r
+                    if TmpStr.startswith(PredVarStr):    \r
+                        IsFuncCall = True\r
+                    \r
+                if PredVarStr.strip() in IgnoredKeywordList:\r
+                    continue\r
+                PredVarList = GetCNameList(PredVarStr)\r
                 # No variable found, maybe value first? like (0 == VarName)\r
                 if len(PredVarList) == 0:\r
                     continue\r
-                # in the form of function call\r
-                if p.match(PredInfo[0][0]):\r
-                    continue\r
+                if SearchInCache:\r
+                    Type = FuncReturnTypeDict.get(PredVarStr)\r
+                    if Type != None:\r
+                        if Type.find('*') == -1:\r
+                            PrintErrorMsg(ERROR_PREDICATE_EXPRESSION_CHECK_NO_BOOLEAN_OPERATOR, 'Predicate Expression: %s' % Exp, FileTable, Str[2])\r
+                        continue\r
+                    \r
+                    if PredVarStr in FuncReturnTypeDict:\r
+                        continue\r
                 \r
                 Type = GetVarInfo(PredVarList, FuncRecord, FullFileName)\r
+                if SearchInCache:\r
+                    FuncReturnTypeDict[PredVarStr] = Type\r
                 if Type == None:\r
                     continue\r
                 if Type.find('*') != -1:\r
@@ -1546,6 +1640,9 @@ def CheckNonBooleanValueComparison(FullFileName):
                 if p.match(TmpStr):\r
                     PredVarStr = PredVarStr[0:PredVarStr.find('(')]\r
                     SearchInCache = True\r
+                    # Only direct function call using IsFuncCall branch. Multi-level ref. function call is considered a variable.\r
+                    if TmpStr.startswith(PredVarStr):    \r
+                        IsFuncCall = True\r
                     \r
                 if PredVarStr.strip() in IgnoredKeywordList:\r
                     continue\r
@@ -1553,9 +1650,7 @@ def CheckNonBooleanValueComparison(FullFileName):
                 # No variable found, maybe value first? like (0 == VarName)\r
                 if len(PredVarList) == 0:\r
                     continue\r
-                # Only direct function call using IsFuncCall branch. Multi-level ref. function call is considered a variable.\r
-                if len(PredVarList) == 1:    \r
-                    IsFuncCall = True   \r
+                   \r
                 if SearchInCache:\r
                     Type = FuncReturnTypeDict.get(PredVarStr)\r
                     if Type != None:\r
@@ -1616,7 +1711,7 @@ def CheckBooleanValueComparison(FullFileName):
         for Exp in GetPredicateListFromPredicateExpStr(Str[0]):\r
             PredInfo = SplitPredicateStr(Exp)\r
             if PredInfo[1] in ('==', '!=') and PredInfo[0][1] in ('TRUE', 'FALSE'):\r
-                PredVarList = GetCNameList(PredInfo[0][0])\r
+                PredVarStr = PredInfo[0][0].strip()\r
                 IsFuncCall = False\r
                 SearchInCache = False\r
                 # PredVarStr may contain '.' or '->'\r
@@ -1624,6 +1719,9 @@ def CheckBooleanValueComparison(FullFileName):
                 if p.match(TmpStr):\r
                     PredVarStr = PredVarStr[0:PredVarStr.find('(')]\r
                     SearchInCache = True\r
+                    # Only direct function call using IsFuncCall branch. Multi-level ref. function call is considered a variable.\r
+                    if TmpStr.startswith(PredVarStr):    \r
+                        IsFuncCall = True\r
                     \r
                 if PredVarStr.strip() in IgnoredKeywordList:\r
                     continue\r
@@ -1631,10 +1729,7 @@ def CheckBooleanValueComparison(FullFileName):
                 # No variable found, maybe value first? like (0 == VarName)\r
                 if len(PredVarList) == 0:\r
                     continue\r
-                \r
-                # Only direct function call using IsFuncCall branch. Multi-level ref. function call is considered a variable.\r
-                if len(PredVarList) == 1:    \r
-                    IsFuncCall = True   \r
+          \r
                 if SearchInCache:\r
                     Type = FuncReturnTypeDict.get(PredVarStr)\r
                     if Type != None:\r
index 5b163d2..5ae9236 100644 (file)
@@ -41,7 +41,7 @@ AutoCorrect = 1
 #\r
 # List customized Modifer here, split with ','\r
 #\r
-ModifierList = IN, OUT, OPTIONAL, UNALIGNED, EFI_RUNTIMESERVICE, EFI_BOOTSERVICE, EFIAPI\r
+ModifierList = IN, OUT, OPTIONAL, UNALIGNED, EFI_RUNTIMESERVICE, EFI_BOOTSERVICE, EFIAPI, TPMINTERNALAPI\r
 \r
 #\r
 # General Checking\r