Browse Source

Add non-embedded attributes to all tests that use older framework versions.

pull/3403/head
Siegfried Pammer 4 months ago
parent
commit
fa50e8d8b3
  1. 45
      ICSharpCode.Decompiler.Tests/Helpers/Tester.cs
  2. 2
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  3. 24
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs
  4. 16
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Structs.cs
  5. 4
      ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs
  6. 16
      ICSharpCode.Decompiler/CSharp/Transforms/EscapeInvalidIdentifiers.cs

45
ICSharpCode.Decompiler.Tests/Helpers/Tester.cs

@ -18,7 +18,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection.PortableExecutable; using System.Reflection.PortableExecutable;
@ -330,6 +329,45 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
return tempFile; return tempFile;
} }
const string nonEmbeddedAttributesSnippet = @"
using System;
#if !NET60
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
internal sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName)
{
}
}
internal class IsExternalInit
{
}
#endif
#if !NET70
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
internal sealed class RequiredMemberAttribute : Attribute
{
}
#endif
#if !NET60
}
#endif
";
static readonly Lazy<string> nonEmbeddedAttributesSnippetFile = new Lazy<string>(GetNonEmbeddedAttributesSnippetFile);
static string GetNonEmbeddedAttributesSnippetFile()
{
// Note: this leaks a temporary file, we're not attempting to delete it, because it is only one.
var tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, nonEmbeddedAttributesSnippet);
return tempFile;
}
public static List<string> GetPreprocessorSymbols(CompilerOptions flags) public static List<string> GetPreprocessorSymbols(CompilerOptions flags)
{ {
var preprocessorSymbols = new List<string>(); var preprocessorSymbols = new List<string>();
@ -419,6 +457,11 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
sourceFileNames.Add(targetFrameworkAttributeSnippetFile.Value); sourceFileNames.Add(targetFrameworkAttributeSnippetFile.Value);
} }
if (targetNet40)
{
sourceFileNames.Add(nonEmbeddedAttributesSnippetFile.Value);
}
var preprocessorSymbols = GetPreprocessorSymbols(flags); var preprocessorSymbols = GetPreprocessorSymbols(flags);
if ((flags & CompilerOptions.UseMcsMask) == 0) if ((flags & CompilerOptions.UseMcsMask) == 0)

2
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -151,7 +151,7 @@
<None Include="TestCases\Pretty\CovariantReturns.cs" /> <None Include="TestCases\Pretty\CovariantReturns.cs" />
<Compile Include="TestCases\VBPretty\VBPropertiesTest.cs" /> <Compile Include="TestCases\VBPretty\VBPropertiesTest.cs" />
<None Include="TestCases\ILPretty\Issue2260SwitchString.cs" /> <None Include="TestCases\ILPretty\Issue2260SwitchString.cs" />
<None Include="TestCases\Pretty\Records.cs" /> <Compile Include="TestCases\Pretty\Records.cs" />
<Compile Include="TestCases\VBPretty\Issue2192.cs" /> <Compile Include="TestCases\VBPretty\Issue2192.cs" />
<Compile Include="Util\FileUtilityTests.cs" /> <Compile Include="Util\FileUtilityTests.cs" />
<Compile Include="TestCases\Pretty\FunctionPointers.cs" /> <Compile Include="TestCases\Pretty\FunctionPointers.cs" />

24
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs

@ -242,27 +242,3 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
} }
#endif #endif
} }
#if !NET60
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
internal sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName)
{
}
}
internal class IsExternalInit
{
}
#endif
#if !NET70
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
internal sealed class RequiredMemberAttribute : Attribute
{
}
#endif
#if !NET60
}
#endif

16
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Structs.cs

@ -60,19 +60,3 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
} }
#endif #endif
} }
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
internal sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName)
{
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
internal sealed class RequiredMemberAttribute : Attribute
{
}
}

4
ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs

@ -1530,8 +1530,8 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
WriteToken(Roles.RBracket); WriteToken(Roles.RBracket);
switch (attributeSection.Parent) switch (attributeSection.Parent)
{ {
case ParameterDeclaration _: case ParameterDeclaration pd:
if (attributeSection.NextSibling is AttributeSection) if (pd.Attributes.Last() != attributeSection)
Space(policy.SpaceBetweenParameterAttributeSections); Space(policy.SpaceBetweenParameterAttributeSections);
else else
Space(); Space();

16
ICSharpCode.Decompiler/CSharp/Transforms/EscapeInvalidIdentifiers.cs

@ -172,13 +172,27 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
"Microsoft.CodeAnalysis.EmbeddedAttribute", "Microsoft.CodeAnalysis.EmbeddedAttribute",
}; };
internal static readonly HashSet<string> nonEmbeddedAttributeNames = new HashSet<string>() {
// non-embedded attributes, but we still want to remove them
"System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute",
"System.Runtime.CompilerServices.RequiredMemberAttribute",
"System.Runtime.CompilerServices.IsExternalInit",
};
public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration) public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
{ {
var typeDefinition = typeDeclaration.GetSymbol() as ITypeDefinition; var typeDefinition = typeDeclaration.GetSymbol() as ITypeDefinition;
if (typeDefinition == null || !attributeNames.Contains(typeDefinition.FullName)) if (typeDefinition == null)
return; return;
if (attributeNames.Contains(typeDefinition.FullName))
{
if (!typeDefinition.HasAttribute(KnownAttribute.Embedded)) if (!typeDefinition.HasAttribute(KnownAttribute.Embedded))
return; return;
}
else if (!nonEmbeddedAttributeNames.Contains(typeDefinition.FullName))
return;
if (typeDeclaration.Parent is NamespaceDeclaration ns && ns.Members.Count == 1) if (typeDeclaration.Parent is NamespaceDeclaration ns && ns.Members.Count == 1)
ns.Remove(); ns.Remove();
else else

Loading…
Cancel
Save