Пример #1
0
 /// <summary>
 /// Create a <see cref="StrongNameKeys"/> for the provided information.
 /// </summary>
 internal abstract StrongNameKeys CreateKeys(string keyFilePath, string keyContainerName, bool hasCounterSignature, CommonMessageProvider messageProvider);
Пример #2
0
        internal static StrongNameKeys Create(ImmutableArray <byte> publicKey, RSAParameters?privateKey, bool hasCounterSignature, CommonMessageProvider messageProvider)
        {
            Debug.Assert(!publicKey.IsDefaultOrEmpty);

            if (MetadataHelpers.IsValidPublicKey(publicKey))
            {
                return(new StrongNameKeys(default(ImmutableArray <byte>), publicKey, privateKey, null, null, hasCounterSignature));
            }
            else
            {
                return(new StrongNameKeys(messageProvider.CreateDiagnostic(messageProvider.ERR_BadCompilationOptionValue, Location.None,
                                                                           nameof(CompilationOptions.CryptoPublicKey), BitConverter.ToString(publicKey.ToArray()))));
            }
        }
Пример #3
0
 internal CommandLineParser(CommonMessageProvider messageProvider, bool isScriptCommandLineParser)
 {
     Debug.Assert(messageProvider != null);
     _messageProvider          = messageProvider;
     IsScriptCommandLineParser = isScriptCommandLineParser;
 }
Пример #4
0
 internal static Diagnostic GetContainerError(CommonMessageProvider messageProvider, string name, object message)
 {
     return(messageProvider.CreateDiagnostic(messageProvider.ERR_PublicKeyContainerFailure, Location.None, name, message));
 }
Пример #5
0
 internal static Diagnostic GetKeyFileError(CommonMessageProvider messageProvider, string path, object message)
 {
     return(messageProvider.CreateDiagnostic(messageProvider.ERR_PublicKeyFileFailure, Location.None, path, message));
 }
Пример #6
0
        internal static StrongNameKeys Create(StrongNameProvider providerOpt, string keyFilePath, string keyContainerName, bool hasCounterSignature, CommonMessageProvider messageProvider)
        {
            if (string.IsNullOrEmpty(keyFilePath) && string.IsNullOrEmpty(keyContainerName))
            {
                return(None);
            }

            if (providerOpt == null)
            {
                var diagnostic = GetError(keyFilePath, keyContainerName, new CodeAnalysisResourcesLocalizableErrorArgument(nameof(CodeAnalysisResources.AssemblySigningNotSupported)), messageProvider);
                return(new StrongNameKeys(diagnostic));
            }

            return(providerOpt.CreateKeys(keyFilePath, keyContainerName, hasCounterSignature, messageProvider));
        }
Пример #7
0
 internal static Diagnostic GetError(string keyFilePath, string keyContainerName, object message, CommonMessageProvider messageProvider)
 {
     if (keyContainerName != null)
     {
         return(GetContainerError(messageProvider, keyContainerName, message));
     }
     else
     {
         return(GetKeyFileError(messageProvider, keyFilePath, message));
     }
 }
Пример #8
0
        internal static ReportDiagnostic GetDiagnosticOptionsFromRulesetFile(string rulesetFileFullPath, out Dictionary <string, ReportDiagnostic> diagnosticOptions, IList <Diagnostic> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            diagnosticOptions = new Dictionary <string, ReportDiagnostic>();
            if (rulesetFileFullPath == null)
            {
                return(ReportDiagnostic.Default);
            }

            return(GetDiagnosticOptionsFromRulesetFile(diagnosticOptions, rulesetFileFullPath, diagnosticsOpt, messageProviderOpt));
        }
Пример #9
0
        private static ReportDiagnostic GetDiagnosticOptionsFromRulesetFile(Dictionary <string, ReportDiagnostic> diagnosticOptions, string resolvedPath, IList <Diagnostic> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(resolvedPath != null);

            var generalDiagnosticOption = ReportDiagnostic.Default;

            try
            {
                var ruleSet = RuleSet.LoadEffectiveRuleSetFromFile(resolvedPath);
                generalDiagnosticOption = ruleSet.GeneralDiagnosticOption;
                foreach (var rule in ruleSet.SpecificDiagnosticOptions)
                {
                    diagnosticOptions.Add(rule.Key, rule.Value);
                }
            }
            catch (InvalidRuleSetException e)
            {
                if (diagnosticsOpt != null && messageProviderOpt != null)
                {
                    diagnosticsOpt.Add(Diagnostic.Create(messageProviderOpt, messageProviderOpt.ERR_CantReadRulesetFile, resolvedPath, e.Message));
                }
            }
            catch (IOException e)
            {
                if (e is FileNotFoundException || e.GetType().Name == "DirectoryNotFoundException")
                {
                    if (diagnosticsOpt != null && messageProviderOpt != null)
                    {
                        diagnosticsOpt.Add(Diagnostic.Create(messageProviderOpt, messageProviderOpt.ERR_CantReadRulesetFile, resolvedPath, new CodeAnalysisResourcesLocalizableErrorArgument(nameof(CodeAnalysisResources.FileNotFound))));
                    }
                }
                else
                {
                    if (diagnosticsOpt != null && messageProviderOpt != null)
                    {
                        diagnosticsOpt.Add(Diagnostic.Create(messageProviderOpt, messageProviderOpt.ERR_CantReadRulesetFile, resolvedPath, e.Message));
                    }
                }
            }

            return(generalDiagnosticOption);
        }
Пример #10
0
        internal override StrongNameKeys CreateKeys(string keyFilePath, string keyContainerName, bool hasCounterSignature, CommonMessageProvider messageProvider)
        {
            var    keyPair   = default(ImmutableArray <byte>);
            var    publicKey = default(ImmutableArray <byte>);
            string container = null;

            if (!string.IsNullOrEmpty(keyFilePath))
            {
                try
                {
                    string resolvedKeyFile = ResolveStrongNameKeyFile(keyFilePath, FileSystem, _keyFileSearchPaths);
                    if (resolvedKeyFile == null)
                    {
                        return(new StrongNameKeys(StrongNameKeys.GetKeyFileError(messageProvider, keyFilePath, CodeAnalysisResources.FileNotFound)));
                    }

                    Debug.Assert(PathUtilities.IsAbsolute(resolvedKeyFile));
                    var fileContent = ImmutableArray.Create(FileSystem.ReadAllBytes(resolvedKeyFile));
                    return(StrongNameKeys.CreateHelper(fileContent, keyFilePath, hasCounterSignature));
                }
                catch (Exception ex)
                {
                    return(new StrongNameKeys(StrongNameKeys.GetKeyFileError(messageProvider, keyFilePath, ex.Message)));
                }
            }
            else if (!string.IsNullOrEmpty(keyContainerName))
            {
                try
                {
                    ReadKeysFromContainer(keyContainerName, out publicKey);
                    container = keyContainerName;
                }
                catch (ClrStrongNameMissingException)
                {
                    return(new StrongNameKeys(StrongNameKeys.GetContainerError(messageProvider, keyContainerName,
                                                                               new CodeAnalysisResourcesLocalizableErrorArgument(nameof(CodeAnalysisResources.AssemblySigningNotSupported)))));
                }
                catch (Exception ex)
                {
                    return(new StrongNameKeys(StrongNameKeys.GetContainerError(messageProvider, keyContainerName, ex.Message)));
                }
            }

            return(new StrongNameKeys(keyPair, publicKey, privateKey: null, container, keyFilePath, hasCounterSignature));
        }
Пример #11
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());
        }
Пример #12
0
        internal static ImmutableArray <PortableExecutableReference> ResolveMetadataReference(CommandLineReference cmdReference, MetadataReferenceResolver metadataResolver, List <DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(metadataResolver != null);
            Debug.Assert((diagnosticsOpt == null) == (messageProviderOpt == null));

            ImmutableArray <PortableExecutableReference> references;

            try
            {
                references = metadataResolver.ResolveReference(cmdReference.Reference, baseFilePath: null, properties: cmdReference.Properties);
            }
            catch (Exception e) when(diagnosticsOpt != null && (e is BadImageFormatException || e is IOException))
            {
                var diagnostic = PortableExecutableReference.ExceptionToDiagnostic(e, messageProviderOpt, Location.None, cmdReference.Reference, cmdReference.Properties.Kind);

                diagnosticsOpt.Add(((DiagnosticWithInfo)diagnostic).Info);
                return(ImmutableArray <PortableExecutableReference> .Empty);
            }

            if (references.IsDefaultOrEmpty && diagnosticsOpt != null)
            {
                diagnosticsOpt.Add(new DiagnosticInfo(messageProviderOpt, messageProviderOpt.ERR_MetadataFileNotFound, cmdReference.Reference));
                return(ImmutableArray <PortableExecutableReference> .Empty);
            }

            return(references);
        }
Пример #13
0
        internal virtual bool ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, List <DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt, List <MetadataReference> resolved)
        {
            bool result = true;

            foreach (CommandLineReference cmdReference in MetadataReferences)
            {
                var references = ResolveMetadataReference(cmdReference, metadataResolver, diagnosticsOpt, messageProviderOpt);
                if (!references.IsDefaultOrEmpty)
                {
                    resolved.AddRange(references);
                }
                else
                {
                    result = false;
                    if (diagnosticsOpt == null)
                    {
                        // no diagnostic, so leaved unresolved reference in list
                        resolved.Add(new UnresolvedMetadataReference(cmdReference.Reference, cmdReference.Properties));
                    }
                }
            }

            return(result);
        }
Пример #14
0
        /// <summary>
        /// Resolves metadata references stored in <see cref="MetadataReferences"/> using given file resolver and metadata provider.
        /// If a non-null diagnostic bag <paramref name="diagnosticsOpt"/> is provided, it catches exceptions that may be generated while reading the metadata file and
        /// reports appropriate diagnostics.
        /// Otherwise, if <paramref name="diagnosticsOpt"/> is null, the exceptions are unhandled.
        /// </summary>
        /// <remarks>
        /// called by CommonCompiler with diagnostics and message provider
        /// </remarks>
        internal IEnumerable <MetadataReference> ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, List <DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(metadataResolver != null);

            var resolved = new List <MetadataReference>();

            this.ResolveMetadataReferences(metadataResolver, diagnosticsOpt, messageProviderOpt, resolved);

            return(resolved);
        }