-
Notifications
You must be signed in to change notification settings - Fork 462
/
InternalImplementationOnlyAnalyzer.cs
65 lines (55 loc) · 3.25 KB
/
InternalImplementationOnlyAnalyzer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Linq;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Microsoft.CodeAnalysis.Analyzers
{
using static CodeAnalysisDiagnosticsResources;
/// <summary>
/// RS1009: <inheritdoc cref="InternalImplementationOnlyTitle"/>
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public class InternalImplementationOnlyAnalyzer : DiagnosticAnalyzer
{
private const string InternalImplementationOnlyAttributeName = "InternalImplementationOnlyAttribute";
private const string InternalImplementationOnlyAttributeFullName = "System.Runtime.CompilerServices.InternalImplementationOnlyAttribute";
public static readonly DiagnosticDescriptor Rule = new(
DiagnosticIds.InternalImplementationOnlyRuleId,
CreateLocalizableResourceString(nameof(InternalImplementationOnlyTitle)),
CreateLocalizableResourceString(nameof(InternalImplementationOnlyMessage)),
DiagnosticCategory.MicrosoftCodeAnalysisCompatibility,
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: CreateLocalizableResourceString(nameof(InternalImplementationOnlyDescription)));
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
}
private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;
// If any interface implemented by this type has the attribute and if the interface and this type are not
// in "internals visible" context, then issue an error.
foreach (INamedTypeSymbol iface in namedTypeSymbol.AllInterfaces)
{
System.Collections.Generic.IEnumerable<AttributeData> attributes = iface.GetAttributes();
// We are doing a string comparison of the name here because we don't care where the attribute comes from.
// CodeAnalysis.dll itself has this attribute and if the user assembly also had it, symbol equality will fail
// but we should still issue the error.
if (attributes.Any(a => a.AttributeClass is { Name: InternalImplementationOnlyAttributeName }
&& a.AttributeClass.ToDisplayString().Equals(InternalImplementationOnlyAttributeFullName, StringComparison.Ordinal)) &&
!iface.ContainingAssembly.GivesAccessTo(namedTypeSymbol.ContainingAssembly))
{
context.ReportDiagnostic(namedTypeSymbol.CreateDiagnostic(Rule, namedTypeSymbol.Name, iface.Name));
break;
}
}
}
}
}