Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asm diff updates #13641

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion src/Microsoft.Cci.Extensions/Comparers/AttributeComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Cci.Extensions;
using Microsoft.Cci.Filters;
using Microsoft.Cci.Writers.CSharp;
Expand All @@ -19,7 +21,7 @@ public AttributeComparer()

public AttributeComparer(ICciFilter filter, bool forCompilation)
{
_helper = new CSDeclarationHelper(filter, forCompilation);
_helper = new CSDeclarationHelper(filter, new List<string>(), forCompilation);
}

public override string GetKey(ICustomAttribute c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private IEnumerable<SyntaxToken> GetTokenList(IDefinition item)
return DiffingService.GetTokenList(item);

if (_declHelper == null)
_declHelper = new CSDeclarationHelper(new PublicOnlyCciFilter());
_declHelper = new CSDeclarationHelper(new PublicOnlyCciFilter(), new List<string>());

return _declHelper.GetTokenList(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public static class CSharpCciExtensions
(byte)'S', (byte)'e', (byte)'r', (byte)'v', (byte)'i', (byte)'c', (byte)'e', (byte)'s'
};

public static string GetCSharpDeclaration(this IDefinition definition, bool includeAttributes = false)
public static string GetCSharpDeclaration(this IDefinition definition, IEnumerable<string> attributesToExclude)
{
using (var stringWriter = new StringWriter())
{
using (var syntaxWriter = new TextSyntaxWriter(stringWriter))
{
var writer = new CSDeclarationWriter(syntaxWriter, new AttributesFilter(includeAttributes), false, true);
var writer = new CSDeclarationWriter(syntaxWriter, new AttributesFilter(true), false, true, attributesToExclude);

var nsp = definition as INamespaceDefinition;
var typeDefinition = definition as ITypeDefinition;
Expand Down
10 changes: 6 additions & 4 deletions src/Microsoft.Cci.Extensions/Mappings/MappingSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class MappingSettings
{
public MappingSettings(bool excludeAttributes = true)
{
this.ElementCount = 2;
this.Filter = new PublicOnlyCciFilter(excludeAttributes);
this.Comparers = CciComparers.Default;
this.DiffFactory = new ElementDifferenceFactory();
ElementCount = 2;
Filter = new PublicOnlyCciFilter(excludeAttributes);
Comparers = CciComparers.Default;
DiffFactory = new ElementDifferenceFactory();
}

public IEnumerable<string> AttributesToExclude { get; set; }

public ICciFilter Filter { get; set; }

public IMappingDifferenceFilter DiffFilter { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.Cci.Extensions/Mappings/SimpleElementMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Cci.Differs;

namespace Microsoft.Cci.Mappings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ public class CSDeclarationHelper
private TokenSyntaxWriter _tokenizer;
private CSDeclarationWriter _tokenWriter;

public CSDeclarationHelper(ICciFilter filter, bool forCompilation = false, bool includePseudoCustomAttributes = false)
private readonly IEnumerable<string> _attributesToExclude;

public CSDeclarationHelper(ICciFilter filter, IEnumerable<string> attributesToExclude, bool forCompilation = false, bool includePseudoCustomAttributes = false)
{
_filter = filter;
_forCompilation = forCompilation;
_includeFakeAttributes = includePseudoCustomAttributes;
_attributesToExclude = attributesToExclude;
}

public string GetString(IDefinition definition, int indentLevel = -1)
Expand Down Expand Up @@ -92,7 +95,7 @@ private void EnsureStringWriter()
StringWriter sw = new StringWriter(_string);
TextSyntaxWriter tsw = new TextSyntaxWriter(sw);

_stringWriter = new CSDeclarationWriter(tsw, _filter, _forCompilation, _includeFakeAttributes);
_stringWriter = new CSDeclarationWriter(tsw, _filter, _forCompilation, _includeFakeAttributes, _attributesToExclude);
}
}

Expand All @@ -101,7 +104,7 @@ private void EnsureTokenWriter()
if (_tokenWriter == null)
{
_tokenizer = new TokenSyntaxWriter();
_tokenWriter = new CSDeclarationWriter(_tokenizer, _filter, _forCompilation, _includeFakeAttributes);
_tokenWriter = new CSDeclarationWriter(_tokenizer, _filter, _forCompilation, _includeFakeAttributes, _attributesToExclude);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Cci.Comparers;
Expand Down Expand Up @@ -370,40 +372,33 @@ private static string EscapeChar(char c, bool inString)
return c.ToString();
}

private static bool ExcludeSpecialAttribute(ICustomAttribute c)
private bool ExcludeSpecialAttribute(ICustomAttribute c)
{
string typeName = c.FullName();

switch (typeName)
if (typeName == "System.ObsoleteAttribute")
{
case "System.ParamArrayAttribute": return true;
case "System.Reflection.AssemblyDelaySignAttribute": return true;
case "System.Reflection.AssemblyKeyFileAttribute": return true;
case "System.Reflection.DefaultMemberAttribute": return true;
case "System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute": return true;
case "System.Runtime.CompilerServices.DynamicAttribute": return true;
case "System.Runtime.CompilerServices.ExtensionAttribute": return true;
case "System.Runtime.CompilerServices.FixedBufferAttribute": return true;
case "System.Runtime.CompilerServices.IsByRefLikeAttribute": return true;
case "System.Runtime.CompilerServices.IsReadOnlyAttribute": return true;
case "System.Runtime.CompilerServices.RequiredMemberAttribute": return true;
case "System.Runtime.CompilerServices.TupleElementNamesAttribute": return true;
case "System.ObsoleteAttribute":
{
var arg = c.Arguments.OfType<IMetadataConstant>().FirstOrDefault();
var arg = c.Arguments.OfType<IMetadataConstant>().FirstOrDefault();

if (arg?.Value is string)
{
string argValue = (string)arg.Value;
if (argValue is "Types with embedded references are not supported in this version of your compiler."
or "Constructors of types with required members are not supported in this version of your compiler.")
{
return true;
}
}
break;
if (arg?.Value is string)
{
string argValue = (string)arg.Value;
if (argValue is "Types with embedded references are not supported in this version of your compiler."
or "Constructors of types with required members are not supported in this version of your compiler.")
{
return true;
}
}
}

foreach (string attributeToExclude in _attributesToExclude)
{
if (typeName.Equals(attributeToExclude))
{
return true;
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void WriteMethodDefinition(IMethodDefinition method)
{
writeVisibility = false;
}

if (method.IsExplicitInterfaceMethod() || method.IsStaticConstructor)
{
writeVisibility = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public partial class CSDeclarationWriter : ICciDeclarationWriter, IDisposable
private bool _includeFakeAttributes;
private bool _alwaysIncludeBase;

private readonly IEnumerable<string> _attributesToExclude;

public CSDeclarationWriter(ISyntaxWriter writer)
: this(writer, new PublicOnlyCciFilter())
{
Expand All @@ -56,10 +58,11 @@ public CSDeclarationWriter(ISyntaxWriter writer, ICciFilter filter, bool forComp
_metadataReaderCache = new SRMetadataPEReaderCache();
}

public CSDeclarationWriter(ISyntaxWriter writer, ICciFilter filter, bool forCompilation, bool includePseudoCustomAttributes = false)
public CSDeclarationWriter(ISyntaxWriter writer, ICciFilter filter, bool forCompilation, bool includePseudoCustomAttributes, IEnumerable<string> attributesToExclude)
: this(writer, filter, forCompilation)
{
_includeFakeAttributes = includePseudoCustomAttributes;
_attributesToExclude = attributesToExclude;
}

public bool ForCompilation
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.DotNet.AsmDiff/AttributesToExclude.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Microsoft.AspNetCore.DataProtection.ApplyPolicyAttribute
System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute
System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute
System.ParamArrayAttribute
System.Reflection.AssemblyDelaySignAttribute
System.Reflection.AssemblyKeyFileAttribute
System.Reflection.DefaultMemberAttribute
System.Runtime.CompilerServices.AsyncStateMachineAttribute
System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute
System.Runtime.CompilerServices.CompilerGeneratedAttribute
System.Runtime.CompilerServices.DynamicAttribute
System.Runtime.CompilerServices.ExtensionAttribute
System.Runtime.CompilerServices.FixedBufferAttribute
System.Runtime.CompilerServices.IsByRefLikeAttribute
System.Runtime.CompilerServices.IsReadOnlyAttribute
System.Runtime.CompilerServices.NullableAttribute
System.Runtime.CompilerServices.NullableContextAttribute
System.Runtime.CompilerServices.RequiredMemberAttribute
System.Runtime.CompilerServices.TupleElementNamesAttribute
System.Runtime.InteropServices.StructLayoutAttribute
30 changes: 21 additions & 9 deletions src/Microsoft.DotNet.AsmDiff/DiffCSharpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class DiffCSharpWriter : MappingsTypeMemberTraverser, IDiffingService, IC
private readonly IEnumerable<DiffComment> _diffComments;

public DiffCSharpWriter(IStyleSyntaxWriter writer, MappingSettings settings, IEnumerable<DiffComment> diffComments)
: this(writer, settings, diffComments, includePseudoCustomAttributes:false)
: this(writer, settings, diffComments, includePseudoCustomAttributes: false)
{
}

Expand All @@ -41,24 +41,36 @@ public DiffCSharpWriter(IStyleSyntaxWriter writer, MappingSettings settings, IEn
{
_syntaxWriter = writer;
_settings = InitializeSettings(settings);
_formatter = new CSDeclarationWriter(_syntaxWriter, _settings.Filter, forCompilation: false, includePseudoCustomAttributes: includePseudoCustomAttributes)
_formatter = new CSDeclarationWriter(_syntaxWriter, _settings.Filter, forCompilation: false, includePseudoCustomAttributes: includePseudoCustomAttributes, _settings.AttributesToExclude)
{
LangVersion = CSDeclarationWriter.LangVersionPreview
};
_declHelper = new CSDeclarationHelper(_settings.Filter, forCompilation: false, includePseudoCustomAttributes: includePseudoCustomAttributes);
_declHelper = new CSDeclarationHelper(_settings.Filter, _settings.AttributesToExclude, forCompilation: false, includePseudoCustomAttributes: includePseudoCustomAttributes);
_diffComments = diffComments ?? Enumerable.Empty<DiffComment>();
}

public DiffCSharpWriter(IStyleSyntaxWriter writer, MappingSettings settings)
: this(writer, settings, null, false)
{
{
}

public bool IncludeSpaceBetweenMemberGroups { get; set; }

public bool IncludeMemberGroupHeadings { get; set; }

public bool HighlightBaseMembers { get; set; }
public bool HighlightBaseMembers
{
get => HighlightMemberOverrides && HighlightInterfaceImplementations;
set
{
HighlightMemberOverrides = true;
HighlightInterfaceImplementations = true;
}
}

public bool HighlightMemberOverrides { get; set; }

public bool HighlightInterfaceImplementations { get; set; }

public bool IncludeAssemblyProperties { get; set; }

Expand Down Expand Up @@ -197,11 +209,11 @@ public override void Visit(MemberMapping member)
{
IDisposable style = null;

if (this.HighlightBaseMembers)
if (HighlightMemberOverrides || HighlightInterfaceImplementations)
{
if (member.Representative.IsInterfaceImplementation())
if (HighlightInterfaceImplementations && member.Representative.IsInterfaceImplementation())
style = _syntaxWriter.StartStyle(SyntaxStyle.InterfaceMember);
else if (member.Representative.IsOverride())
else if (HighlightMemberOverrides && member.Representative.IsOverride())
style = _syntaxWriter.StartStyle(SyntaxStyle.InheritedMember);
}

Expand Down Expand Up @@ -237,7 +249,7 @@ private static bool IsPropertyOrEventAccessor(ITypeDefinitionMember representati
foreach (var comment in commentSet)
{
reviewCommentWriter.WriteReviewComment(comment.Author, comment.Text);
_syntaxWriter.WriteLine();
_syntaxWriter.WriteLine();
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/Microsoft.DotNet.AsmDiff/DiffConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;

namespace Microsoft.DotNet.AsmDiff
{
public sealed class DiffConfiguration
Expand All @@ -15,14 +18,16 @@ public DiffConfiguration()
DiffConfigurationOptions.IncludeUnchanged |
DiffConfigurationOptions.IncludeAddedTypes |
DiffConfigurationOptions.IncludeRemovedTypes |
DiffConfigurationOptions.HighlightBaseMembers;
DiffConfigurationOptions.HightlightMemberOverrides |
DiffConfigurationOptions.HighlightInterfaceImplementations;
}

public DiffConfiguration(AssemblySet left, AssemblySet right, DiffConfigurationOptions options)
public DiffConfiguration(AssemblySet left, AssemblySet right, DiffConfigurationOptions options, IEnumerable<string> attributesToExclude)
{
Left = left;
Right = right;
Options = options;
AttributesToExclude = attributesToExclude;
}

public AssemblySet Left { get; private set; }
Expand All @@ -43,5 +48,7 @@ public bool IsDiff
}

public DiffConfigurationOptions Options { get; private set; }

public IEnumerable<string> AttributesToExclude { get; private set; }
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.DotNet.AsmDiff/DiffConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static DiffConfiguration UpdateOptions(this DiffConfiguration configurati
{
var left = configuration.Left;
var right = configuration.Right;
return new DiffConfiguration(left, right, options);
return new DiffConfiguration(left, right, options, configuration.AttributesToExclude);
}

public static bool IsOptionSet(this DiffConfiguration configuration, DiffConfigurationOptions option)
Expand Down Expand Up @@ -147,7 +147,7 @@ private static DiffConfiguration UpdateAssemblies(this DiffConfiguration configu
var newLeft = isLeft ? assemblySet : configuration.Left;
var newRight = isRight ? assemblySet : configuration.Right;

return new DiffConfiguration(newLeft, newRight, configuration.Options);
return new DiffConfiguration(newLeft, newRight, configuration.Options, configuration.AttributesToExclude);
}
}
}
7 changes: 5 additions & 2 deletions src/Microsoft.DotNet.AsmDiff/DiffConfigurationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ public enum DiffConfigurationOptions
GroupByAssembly = 0x400,
FlattenTypes = 0x800,
TypesOnly = 0x1000,
HighlightBaseMembers = 0x2000,
AlwaysDiffMembers = 0x4000,
IncludeAddedTypes = 0x8000,
IncludeRemovedTypes = 0x10000,
StrikeRemoved = 0x20000
StrikeRemoved = 0x20000,
HightlightMemberOverrides = 0x40000,
HighlightInterfaceImplementations = 0x80000,

HighlightBaseMembers = HightlightMemberOverrides | HighlightInterfaceImplementations,
}
}
4 changes: 3 additions & 1 deletion src/Microsoft.DotNet.AsmDiff/DiffDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public sealed class DiffDocument
public AssemblySet Right { get; private set; }
public ReadOnlyCollection<DiffLine> Lines { get; private set; }
public ReadOnlyCollection<DiffApiDefinition> ApiDefinitions { get; private set; }
public IEnumerable<string> AttributesToExclude { get; private set; }

public DiffDocument(AssemblySet left, AssemblySet right, IEnumerable<DiffLine> lines, IEnumerable<DiffApiDefinition> apiDefinitions)
public DiffDocument(AssemblySet left, AssemblySet right, IEnumerable<DiffLine> lines, IEnumerable<DiffApiDefinition> apiDefinitions, IEnumerable<string> attributesToExclude)
{
Left = left;
Right = right;
Lines = new ReadOnlyCollection<DiffLine>(lines.ToArray());
ApiDefinitions = new ReadOnlyCollection<DiffApiDefinition>(apiDefinitions.ToArray());
AttributesToExclude = attributesToExclude;
}

public bool IsDiff
Expand Down