提交 52560b0a 编写于 作者: M Manish Vasani

Gracefully handle format exception from Diagnostic.GetMessage() when the...

Gracefully handle format exception from Diagnostic.GetMessage() when the analyzer reports a diagnostic with incorrect format arguments.

Fixes #4589
上级 5546c6a9
......@@ -6688,6 +6688,28 @@ public void AnalyzerDiagnosticThrowsInGetMessage()
CleanupAllGeneratedFiles(srcFile.Path);
}
[Fact]
[WorkItem(4589, "https://github.com/dotnet/roslyn/issues/4589")]
public void AnalyzerReportsMisformattedDiagnostic()
{
var srcFile = Temp.CreateFile().WriteAllText(@"class C {}");
var srcDirectory = Path.GetDirectoryName(srcFile.Path);
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var csc = new MockCSharpCompiler(null, _baseDirectory, new[] { "/t:library", srcFile.Path },
analyzer: new AnalyzerReportingMisformattedDiagnostic());
var exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);
var output = outWriter.ToString();
// Verify that the diagnostic reported by AnalyzerReportingMisformattedDiagnostic is reported with the message format string, instead of the formatted message.
Assert.Contains(AnalyzerThatThrowsInGetMessage.Rule.Id, output, StringComparison.Ordinal);
Assert.Contains(AnalyzerThatThrowsInGetMessage.Rule.MessageFormat.ToString(CultureInfo.InvariantCulture), output, StringComparison.Ordinal);
CleanupAllGeneratedFiles(srcFile.Path);
}
[Fact]
public void ErrorPathsFromLineDirectives()
{
......
......@@ -99,7 +99,16 @@ public override string GetMessage(IFormatProvider formatProvider = null)
}
var localizedMessageFormat = _descriptor.MessageFormat.ToString(formatProvider);
return string.Format(formatProvider, localizedMessageFormat, _messageArgs);
try
{
return string.Format(formatProvider, localizedMessageFormat, _messageArgs);
}
catch (Exception)
{
// Analyzer reported diagnostic with invalid format arguments, so just return the unformatted message.
return localizedMessageFormat;
}
}
internal override IReadOnlyList<object> Arguments
......
......@@ -7231,6 +7231,28 @@ End Class
CleanupAllGeneratedFiles(source)
End Sub
<Fact>
<WorkItem(4589, "https://github.com/dotnet/roslyn/issues/4589")>
Public Sub AnalyzerReportsMisformattedDiagnostic()
Dim source As String = Temp.CreateFile().WriteAllText(<text>
Class C
End Class
</text>.Value).Path
Dim vbc = New MockVisualBasicCompiler(Nothing, _baseDirectory, {"/t:library", source},
analyzer:=New AnalyzerReportingMisformattedDiagnostic)
Dim outWriter = New StringWriter()
Dim exitCode = vbc.Run(outWriter, Nothing)
Assert.Equal(0, exitCode)
Dim output = outWriter.ToString()
' Verify that the diagnostic reported by AnalyzerReportingMisformattedDiagnostic is reported with the message format string, instead of the formatted message.
Assert.Contains(AnalyzerThatThrowsInGetMessage.Rule.Id, output, StringComparison.Ordinal)
Assert.Contains(AnalyzerThatThrowsInGetMessage.Rule.MessageFormat.ToString(CultureInfo.InvariantCulture), output, StringComparison.Ordinal)
CleanupAllGeneratedFiles(source)
End Sub
<Fact>
Public Sub AdditionalFileDiagnostics()
Dim dir = Temp.CreateDirectory()
......
......@@ -358,6 +358,28 @@ protected override string GetText(IFormatProvider formatProvider)
}
}
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class AnalyzerReportingMisformattedDiagnostic : DiagnosticAnalyzer
{
public static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
"ID1",
"Title1",
"Symbol Name: {0}, Extra argument: {1}",
"Category1",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.RegisterSymbolAction(symbolContext =>
{
// Report diagnostic with incorrect number of message format arguments.
symbolContext.ReportDiagnostic(Diagnostic.Create(Rule, symbolContext.Symbol.Locations[0], symbolContext.Symbol.Name));
}, SymbolKind.NamedType);
}
}
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public class HiddenDiagnosticsCompilationAnalyzer : DiagnosticAnalyzer
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册