コード例 #1
0
        internal ImmutableArray <DiagnosticAnalyzer> ResolveAnalyzersFromArguments(
            string language,
            List <DiagnosticInfo> diagnostics,
            CommonMessageProvider messageProvider,
            IAnalyzerAssemblyLoader analyzerLoader)
        {
            var analyzerBuilder = ImmutableArray.CreateBuilder <DiagnosticAnalyzer>();

            EventHandler <AnalyzerLoadFailureEventArgs> errorHandler = (o, e) =>
            {
                var            analyzerReference = o as AnalyzerFileReference;
                DiagnosticInfo diagnostic;
                switch (e.ErrorCode)
                {
                case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer:
                    diagnostic = new DiagnosticInfo(messageProvider, messageProvider.WRN_UnableToLoadAnalyzer, analyzerReference.FullPath, e.Message);
                    break;

                case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToCreateAnalyzer:
                    diagnostic = new DiagnosticInfo(messageProvider, messageProvider.WRN_AnalyzerCannotBeCreated, e.TypeName, analyzerReference.FullPath, e.Message);
                    break;

                case AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers:
                    diagnostic = new DiagnosticInfo(messageProvider, messageProvider.WRN_NoAnalyzerInAssembly, analyzerReference.FullPath);
                    break;

                case AnalyzerLoadFailureEventArgs.FailureErrorCode.None:
                default:
                    return;
                }

                // Filter this diagnostic based on the compilation options so that /nowarn and /warnaserror etc. take effect.
                diagnostic = messageProvider.FilterDiagnosticInfo(diagnostic, this.CompilationOptions);

                if (diagnostic != null)
                {
                    diagnostics.Add(diagnostic);
                }
            };

            var resolvedReferences = ArrayBuilder <AnalyzerFileReference> .GetInstance();

            foreach (var reference in AnalyzerReferences)
            {
                var resolvedReference = ResolveAnalyzerReference(reference, analyzerLoader);
                if (resolvedReference != null)
                {
                    resolvedReferences.Add(resolvedReference);

                    // register the reference to the analyzer loader:
                    analyzerLoader.AddDependencyLocation(resolvedReference.FullPath);
                }
                else
                {
                    diagnostics.Add(new DiagnosticInfo(messageProvider, messageProvider.ERR_MetadataFileNotFound, reference.FilePath));
                }
            }

            // All analyzer references are registered now, we can start loading them:
            foreach (var resolvedReference in resolvedReferences)
            {
                resolvedReference.AnalyzerLoadFailed += errorHandler;
                resolvedReference.AddAnalyzers(analyzerBuilder, language);
                resolvedReference.AnalyzerLoadFailed -= errorHandler;
            }

            resolvedReferences.Free();

            return(analyzerBuilder.ToImmutable());
        }