static bool TryLogSingleWarning(LinkContext context, int code, MessageOrigin origin, string subcategory) { if (subcategory != MessageSubCategory.TrimAnalysis) { return(false); } // There are valid cases where we can't map the message to an assembly // For example if it's caused by something in an xml file passed on the command line // In that case, give up on single-warn collapse and just print out the warning on its own. var assembly = origin.Provider switch { AssemblyDefinition asm => asm, TypeDefinition type => type.Module.Assembly, IMemberDefinition member => member.DeclaringType.Module.Assembly, _ => null }; if (assembly == null) { return(false); } // Any IL2026 warnings left in an assembly with an IsTrimmable attribute are considered intentional // and should not be collapsed, so that the user-visible RUC message gets printed. if (code == 2026 && context.IsTrimmable(assembly)) { return(false); } var assemblyName = assembly.Name.Name; if (!context.IsSingleWarn(assemblyName)) { return(false); } if (context.AssembliesWithGeneratedSingleWarning.Add(assemblyName)) { context.LogWarning(context.GetAssemblyLocation(assembly), DiagnosticId.AssemblyProducedTrimWarnings, assemblyName); } return(true); }
static bool TryLogSingleWarning(LinkContext context, int code, MessageOrigin origin, string subcategory) { if (subcategory != MessageSubCategory.TrimAnalysis) { return(false); } Debug.Assert(origin.Provider != null); var assembly = origin.Provider switch { AssemblyDefinition asm => asm, TypeDefinition type => type.Module.Assembly, IMemberDefinition member => member.DeclaringType.Module.Assembly, _ => throw new NotSupportedException() }; Debug.Assert(assembly != null); if (assembly == null) { return(false); } // Any IL2026 warnings left in an assembly with an IsTrimmable attribute are considered intentional // and should not be collapsed, so that the user-visible RUC message gets printed. if (code == 2026 && context.IsTrimmable(assembly)) { return(false); } var assemblyName = assembly.Name.Name; if (!context.IsSingleWarn(assemblyName)) { return(false); } if (context.AssembliesWithGeneratedSingleWarning.Add(assemblyName)) { context.LogWarning($"Assembly '{assemblyName}' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries", 2104, context.GetAssemblyLocation(assembly)); } return(true); }