Skip to content

Commit

Permalink
Title and message resources should be enforced to exist to prevent pr…
Browse files Browse the repository at this point in the history
…inting empty messages (#64151)

Sync ILLink.Shared folder with the latest version in dotnet/linker main branch

List of changes include:
- Enforce title and message resources to exist to prevent printing empty messages
- All diagnostics produced by linker now have a DiagnosticId, a title and a message
- Schema for xml link attributes file

- Added a readme file to the ILLink.Shared project to keep track of the commit is being used from dotnet/linker
  • Loading branch information
tlakollo authored Jan 24, 2022
1 parent 1f2116c commit 8541321
Show file tree
Hide file tree
Showing 17 changed files with 1,278 additions and 76 deletions.
8 changes: 1 addition & 7 deletions src/coreclr/tools/Common/Compiler/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Internal.TypeSystem.Ecma;

using ILCompiler.Logging;
using ILLink.Shared;

using ILSequencePoint = Internal.IL.ILSequencePoint;
using MethodIL = Internal.IL.MethodIL;
Expand Down Expand Up @@ -198,11 +199,4 @@ private static string GetModuleFileName(ModuleDesc module)
return assemblyName;
}
}

public static class MessageSubCategory
{
public const string None = "";
public const string TrimAnalysis = "Trim analysis";
public const string AotAnalysis = "AOT analysis";
}
}
2 changes: 2 additions & 0 deletions src/coreclr/tools/Common/Compiler/Logging/MessageContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;

using ILLink.Shared;

using Debug = System.Diagnostics.Debug;

namespace ILCompiler.Logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;

using ILLink.Shared;

using Debug = System.Diagnostics.Debug;

namespace ILCompiler.Dataflow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,8 @@ public override bool HandleCall(MethodIL callingMethodBody, MethodDesc calledMet
// We don't know what method the `MakeGenericMethod` was called on, so we have to assume
// that the method may have requirements which we can't fullfil -> warn.
reflectionContext.RecordUnrecognizedPattern(
(int)DiagnosticId.MakeGenericMethodCannotBeStaticallyAnalyzed,
new DiagnosticString(DiagnosticId.MakeGenericMethodCannotBeStaticallyAnalyzed).GetMessage(DiagnosticUtilities.GetMethodSignatureDisplayName(calledMethod)));
(int)DiagnosticId.MakeGenericMethod,
new DiagnosticString(DiagnosticId.MakeGenericMethod).GetMessage(DiagnosticUtilities.GetMethodSignatureDisplayName(calledMethod)));
}

RequireDynamicallyAccessedMembers(
Expand All @@ -1048,8 +1048,8 @@ public override bool HandleCall(MethodIL callingMethodBody, MethodDesc calledMet
// We don't know what method the `MakeGenericMethod` was called on, so we have to assume
// that the method may have requirements which we can't fullfil -> warn.
reflectionContext.RecordUnrecognizedPattern(
(int)DiagnosticId.MakeGenericMethodCannotBeStaticallyAnalyzed,
new DiagnosticString(DiagnosticId.MakeGenericMethodCannotBeStaticallyAnalyzed).GetMessage(DiagnosticUtilities.GetMethodSignatureDisplayName(calledMethod)));
(int)DiagnosticId.MakeGenericMethod,
new DiagnosticString(DiagnosticId.MakeGenericMethod).GetMessage(DiagnosticUtilities.GetMethodSignatureDisplayName(calledMethod)));
}

RequireDynamicallyAccessedMembers(
Expand Down Expand Up @@ -2162,8 +2162,8 @@ public override bool HandleCall(MethodIL callingMethodBody, MethodDesc calledMet
// We don't know what method the `MakeGenericMethod` was called on, so we have to assume
// that the method may have requirements which we can't fullfil -> warn.
reflectionContext.RecordUnrecognizedPattern(
(int)DiagnosticId.MakeGenericMethodCannotBeStaticallyAnalyzed,
new DiagnosticString(DiagnosticId.MakeGenericMethodCannotBeStaticallyAnalyzed).GetMessage(
(int)DiagnosticId.MakeGenericMethod,
new DiagnosticString(DiagnosticId.MakeGenericMethod).GetMessage(
DiagnosticUtilities.GetMethodSignatureDisplayName(calledMethod)));
}
}
Expand Down Expand Up @@ -3066,8 +3066,8 @@ void ValidateGenericMethodInstantiation(
if (!AnalyzeGenericInstantiationTypeArray(genericParametersArray, ref reflectionContext, reflectionMethod, genericMethod.GetMethodDefinition().Instantiation))
{
reflectionContext.RecordUnrecognizedPattern(
(int)DiagnosticId.MakeGenericMethodCannotBeStaticallyAnalyzed,
new DiagnosticString(DiagnosticId.MakeGenericMethodCannotBeStaticallyAnalyzed).GetMessage(DiagnosticUtilities.GetMethodSignatureDisplayName(reflectionMethod)));
(int)DiagnosticId.MakeGenericMethod,
new DiagnosticString(DiagnosticId.MakeGenericMethod).GetMessage(DiagnosticUtilities.GetMethodSignatureDisplayName(reflectionMethod)));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Internal.IL;
using Internal.TypeSystem;

using ILLink.Shared;

namespace ILCompiler.Dataflow
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;

using ILLink.Shared;

using Debug = System.Diagnostics.Debug;

namespace ILCompiler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using ILCompiler.Metadata;
using ILCompiler.DependencyAnalysis;
using ILCompiler.DependencyAnalysisFramework;
using ILLink.Shared;

using FlowAnnotations = ILCompiler.Dataflow.FlowAnnotations;
using DependencyList = ILCompiler.DependencyAnalysisFramework.DependencyNodeCore<ILCompiler.DependencyAnalysis.NodeFactory>.DependencyList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,5 @@
</Compile>
</ItemGroup>

<Import Project="..\ILLink.Shared\ILLink.Shared.projitems" Label="Shared" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using ILCompiler.DependencyAnalysis;
using ILCompiler.DependencyAnalysisFramework;
using ILLink.Shared;

using Internal.IL;
using Internal.IL.Stubs;
Expand Down
8 changes: 2 additions & 6 deletions src/coreclr/tools/aot/ILCompiler/ILCompiler.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>ilc</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -121,10 +121,6 @@
On Linux renaming the library makes it difficult to debug it. -->
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Content Remove="$(RuntimeBinDir)$(LibPrefix)clrjit_$(TargetSpec)$(LibSuffix)" />
<Content Include="$(RuntimeBinDir)$(LibPrefix)clrjit$(LibSuffix)"
CopyToOutputDirectory="PreserveNewest"
CopyToPublishDirectory="PreserveNewest"
Link="$(LibPrefix)clrjit_$(TargetSpec)$(LibSuffix)"
/>
<Content Include="$(RuntimeBinDir)$(LibPrefix)clrjit$(LibSuffix)" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" Link="$(LibPrefix)clrjit_$(TargetSpec)$(LibSuffix)" />
</ItemGroup>
</Project>
156 changes: 153 additions & 3 deletions src/coreclr/tools/aot/ILLink.Shared/DiagnosticId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,123 @@ namespace ILLink.Shared
{
public enum DiagnosticId
{
// Linker error ids.
XmlFeatureDoesNotSpecifyFeatureValue = 1001,
XmlUnsupportedNonBooleanValueForFeature = 1002,
XmlException = 1003,
_unused_FailedToProcessDescriptorFile = 1004,
CouldNotFindMethodInAssembly = 1005,
CannotStubConstructorWhenBaseTypeDoesNotHaveConstructor = 1006,
CouldNotFindType = 1007,
CouldNotFindConstructor = 1008,
CouldNotFindAssemblyReference = 1009,
CouldNotLoadAssembly = 1010,
FailedToWriteOutput = 1011,
LinkerUnexpectedError = 1012,
ErrorProcessingXmlLocation = 1013,
XmlDocumentLocationHasInvalidFeatureDefault = 1014,
UnrecognizedCommandLineOption = 1015,
InvalidWarningVersion = 1016,
InvalidGenerateWarningSuppressionsValue = 1017,
MissingArgumentForCommanLineOptionName = 1018,
CustomDataFormatIsInvalid = 1019,
NoFilesToLinkSpecified = 1020,
NewMvidAndDeterministicCannotBeUsedAtSameTime = 1021,
AssemblyInCustomStepOptionCouldNotBeFound = 1022,
AssemblyPathInCustomStepMustBeFullyQualified = 1023,
InvalidArgForCustomStep = 1024,
ExpectedSignToControlNewStepInsertion = 1025,
PipelineStepCouldNotBeFound = 1026,
CustomStepTypeCouldNotBeFound = 1027,
CustomStepTypeIsIncompatibleWithLinkerVersion = 1028,
InvalidOptimizationValue = 1029,
InvalidArgumentForTokenOption = 1030,
InvalidAssemblyAction = 1031,
RootAssemblyCouldNotBeFound = 1032,
XmlDescriptorCouldNotBeFound = 1033,
RootAssemblyDoesNotHaveEntryPoint = 1034,
RootAssemblyCannotUseAction = 1035,
InvalidAssemblyName = 1036,
InvalidAssemblyRootMode = 1037,
ExportedTypeCannotBeResolved = 1038,
ReferenceAssemblyCouldNotBeLoaded = 1039,
FailedToResolveMetadataElement = 1040,
TypeUsedWithAttributeValueCouldNotBeFound = 1041,
CannotConverValueToType = 1042,
CustomAttributeArgumentForTypeRequiresNestedNode = 1043,
CouldNotResolveCustomAttributeTypeValue = 1044,
UnexpectedAttributeArgumentType = 1045,
InvalidMetadataOption = 1046,

// Linker diagnostic ids.
TypeHasNoFieldsToPreserve = 2001,
TypeHasNoMethodsToPreserve = 2002,
CouldNotResolveDependencyAssembly = 2003,
CouldNotResolveDependencyType = 2004,
CouldNotResolveDependencyMember = 2005,
_unused_UnrecognizedReflectionPattern = 2006,
XmlCouldNotResolveAssembly = 2007,
XmlCouldNotResolveType = 2008,
XmlCouldNotFindMethodOnType = 2009,
XmlInvalidValueForStub = 2010,
XmlUnkownBodyModification = 2011,
XmlCouldNotFindFieldOnType = 2012,
XmlSubstitutedFieldNeedsToBeStatic = 2013,
XmlMissingSubstitutionValueForField = 2014,
XmlInvalidSubstitutionValueForField = 2015,
XmlCouldNotFindEventOnType = 2016,
XmlCouldNotFindPropertyOnType = 2017,
XmlCouldNotFindGetAccesorOfPropertyOnType = 2018,
XmlCouldNotFindSetAccesorOfPropertyOnType = 2019,
_unused_RearrangedXmlWarning1 = 2020,
_unused_RearrangedXmlWarning2 = 2021,
XmlCouldNotFindMatchingConstructorForCustomAttribute = 2022,
XmlMoreThanOneReturnElementForMethod = 2023,
XmlMoreThanOneValyForParameterOfMethod = 2024,
XmlDuplicatePreserveMember = 2025,
RequiresUnreferencedCode = 2026,
AttributeShouldOnlyBeUsedOnceOnMember = 2027,
AttributeDoesntHaveTheRequiredNumberOfParameters = 2028,
XmlElementDoesNotContainRequiredAttributeFullname = 2029,
XmlCouldNotResolveAssemblyForAttribute = 2030,
XmlAttributeTypeCouldNotBeFound = 2031,
UnrecognizedParameterInMethodCreateInstance = 2032,
DeprecatedPreserveDependencyAttribute = 2033,
DynamicDependencyAttributeCouldNotBeAnalyzed = 2034,
UnresolvedAssemblyInDynamicDependencyAttribute = 2035,
UnresolvedTypeInDynamicDependencyAttribute = 2036,
NoMembersResolvedForMemberSignatureOrType = 2037,
XmlMissingNameAttributeInResource = 2038,
XmlInvalidValueForAttributeActionForResource = 2039,
XmlCouldNotFindResourceToRemoveInAssembly = 2040,
DynamicallyAccessedMembersIsNotAllowedOnMethods = 2041,
DynamicallyAccessedMembersCouldNotFindBackingField = 2042,
DynamicallyAccessedMembersConflictsBetweenPropertyAndAccessor = 2043,
XmlCouldNotFindAnyTypeInNamespace = 2044,
AttributeIsReferencedButTrimmerRemoveAllInstances = 2045,
RequiresUnreferencedCodeAttributeMismatch = 2046,
_unused_DynamicallyAccessedMembersMismatchBetweenOverrides = 2047,
XmlRemoveAttributeInstancesCanOnlyBeUsedOnType = 2048,
_unused_UnrecognizedInternalAttribute = 2049,
CorrectnessOfCOMCannotBeGuaranteed = 2050,
XmlPropertyDoesNotContainAttributeName = 2051,
XmlCouldNotFindProperty = 2052,
_unused_XmlInvalidPropertyValueForProperty = 2053,
_unused_XmlInvalidArgumentForParameterOfType = 2054,
MakeGenericType = 2055,
DynamicallyAccessedMembersOnPropertyConflictsWithBackingField = 2056,
UnrecognizedTypeNameInTypeGetType = 2057,
ParametersOfAssemblyCreateInstanceCannotBeAnalyzed = 2058,
UnrecognizedTypeInRuntimeHelpersRunClassConstructor = 2059,
MakeGenericMethod = 2060,
RequiresOnBaseClass = 2109,
RequiresUnreferencedCodeOnStaticConstructor = 2116,
UnresolvedAssemblyInCreateInstance = 2061,
MethodParameterCannotBeStaticallyDetermined = 2062,
MethodReturnValueCannotBeStaticallyDetermined = 2063,
FieldValueCannotBeStaticallyDetermined = 2064,
ImplicitThisCannotBeStaticallyDetermined = 2065,
TypePassedToGenericParameterCannotBeStaticallyDetermined = 2066,

// Dynamically Accessed Members attribute mismatch.
MakeGenericMethodCannotBeStaticallyAnalyzed = 2060,
DynamicallyAccessedMembersMismatchParameterTargetsParameter = 2067,
DynamicallyAccessedMembersMismatchParameterTargetsMethodReturnType = 2068,
DynamicallyAccessedMembersMismatchParameterTargetsField = 2069,
Expand All @@ -41,7 +147,32 @@ public enum DiagnosticId
DynamicallyAccessedMembersMismatchTypeArgumentTargetsField = 2089,
DynamicallyAccessedMembersMismatchTypeArgumentTargetsThisParameter = 2090,
DynamicallyAccessedMembersMismatchTypeArgumentTargetsGenericParameter = 2091,
DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides = 2092,
DynamicallyAccessedMembersMismatchOnMethodReturnValueBetweenOverrides = 2093,
DynamicallyAccessedMembersMismatchOnImplicitThisBetweenOverrides = 2094,
DynamicallyAccessedMembersMismatchOnGenericParameterBetweenOverrides = 2095,

CaseInsensitiveTypeGetTypeCallIsNotSupported = 2096,
DynamicallyAccessedMembersOnFieldCanOnlyApplyToTypesOrStrings = 2097,
DynamicallyAccessedMembersOnMethodParameterCanOnlyApplyToTypesOrStrings = 2098,
DynamicallyAccessedMembersOnPropertyCanOnlyApplyToTypesOrStrings = 2099,
XmlUnsuportedWildcard = 2100,
AssemblyWithEmbeddedXmlApplyToAnotherAssembly = 2101,
InvalidIsTrimmableValue = 2102,
PropertyAccessorParameterInLinqExpressionsCannotBeStaticallyDetermined = 2103,
AssemblyProducedTrimWarnings = 2104,
TypeWasNotFoundInAssemblyNorBaseLibrary = 2105,
DynamicallyAccessedMembersOnMethodReturnValueCanOnlyApplyToTypesOrStrings = 2106,
MethodsAreAssociatedWithStateMachine = 2107,
InvalidScopeInUnconditionalSuppressMessage = 2108,
RequiresUnreferencedCodeOnBaseClass = 2109,
DynamicallyAccessedMembersFieldAccessedViaReflection = 2110,
DynamicallyAccessedMembersMethodAccessedViaReflection = 2111,
DynamicallyAccessedMembersOnTypeReferencesMemberWithRequiresUnreferencedCode = 2112,
DynamicallyAccessedMembersOnTypeReferencesMemberOnBaseWithRequiresUnreferencedCode = 2113,
DynamicallyAccessedMembersOnTypeReferencesMemberWithDynamicallyAccessedMembers = 2114,
DynamicallyAccessedMembersOnTypeReferencesMemberOnBaseWithDynamicallyAccessedMembers = 2115,
RequiresUnreferencedCodeOnStaticConstructor = 2116,

// Single-file diagnostic ids.
AvoidAssemblyLocationInSingleFile = 3000,
Expand All @@ -62,5 +193,24 @@ public enum DiagnosticId
public static class DiagnosticIdExtensions
{
public static string AsString(this DiagnosticId diagnosticId) => $"IL{(int)diagnosticId}";

public static string GetDiagnosticSubcategory(this DiagnosticId diagnosticId) =>
(int)diagnosticId switch
{
2026 => MessageSubCategory.TrimAnalysis,
2032 => MessageSubCategory.TrimAnalysis,
2041 => MessageSubCategory.TrimAnalysis,
2042 => MessageSubCategory.TrimAnalysis,
2043 => MessageSubCategory.TrimAnalysis,
2045 => MessageSubCategory.TrimAnalysis,
2046 => MessageSubCategory.TrimAnalysis,
2050 => MessageSubCategory.TrimAnalysis,
var x when x >= 2055 && x <= 2099 => MessageSubCategory.TrimAnalysis,
2103 => MessageSubCategory.TrimAnalysis,
2106 => MessageSubCategory.TrimAnalysis,
2107 => MessageSubCategory.TrimAnalysis,
var x when x >= 2109 && x <= 2116 => MessageSubCategory.TrimAnalysis,
_ => MessageSubCategory.None,
};
}
}
10 changes: 6 additions & 4 deletions src/coreclr/tools/aot/ILLink.Shared/DiagnosticString.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace ILLink.Shared
{
public readonly struct DiagnosticString
Expand All @@ -11,15 +13,15 @@ public readonly struct DiagnosticString
public DiagnosticString(DiagnosticId diagnosticId)
{
var resourceManager = SharedStrings.ResourceManager;
_titleFormat = resourceManager.GetString($"{diagnosticId}Title") ?? string.Empty;
_messageFormat = resourceManager.GetString($"{diagnosticId}Message") ?? string.Empty;
_titleFormat = resourceManager.GetString($"{diagnosticId}Title") ?? throw new InvalidOperationException($"{diagnosticId} does not have a matching resource called {diagnosticId}Title");
_messageFormat = resourceManager.GetString($"{diagnosticId}Message") ?? throw new InvalidOperationException($"{diagnosticId} does not have a matching resource called {diagnosticId}Message");
}

public DiagnosticString(string diagnosticResourceStringName)
{
var resourceManager = SharedStrings.ResourceManager;
_titleFormat = resourceManager.GetString($"{diagnosticResourceStringName}Title") ?? string.Empty;
_messageFormat = resourceManager.GetString($"{diagnosticResourceStringName}Message") ?? string.Empty;
_titleFormat = resourceManager.GetString($"{diagnosticResourceStringName}Title") ?? throw new InvalidOperationException($"{diagnosticResourceStringName} does not have a matching resource called {diagnosticResourceStringName}Title");
_messageFormat = resourceManager.GetString($"{diagnosticResourceStringName}Message") ?? throw new InvalidOperationException($"{diagnosticResourceStringName} does not have a matching resource called {diagnosticResourceStringName}Message");
}

public string GetMessage(params string[] args) =>
Expand Down
Loading

0 comments on commit 8541321

Please sign in to comment.