Skip to content

Commit

Permalink
Only the compiler analyzer can use CS or BC prefix for diagnostics (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Dec 14, 2017
1 parent 49ada5d commit 850f809
Show file tree
Hide file tree
Showing 31 changed files with 196 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,36 @@ public void TestReportingDiagnosticWithInvalidId()
.WithLocation(1, 1));
}

[Fact, WorkItem(23667, "https://github.com/dotnet/roslyn/issues/23667")]
public void TestReportingDiagnosticWithCSharpCompilerId()
{
string source = @"";
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerWithCSharpCompilerDiagnosticId() };
string message = new ArgumentException(string.Format(CodeAnalysisResources.CompilerDiagnosticIdReported, AnalyzerWithCSharpCompilerDiagnosticId.Descriptor.Id), "diagnostic").Message;

CreateCompilationWithMscorlib45(source)
.VerifyDiagnostics()
.VerifyAnalyzerDiagnostics(analyzers, null, null, logAnalyzerExceptionAsDiagnostics: true,
expected: Diagnostic("AD0001")
.WithArguments("Microsoft.CodeAnalysis.CommonDiagnosticAnalyzers+AnalyzerWithCSharpCompilerDiagnosticId", "System.ArgumentException", message)
.WithLocation(1, 1));
}

[Fact, WorkItem(23667, "https://github.com/dotnet/roslyn/issues/23667")]
public void TestReportingDiagnosticWithBasicCompilerId()
{
string source = @"";
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerWithBasicCompilerDiagnosticId() };
string message = new ArgumentException(string.Format(CodeAnalysisResources.CompilerDiagnosticIdReported, AnalyzerWithBasicCompilerDiagnosticId.Descriptor.Id), "diagnostic").Message;

CreateCompilationWithMscorlib45(source)
.VerifyDiagnostics()
.VerifyAnalyzerDiagnostics(analyzers, null, null, logAnalyzerExceptionAsDiagnostics: true,
expected: Diagnostic("AD0001")
.WithArguments("Microsoft.CodeAnalysis.CommonDiagnosticAnalyzers+AnalyzerWithBasicCompilerDiagnosticId", "System.ArgumentException", message)
.WithLocation(1, 1));
}

[Fact, WorkItem(7173, "https://github.com/dotnet/roslyn/issues/7173")]
public void TestReportingDiagnosticWithInvalidLocation()
{
Expand Down
9 changes: 9 additions & 0 deletions src/Compilers/Core/Portable/CodeAnalysisResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Compilers/Core/Portable/CodeAnalysisResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@
<data name="InvalidDiagnosticIdReported" xml:space="preserve">
<value>Reported diagnostic has an ID '{0}', which is not a valid identifier.</value>
</data>
<data name="CompilerDiagnosticIdReported" xml:space="preserve">
<value>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</value>
</data>
<data name="InvalidDiagnosticLocationReported" xml:space="preserve">
<value>Reported diagnostic '{0}' has a source location in file '{1}', which is not part of the compilation being analyzed.</value>
</data>
Expand Down
17 changes: 17 additions & 0 deletions src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ internal bool IsSupportedDiagnostic(DiagnosticAnalyzer analyzer, Diagnostic diag
return true;
}

if (IsCompilerReservedDiagnostic(diagnostic.Id))
{
throw new ArgumentException(string.Format(CodeAnalysisResources.CompilerDiagnosticIdReported, diagnostic.Id), nameof(diagnostic));
}

// Get all the supported diagnostics and scan them linearly to see if the reported diagnostic is supported by the analyzer.
// The linear scan is okay, given that this runs only if a diagnostic is being reported and a given analyzer is quite unlikely to have hundreds of thousands of supported diagnostics.
var supportedDescriptors = GetSupportedDiagnosticDescriptors(analyzer, analyzerExecutor);
Expand All @@ -182,6 +187,18 @@ internal bool IsSupportedDiagnostic(DiagnosticAnalyzer analyzer, Diagnostic diag
return false;
}

private static bool IsCompilerReservedDiagnostic(string id)
{
// Only the compiler analyzer should produce diagnostics with CS or BC prefixes (followed by digit)
if (id.Length >= 3 && (id.StartsWith("CS", StringComparison.Ordinal) || id.StartsWith("BC", StringComparison.Ordinal)))
{
char thirdChar = id[2];
return thirdChar >= '0' && thirdChar <= '9';
}

return false;
}

/// <summary>
/// Returns true if all the diagnostics that can be produced by this analyzer are suppressed through options.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/xlf/CodeAnalysisResources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@
<target state="new">Warning: Could not enable multicore JIT due to exception: {0}.</target>
<note />
</trans-unit>
<trans-unit id="CompilerDiagnosticIdReported">
<source>Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</source>
<target state="new">Reported diagnostic has an ID '{0}', which only a compiler should be reporting.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Composition;
using Microsoft.CodeAnalysis.AddImport;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Packaging;
using Microsoft.CodeAnalysis.SymbolSearch;

Expand Down Expand Up @@ -122,7 +123,8 @@ internal static class AddImportDiagnosticIds
CS0616,
CS1580,
CS1581,
CS8129);
CS8129,
IDEDiagnosticIds.UnboundIdentifierId);

public static ImmutableArray<string> FixableDiagnosticIds =
FixableTypeIds.Concat(ImmutableArray.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.LanguageServices;
Expand Down Expand Up @@ -153,6 +154,7 @@ protected override bool CanAddImportForType(string diagnosticId, SyntaxNode node
switch (diagnosticId)
{
case CS0103:
case IDEDiagnosticIds.UnboundIdentifierId:
case CS0246:
case CS0305:
case CS0308:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CodeFixes.GenerateMember;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.GenerateType;
using Microsoft.CodeAnalysis.Shared.Extensions;

Expand All @@ -29,7 +30,7 @@ internal class GenerateTypeCodeFixProvider : AbstractGenerateMemberCodeFixProvid

public override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create(CS0103, CS0117, CS0234, CS0246, CS0305, CS0308, CS0426, CS0616); }
get { return ImmutableArray.Create(CS0103, CS0117, CS0234, CS0246, CS0305, CS0308, CS0426, CS0616, IDEDiagnosticIds.UnboundIdentifierId); }
}

protected override bool IsCandidate(SyntaxNode node, SyntaxToken token, Diagnostic diagnostic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ namespace Microsoft.CodeAnalysis.CSharp.Diagnostics
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CSharpUnboundIdentifiersDiagnosticAnalyzer : UnboundIdentifiersDiagnosticAnalyzerBase<SyntaxKind, SimpleNameSyntax, QualifiedNameSyntax, IncompleteMemberSyntax, LambdaExpressionSyntax>
{
private const string NameNotInContext = "CS0103";
private readonly LocalizableString _nameNotInContextMessageFormat = new LocalizableResourceString(nameof(CSharpFeaturesResources.The_name_0_does_not_exist_in_the_current_context), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources));
private readonly LocalizableString _nameNotInContextMessageFormat =
new LocalizableResourceString(nameof(CSharpFeaturesResources.The_name_0_does_not_exist_in_the_current_context), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources));

private const string ConstructorOverloadResolutionFailure = "CS1729";
private readonly LocalizableString _constructorOverloadResolutionFailureMessageFormat = new LocalizableResourceString(nameof(CSharpFeaturesResources._0_does_not_contain_a_constructor_that_takes_that_many_arguments), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources));
private readonly LocalizableString _constructorOverloadResolutionFailureMessageFormat =
new LocalizableResourceString(nameof(CSharpFeaturesResources._0_does_not_contain_a_constructor_that_takes_that_many_arguments), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources));

private static readonly ImmutableArray<SyntaxKind> s_kindsOfInterest = ImmutableArray.Create(SyntaxKind.IncompleteMember, SyntaxKind.ParenthesizedLambdaExpression, SyntaxKind.SimpleLambdaExpression);

protected override ImmutableArray<SyntaxKind> SyntaxKindsOfInterest => s_kindsOfInterest;

protected override DiagnosticDescriptor DiagnosticDescriptor => GetDiagnosticDescriptor(NameNotInContext, _nameNotInContextMessageFormat);
protected override DiagnosticDescriptor DiagnosticDescriptor => GetDiagnosticDescriptor(IDEDiagnosticIds.UnboundIdentifierId, _nameNotInContextMessageFormat);

protected override DiagnosticDescriptor DiagnosticDescriptor2 => GetDiagnosticDescriptor(ConstructorOverloadResolutionFailure, _constructorOverloadResolutionFailureMessageFormat);
protected override DiagnosticDescriptor DiagnosticDescriptor2 => GetDiagnosticDescriptor(IDEDiagnosticIds.UnboundConstructorId, _constructorOverloadResolutionFailureMessageFormat);

protected override bool ConstructorDoesNotExist(SyntaxNode node, SymbolInfo info, SemanticModel model)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CodeFixes.GenerateMember;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor;
using Microsoft.CodeAnalysis.Shared.Extensions;

Expand All @@ -23,7 +24,7 @@ internal static class GenerateConstructorDiagnosticIds
public const string CS7036 = nameof(CS7036); // CS7036: There is no argument given that corresponds to the required formal parameter 'v' of 'C.C(int)'

public static readonly ImmutableArray<string> AllDiagnosticIds =
ImmutableArray.Create(CS0122, CS1729, CS1739, CS1503, CS7036);
ImmutableArray.Create(CS0122, CS1729, CS1739, CS1503, CS7036, IDEDiagnosticIds.UnboundConstructorId);

public static readonly ImmutableArray<string> TooManyArgumentsDiagnosticIds =
ImmutableArray.Create(CS1729);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ internal static class IDEDiagnosticIds
public const string ErrorReadingRulesetId = "IDE1004";
public const string InvokeDelegateWithConditionalAccessId = "IDE1005";
public const string NamingRuleId = "IDE1006";
public const string UnboundIdentifierId = "IDE1007";
public const string UnboundConstructorId = "IDE1008";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Imports System.Collections.Immutable
Imports System.Composition
Imports Microsoft.CodeAnalysis.AddImport
Imports Microsoft.CodeAnalysis.CodeFixes
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.Packaging
Imports Microsoft.CodeAnalysis.SymbolSearch

Expand Down Expand Up @@ -100,7 +101,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.AddImport

Public Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String)
Get
Return ImmutableArray.Create(BC30002, BC30451, BC30456, BC32042, BC36593, BC32045, BC30389, BC31504, BC32016, BC36610, BC36719, BC30512, BC30390, BC42309, BC30182)
Return ImmutableArray.Create(BC30002, BC30451, BC30456, BC32042, BC36593, BC32045, BC30389, BC31504, BC32016, BC36610,
BC36719, BC30512, BC30390, BC42309, BC30182, IDEDiagnosticIds.UnboundIdentifierId)
End Get
End Property
End Class
Expand Down
Loading

0 comments on commit 850f809

Please sign in to comment.