Skip to content

Commit 65d3d0a

Browse files
(Prototype)
Modifying DiagnosticsInfo for the case of only one argument a string.
1 parent b9dbe29 commit 65d3d0a

File tree

7 files changed

+112
-66
lines changed

7 files changed

+112
-66
lines changed

src/Compilers/CSharp/Portable/CSharpCodeAnalysis.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<DefineConstants>TRACE;DEBUG</DefineConstants>
4949
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
5050
<CodeAnalysisRuleSet>..\CSharpCodeAnalysisRules.ruleset</CodeAnalysisRuleSet>
51+
<LangVersion>6</LangVersion>
5152
</PropertyGroup>
5253
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
5354
<CodeAnalysisRuleSet>..\CSharpCodeAnalysisRules.ruleset</CodeAnalysisRuleSet>

src/Compilers/CSharp/Portable/Errors/SyntaxDiagnosticInfo.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ internal class SyntaxDiagnosticInfo : DiagnosticInfo
1111
internal readonly int Offset;
1212
internal readonly int Width;
1313

14-
internal SyntaxDiagnosticInfo(int offset, int width, ErrorCode code, params object[] args)
14+
15+
internal SyntaxDiagnosticInfo(int offset, int width, ErrorCode code, string arg)
16+
: base(CSharp.MessageProvider.Instance, (int)code, arg)
17+
{
18+
Debug.Assert(width >= 0);
19+
this.Offset = offset;
20+
this.Width = width;
21+
}
22+
internal SyntaxDiagnosticInfo(int offset, int width, ErrorCode code, params object[] args)
1523
: base(CSharp.MessageProvider.Instance, (int)code, args)
1624
{
1725
Debug.Assert(width >= 0);

src/Compilers/CSharp/Portable/Errors/XmlSyntaxDiagnosticInfo.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ namespace Microsoft.CodeAnalysis.CSharp
99
internal sealed class XmlSyntaxDiagnosticInfo : SyntaxDiagnosticInfo
1010
{
1111
private readonly XmlParseErrorCode xmlErrorCode;
12-
13-
internal XmlSyntaxDiagnosticInfo(XmlParseErrorCode code, params object[] args)
12+
internal XmlSyntaxDiagnosticInfo(XmlParseErrorCode code, string arg)
13+
: this(0, 0, code, arg)
14+
{
15+
}
16+
internal XmlSyntaxDiagnosticInfo(XmlParseErrorCode code, params object[] args)
1417
: this(0, 0, code, args)
1518
{
1619
}

src/Compilers/CSharp/Portable/Parser/AbstractLexer.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ protected SyntaxDiagnosticInfo[] GetErrors(int leadingTriviaWidth)
6060
return null;
6161
}
6262
}
63+
protected void AddError(int position, int width, ErrorCode code, string arg )
64+
{
65+
this.AddError(this.MakeError(position, width, code, arg));
66+
}
6367

64-
protected void AddError(int position, int width, ErrorCode code, params object[] args)
68+
protected void AddError(int position, int width, ErrorCode code, object[] args = null)
6569
{
6670
this.AddError(this.MakeError(position, width, code, args));
6771
}
6872

69-
protected void AddError(int position, int width, XmlParseErrorCode code, params object[] args)
73+
protected void AddError(int position, int width, XmlParseErrorCode code, object[] args = null)
7074
{
7175
this.AddError(this.MakeError(position, width, code, args));
7276
}
@@ -77,7 +81,7 @@ protected void AddError(ErrorCode code, params object[] args)
7781
}
7882

7983

80-
protected void AddError(XmlParseErrorCode code, params object[] args)
84+
protected void AddError(XmlParseErrorCode code, object[] args = null)
8185
{
8286
this.AddError(MakeError(code, args));
8387
}
@@ -95,13 +99,21 @@ protected void AddError(SyntaxDiagnosticInfo error)
9599
}
96100
}
97101

98-
protected SyntaxDiagnosticInfo MakeError(int position, int width, ErrorCode code, params object[] args)
102+
103+
protected SyntaxDiagnosticInfo MakeError(int position, int width, ErrorCode code, string arg )
104+
{
105+
int offset = GetLexemeOffsetFromPosition(position);
106+
return new SyntaxDiagnosticInfo(offset, width, code, arg);
107+
}
108+
109+
110+
protected SyntaxDiagnosticInfo MakeError(int position, int width, ErrorCode code, object[] args = null)
99111
{
100112
int offset = GetLexemeOffsetFromPosition(position);
101-
return new SyntaxDiagnosticInfo(offset, width, code, args);
113+
return new SyntaxDiagnosticInfo( offset, width, code, args);
102114
}
103115

104-
protected XmlSyntaxDiagnosticInfo MakeError(int position, int width, XmlParseErrorCode code, params object[] args)
116+
protected XmlSyntaxDiagnosticInfo MakeError(int position, int width, XmlParseErrorCode code, object[] args = null)
105117
{
106118
int offset = GetLexemeOffsetFromPosition(position);
107119
return new XmlSyntaxDiagnosticInfo(offset, width, code, args);
@@ -112,14 +124,23 @@ private int GetLexemeOffsetFromPosition(int position)
112124
return position >= TextWindow.LexemeStartPosition ? position - TextWindow.LexemeStartPosition : position;
113125
}
114126

115-
protected static SyntaxDiagnosticInfo MakeError(ErrorCode code, params object[] args)
127+
protected static SyntaxDiagnosticInfo MakeError(ErrorCode code, string arg)
128+
{
129+
return new SyntaxDiagnosticInfo(code, arg);
130+
}
131+
132+
protected static SyntaxDiagnosticInfo MakeError(ErrorCode code, object[] args = null)
116133
{
117134
return new SyntaxDiagnosticInfo(code, args);
118135
}
119136

120-
protected static XmlSyntaxDiagnosticInfo MakeError(XmlParseErrorCode code, params object[] args)
137+
protected static XmlSyntaxDiagnosticInfo MakeError(XmlParseErrorCode code, object[] args = null)
121138
{
122139
return new XmlSyntaxDiagnosticInfo(0, 0, code, args);
123140
}
141+
protected static XmlSyntaxDiagnosticInfo MakeError(XmlParseErrorCode code, string arg)
142+
{
143+
return new XmlSyntaxDiagnosticInfo(0, 0, code, arg);
124144
}
145+
}
125146
}

src/Compilers/CSharp/Portable/Parser/Lexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ private double GetValueDouble(string text)
12431243
if (!Double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out result))
12441244
{
12451245
//we've already lexed the literal, so the error must be from overflow
1246-
this.AddError(MakeError(ErrorCode.ERR_FloatOverflow, "double"));
1246+
this.AddError(MakeError(ErrorCode.ERR_FloatOverflow, "double" ));
12471247
}
12481248

12491249
return result;

src/Compilers/CSharp/Portable/Parser/Lexer_StringLiteral.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ private void ScanInterpolatedStringLiteralContents(ArrayBuilder<Interpolation> i
423423
char ch = lexer.ScanEscapeSequence(out c2);
424424
if ((ch == '{' || ch == '}') && error == null)
425425
{
426-
error = lexer.MakeError(escapeStart, lexer.TextWindow.Position - escapeStart, ErrorCode.ERR_EscapedCurly, ch);
426+
error = lexer.MakeError(escapeStart, lexer.TextWindow.Position - escapeStart, ErrorCode.ERR_EscapedCurly, ch.ToString());
427427
}
428428

429429
continue;
@@ -450,7 +450,7 @@ private void ScanFormatSpecifier()
450450
ch = lexer.ScanEscapeSequence(out c2);
451451
if ((ch == '{' || ch == '}') && error == null)
452452
{
453-
error = lexer.MakeError(pos, 1, ErrorCode.ERR_EscapedCurly, ch);
453+
error = lexer.MakeError(pos, 1, ErrorCode.ERR_EscapedCurly, ch.ToString());
454454
}
455455
}
456456
else if (ch == '"')

src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal partial class DiagnosticInfo : IFormattable, IObjectWritable, IObjectRe
2626
private readonly DiagnosticSeverity _defaultSeverity;
2727
private readonly DiagnosticSeverity _effectiveSeverity;
2828
private readonly object[] _arguments;
29+
private string _arg = null;
2930

3031
private static ImmutableDictionary<int, DiagnosticDescriptor> s_errorCodeToDescriptorMap = ImmutableDictionary<int, DiagnosticDescriptor>.Empty;
3132

@@ -41,10 +42,19 @@ internal DiagnosticInfo(CommonMessageProvider messageProvider, int errorCode)
4142
_defaultSeverity = messageProvider.GetSeverity(errorCode);
4243
_effectiveSeverity = _defaultSeverity;
4344
}
45+
// Only the compiler creates instances.
46+
internal DiagnosticInfo(CommonMessageProvider messageProvider, int errorCode, string arg)
47+
{
48+
_messageProvider = messageProvider;
49+
_errorCode = errorCode;
50+
_defaultSeverity = messageProvider.GetSeverity(errorCode);
51+
_effectiveSeverity = _defaultSeverity;
52+
_arg = arg;
53+
}
4454

4555
// Only the compiler creates instances.
4656
internal DiagnosticInfo(CommonMessageProvider messageProvider, int errorCode, params object[] arguments)
47-
: this(messageProvider, errorCode)
57+
: this(messageProvider, errorCode)
4858
{
4959
AssertMessageSerializable(arguments);
5060

@@ -53,11 +63,10 @@ internal DiagnosticInfo(CommonMessageProvider messageProvider, int errorCode, pa
5363

5464
private DiagnosticInfo(DiagnosticInfo original, DiagnosticSeverity overridenSeverity)
5565
{
56-
_messageProvider = original.MessageProvider;
57-
_errorCode = original._errorCode;
58-
_defaultSeverity = original.DefaultSeverity;
59-
_arguments = original._arguments;
60-
66+
_messageProvider = original.MessageProvider;
67+
_errorCode = original._errorCode;
68+
_defaultSeverity = original.DefaultSeverity;
69+
_arguments = original._arguments;
6170
_effectiveSeverity = overridenSeverity;
6271
}
6372

@@ -74,13 +83,13 @@ private static DiagnosticDescriptor GetOrCreateDescriptor(int errorCode, Diagnos
7483

7584
private static DiagnosticDescriptor CreateDescriptor(int errorCode, DiagnosticSeverity defaultSeverity, CommonMessageProvider messageProvider)
7685
{
77-
var id = messageProvider.GetIdForErrorCode(errorCode);
78-
var title = messageProvider.GetTitle(errorCode);
79-
var description = messageProvider.GetDescription(errorCode);
86+
var id = messageProvider.GetIdForErrorCode(errorCode);
87+
var title = messageProvider.GetTitle(errorCode);
88+
var description = messageProvider.GetDescription(errorCode);
8089
var messageFormat = messageProvider.GetMessageFormat(errorCode);
81-
var helpLink = messageProvider.GetHelpLink(errorCode);
82-
var category = messageProvider.GetCategory(errorCode);
83-
var customTags = GetCustomTags(defaultSeverity);
90+
var helpLink = messageProvider.GetHelpLink(errorCode);
91+
var category = messageProvider.GetCategory(errorCode);
92+
var customTags = GetCustomTags(defaultSeverity);
8493
return new DiagnosticDescriptor(id, title, messageFormat, category, defaultSeverity,
8594
isEnabledByDefault: true, description: description, helpLinkUri: helpLink, customTags: customTags);
8695
}
@@ -91,25 +100,16 @@ internal static void AssertMessageSerializable(object[] args)
91100
foreach (var arg in args)
92101
{
93102
Debug.Assert(arg != null);
94-
95-
if (arg is IMessageSerializable)
96-
{
97-
continue;
98-
}
103+
if (arg is IMessageSerializable) continue;
99104

100105
var type = arg.GetType();
101-
if (type == typeof(string) || type == typeof(AssemblyIdentity))
102-
{
103-
continue;
104-
}
106+
if (type == typeof(string) || type == typeof(AssemblyIdentity)) continue;
105107

106108
var info = type.GetTypeInfo();
107-
if (info.IsPrimitive)
108-
{
109-
continue;
110-
}
109+
if (info.IsPrimitive) continue;
110+
111111

112-
Debug.Assert(false, "Unexpected type: " + type);
112+
Debug.Assert(false, $"Unexpected type: {type}");
113113
}
114114
}
115115

@@ -119,10 +119,8 @@ internal DiagnosticInfo(CommonMessageProvider messageProvider, bool isWarningAsE
119119
{
120120
Debug.Assert(!isWarningAsError || _defaultSeverity == DiagnosticSeverity.Warning);
121121

122-
if (isWarningAsError)
123-
{
124-
_effectiveSeverity = DiagnosticSeverity.Error;
125-
}
122+
if (isWarningAsError) _effectiveSeverity = DiagnosticSeverity.Error;
123+
126124
}
127125

128126
// Create a copy of this instance with a explicit overridden severity
@@ -155,12 +153,20 @@ protected virtual void WriteTo(ObjectWriter writer)
155153
writer.WriteString(arg.ToString());
156154
}
157155
}
156+
else if (count == 0)
157+
{
158+
if (_arg != null)
159+
{
160+
writer.WriteString(_arg);
161+
}
162+
}
158163
}
159164

160165
Func<ObjectReader, object> IObjectReadable.GetReader()
161166
{
162167
return this.GetReader();
163168
}
169+
164170

165171
protected virtual Func<ObjectReader, object> GetReader()
166172
{
@@ -341,26 +347,33 @@ public virtual string GetMessage(IFormatProvider formatProvider = null)
341347

342348
protected object[] GetArgumentsToUse(IFormatProvider formatProvider)
343349
{
344-
object[] argumentsToUse = null;
345-
for (int i = 0; i < _arguments.Length; i++)
350+
if (_arguments != null)
346351
{
347-
var embedded = _arguments[i] as DiagnosticInfo;
348-
if (embedded != null)
352+
object[] argumentsToUse = null;
353+
for (int i = 0; i < _arguments.Length; i++)
349354
{
350-
argumentsToUse = InitializeArgumentListIfNeeded(argumentsToUse);
351-
argumentsToUse[i] = embedded.GetMessage(formatProvider);
352-
continue;
353-
}
355+
var embedded = _arguments[i] as DiagnosticInfo;
356+
if (embedded != null)
357+
{
358+
argumentsToUse = InitializeArgumentListIfNeeded(argumentsToUse);
359+
argumentsToUse[i] = embedded.GetMessage(formatProvider);
360+
continue;
361+
}
354362

355-
var symbol = _arguments[i] as ISymbol;
356-
if (symbol != null)
357-
{
358-
argumentsToUse = InitializeArgumentListIfNeeded(argumentsToUse);
359-
argumentsToUse[i] = _messageProvider.ConvertSymbolToString(_errorCode, symbol);
363+
var symbol = _arguments[i] as ISymbol;
364+
if (symbol != null)
365+
{
366+
argumentsToUse = InitializeArgumentListIfNeeded(argumentsToUse);
367+
argumentsToUse[i] = _messageProvider.ConvertSymbolToString(_errorCode, symbol);
368+
}
360369
}
361-
}
362370

363-
return argumentsToUse ?? _arguments;
371+
return argumentsToUse ?? _arguments;
372+
}
373+
else
374+
{
375+
return new object[] { _arg };
376+
}
364377
}
365378

366379
private object[] InitializeArgumentListIfNeeded(object[] argumentsToUse)
@@ -378,7 +391,7 @@ private object[] InitializeArgumentListIfNeeded(object[] argumentsToUse)
378391

379392
internal object[] Arguments
380393
{
381-
get { return _arguments; }
394+
get { return (_arg == null) ? _arguments : new object[] { _arg }; }
382395
}
383396

384397
internal CommonMessageProvider MessageProvider
@@ -455,14 +468,14 @@ private string GetDebuggerDisplay()
455468
// sure we don't call ToString for those.
456469
switch (Code)
457470
{
458-
case InternalErrorCode.Unknown:
459-
return "Unresolved DiagnosticInfo";
471+
case InternalErrorCode.Unknown:
472+
return "Unresolved DiagnosticInfo";
460473

461-
case InternalErrorCode.Void:
462-
return "Void DiagnosticInfo";
474+
case InternalErrorCode.Void:
475+
return "Void DiagnosticInfo";
463476

464-
default:
465-
return ToString();
477+
default:
478+
return ToString();
466479
}
467480
}
468481

0 commit comments

Comments
 (0)