AnalyzerHelper.cs 12.8 KB
Newer Older
S
Sam Harwell 已提交
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
H
heejaechang 已提交
2

M
Manish Vasani 已提交
3
using System;
4
using System.Linq;
5
using System.Reflection;
M
Manish Vasani 已提交
6
using Microsoft.CodeAnalysis.ErrorReporting;
7
using Microsoft.CodeAnalysis.Options;
8
using Roslyn.Utilities;
9
using System.IO;
J
Jonathon Marolf 已提交
10 11
using System.Threading;
using System.Threading.Tasks;
M
Manish Vasani 已提交
12

H
heejaechang 已提交
13 14 15 16 17 18 19
namespace Microsoft.CodeAnalysis.Diagnostics
{
    internal static class AnalyzerHelper
    {
        private const string CSharpCompilerAnalyzerTypeName = "Microsoft.CodeAnalysis.Diagnostics.CSharp.CSharpCompilerDiagnosticAnalyzer";
        private const string VisualBasicCompilerAnalyzerTypeName = "Microsoft.CodeAnalysis.Diagnostics.VisualBasic.VisualBasicCompilerDiagnosticAnalyzer";

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
        // These are the error codes of the compiler warnings. 
        // Keep the ids the same so that de-duplication against compiler errors
        // works in the error list (after a build).
        internal const string WRN_AnalyzerCannotBeCreatedIdCS = "CS8032";
        internal const string WRN_AnalyzerCannotBeCreatedIdVB = "BC42376";
        internal const string WRN_NoAnalyzerInAssemblyIdCS = "CS8033";
        internal const string WRN_NoAnalyzerInAssemblyIdVB = "BC42377";
        internal const string WRN_UnableToLoadAnalyzerIdCS = "CS8034";
        internal const string WRN_UnableToLoadAnalyzerIdVB = "BC42378";

        // Shared with Compiler
        internal const string AnalyzerExceptionDiagnosticId = "AD0001";
        internal const string AnalyzerDriverExceptionDiagnosticId = "AD0002";

        // IDE only errors
        internal const string WRN_AnalyzerCannotBeCreatedId = "AD1000";
        internal const string WRN_NoAnalyzerInAssemblyId = "AD1001";
        internal const string WRN_UnableToLoadAnalyzerId = "AD1002";

39 40
        private const string AnalyzerExceptionDiagnosticCategory = "Intellisense";

41 42 43 44 45
        public static bool IsWorkspaceDiagnosticAnalyzer(this DiagnosticAnalyzer analyzer)
        {
            return analyzer is DocumentDiagnosticAnalyzer || analyzer is ProjectDiagnosticAnalyzer;
        }

46
        public static bool IsBuiltInAnalyzer(this DiagnosticAnalyzer analyzer)
H
heejaechang 已提交
47
        {
48 49 50
            return analyzer is IBuiltInAnalyzer || analyzer.IsWorkspaceDiagnosticAnalyzer() || analyzer.IsCompilerAnalyzer();
        }

51
        public static bool IsOpenFileOnly(this DiagnosticAnalyzer analyzer, Workspace workspace)
52
        {
C
CyrusNajmabadi 已提交
53
            if (analyzer is IBuiltInAnalyzer builtInAnalyzer)
54
            {
55 56 57 58 59 60
                return builtInAnalyzer.OpenFileOnly(workspace);
            }

            return false;
        }

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        public static bool ContainsOpenFileOnlyAnalyzers(this CompilationWithAnalyzers analyzerDriverOpt, Workspace workspace)
        {
            if (analyzerDriverOpt == null)
            {
                // not Roslyn. no open file only analyzers
                return false;
            }

            foreach (var analyzer in analyzerDriverOpt.Analyzers)
            {
                if (analyzer.IsOpenFileOnly(workspace))
                {
                    return true;
                }
            }

            return false;
        }

H
Heejae Chang 已提交
80
        public static bool HasNonHiddenDescriptor(this DiagnosticAnalyzerService service, DiagnosticAnalyzer analyzer, Project project)
81
        {
82 83 84 85 86 87 88 89 90 91 92 93 94 95
            // most of analyzers, number of descriptor is quite small, so this should be cheap.
            return service.GetDiagnosticDescriptors(analyzer).Any(d => d.GetEffectiveSeverity(project.CompilationOptions) != ReportDiagnostic.Hidden);
        }

        public static ReportDiagnostic GetEffectiveSeverity(this DiagnosticDescriptor descriptor, CompilationOptions options)
        {
            return options == null
                ? descriptor.DefaultSeverity.MapSeverityToReport()
                : descriptor.GetEffectiveSeverity(options);
        }

        public static ReportDiagnostic MapSeverityToReport(this DiagnosticSeverity severity)
        {
            switch (severity)
96
            {
97 98 99 100 101 102 103 104 105
                case DiagnosticSeverity.Hidden:
                    return ReportDiagnostic.Hidden;
                case DiagnosticSeverity.Info:
                    return ReportDiagnostic.Info;
                case DiagnosticSeverity.Warning:
                    return ReportDiagnostic.Warn;
                case DiagnosticSeverity.Error:
                    return ReportDiagnostic.Error;
                default:
106
                    throw ExceptionUtilities.UnexpectedValue(severity);
107
            }
H
heejaechang 已提交
108 109
        }

110
        public static bool IsCompilerAnalyzer(this DiagnosticAnalyzer analyzer)
H
heejaechang 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        {
            // TODO: find better way.
            var typeString = analyzer.GetType().ToString();
            if (typeString == CSharpCompilerAnalyzerTypeName)
            {
                return true;
            }

            if (typeString == VisualBasicCompilerAnalyzerTypeName)
            {
                return true;
            }

            return false;
        }
M
Manish Vasani 已提交
126

127
        public static ValueTuple<string, VersionStamp> GetAnalyzerIdAndVersion(this DiagnosticAnalyzer analyzer)
128 129 130
        {
            // Get the unique ID for given diagnostic analyzer.
            // note that we also put version stamp so that we can detect changed analyzer.
131 132
            var typeInfo = analyzer.GetType().GetTypeInfo();
            return ValueTuple.Create(analyzer.GetAnalyzerId(), GetAnalyzerVersion(CorLightup.Desktop.GetAssemblyLocation(typeInfo.Assembly)));
133 134
        }

H
Heejae Chang 已提交
135 136
        public static string GetAnalyzerAssemblyName(this DiagnosticAnalyzer analyzer)
        {
137 138
            var typeInfo = analyzer.GetType().GetTypeInfo();
            return typeInfo.Assembly.GetName().Name;
H
Heejae Chang 已提交
139 140
        }

141
        public static Task<OptionSet> GetDocumentOptionSetAsync(this AnalyzerOptions analyzerOptions, SyntaxTree syntaxTree, CancellationToken cancellationToken)
142
        {
143 144
            var workspaceAnalyzerOptions = analyzerOptions as WorkspaceAnalyzerOptions;
            if (workspaceAnalyzerOptions == null)
J
Jonathon Marolf 已提交
145
            {
146
                return SpecializedTasks.Default<OptionSet>();
J
Jonathon Marolf 已提交
147 148
            }

149
            return workspaceAnalyzerOptions.GetDocumentOptionSetAsync(syntaxTree, cancellationToken);
150 151
        }

152
        internal static void OnAnalyzerException_NoTelemetryLogging(
153
            Exception ex,
M
Manish Vasani 已提交
154
            DiagnosticAnalyzer analyzer,
155
            Diagnostic diagnostic,
M
Manish Vasani 已提交
156
            AbstractHostDiagnosticUpdateSource hostDiagnosticUpdateSource,
157
            ProjectId projectIdOpt)
M
Manish Vasani 已提交
158
        {
159
            if (diagnostic != null)
160
            {
161
                hostDiagnosticUpdateSource?.ReportAnalyzerDiagnostic(analyzer, diagnostic, hostDiagnosticUpdateSource?.Workspace, projectIdOpt);
162
            }
M
Manish Vasani 已提交
163

164
            if (IsBuiltInAnalyzer(analyzer))
165
            {
166 167 168 169 170 171 172 173 174
                FatalError.ReportWithoutCrashUnlessCanceled(ex);
            }
        }

        internal static void OnAnalyzerExceptionForSupportedDiagnostics(DiagnosticAnalyzer analyzer, Exception exception, AbstractHostDiagnosticUpdateSource hostDiagnosticUpdateSource)
        {
            if (exception is OperationCanceledException)
            {
                return;
175
            }
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

            var diagnostic = CreateAnalyzerExceptionDiagnostic(analyzer, exception);
            OnAnalyzerException_NoTelemetryLogging(exception, analyzer, diagnostic, hostDiagnosticUpdateSource, projectIdOpt: null);
        }

        /// <summary>
        /// Create a diagnostic for exception thrown by the given analyzer.
        /// </summary>
        /// <remarks>
        /// Keep this method in sync with "AnalyzerExecutor.CreateAnalyzerExceptionDiagnostic".
        /// </remarks>
        internal static Diagnostic CreateAnalyzerExceptionDiagnostic(DiagnosticAnalyzer analyzer, Exception e)
        {
            var analyzerName = analyzer.ToString();

            // TODO: It is not ideal to create a new descriptor per analyzer exception diagnostic instance.
            // However, until we add a LongMessage field to the Diagnostic, we are forced to park the instance specific description onto the Descriptor's Description field.
            // This requires us to create a new DiagnosticDescriptor instance per diagnostic instance.
            var descriptor = new DiagnosticDescriptor(AnalyzerExceptionDiagnosticId,
195 196 197
                title: FeaturesResources.User_Diagnostic_Analyzer_Failure,
                messageFormat: FeaturesResources.Analyzer_0_threw_an_exception_of_type_1_with_message_2,
                description: string.Format(FeaturesResources.Analyzer_0_threw_the_following_exception_colon_1, analyzerName, e.CreateDiagnosticDescription()),
198
                category: AnalyzerExceptionDiagnosticCategory,
199
                defaultSeverity: DiagnosticSeverity.Warning,
200 201 202 203
                isEnabledByDefault: true,
                customTags: WellKnownDiagnosticTags.AnalyzerException);

            return Diagnostic.Create(descriptor, Location.None, analyzerName, e.GetType(), e.Message);
M
Manish Vasani 已提交
204
        }
205

H
Heejae Chang 已提交
206
        private static VersionStamp GetAnalyzerVersion(string path)
207
        {
208
            if (path == null || !File.Exists(path))
209 210 211 212
            {
                return VersionStamp.Default;
            }

213
            return VersionStamp.Create(File.GetLastWriteTimeUtc(path));
214
        }
215 216 217 218 219 220 221 222 223

        public static DiagnosticData CreateAnalyzerLoadFailureDiagnostic(string fullPath, AnalyzerLoadFailureEventArgs e)
        {
            return CreateAnalyzerLoadFailureDiagnostic(null, null, null, fullPath, e);
        }

        public static DiagnosticData CreateAnalyzerLoadFailureDiagnostic(
            Workspace workspace, ProjectId projectId, string language, string fullPath, AnalyzerLoadFailureEventArgs e)
        {
C
CyrusNajmabadi 已提交
224
            if (!TryGetErrorMessage(language, fullPath, e, out var id, out var message, out var messageFormat, out var description))
225 226 227 228 229 230
            {
                return null;
            }

            return new DiagnosticData(
                id,
231
                FeaturesResources.Roslyn_HostError,
232 233 234 235
                message,
                messageFormat,
                severity: DiagnosticSeverity.Warning,
                isEnabledByDefault: true,
236
                description: description,
237 238 239 240 241 242 243
                warningLevel: 0,
                workspace: workspace,
                projectId: projectId);
        }

        private static bool TryGetErrorMessage(
            string language, string fullPath, AnalyzerLoadFailureEventArgs e,
244
            out string id, out string message, out string messageFormat, out string description)
245 246 247 248 249
        {
            switch (e.ErrorCode)
            {
                case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer:
                    id = Choose(language, WRN_UnableToLoadAnalyzerId, WRN_UnableToLoadAnalyzerIdCS, WRN_UnableToLoadAnalyzerIdVB);
250 251
                    messageFormat = FeaturesResources.Unable_to_load_Analyzer_assembly_0_colon_1;
                    message = string.Format(FeaturesResources.Unable_to_load_Analyzer_assembly_0_colon_1, fullPath, e.Message);
252
                    description = e.Exception.CreateDiagnosticDescription();
253 254 255
                    break;
                case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToCreateAnalyzer:
                    id = Choose(language, WRN_AnalyzerCannotBeCreatedId, WRN_AnalyzerCannotBeCreatedIdCS, WRN_AnalyzerCannotBeCreatedIdVB);
256 257
                    messageFormat = FeaturesResources.An_instance_of_analyzer_0_cannot_be_created_from_1_colon_2;
                    message = string.Format(FeaturesResources.An_instance_of_analyzer_0_cannot_be_created_from_1_colon_2, e.TypeName, fullPath, e.Message);
258
                    description = e.Exception.CreateDiagnosticDescription();
259 260 261
                    break;
                case AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers:
                    id = Choose(language, WRN_NoAnalyzerInAssemblyId, WRN_NoAnalyzerInAssemblyIdCS, WRN_NoAnalyzerInAssemblyIdVB);
262 263
                    messageFormat = FeaturesResources.The_assembly_0_does_not_contain_any_analyzers;
                    message = string.Format(FeaturesResources.The_assembly_0_does_not_contain_any_analyzers, fullPath);
264
                    description = e.Exception.CreateDiagnosticDescription();
265 266 267 268 269 270
                    break;
                case AnalyzerLoadFailureEventArgs.FailureErrorCode.None:
                default:
                    id = string.Empty;
                    message = string.Empty;
                    messageFormat = string.Empty;
271
                    description = string.Empty;
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
                    return false;
            }

            return true;
        }

        private static string Choose(string language, string noLanguageMessage, string csharpMessage, string vbMessage)
        {
            if (language == null)
            {
                return noLanguageMessage;
            }

            return language == LanguageNames.CSharp ? csharpMessage : vbMessage;
        }
H
heejaechang 已提交
287
    }
S
Sam Harwell 已提交
288
}