]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Ecc/Check.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / Check.py
index ea739043e0bca88c58b52d2e5025941e938e15d5..86bb8562babc01fc7bf25f412a94a915c6edcf99 100644 (file)
@@ -2,22 +2,17 @@
 # This file is used to define checkpoints used by ECC tool\r
 #\r
 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution.  The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-#\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
+from __future__ import absolute_import\r
 import Common.LongFilePathOs as os\r
 import re\r
 from CommonDataClass.DataClass import *\r
 import Common.DataType as DT\r
-from EccToolError import *\r
-from MetaDataParser import ParseHeaderCommentSection\r
-import EccGlobalData\r
-import c\r
+from Ecc.EccToolError import *\r
+from Ecc.MetaDataParser import ParseHeaderCommentSection\r
+from Ecc import EccGlobalData\r
+from Ecc import c\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
 \r
@@ -187,6 +182,60 @@ class Check(object):
     def GeneralCheck(self):\r
         self.GeneralCheckNonAcsii()\r
         self.UniCheck()\r
+        self.GeneralCheckNoTab()\r
+        self.GeneralCheckLineEnding()\r
+        self.GeneralCheckTrailingWhiteSpaceLine()\r
+\r
+    # Check whether NO Tab is used, replaced with spaces\r
+    def GeneralCheckNoTab(self):\r
+        if EccGlobalData.gConfig.GeneralCheckNoTab == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
+            EdkLogger.quiet("Checking No TAB used in file ...")\r
+            SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""\r
+            RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
+            for Record in RecordSet:\r
+                if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:\r
+                    op = open(Record[1]).readlines()\r
+                    IndexOfLine = 0\r
+                    for Line in op:\r
+                        IndexOfLine += 1\r
+                        IndexOfChar = 0\r
+                        for Char in Line:\r
+                            IndexOfChar += 1\r
+                            if Char == '\t':\r
+                                OtherMsg = "File %s has TAB char at line %s column %s" % (Record[1], IndexOfLine, IndexOfChar)\r
+                                EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_NO_TAB, OtherMsg=OtherMsg, BelongsToTable='File', BelongsToItem=Record[0])\r
+\r
+    # Check Only use CRLF (Carriage Return Line Feed) line endings.\r
+    def GeneralCheckLineEnding(self):\r
+        if EccGlobalData.gConfig.GeneralCheckLineEnding == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
+            EdkLogger.quiet("Checking line ending in file ...")\r
+            SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""\r
+            RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
+            for Record in RecordSet:\r
+                if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:\r
+                    op = open(Record[1], 'rb').readlines()\r
+                    IndexOfLine = 0\r
+                    for Line in op:\r
+                        IndexOfLine += 1\r
+                        if not bytes.decode(Line).endswith('\r\n'):\r
+                            OtherMsg = "File %s has invalid line ending at line %s" % (Record[1], IndexOfLine)\r
+                            EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_INVALID_LINE_ENDING, OtherMsg=OtherMsg, BelongsToTable='File', BelongsToItem=Record[0])\r
+\r
+    # Check if there is no trailing white space in one line.\r
+    def GeneralCheckTrailingWhiteSpaceLine(self):\r
+        if EccGlobalData.gConfig.GeneralCheckTrailingWhiteSpaceLine == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
+            EdkLogger.quiet("Checking trailing white space line in file ...")\r
+            SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""\r
+            RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
+            for Record in RecordSet:\r
+                if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:\r
+                    op = open(Record[1], 'r').readlines()\r
+                    IndexOfLine = 0\r
+                    for Line in op:\r
+                        IndexOfLine += 1\r
+                        if Line.replace('\r', '').replace('\n', '').endswith(' '):\r
+                            OtherMsg = "File %s has trailing white spaces at line %s" % (Record[1], IndexOfLine)\r
+                            EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_TRAILING_WHITE_SPACE_LINE, OtherMsg=OtherMsg, BelongsToTable='File', BelongsToItem=Record[0])\r
 \r
     # Check whether file has non ACSII char\r
     def GeneralCheckNonAcsii(self):\r
@@ -215,6 +264,66 @@ class Check(object):
         self.FunctionLayoutCheckPrototype()\r
         self.FunctionLayoutCheckBody()\r
         self.FunctionLayoutCheckLocalVariable()\r
+        self.FunctionLayoutCheckDeprecated()\r
+    \r
+    # To check if the deprecated functions are used\r
+    def FunctionLayoutCheckDeprecated(self):\r
+        if EccGlobalData.gConfig.CFunctionLayoutCheckNoDeprecated == '1' or EccGlobalData.gConfig.CFunctionLayoutCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
+            EdkLogger.quiet("Checking function no deprecated one being used ...")\r
+\r
+            DeprecatedFunctionSet = ('UnicodeValueToString',\r
+                                     'AsciiValueToString',\r
+                                     'StrCpy',\r
+                                     'StrnCpy',\r
+                                     'StrCat',\r
+                                     'StrnCat',\r
+                                     'UnicodeStrToAsciiStr',\r
+                                     'AsciiStrCpy',\r
+                                     'AsciiStrnCpy',\r
+                                     'AsciiStrCat',\r
+                                     'AsciiStrnCat',\r
+                                     'AsciiStrToUnicodeStr',\r
+                                     'PcdSet8',\r
+                                     'PcdSet16',\r
+                                     'PcdSet32',\r
+                                     'PcdSet64',\r
+                                     'PcdSetPtr',\r
+                                     'PcdSetBool',\r
+                                     'PcdSetEx8',\r
+                                     'PcdSetEx16',\r
+                                     'PcdSetEx32',\r
+                                     'PcdSetEx64',\r
+                                     'PcdSetExPtr',\r
+                                     'PcdSetExBool',\r
+                                     'LibPcdSet8',\r
+                                     'LibPcdSet16',\r
+                                     'LibPcdSet32',\r
+                                     'LibPcdSet64',\r
+                                     'LibPcdSetPtr',\r
+                                     'LibPcdSetBool',\r
+                                     'LibPcdSetEx8',\r
+                                     'LibPcdSetEx16',\r
+                                     'LibPcdSetEx32',\r
+                                     'LibPcdSetEx64',\r
+                                     'LibPcdSetExPtr',\r
+                                     'LibPcdSetExBool',\r
+                                     'GetVariable',\r
+                                     'GetEfiGlobalVariable',\r
+                                     )\r
+\r
+            for IdentifierTable in EccGlobalData.gIdentifierTableList:\r
+                SqlCommand = """select ID, Name, BelongsToFile from %s\r
+                                where Model = %s """ % (IdentifierTable, MODEL_IDENTIFIER_FUNCTION_CALLING)\r
+                RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
+                for Record in RecordSet:\r
+                    for Key in DeprecatedFunctionSet:\r
+                        if Key == Record[1]:\r
+                            if not EccGlobalData.gException.IsException(ERROR_C_FUNCTION_LAYOUT_CHECK_NO_DEPRECATE, Key):\r
+                                OtherMsg = 'The function [%s] is deprecated which should NOT be used' % Key\r
+                                EccGlobalData.gDb.TblReport.Insert(ERROR_C_FUNCTION_LAYOUT_CHECK_NO_DEPRECATE,\r
+                                                                   OtherMsg=OtherMsg,\r
+                                                                   BelongsToTable=IdentifierTable,\r
+                                                                   BelongsToItem=Record[0])\r
 \r
     def WalkTree(self):\r
         IgnoredPattern = c.GetIgnoredDirListPattern()\r
@@ -531,13 +640,23 @@ class Check(object):
         if EccGlobalData.gConfig.IncludeFileCheckData == '1' or EccGlobalData.gConfig.IncludeFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
             EdkLogger.quiet("Checking header file data ...")\r
 \r
+            # Get all typedef functions\r
+            gAllTypedefFun = []\r
+            for IdentifierTable in EccGlobalData.gIdentifierTableList:\r
+                SqlCommand = """select Name from %s\r
+                                where Model = %s """ % (IdentifierTable, MODEL_IDENTIFIER_TYPEDEF)\r
+                RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
+                for Record in RecordSet:\r
+                    if Record[0].startswith('('):\r
+                        gAllTypedefFun.append(Record[0])\r
+\r
 #            for Dirpath, Dirnames, Filenames in self.WalkTree():\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
             for FullName in EccGlobalData.gHFileList:\r
-                MsgList = c.CheckHeaderFileData(FullName)\r
+                MsgList = c.CheckHeaderFileData(FullName, gAllTypedefFun)\r
 \r
     # Doxygen document checking\r
     def DoxygenCheck(self):\r
@@ -563,17 +682,17 @@ class Check(object):
                         op = open(FullName).readlines()\r
                         FileLinesList = op\r
                         LineNo             = 0\r
-                        CurrentSection     = MODEL_UNKNOWN \r
+                        CurrentSection     = MODEL_UNKNOWN\r
                         HeaderSectionLines       = []\r
-                        HeaderCommentStart = False \r
+                        HeaderCommentStart = False\r
                         HeaderCommentEnd   = False\r
-                        \r
+\r
                         for Line in FileLinesList:\r
                             LineNo   = LineNo + 1\r
                             Line     = Line.strip()\r
                             if (LineNo < len(FileLinesList) - 1):\r
                                 NextLine = FileLinesList[LineNo].strip()\r
-            \r
+\r
                             #\r
                             # blank line\r
                             #\r
@@ -600,8 +719,8 @@ class Check(object):
                                     #\r
                                     HeaderSectionLines.append((Line, LineNo))\r
                                     HeaderCommentStart = True\r
-                                    continue        \r
-            \r
+                                    continue\r
+\r
                             #\r
                             # Collect Header content.\r
                             #\r
@@ -635,7 +754,7 @@ class Check(object):
                                 if EccGlobalData.gConfig.HeaderCheckFileCommentEnd == '1' or EccGlobalData.gConfig.HeaderCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
                                     EccGlobalData.gDb.TblReport.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER, Msg, "File", Result[0])\r
 \r
-                                     \r
+\r
 \r
     # Check whether the function headers are followed Doxygen special documentation blocks in section 2.3.5\r
     def DoxygenCheckFunctionHeader(self):\r
@@ -827,7 +946,7 @@ class Check(object):
                     for FilePath in FilePathList:\r
                         if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_LIBRARY_NAME_DUPLICATE, Record[1]):\r
                             EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_LIBRARY_NAME_DUPLICATE, OtherMsg="The Library Class [%s] is duplicated in '%s' line %s and line %s." % (Record[1], FilePath, Record[3], Record[4]), BelongsToTable='Dsc', BelongsToItem=Record[0])\r
-    \r
+\r
     # Check the header file in Include\Library directory whether be defined in the package DEC file.\r
     def MetaDataFileCheckLibraryDefinedInDec(self):\r
         if EccGlobalData.gConfig.MetaDataFileCheckLibraryDefinedInDec == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
@@ -842,9 +961,9 @@ class Check(object):
                 if not LibraryDec:\r
                     if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_LIBRARY_NOT_DEFINED, LibraryInInf):\r
                         EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_LIBRARY_NOT_DEFINED, \\r
-                                            OtherMsg="The Library Class [%s] in %s line is not defined in the associated package file." % (LibraryInInf, Line), \r
+                                            OtherMsg="The Library Class [%s] in %s line is not defined in the associated package file." % (LibraryInInf, Line),\r
                                             BelongsToTable='Inf', BelongsToItem=ID)\r
-    \r
+\r
     # Check whether an Inf file is specified in the FDF file, but not in the Dsc file, then the Inf file must be for a Binary module only\r
     def MetaDataFileCheckBinaryInfInFdf(self):\r
         if EccGlobalData.gConfig.MetaDataFileCheckBinaryInfInFdf == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
@@ -1244,7 +1363,7 @@ class Check(object):
                      group by A.ID\r
                      """ % (Table.Table, Table.Table, Model, Model)\r
         RecordSet = Table.Exec(SqlCommand)\r
-        for Record in RecordSet:     \r
+        for Record in RecordSet:\r
             if not EccGlobalData.gException.IsException(ErrorID, Record[2]):\r
                 EccGlobalData.gDb.TblReport.Insert(ErrorID, OtherMsg="The %s value [%s] is used more than one time" % (Name.upper(), Record[2]), BelongsToTable=Table.Table, BelongsToItem=Record[0])\r
 \r
@@ -1278,7 +1397,7 @@ class Check(object):
     # Check whether only capital letters are used for #define declarations\r
     def NamingConventionCheckDefineStatement(self, FileTable):\r
         if EccGlobalData.gConfig.NamingConventionCheckDefineStatement == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
-            EdkLogger.quiet("Checking naming covention of #define statement ...")\r
+            EdkLogger.quiet("Checking naming convention of #define statement ...")\r
 \r
             SqlCommand = """select ID, Value from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_MACRO_DEFINE)\r
             RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
@@ -1293,7 +1412,7 @@ class Check(object):
     # Check whether only capital letters are used for typedef declarations\r
     def NamingConventionCheckTypedefStatement(self, FileTable):\r
         if EccGlobalData.gConfig.NamingConventionCheckTypedefStatement == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
-            EdkLogger.quiet("Checking naming covention of #typedef statement ...")\r
+            EdkLogger.quiet("Checking naming convention of #typedef statement ...")\r
 \r
             SqlCommand = """select ID, Name from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_TYPEDEF)\r
             RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
@@ -1313,13 +1432,13 @@ class Check(object):
     # Check whether the #ifndef at the start of an include file uses both prefix and postfix underscore characters, '_'.\r
     def NamingConventionCheckIfndefStatement(self, FileTable):\r
         if EccGlobalData.gConfig.NamingConventionCheckIfndefStatement == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
-            EdkLogger.quiet("Checking naming covention of #ifndef statement ...")\r
+            EdkLogger.quiet("Checking naming convention of #ifndef statement ...")\r
 \r
             SqlCommand = """select ID, Value from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_MACRO_IFNDEF)\r
             RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
             for Record in RecordSet:\r
                 Name = Record[1].replace('#ifndef', '').strip()\r
-                if Name[0] != '_' or Name[-1] != '_':\r
+                if Name[-1] != '_':\r
                     if not EccGlobalData.gException.IsException(ERROR_NAMING_CONVENTION_CHECK_IFNDEF_STATEMENT, Name):\r
                         EccGlobalData.gDb.TblReport.Insert(ERROR_NAMING_CONVENTION_CHECK_IFNDEF_STATEMENT, OtherMsg="The #ifndef name [%s] does not follow the rules" % (Name), BelongsToTable=FileTable, BelongsToItem=Record[0])\r
 \r
@@ -1330,7 +1449,7 @@ class Check(object):
     # Check whether the path name followed the rule\r
     def NamingConventionCheckPathName(self):\r
         if EccGlobalData.gConfig.NamingConventionCheckPathName == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
-            EdkLogger.quiet("Checking naming covention of file path name ...")\r
+            EdkLogger.quiet("Checking naming convention of file path name ...")\r
             Pattern = re.compile(r'^[A-Z]+\S*[a-z]\S*$')\r
             SqlCommand = """select ID, Name from File"""\r
             RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
@@ -1347,7 +1466,7 @@ class Check(object):
     # Check whether the variable name followed the rule\r
     def NamingConventionCheckVariableName(self, FileTable):\r
         if EccGlobalData.gConfig.NamingConventionCheckVariableName == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
-            EdkLogger.quiet("Checking naming covention of variable name ...")\r
+            EdkLogger.quiet("Checking naming convention of variable name ...")\r
             Pattern = re.compile(r'^[A-Zgm]+\S*[a-z]\S*$')\r
 \r
             SqlCommand = """select ID, Name from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_VARIABLE)\r
@@ -1367,7 +1486,7 @@ class Check(object):
     # Check whether the function name followed the rule\r
     def NamingConventionCheckFunctionName(self):\r
         if EccGlobalData.gConfig.NamingConventionCheckFunctionName == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
-            EdkLogger.quiet("Checking naming covention of function name ...")\r
+            EdkLogger.quiet("Checking naming convention of function name ...")\r
             Pattern = re.compile(r'^[A-Z]+\S*[a-z]\S*$')\r
             SqlCommand = """select ID, Name from Function"""\r
             RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r
@@ -1379,7 +1498,7 @@ class Check(object):
     # Check whether NO use short variable name with single character\r
     def NamingConventionCheckSingleCharacterVariable(self, FileTable):\r
         if EccGlobalData.gConfig.NamingConventionCheckSingleCharacterVariable == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':\r
-            EdkLogger.quiet("Checking naming covention of single character variable name ...")\r
+            EdkLogger.quiet("Checking naming convention of single character variable name ...")\r
 \r
             SqlCommand = """select ID, Name from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_VARIABLE)\r
             RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)\r