Skip to content
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

Add diagnostic for Logging Generator: can't have malformed format strings #52040

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static class DiagnosticDescriptors

public static DiagnosticDescriptor MalformedFormatStrings { get; } = new DiagnosticDescriptor(
id: "SYSLIB1022",
title: new LocalizableResourceString(nameof(SR.MalformedFormatStringsMessage), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
title: new LocalizableResourceString(nameof(SR.MalformedFormatStringsTitle), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
messageFormat: new LocalizableResourceString(nameof(SR.MalformedFormatStringsMessage), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
category: "LoggingGenerator",
DiagnosticSeverity.Error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Text;

namespace Microsoft.Extensions.Logging.Generators
{
Expand Down Expand Up @@ -195,6 +196,16 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
Diag(DiagnosticDescriptors.RedundantQualifierInMessage, ma.GetLocation(), method.Identifier.ToString());
}

try
{
TemplateHelper.Parse(msg, lm.TemplateList);
}
catch (ArgumentException ex)
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
{
Diag(DiagnosticDescriptors.MalformedFormatStrings, ma.GetLocation(), ex.Message);
keepMethod = false;
}

bool foundLogger = false;
bool foundException = false;
bool foundLogLevel = level != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,11 @@
<data name="InconsistentTemplateCasingMessage" xml:space="preserve">
<value>Can't have the same template with different casing</value>
</data>
<data name="MalformedFormatStringsTitle" xml:space="preserve">
<value>Can't have malformed format strings (like dangling curly brackets, etc).</value>
</data>
<data name="MalformedFormatStringsMessage" xml:space="preserve">
<value>Can't have malformed format strings (like dangling {, etc)</value>
<value>Can't have malformed format strings (like dangling curly brackets, etc). {0}</value>
</data>
<data name="GeneratingForMax6ArgumentsMessage" xml:space="preserve">
<value>Generating more than 6 arguments is not supported</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsMessage">
<source>Can't have malformed format strings (like dangling {, etc)</source>
<target state="new">Can't have malformed format strings (like dangling {, etc)</target>
<source>Can't have malformed format strings (like dangling curly brackets, etc). {0}</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc). {0}</target>
<note />
</trans-unit>
<trans-unit id="MalformedFormatStringsTitle">
<source>Can't have malformed format strings (like dangling curly brackets, etc).</source>
<target state="new">Can't have malformed format strings (like dangling curly brackets, etc).</target>
<note />
</trans-unit>
<trans-unit id="MissingLogLevelMessage">
Expand Down
Loading