ECC: include file check feature added.
authorjlin16 <jlin16@7335b38e-4728-0410-8992-fb3ffe349368>
Fri, 21 Mar 2008 11:23:40 +0000 (11:23 +0000)
committerjlin16 <jlin16@7335b38e-4728-0410-8992-fb3ffe349368>
Fri, 21 Mar 2008 11:23:40 +0000 (11:23 +0000)
git-svn-id: https://buildtools.tianocore.org/svn/buildtools/trunk/BaseTools@1084 7335b38e-4728-0410-8992-fb3ffe349368

Source/Python/Ecc/Check.py
Source/Python/Ecc/CodeFragmentCollector.py
Source/Python/Ecc/c.py

index 6b2236d..6287704 100644 (file)
@@ -33,7 +33,49 @@ class Check(object):
     def Check(self):\r
         self.MetaDataFileCheck()\r
         self.DoxygenCheck()\r
+        self.IncludeFileCheck()\r
     \r
+    #\r
+    # Include file checking\r
+    #\r
+    def IncludeFileCheck(self):\r
+        self.IncludeFileCheckIfndef()\r
+        self.IncludeFileCheckData()\r
+    \r
+    #\r
+    # Check whether all include file contents is guarded by a #ifndef statement.\r
+    #\r
+    def IncludeFileCheckIfndef(self):\r
+        if EccGlobalData.gConfig.IncludeFileCheckIfndefStatement == '1' or EccGlobalData.gConfig.IncludeFileCheckAll == '1':\r
+            EdkLogger.quiet("Checking header file ifndef ...")\r
+            Tuple = os.walk(EccGlobalData.gTarget)\r
+            IgnoredPattern = re.compile(r'.*[\\/](?:BUILD|CVS|\.SVN|INTELRESTRICTEDTOOLS|INTELRESTRICTEDPKG)[\\/].*')\r
+        \r
+            for Dirpath, Dirnames, Filenames in Tuple:\r
+                if IgnoredPattern.match(Dirpath.upper()) or Dirpath.find('.svn') != -1:\r
+                    continue\r
+                for F in Filenames:\r
+                    if os.path.splitext(F)[1] in ('.h'):\r
+                        FullName = os.path.join(Dirpath, F)\r
+                        MsgList = c.CheckHeaderFileIfndef(FullName)\r
+    \r
+    #\r
+    # Check whether include files NOT contain code or define data variables\r
+    #\r
+    def IncludeFileCheckData(self):\r
+        if EccGlobalData.gConfig.IncludeFileCheckData == '1' or EccGlobalData.gConfig.IncludeFileCheckAll == '1':\r
+            EdkLogger.quiet("Checking header file data ...")\r
+            Tuple = os.walk(EccGlobalData.gTarget)\r
+            IgnoredPattern = re.compile(r'.*[\\/](?:BUILD|CVS|\.SVN|INTELRESTRICTEDTOOLS|INTELRESTRICTEDPKG)[\\/].*')\r
+        \r
+            for Dirpath, Dirnames, Filenames in Tuple:\r
+                if IgnoredPattern.match(Dirpath.upper()) or Dirpath.find('.svn') != -1:\r
+                    continue\r
+                for F in Filenames:\r
+                    if os.path.splitext(F)[1] in ('.h'):\r
+                        FullName = os.path.join(Dirpath, F)\r
+                        MsgList = c.CheckHeaderFileData(FullName)\r
+        \r
     #\r
     # Doxygen document checking\r
     #\r
index bf7a62f..761fad8 100644 (file)
@@ -383,6 +383,15 @@ class CodeFragmentCollector:
                 InComment = True\r
             else:\r
                 self.__GetOneChar()\r
+        \r
+        EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
+                \r
+        if InComment and DoubleSlashComment:\r
+            CommentObj.EndPos = EndLinePos\r
+            FileProfile.CommentList.append(CommentObj)\r
+        if InComment and HashComment and not PPExtend:\r
+            PPDirectiveObj.EndPos = EndLinePos\r
+            FileProfile.PPDirectiveList.append(PPDirectiveObj)\r
 \r
         self.Rewind()\r
 \r
@@ -497,6 +506,14 @@ class CodeFragmentCollector:
             else:\r
                 self.__GetOneChar()\r
 \r
+        EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
+                \r
+        if InComment and DoubleSlashComment:\r
+            CommentObj.EndPos = EndLinePos\r
+            FileProfile.CommentList.append(CommentObj)\r
+        if InComment and HashComment and not PPExtend:\r
+            PPDirectiveObj.EndPos = EndLinePos\r
+            FileProfile.PPDirectiveList.append(PPDirectiveObj)\r
         self.Rewind()\r
 \r
     ## ParseFile() method\r
index ad05f1d..2a4c2d3 100644 (file)
@@ -79,8 +79,14 @@ def GetIdentifierList():
             IdVar = DataClass.IdentifierClass(-1, var.Modifier, '', var.Declarator, '', DataClass.MODEL_IDENTIFIER_FUNCTION_DECLARATION, -1, -1, var.StartPos[0],var.StartPos[1],var.EndPos[0],var.EndPos[1])\r
             IdList.append(IdVar)\r
             continue\r
-        for decl in var.Declarator.split(','):\r
-            DeclList = decl.split('=')\r
+        \r
+        if var.Declarator.find('{') == -1:      \r
+            for decl in var.Declarator.split(','):\r
+                DeclList = decl.split('=')\r
+                IdVar = DataClass.IdentifierClass(-1, var.Modifier, '', DeclList[0].strip(), (len(DeclList) > 1 and [DeclList[1]]or [''])[0], DataClass.MODEL_IDENTIFIER_VARIABLE, -1, -1, var.StartPos[0],var.StartPos[1],var.EndPos[0],var.EndPos[1])\r
+                IdList.append(IdVar)\r
+        else:\r
+            DeclList = var.Declarator.split('=')\r
             IdVar = DataClass.IdentifierClass(-1, var.Modifier, '', DeclList[0].strip(), (len(DeclList) > 1 and [DeclList[1]]or [''])[0], DataClass.MODEL_IDENTIFIER_VARIABLE, -1, -1, var.StartPos[0],var.StartPos[1],var.EndPos[0],var.EndPos[1])\r
             IdList.append(IdVar)\r
             \r
@@ -252,6 +258,72 @@ def GetTableID(FullFileName, ErrorMsgList):
         return -1\r
     return FileID\r
 \r
+def CheckHeaderFileData(FullFileName):\r
+    ErrorMsgList = []\r
+    \r
+    FileID = GetTableID(FullFileName, ErrorMsgList)\r
+    if FileID < 0:\r
+        return ErrorMsgList\r
+    \r
+    Db = GetDB()\r
+    FileTable = 'Identifier' + str(FileID)\r
+    SqlStatement = """ select ID, Modifier\r
+                       from %s\r
+                       where Model = %d\r
+                   """ % (FileTable, DataClass.MODEL_IDENTIFIER_VARIABLE)\r
+    ResultSet = Db.TblFile.Exec(SqlStatement)\r
+    for Result in ResultSet:\r
+        if not Result[1].startswith('extern'):\r
+            PrintErrorMsg(ERROR_INCLUDE_FILE_CHECK_DATA, 'Variable definition appears in header file', FileTable, Result[0])\r
+        \r
+    SqlStatement = """ select ID\r
+                       from Function\r
+                       where BelongsToFile = %d\r
+                   """ % FileID\r
+    ResultSet = Db.TblFile.Exec(SqlStatement)\r
+    for Result in ResultSet:\r
+        PrintErrorMsg(ERROR_INCLUDE_FILE_CHECK_DATA, 'Function definition appears in header file', 'Function', Result[0])\r
+\r
+    return ErrorMsgList\r
+\r
+def CheckHeaderFileIfndef(FullFileName):\r
+    ErrorMsgList = []\r
+    \r
+    FileID = GetTableID(FullFileName, ErrorMsgList)\r
+    if FileID < 0:\r
+        return ErrorMsgList\r
+    \r
+    Db = GetDB()\r
+    FileTable = 'Identifier' + str(FileID)\r
+    SqlStatement = """ select Value, StartLine\r
+                       from %s\r
+                       where Model = %d order by StartLine\r
+                   """ % (FileTable, DataClass.MODEL_IDENTIFIER_MACRO_IFNDEF)\r
+    ResultSet = Db.TblFile.Exec(SqlStatement)\r
+    if len(ResultSet) == 0:\r
+        PrintErrorMsg(ERROR_INCLUDE_FILE_CHECK_IFNDEF_STATEMENT_1, '', 'File', FileID)\r
+        return ErrorMsgList\r
+    for Result in ResultSet:\r
+        SqlStatement = """ select Value, EndLine\r
+                       from %s\r
+                       where EndLine < %d\r
+                   """ % (FileTable, Result[1])\r
+        ResultSet = Db.TblFile.Exec(SqlStatement)\r
+        for Result in ResultSet:\r
+            if not Result[0].startswith('/*') and not Result[0].startswith('//'):\r
+                PrintErrorMsg(ERROR_INCLUDE_FILE_CHECK_IFNDEF_STATEMENT_2, '', 'File', FileID)\r
+        break\r
+    \r
+    SqlStatement = """ select Value\r
+                       from %s\r
+                       where StartLine > (select max(EndLine) from %s where Model = %d)\r
+                   """ % (FileTable, FileTable, DataClass.MODEL_IDENTIFIER_MACRO_ENDIF)\r
+    ResultSet = Db.TblFile.Exec(SqlStatement)\r
+    for Result in ResultSet:\r
+        if not Result[0].startswith('/*') and not Result[0].startswith('//'):\r
+            PrintErrorMsg(ERROR_INCLUDE_FILE_CHECK_IFNDEF_STATEMENT_3, '', 'File', FileID)\r
+    return ErrorMsgList\r
+\r
 def CheckDoxygenCommand(FullFileName):\r
     ErrorMsgList = []\r
     \r