Skip to content

Commit fe9009a

Browse files
committed
[generator] report warning/error location within XML metadata fixup.
1 parent 4aa2cde commit fe9009a

File tree

2 files changed

+76
-22
lines changed

2 files changed

+76
-22
lines changed

tools/generator/ApiFixup.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ void Process (XmlDocument meta_doc, string apiLevelString, int productVersion)
9191
api_node.ParentNode.RemoveChild (api_node);
9292
if (matches.Count == 0)
9393
// BG8A00
94-
Report.Warning (0, Report.WarningApiFixup + 0, "<remove-node path=\"{0}\"/> matched no nodes.", path);
94+
Report.Warning (0, Report.WarningApiFixup + 0, null, metanav, "<remove-node path=\"{0}\"/> matched no nodes.", path);
9595
} catch (XPathException e) {
9696
// BG4A01
97-
Report.Error (Report.ErrorApiFixup + 1, e, "Invalid XPath specification: {0}", path);
97+
Report.Error (Report.ErrorApiFixup + 1, e, metanav, "Invalid XPath specification: {0}", path);
9898
}
9999
break;
100100
case "add-node":
@@ -109,10 +109,10 @@ void Process (XmlDocument meta_doc, string apiLevelString, int productVersion)
109109
}
110110
if (!matched)
111111
// BG8A01
112-
Report.Warning (0, Report.WarningApiFixup + 1, "<add-node path=\"{0}\"/> matched no nodes.", path);
112+
Report.Warning (0, Report.WarningApiFixup + 1, null, metanav, "<add-node path=\"{0}\"/> matched no nodes.", path);
113113
} catch (XPathException e) {
114114
// BG4A02
115-
Report.Error (Report.ErrorApiFixup + 2, e, "Invalid XPath specification: {0}", path);
115+
Report.Error (Report.ErrorApiFixup + 2, e, metanav, "Invalid XPath specification: {0}", path);
116116
}
117117
break;
118118
case "change-node":
@@ -135,18 +135,18 @@ void Process (XmlDocument meta_doc, string apiLevelString, int productVersion)
135135

136136
if (!matched)
137137
// BG8A03
138-
Report.Warning (0, Report.WarningApiFixup + 3, "<change-node-type path=\"{0}\"/> matched no nodes.", path);
138+
Report.Warning (0, Report.WarningApiFixup + 3, null, metanav, "<change-node-type path=\"{0}\"/> matched no nodes.", path);
139139
} catch (XPathException e) {
140140
// BG4A03
141-
Report.Error (Report.ErrorApiFixup + 3, e, "Invalid XPath specification: {0}", path);
141+
Report.Error (Report.ErrorApiFixup + 3, e, metanav, "Invalid XPath specification: {0}", path);
142142
}
143143
break;
144144
case "attr":
145145
try {
146146
string attr_name = metanav.XGetAttribute ("name", "");
147147
if (string.IsNullOrEmpty (attr_name))
148148
// BG4A07
149-
Report.Error (Report.ErrorApiFixup + 7, "Target attribute name is not specified for path: {0}", path);
149+
Report.Error (Report.ErrorApiFixup + 7, null, metanav, "Target attribute name is not specified for path: {0}", path);
150150
var nodes = attr_last_cache != null ?
151151
(IEnumerable<XPathNavigator>) new XPathNavigator [] {attr_last_cache} :
152152
api_nav.Select (path).OfType<XPathNavigator> ();
@@ -159,12 +159,12 @@ void Process (XmlDocument meta_doc, string apiLevelString, int productVersion)
159159
}
160160
if (attr_matched == 0)
161161
// BG8A04
162-
Report.Warning (0, Report.WarningApiFixup + 4, "<attr path=\"{0}\"/> matched no nodes.", path);
162+
Report.Warning (0, Report.WarningApiFixup + 4, null, metanav, "<attr path=\"{0}\"/> matched no nodes.", path);
163163
if (attr_matched != 1)
164164
attr_last_cache = null;
165165
} catch (XPathException e) {
166166
// BG4A04
167-
Report.Error (Report.ErrorApiFixup + 4, e, "Invalid XPath specification: {0}", path);
167+
Report.Error (Report.ErrorApiFixup + 4, e, metanav, "Invalid XPath specification: {0}", path);
168168
}
169169
break;
170170
case "move-node":
@@ -185,10 +185,10 @@ void Process (XmlDocument meta_doc, string apiLevelString, int productVersion)
185185
}
186186
if (!matched)
187187
// BG8A05
188-
Report.Warning (0, Report.WarningApiFixup + 5, "<move-node path=\"{0}\"/> matched no nodes.", path);
188+
Report.Warning (0, Report.WarningApiFixup + 5, null, metanav, "<move-node path=\"{0}\"/> matched no nodes.", path);
189189
} catch (XPathException e) {
190190
// BG4A05
191-
Report.Error (Report.ErrorApiFixup + 5, e, "Invalid XPath specification: {0}", path);
191+
Report.Error (Report.ErrorApiFixup + 5, e, metanav, "Invalid XPath specification: {0}", path);
192192
}
193193
break;
194194
case "remove-attr":
@@ -206,10 +206,10 @@ void Process (XmlDocument meta_doc, string apiLevelString, int productVersion)
206206

207207
if (!matched)
208208
// BG8A06
209-
Report.Warning (0, Report.WarningApiFixup + 6, "<remove-attr path=\"{0}\"/> matched no nodes.", path);
209+
Report.Warning (0, Report.WarningApiFixup + 6, null, metanav, "<remove-attr path=\"{0}\"/> matched no nodes.", path);
210210
} catch (XPathException e) {
211211
// BG4A06
212-
Report.Error (Report.ErrorApiFixup + 6, e, "Invalid XPath specification: {0}", path);
212+
Report.Error (Report.ErrorApiFixup + 6, e, metanav, "Invalid XPath specification: {0}", path);
213213
}
214214
break;
215215
}

tools/generator/Report.cs

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Linq;
3+
using System.Xml;
4+
using System.Xml.XPath;
35

46
namespace MonoDroid.Generation
57
{
@@ -31,25 +33,67 @@ public class Report
3133

3234
public static void Error (int errorCode, string format, params object[] args)
3335
{
34-
Error (errorCode, null, format, args);
36+
Error (errorCode, null, null, -1, -1, format, args);
3537
}
36-
37-
public static void Error (int errorCode, Exception innerException, string format, params object[] args)
38+
39+
public static void Error (int errorCode, string sourceFile, int line, int column, string format, params object [] args)
3840
{
39-
throw new BindingGeneratorException (errorCode, string.Format (format, args), innerException);
41+
Error (errorCode, null, sourceFile, line, column, format, args);
42+
}
43+
44+
public static void Error (int errorCode, Exception innerException, string format, params object [] args)
45+
{
46+
Error (errorCode, innerException, null, -1, -1, format, args);
47+
}
48+
49+
public static void Error (int errorCode, Exception innerException, XPathNavigator nav, string format, params object [] args)
50+
{
51+
var hn = nav as IHasXmlNode;
52+
var baseUri = hn != null ? hn.GetNode ().OwnerDocument.BaseURI : nav.BaseURI;
53+
Uri uri;
54+
string file = Uri.TryCreate (baseUri, UriKind.Absolute, out uri) ? uri.LocalPath : null;
55+
IXmlLineInfo li = nav as IXmlLineInfo;
56+
li = li != null && li.HasLineInfo () ? li : null;
57+
Error (errorCode, innerException, file, li != null ? li.LineNumber : -1, li != null ? li.LinePosition : -1, format, args);
58+
}
59+
60+
public static void Error (int errorCode, Exception innerException, string sourceFile, int line, int column, string format, params object[] args)
61+
{
62+
throw new BindingGeneratorException (errorCode, sourceFile, line, column, string.Format (format, args), innerException);
4063
}
4164

4265
public static void Warning (int verbosity, int errorCode, string format, params object[] args)
4366
{
4467
Warning (verbosity, errorCode, null, format, args);
4568
}
69+
70+
public static void Warning (int verbosity, int errorCode, Exception innerException, XPathNavigator nav, string format, params object [] args)
71+
{
72+
var hn = nav as IHasXmlNode;
73+
var baseUri = hn != null ? hn.GetNode ().OwnerDocument.BaseURI : nav.BaseURI;
74+
Uri uri;
75+
string file = Uri.TryCreate (baseUri, UriKind.Absolute, out uri) ? uri.LocalPath : null;
76+
IXmlLineInfo li = nav as IXmlLineInfo;
77+
li = li != null && li.HasLineInfo () ? li : null;
78+
Warning (verbosity, errorCode, innerException, file, li != null ? li.LineNumber : -1, li != null ? li.LinePosition : -1, format, args);
79+
}
80+
81+
public static void Warning (int verbosity, int errorCode, string sourceFile, int line, int column, string format, params object[] args)
82+
{
83+
Warning (verbosity, errorCode, null, sourceFile, line, column, format, args);
84+
}
85+
86+
public static void Warning (int verbosity, int errorCode, Exception innerException, string format, params object [] args)
87+
{
88+
Warning (verbosity, errorCode, innerException, null, -1, -1, format, args);
89+
}
4690

47-
public static void Warning (int verbosity, int errorCode, Exception innerException, string format, params object[] args)
91+
public static void Warning (int verbosity, int errorCode, Exception innerException, string sourceFile, int line, int column, string format, params object[] args)
4892
{
4993
if (verbosity > (Verbosity ?? 0))
5094
return;
5195
string supp = innerException != null ? " For details, see verbose output." : null;
52-
Console.Error.WriteLine (Format (false, errorCode, format, args) + supp);
96+
Console.Error.WriteLine (Format (false, errorCode, sourceFile, line, column, format, args) + supp);
5397
if (innerException != null)
5498
Console.Error.WriteLine (innerException);
5599
}
@@ -60,10 +104,16 @@ public static void Verbose (int verbosity, string format, params object[] args)
60104
return;
61105
Console.Error.WriteLine (format, args);
62106
}
107+
108+
public static string Format (bool error, int errorCode, string format, params object [] args)
109+
{
110+
return Format (error, errorCode, null, -1, -1, format, args);
111+
}
63112

64-
public static string Format (bool error, int errorCode, string format, params object[] args)
113+
public static string Format (bool error, int errorCode, string sourceFile, int line, int column, string format, params object[] args)
65114
{
66-
return string.Format ("{0}{1} BG{2:X04}: ", "" /* origin? */, error ? "error" : "warning", errorCode) +
115+
var origin = sourceFile != null ? sourceFile + (line > 0 ? column > 0 ? $"({line}, {column})" : $"({line})" : null) + ' ' : null;
116+
return string.Format ("{0}{1} BG{2:X04}: ", origin, error ? "error" : "warning", errorCode) +
67117
string.Format (format, args);
68118
}
69119
}
@@ -75,7 +125,11 @@ public BindingGeneratorException (int errorCode, string message)
75125
{
76126
}
77127
public BindingGeneratorException (int errorCode, string message, Exception innerException)
78-
: base (Report.Format (true, errorCode, message), innerException)
128+
: this (errorCode, null, -1, -1, message, innerException)
129+
{
130+
}
131+
public BindingGeneratorException (int errorCode, string sourceFile, int line, int column, string message, Exception innerException)
132+
: base (Report.Format (true, errorCode, sourceFile, line, column, message), innerException)
79133
{
80134
}
81135
}

0 commit comments

Comments
 (0)