Beispiel #1
0
            > GetAnalyzerTypeNameMap(
            string fullPath,
            Type attributeType,
            AttributeLanguagesFunc languagesFunc
            )
        {
            using var assembly = AssemblyMetadata.CreateFromFile(fullPath);

            // This is longer than strictly necessary to avoid thrashing the GC with string allocations
            // in the call to GetFullyQualifiedTypeNames. Specifically, this checks for the presence of
            // supported languages prior to creating the type names.
            var typeNameMap =
                from module in assembly.GetModules()
                from typeDefHandle in module.MetadataReader.TypeDefinitions
                let typeDef = module.MetadataReader.GetTypeDefinition(typeDefHandle)
                              let supportedLanguages = GetSupportedLanguages(
                    typeDef,
                    module.Module,
                    attributeType,
                    languagesFunc
                    )
                                                       where supportedLanguages.Any()
                                                       let typeName = GetFullyQualifiedTypeName(typeDef, module.Module)
                                                                      from supportedLanguage in supportedLanguages
                                                                      group typeName by supportedLanguage;

            return(typeNameMap.ToImmutableSortedDictionary(
                       g => g.Key,
                       g => g.ToImmutableSortedSet(StringComparer.OrdinalIgnoreCase),
                       StringComparer.OrdinalIgnoreCase
                       ));
        }
Beispiel #2
0
 internal Extensions(AnalyzerFileReference reference, AttributePredicate attributePredicate, AttributeLanguagesFunc languagesFunc)
 {
     _reference                 = reference;
     _attributePredicate        = attributePredicate;
     _languagesFunc             = languagesFunc;
     _lazyAllExtensions         = default;
     _lazyExtensionsPerLanguage = ImmutableDictionary <string, ImmutableArray <TExtension> > .Empty;
 }
Beispiel #3
0
 internal Extensions(AnalyzerFileReference reference, Type attributeType, AttributeLanguagesFunc languagesFunc, bool allowNetFramework)
 {
     _reference                 = reference;
     _attributeType             = attributeType;
     _languagesFunc             = languagesFunc;
     _allowNetFramework         = allowNetFramework;
     _lazyAllExtensions         = default;
     _lazyExtensionsPerLanguage = ImmutableDictionary <string, ImmutableArray <TExtension> > .Empty;
 }
Beispiel #4
0
        private static IEnumerable <string> GetSupportedLanguages(
            TypeDefinition typeDef,
            PEModule peModule,
            Type attributeType,
            AttributeLanguagesFunc languagesFunc
            )
        {
            IEnumerable <string>?result = null;

            foreach (CustomAttributeHandle customAttrHandle in typeDef.GetCustomAttributes())
            {
                if (
                    peModule.IsTargetAttribute(
                        customAttrHandle,
                        attributeType.Namespace !,
                        attributeType.Name,
                        ctor: out _
                        )
                    )
                {
                    if (
                        languagesFunc(peModule, customAttrHandle) is { } attributeSupportedLanguages
                        )
                    {
                        if (result is null)
                        {
                            result = attributeSupportedLanguages;
                        }
                        else
                        {
                            // This is a slow path, but only occurs if a single type has multiple
                            // DiagnosticAnalyzerAttribute instances applied to it.
                            result = result.Concat(attributeSupportedLanguages);
                        }
                    }
                }
            }

            return(result ?? SpecializedCollections.EmptyEnumerable <string>());
        }
Beispiel #5
0
        private static IEnumerable <string> GetSupportedLanguages(TypeDefinition typeDef, PEModule peModule, Type attributeType, AttributeLanguagesFunc languagesFunc)
        {
            var attributeLanguagesList = from customAttrHandle in typeDef.GetCustomAttributes()
                                         where peModule.IsTargetAttribute(customAttrHandle, attributeType.Namespace !, attributeType.Name, ctor: out _)
                                         let attributeSupportedLanguages = languagesFunc(peModule, customAttrHandle)
                                                                           where attributeSupportedLanguages != null
                                                                           select attributeSupportedLanguages;

            return(attributeLanguagesList.SelectMany(x => x));
        }
Beispiel #6
0
        private static IEnumerable <string> GetSupportedLanguages(TypeDefinition typeDef, PEModule peModule, AttributePredicate attributePredicate, AttributeLanguagesFunc languagesFunc)
        {
            var attributeLanguagesList = from customAttrHandle in typeDef.GetCustomAttributes()
                                         where attributePredicate(peModule, customAttrHandle)
                                         let attributeSupportedLanguages = languagesFunc(peModule, customAttrHandle)
                                                                           where attributeSupportedLanguages != null
                                                                           select attributeSupportedLanguages;

            return(attributeLanguagesList.SelectMany(x => x));
        }
 private static IEnumerable <string> GetSupportedLanguages(TypeDefinition typeDef, PEModule peModule, Type attributeType, AttributeLanguagesFunc languagesFunc)
 {
     foreach (CustomAttributeHandle customAttrHandle in typeDef.GetCustomAttributes())
     {
         if (peModule.IsTargetAttribute(customAttrHandle, attributeType.Namespace !, attributeType.Name, ctor: out _))
         {
             IEnumerable <string>?attributeSupportedLanguages = languagesFunc(peModule, customAttrHandle);
             if (attributeSupportedLanguages != null)
             {
                 foreach (string item in attributeSupportedLanguages)
                 {
                     yield return(item);
                 }
             }
         }
     }
 }