|
| 1 | +// Copyright 2021-present MongoDB Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License") |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using MongoDB.Analyzer.Core.HelperResources; |
| 16 | +using MongoDB.Analyzer.Core.Utilities; |
| 17 | + |
| 18 | +namespace MongoDB.Analyzer.Core.Linq; |
| 19 | + |
| 20 | +internal static class EFAnalyzer |
| 21 | +{ |
| 22 | + public static bool AnalyzeEFQueryable(MongoAnalysisContext context) |
| 23 | + { |
| 24 | + var sw = Stopwatch.StartNew(); |
| 25 | + var stats = AnalysisStats.Empty; |
| 26 | + ExpressionsAnalysis efAnalysis = null; |
| 27 | + |
| 28 | + try |
| 29 | + { |
| 30 | + context.Logger.Log("Started EF analysis"); |
| 31 | + |
| 32 | + efAnalysis = LinqExpressionProcessor.ProcessSemanticModel(context, AnalysisType.EF); |
| 33 | + |
| 34 | + ReportInvalidExpressions(context, efAnalysis); |
| 35 | + stats = ReportMqlOrInvalidExpressions(context, efAnalysis); |
| 36 | + |
| 37 | + sw.Stop(); |
| 38 | + context.Logger.Log($"EF analysis ended: with {stats.MqlCount} mql translations, {stats.DriverExceptionsCount} unsupported expressions, {stats.InternalExceptionsCount} internal exceptions in {sw.ElapsedMilliseconds}ms."); |
| 39 | + } |
| 40 | + catch (Exception ex) |
| 41 | + { |
| 42 | + sw.Stop(); |
| 43 | + context.Logger.Log($"EF analysis ended after {sw.ElapsedMilliseconds} ms with exception {ex}"); |
| 44 | + } |
| 45 | + |
| 46 | + var telemetry = AnalysisUtilities.GetTelemetry(efAnalysis, stats, sw); |
| 47 | + if (telemetry.ExpressionsFound > 0) |
| 48 | + { |
| 49 | + context.Telemetry.EFAnalysisResult(telemetry); |
| 50 | + } |
| 51 | + |
| 52 | + return telemetry.ExpressionsFound > 0; |
| 53 | + } |
| 54 | + |
| 55 | + private static void ReportInvalidExpressions(MongoAnalysisContext context, ExpressionsAnalysis efExpressionAnalysis) |
| 56 | + { |
| 57 | + var semanticContext = context.SemanticModelAnalysisContext; |
| 58 | + |
| 59 | + if (efExpressionAnalysis.InvalidExpressionNodes.EmptyOrNull()) |
| 60 | + { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + var driverVersion = ReferencesProvider.GetMongoDBDriverVersion(semanticContext.SemanticModel.Compilation.References); |
| 65 | + var driverVersionString = driverVersion?.ToString(3); |
| 66 | + |
| 67 | + foreach (var invalidExpressionNode in efExpressionAnalysis.InvalidExpressionNodes) |
| 68 | + { |
| 69 | + var diagnostics = Diagnostic.Create( |
| 70 | + DiagnosticsRules.DiagnosticRuleNotSupportedEFExpression, |
| 71 | + invalidExpressionNode.OriginalExpression.GetLocation(), |
| 72 | + AnalysisUtilities.DecorateMessage(invalidExpressionNode.Errors.FirstOrDefault(), driverVersionString, context.Settings)); |
| 73 | + |
| 74 | + semanticContext.ReportDiagnostic(diagnostics); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static AnalysisStats ReportMqlOrInvalidExpressions(MongoAnalysisContext context, ExpressionsAnalysis efExpressionAnalysis) |
| 79 | + { |
| 80 | + var semanticContext = context.SemanticModelAnalysisContext; |
| 81 | + |
| 82 | + if (efExpressionAnalysis.AnalysisNodeContexts.EmptyOrNull()) |
| 83 | + { |
| 84 | + return AnalysisStats.Empty; |
| 85 | + } |
| 86 | + |
| 87 | + var compilationResult = AnalysisCodeGenerator.Compile(context, efExpressionAnalysis); |
| 88 | + if (!compilationResult.Success) |
| 89 | + { |
| 90 | + return AnalysisStats.Empty; |
| 91 | + } |
| 92 | + |
| 93 | + var driverVersion = compilationResult.LinqTestCodeExecutor.DriverVersion; |
| 94 | + var settings = context.Settings; |
| 95 | + int mqlCount = 0, internalExceptionsCount = 0, driverExceptionsCount = 0; |
| 96 | + var typesMapper = new TypesMapper( |
| 97 | + MqlGeneratorSyntaxElements.Linq.MqlGeneratorNamespace, |
| 98 | + context.TypesProcessor.GeneratedTypeToOriginalTypeMapping); |
| 99 | + |
| 100 | + foreach (var analysisContext in efExpressionAnalysis.AnalysisNodeContexts) |
| 101 | + { |
| 102 | + var mqlResult = compilationResult.LinqTestCodeExecutor.GenerateMql(analysisContext.EvaluationMethodName); |
| 103 | + var locations = analysisContext.Node.Locations; |
| 104 | + |
| 105 | + if (mqlResult.Mql != null) |
| 106 | + { |
| 107 | + var mql = analysisContext.Node.ConstantsRemapper.RemapConstants(mqlResult.Mql); |
| 108 | + var diagnosticDescriptor = DiagnosticsRules.DiagnosticRuleEF2MQL; |
| 109 | + var decoratedMessage = AnalysisUtilities.DecorateMessage(mql, driverVersion, settings); |
| 110 | + semanticContext.ReportDiagnostics(diagnosticDescriptor, decoratedMessage, locations); |
| 111 | + mqlCount++; |
| 112 | + } |
| 113 | + else if (mqlResult.Exception != null) |
| 114 | + { |
| 115 | + var isDriverOrLinqException = AnalysisUtilities.IsDriverOrLinqException(mqlResult); |
| 116 | + |
| 117 | + if (isDriverOrLinqException || settings.OutputInternalExceptions) |
| 118 | + { |
| 119 | + var diagnosticDescriptor = DiagnosticsRules.DiagnosticRuleNotSupportedEFExpression; |
| 120 | + var message = AnalysisUtilities.GetExceptionMessage(mqlResult.Exception, typesMapper, AnalysisType.EF); |
| 121 | + var decoratedMessage = AnalysisUtilities.DecorateMessage(message, driverVersion, context.Settings); |
| 122 | + |
| 123 | + semanticContext.ReportDiagnostics(diagnosticDescriptor, decoratedMessage, locations); |
| 124 | + } |
| 125 | + |
| 126 | + if (!isDriverOrLinqException) |
| 127 | + { |
| 128 | + context.Logger.Log($"Exception while analyzing {analysisContext.Node}: {mqlResult.Exception.InnerException?.Message}"); |
| 129 | + internalExceptionsCount++; |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + driverExceptionsCount++; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return new AnalysisStats(mqlCount, 0, internalExceptionsCount, driverExceptionsCount, compilationResult.MongoDBDriverVersion.ToString(3), null); |
| 139 | + } |
| 140 | +} |
0 commit comments