| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Linq; |
| | | 7 | | using Microsoft.CodeAnalysis; |
| | | 8 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 9 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 10 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.BuildTools; |
| | | 13 | | |
| | | 14 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 15 | | public sealed class PrivateServiceContractAnalyzer : DiagnosticAnalyzer |
| | | 16 | | { |
| | 1 | 17 | | private static readonly ImmutableArray<DiagnosticDescriptor> s_supportedDiagnostics = |
| | 1 | 18 | | new[] |
| | 1 | 19 | | { |
| | 1 | 20 | | DiagnosticDescriptors.PrivateServiceContractAnalyzer__03XX.PrivateServiceContract, |
| | 1 | 21 | | } |
| | 1 | 22 | | .ToImmutableArray(); |
| | | 23 | | |
| | | 24 | | public override void Initialize(AnalysisContext context) |
| | | 25 | | { |
| | 16 | 26 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDia |
| | | 27 | | #if !DEBUG |
| | 16 | 28 | | context.EnableConcurrentExecution(); |
| | | 29 | | #endif |
| | 16 | 30 | | context.RegisterCompilationStartAction(compilationStartContext => |
| | 16 | 31 | | { |
| | 10 | 32 | | bool enableOperationInvokerGenerator = compilationStartContext.Options.AnalyzerConfigOptionsProvider |
| | 10 | 33 | | .GlobalOptions.TryGetValue("build_property.EnableCoreWCFOperationInvokerGenerator", out string? enableSo |
| | 10 | 34 | | && enableSourceGenerator == "true"; |
| | 16 | 35 | | |
| | 10 | 36 | | if (!enableOperationInvokerGenerator) |
| | 16 | 37 | | { |
| | 2 | 38 | | return; |
| | 16 | 39 | | } |
| | 16 | 40 | | |
| | 8 | 41 | | INamedTypeSymbol? coreWcfServiceContractSymbol = compilationStartContext.Compilation.GetTypeByMetadataName(" |
| | 8 | 42 | | INamedTypeSymbol? sSMServiceContractSymbol = compilationStartContext.Compilation.GetTypeByMetadataName("Syst |
| | 8 | 43 | | INamedTypeSymbol? coreWcfOperationContractSymbol = compilationStartContext.Compilation.GetTypeByMetadataName |
| | 8 | 44 | | INamedTypeSymbol? sSMOperationContractSymbol = compilationStartContext.Compilation.GetTypeByMetadataName("Sy |
| | 16 | 45 | | |
| | 8 | 46 | | compilationStartContext.RegisterSyntaxNodeAction( |
| | 8 | 47 | | context => ErrorWhenServiceContractIsPrivateInterface(context, coreWcfServiceContractSymbol, sSMServiceC |
| | 8 | 48 | | SyntaxKind.InvocationExpression); |
| | 24 | 49 | | }); |
| | 16 | 50 | | } |
| | | 51 | | |
| | | 52 | | private void ErrorWhenServiceContractIsPrivateInterface(SyntaxNodeAnalysisContext context, INamedTypeSymbol? coreWcf |
| | | 53 | | { |
| | 8 | 54 | | InvocationExpressionSyntax invocationExpressionSyntax = (InvocationExpressionSyntax)context.Node; |
| | 8 | 55 | | IMethodSymbol? methodSymbol = context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax, context.Cancellati |
| | 8 | 56 | | if (methodSymbol == null) |
| | | 57 | | { |
| | 0 | 58 | | return; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | IEnumerable<INamedTypeSymbol> serviceContracts; |
| | 8 | 62 | | if (methodSymbol.ContainingType?.TypeKind == TypeKind.Interface) |
| | | 63 | | { |
| | 4 | 64 | | serviceContracts = new[] { methodSymbol.ContainingType }; |
| | | 65 | | } |
| | | 66 | | else |
| | | 67 | | { |
| | 4 | 68 | | serviceContracts = from @interface in methodSymbol.ContainingType.AllInterfaces |
| | 4 | 69 | | where @interface.GetOneAttributeOf(coreWcfServiceContractSymbol, sSMServiceContractSymbol) is not null |
| | 4 | 70 | | select @interface; |
| | | 71 | | } |
| | | 72 | | |
| | 8 | 73 | | var privateServiceContracts = from @interface in serviceContracts |
| | 8 | 74 | | where @interface.IsPrivate() |
| | 8 | 75 | | select @interface; |
| | | 76 | | |
| | 8 | 77 | | var operationContracts = from @interface in privateServiceContracts |
| | 16 | 78 | | from member in @interface.GetMembers() |
| | 8 | 79 | | let methodMember = member as IMethodSymbol |
| | 8 | 80 | | where methodMember is not null |
| | 8 | 81 | | let operationContractAttribute = methodMember.GetOneAttributeOf(coreWcfOperationContractSymbol, sSMOperation |
| | 8 | 82 | | where operationContractAttribute is not null |
| | 16 | 83 | | select (@interface, methodMember); |
| | | 84 | | |
| | 32 | 85 | | foreach (var (privateServiceContract, operationContract) in operationContracts) |
| | | 86 | | { |
| | 8 | 87 | | context.ReportDiagnostic(DiagnosticDescriptors.PrivateServiceContractAnalyzer__03XX.PrivateServiceContractEr |
| | | 88 | | } |
| | 8 | 89 | | } |
| | | 90 | | |
| | 206 | 91 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => s_supportedDiagnostics; |
| | | 92 | | } |