* ToolTask.cs: Use regex to parse output. Regex is from monodevelop.
authorAnkit Jain <radical@corewars.org>
Wed, 7 Apr 2010 21:24:16 +0000 (21:24 -0000)
committerAnkit Jain <radical@corewars.org>
Wed, 7 Apr 2010 21:24:16 +0000 (21:24 -0000)
svn path=/trunk/mcs/; revision=154992

mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/ChangeLog
mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/ToolTask.cs

index 3af2f2a1f3dae31fc1670f681cfce7607c86427d..da51c4d49aed2323bca209a57895e548c0f3c482 100644 (file)
@@ -1,3 +1,7 @@
+2010-04-08  Ankit Jain  <jankit@novell.com>
+
+       * ToolTask.cs: Use regex to parse output. Regex is from monodevelop.
+
 2010-04-08  Ankit Jain  <jankit@novell.com>
 
        * ToolTask.cs (ExecuteTool): Check that the tool exists.
index 226c72cc7668d4aa0e23cd5c036448807699c4bf..f3c3b676b307e20a15fbff7d8eeb14d79944232b 100644 (file)
@@ -56,8 +56,6 @@ namespace Microsoft.Build.Utilities
                StringBuilder toolOutput;
                bool typeLoadException;
 
-               static Regex            regex;
-               
                protected ToolTask ()
                        : this (null, null)
                {
@@ -81,19 +79,6 @@ namespace Microsoft.Build.Utilities
                        this.environmentOverride = new SCS.ProcessStringDictionary ();
                }
 
-               static ToolTask ()
-               {
-                       regex = new Regex (
-                               @"^\s*"
-                               + @"(((?<ORIGIN>(((\d+>)?[a-zA-Z]?:[^:]*)|([^:]*))):)"
-                               + "|())"
-                               + "(?<SUBCATEGORY>(()|([^:]*? )))"
-                               + "(?<CATEGORY>(error|warning)) "
-                               + "(?<CODE>[^:]*):"
-                               + "(?<TEXT>.*)$",
-                               RegexOptions.IgnoreCase);
-               }
-
                [MonoTODO]
                protected virtual bool CallHostObjectToExecute ()
                {
@@ -238,82 +223,33 @@ namespace Microsoft.Build.Utilities
                                singleLine.StartsWith ("Compilation failed"))
                                return;
 
-                       string filename, origin, category, code, subcategory, text;
-                       int lineNumber, columnNumber, endLineNumber, endColumnNumber;
-               
-                       Match m = regex.Match (singleLine);
-                       origin = m.Groups [regex.GroupNumberFromName ("ORIGIN")].Value;
-                       category = m.Groups [regex.GroupNumberFromName ("CATEGORY")].Value;
-                       code = m.Groups [regex.GroupNumberFromName ("CODE")].Value;
-                       subcategory = m.Groups [regex.GroupNumberFromName ("SUBCATEGORY")].Value;
-                       text = m.Groups [regex.GroupNumberFromName ("TEXT")].Value;
-                       
-                       ParseOrigin (origin, out filename, out lineNumber, out columnNumber, out endLineNumber, out endColumnNumber);
-
-                       if (category == "warning") {
-                               Log.LogWarning (subcategory, code, null, filename, lineNumber, columnNumber, endLineNumber,
-                                       endColumnNumber, text, null);
-                       } else if (category == "error") {
-                               Log.LogError (subcategory, code, null, filename, lineNumber, columnNumber, endLineNumber,
-                                       endColumnNumber, text, null);
+                       Match match = CscErrorRegex.Match (singleLine);
+                       if (!match.Success)
+                               return;
+
+                       string filename = match.Result ("${file}") ?? "";
+                       string line = match.Result ("${line}");
+                       int lineNumber = !string.IsNullOrEmpty (line) ? Int32.Parse (line) : 0;
+
+                       string col = match.Result ("${column}");
+                       int columnNumber = 0;
+                       if (!string.IsNullOrEmpty (col))
+                               columnNumber = col == "255+" ? -1 : Int32.Parse (col);
+
+                       string category = match.Result ("${level}");
+                       string code = match.Result ("${number}");
+                       string text = match.Result ("${message}");
+
+                       if (String.Compare (category, "warning", StringComparison.OrdinalIgnoreCase) == 0) {
+                               Log.LogWarning (null, code, null, filename, lineNumber, columnNumber, -1,
+                                       -1, text, null);
+                       } else if (String.Compare (category, "error", StringComparison.OrdinalIgnoreCase) == 0) {
+                               Log.LogError (null, code, null, filename, lineNumber, columnNumber, -1,
+                                       -1, text, null);
                        } else {
                                Log.LogMessage (importance, singleLine);
                        }
                }
-               
-               private void ParseOrigin (string origin, out string filename,
-                                    out int lineNumber, out int columnNumber,
-                                    out int endLineNumber, out int endColumnNumber)
-               {
-                       int lParen;
-                       string[] temp;
-                       string[] left, right;
-                       
-                       if (origin.IndexOf ('(') != -1 ) {
-                               lParen = origin.IndexOf ('(');
-                               filename = origin.Substring (0, lParen);
-                               temp = origin.Substring (lParen + 1, origin.Length - lParen - 2).Split (',');
-                               if (temp.Length == 1) {
-                                       left = temp [0].Split ('-');
-                                       if (left.Length == 1) {
-                                               lineNumber = Int32.Parse (left [0]);
-                                               columnNumber = 0;
-                                               endLineNumber = 0;
-                                               endColumnNumber = 0;
-                                       } else if (left.Length == 2) {
-                                               lineNumber = Int32.Parse (left [0]);
-                                               columnNumber = 0;
-                                               endLineNumber = Int32.Parse (left [1]);
-                                               endColumnNumber = 0;
-                                       } else
-                                               throw new Exception ("Invalid line/column format.");
-                               } else if (temp.Length == 2) {
-                                       right = temp [1].Split ('-');
-                                       lineNumber = Int32.Parse (temp [0]);
-                                       endLineNumber = 0;
-                                       if (right.Length == 1) {
-                                               columnNumber = Int32.Parse (right [0]);
-                                               endColumnNumber = 0;
-                                       } else if (right.Length == 2) {
-                                               columnNumber = Int32.Parse (right [0]);
-                                               endColumnNumber = Int32.Parse (right [0]);
-                                       } else
-                                               throw new Exception ("Invalid line/column format.");
-                               } else if (temp.Length == 4) {
-                                       lineNumber = Int32.Parse (temp [0]);
-                                       endLineNumber = Int32.Parse (temp [2]);
-                                       columnNumber = Int32.Parse (temp [1]);
-                                       endColumnNumber = Int32.Parse (temp [3]);
-                               } else
-                                       throw new Exception ("Invalid line/column format.");
-                       } else {
-                               filename = origin;
-                               lineNumber = 0;
-                               columnNumber = 0;
-                               endLineNumber = 0;
-                               endColumnNumber = 0;
-                       }
-               }
 
                protected virtual string GenerateCommandLineCommands ()
                {
@@ -452,6 +388,17 @@ namespace Microsoft.Build.Utilities
                                        toolPath  = value;
                        }
                }
+
+               // Snatched from our codedom code, with some changes to make it compatible with csc
+               // (the line+column group is optional is csc)
+               static Regex errorRegex;
+               static Regex CscErrorRegex {
+                       get {
+                               if (errorRegex == null)
+                                       errorRegex = new Regex (@"^(\s*(?<file>[^\(]+)(\((?<line>\d*)(,(?<column>\d*[\+]*))?\))?:\s+)*(?<level>\w+)\s+(?<number>.*\d):\s*(?<message>.*)", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
+                               return errorRegex;
+                       }
+               }
        }
 }