Skip to content

[generator] Fix MSBuild warning/error format for VS. #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/generator-Tests/Unit-Tests/ReportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MonoDroid.Generation;
using NUnit.Framework;

namespace generatortests
{
public class ReportTests
{
[Test]
public void FormatTests ()
{
var code = 0x37;
var msg = "There was a {0} error";
var args = "bad";
var sourcefile = @"C:\code\test.cs";
var line = 32;
var col = 12;

Assert.AreEqual ("error BG0037: There was a bad error", Report.Format (true, code, null, 0, 0, msg, args));
Assert.AreEqual (@"C:\code\test.cs: error BG0037: There was a bad error", Report.Format (true, code, sourcefile, 0, 0, msg, args));
Assert.AreEqual (@"C:\code\test.cs(32): error BG0037: There was a bad error", Report.Format (true, code, sourcefile, line, 0, msg, args));
Assert.AreEqual (@"C:\code\test.cs(32, 12): error BG0037: There was a bad error", Report.Format (true, code, sourcefile, line, col, msg, args));
Assert.AreEqual (@"C:\code\test.cs(32, 12): warning BG0037: There was a bad error", Report.Format (false, code, sourcefile, line, col, msg, args));
}
}
}
22 changes: 19 additions & 3 deletions tools/generator/Utilities/Report.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,25 @@ public static string FormatCodedMessage (bool error, LocalizedMessage message, p

public static string Format (bool error, int errorCode, string sourceFile, int line, int column, string format, params object[] args)
{
var origin = sourceFile != null ? sourceFile + (line > 0 ? column > 0 ? $"({line}, {column})" : $"({line})" : null) + ' ' : null;
return string.Format ("{0}{1} BG{2:X04}: ", origin, error ? "error" : "warning", errorCode) +
string.Format (format, args);
var origin = FormatOrigin (sourceFile, line, column);

return $"{origin}{(error ? "error" : "warning")} BG{errorCode:X04}: " + string.Format (format, args);
}

static string FormatOrigin (string sourceFile, int line, int column)
{
if (string.IsNullOrWhiteSpace (sourceFile))
return null;

var ret = sourceFile;

if (line == 0)
return ret + ": ";

if (column > 0)
return ret + $"({line}, {column}): ";

return ret + $"({line}): ";
}
}

Expand Down