Skip to content

Commit 876442f

Browse files
authored
[generator] Fix MSBuild warning/error format for Visual Studio (#765)
In b858dc5 we updated `generator` warnings/errors to give line & column information in more places. However, the existing method for formatting the line & column information was wrong: // Correct C:\code\Metadata.xml(2, 6): warning BG8A04: <attr path="/api/package[@name='androidx.appcompat.widget']/class[@name='RoundRectDrawableWithShadow']"/> matched no nodes. // Incorrect C:\code\Metadata.xml(2, 6) warning BG8A04: <attr path="/api/package[@name='androidx.appcompat.widget']/class[@name='RoundRectDrawableWithShadow']"/> matched no nodes. By omitting the colon after the line & column information, Visual Studio parses the colon within `C:\` instead, resulting in: ![image](https://user-images.githubusercontent.com/179295/102400384-7500b100-3fa7-11eb-8f35-aae2a04f3a58.png) * Filename: `C` * Line number: 1 This is actually worse than what we previously had, as double-clicking it does nothing, as `C` is not a valid file on disk. Rewrite the `Report.Format()` method to be a little clearer to read and add the required colon.
1 parent 3f6cf72 commit 876442f

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using MonoDroid.Generation;
7+
using NUnit.Framework;
8+
9+
namespace generatortests
10+
{
11+
public class ReportTests
12+
{
13+
[Test]
14+
public void FormatTests ()
15+
{
16+
var code = 0x37;
17+
var msg = "There was a {0} error";
18+
var args = "bad";
19+
var sourcefile = @"C:\code\test.cs";
20+
var line = 32;
21+
var col = 12;
22+
23+
Assert.AreEqual ("error BG0037: There was a bad error", Report.Format (true, code, null, 0, 0, msg, args));
24+
Assert.AreEqual (@"C:\code\test.cs: error BG0037: There was a bad error", Report.Format (true, code, sourcefile, 0, 0, msg, args));
25+
Assert.AreEqual (@"C:\code\test.cs(32): error BG0037: There was a bad error", Report.Format (true, code, sourcefile, line, 0, msg, args));
26+
Assert.AreEqual (@"C:\code\test.cs(32, 12): error BG0037: There was a bad error", Report.Format (true, code, sourcefile, line, col, msg, args));
27+
Assert.AreEqual (@"C:\code\test.cs(32, 12): warning BG0037: There was a bad error", Report.Format (false, code, sourcefile, line, col, msg, args));
28+
}
29+
}
30+
}

tools/generator/Utilities/Report.cs

+19-3
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,25 @@ public static string FormatCodedMessage (bool error, LocalizedMessage message, p
129129

130130
public static string Format (bool error, int errorCode, string sourceFile, int line, int column, string format, params object[] args)
131131
{
132-
var origin = sourceFile != null ? sourceFile + (line > 0 ? column > 0 ? $"({line}, {column})" : $"({line})" : null) + ' ' : null;
133-
return string.Format ("{0}{1} BG{2:X04}: ", origin, error ? "error" : "warning", errorCode) +
134-
string.Format (format, args);
132+
var origin = FormatOrigin (sourceFile, line, column);
133+
134+
return $"{origin}{(error ? "error" : "warning")} BG{errorCode:X04}: " + string.Format (format, args);
135+
}
136+
137+
static string FormatOrigin (string sourceFile, int line, int column)
138+
{
139+
if (string.IsNullOrWhiteSpace (sourceFile))
140+
return null;
141+
142+
var ret = sourceFile;
143+
144+
if (line == 0)
145+
return ret + ": ";
146+
147+
if (column > 0)
148+
return ret + $"({line}, {column}): ";
149+
150+
return ret + $"({line}): ";
135151
}
136152
}
137153

0 commit comments

Comments
 (0)