]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/Trim: Fix the bug for stripping when no line directive in file
authorYonghong Zhu <yonghong.zhu@intel.com>
Wed, 17 Feb 2016 06:58:02 +0000 (14:58 +0800)
committerYonghong Zhu <yonghong.zhu@intel.com>
Fri, 19 Feb 2016 08:11:08 +0000 (16:11 +0800)
when no line directive in file, the tool still need to strip the typedef
statement (eg: typedef struct, typedef union ..).

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Yonghong Zhu <yonghong.zhu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/Python/Trim/Trim.py

index 8fefa1bcd9d273f03e8a0f431e3fdb092bf0dd0b..e5fe730f2ca004ed0da509639a993b2d2d37d9a2 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # Trim files preprocessed by compiler\r
 #\r
-# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2016, 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
@@ -29,7 +29,7 @@ from Common.LongFilePathSupport import OpenLongFilePath as open
 # Version and Copyright\r
 __version_number__ = ("0.10" + " " + gBUILD_VERSION)\r
 __version__ = "%prog Version " + __version_number__\r
-__copyright__ = "Copyright (c) 2007-2010, Intel Corporation. All rights reserved."\r
+__copyright__ = "Copyright (c) 2007-2016, Intel Corporation. All rights reserved."\r
 \r
 ## Regular expression for matching Line Control directive like "#line xxx"\r
 gLineControlDirective = re.compile('^\s*#(?:line)?\s+([0-9]+)\s+"*([^"]*)"')\r
@@ -37,6 +37,10 @@ gLineControlDirective = re.compile('^\s*#(?:line)?\s+([0-9]+)\s+"*([^"]*)"')
 gTypedefPattern = re.compile("^\s*typedef\s+struct(\s+\w+)?\s*[{]*$", re.MULTILINE)\r
 ## Regular expression for matching "#pragma pack"\r
 gPragmaPattern = re.compile("^\s*#pragma\s+pack", re.MULTILINE)\r
+## Regular expression for matching "typedef"\r
+gTypedef_SinglePattern = re.compile("^\s*typedef", re.MULTILINE)\r
+## Regular expression for matching "typedef struct, typedef union, struct, union"\r
+gTypedef_MulPattern = re.compile("^\s*(typedef)?\s+(struct|union)(\s+\w+)?\s*[{]*$", re.MULTILINE)\r
 \r
 #\r
 # The following number pattern match will only match if following criteria is met:\r
@@ -206,7 +210,34 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):
 \r
     # in case there's no line directive or linemarker found\r
     if (not LineControlDirectiveFound) and NewLines == []:\r
-        NewLines = Lines\r
+        MulPatternFlag = False\r
+        SinglePatternFlag = False\r
+        Brace = 0\r
+        for Index in range(len(Lines)):\r
+            Line = Lines[Index]\r
+            if MulPatternFlag == False and gTypedef_MulPattern.search(Line) == None:\r
+                if SinglePatternFlag == False and gTypedef_SinglePattern.search(Line) == None:\r
+                    # remove "#pragram pack" directive\r
+                    if gPragmaPattern.search(Line) == None:\r
+                        NewLines.append(Line)\r
+                    continue\r
+                elif SinglePatternFlag == False:\r
+                    SinglePatternFlag = True\r
+                if Line.find(";") >= 0:\r
+                    SinglePatternFlag = False\r
+            elif MulPatternFlag == False:\r
+                # found "typedef struct, typedef union, union, struct", keep its position and set a flag\r
+                MulPatternFlag = True\r
+\r
+            # match { and } to find the end of typedef definition\r
+            if Line.find("{") >= 0:\r
+                Brace += 1\r
+            elif Line.find("}") >= 0:\r
+                Brace -= 1\r
+\r
+            # "typedef struct, typedef union, union, struct" must end with a ";"\r
+            if Brace == 0 and Line.find(";") >= 0:\r
+                MulPatternFlag = False\r
 \r
     # save to file\r
     try:\r