Browse Source

Fix #1758: Input var name conflicting with framework class name

pull/1769/head
Siegfried Pammer 6 years ago
parent
commit
83c525c1c2
  1. 53
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/QualifierTests.cs
  2. 1
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  3. 2
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  4. 80
      ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUsingDeclarations.cs
  5. 8
      ILSpy/Languages/CSharpHighlightingTokenWriter.cs

53
ICSharpCode.Decompiler.Tests/TestCases/Pretty/QualifierTests.cs

@ -128,9 +128,45 @@ namespace ICSharpCode.Decompiler.Tests.Pretty @@ -128,9 +128,45 @@ namespace ICSharpCode.Decompiler.Tests.Pretty
}
}
private class i
{
public static void Test()
{
}
}
private class value
{
public static int item;
public static void Test()
{
}
}
private int fieldConflict;
private int innerConflict;
private static int PropertyValueParameterConflictsWithTypeName {
get {
return value.item;
}
set {
QualifierTests.value.item = value;
}
}
private int this[string[] Array] {
get {
System.Array.Sort(Array);
return 0;
}
set {
System.Array.Sort(Array);
QualifierTests.value.item = value;
}
}
private void NoParameters()
{
Delegate(Parameter);
@ -209,6 +245,23 @@ namespace ICSharpCode.Decompiler.Tests.Pretty @@ -209,6 +245,23 @@ namespace ICSharpCode.Decompiler.Tests.Pretty
{
}
private void ParameterConflictsWithTypeName(string[] Array)
{
System.Array.Sort(Array);
}
private void LocalConflictsWithTypeName()
{
for (int i = 0; i < 10; i++) {
QualifierTests.i.Test();
}
}
public QualifierTests(string[] Array)
{
System.Array.Sort(Array);
}
}
internal static class ZExt

1
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -1298,6 +1298,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1298,6 +1298,7 @@ namespace ICSharpCode.Decompiler.CSharp
parameter.AddAnnotation(new ILVariableResolveResult(v, method.Parameters[i].Type));
i++;
}
entityDecl.AddAnnotation(function);
}
var localSettings = settings.Clone();

2
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -203,7 +203,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -203,7 +203,7 @@ namespace ICSharpCode.Decompiler.CSharp
return true;
}
foreach (var f in function.LocalFunctions.OfType<ILFunction>()) {
foreach (var f in function.LocalFunctions) {
if (f.Name == name)
return true;
}

80
ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUsingDeclarations.cs

@ -25,6 +25,7 @@ using ICSharpCode.Decompiler.CSharp.Syntax; @@ -25,6 +25,7 @@ using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.CSharp.TypeSystem;
using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation;
namespace ICSharpCode.Decompiler.CSharp.Transforms
{
@ -55,8 +56,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -55,8 +56,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
for (int i = 1; i < parts.Length; i++) {
nsType = new MemberType { Target = nsType, MemberName = parts[i] };
}
var reference = nsType.ToTypeReference(NameLookupMode.TypeInUsingDeclaration) as TypeOrNamespaceReference;
if (reference != null)
if (nsType.ToTypeReference(NameLookupMode.TypeInUsingDeclaration) is TypeOrNamespaceReference reference)
usingScope.Usings.Add(reference);
rootNode.InsertChildAfter(insertionPoint, new UsingDeclaration { Import = nsType }, SyntaxTree.MemberRole);
}
@ -109,28 +109,15 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -109,28 +109,15 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
}
base.VisitNamespaceDeclaration(namespaceDeclaration);
currentNamespace = oldNamespace;
}/*
public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
{
string oldNamespace = currentNamespace;
if (!(typeDeclaration.Parent is NamespaceDeclaration || typeDeclaration.Parent is TypeDeclaration)) {
var symbol = typeDeclaration.GetSymbol() as ITypeDefinition;
if (symbol != null) {
currentNamespace = symbol.Namespace;
DeclaredNamespaces.Add(currentNamespace);
}
}
base.VisitTypeDeclaration(typeDeclaration);
currentNamespace = oldNamespace;
}*/
}
}
sealed class FullyQualifyAmbiguousTypeNamesVisitor : DepthFirstAstVisitor
{
Stack<CSharpTypeResolveContext> context;
readonly Stack<CSharpTypeResolveContext> context;
readonly bool ignoreUsingScope;
TypeSystemAstBuilder astBuilder;
bool ignoreUsingScope;
public FullyQualifyAmbiguousTypeNamesVisitor(TransformContext context, UsingScope usingScope)
{
@ -152,9 +139,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -152,9 +139,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
this.astBuilder = CreateAstBuilder(currentContext);
}
static TypeSystemAstBuilder CreateAstBuilder(CSharpTypeResolveContext context)
static TypeSystemAstBuilder CreateAstBuilder(CSharpTypeResolveContext context, IL.ILFunction function = null)
{
return new TypeSystemAstBuilder(new CSharpResolver(context)) {
CSharpResolver resolver = new CSharpResolver(context);
if (function != null) {
foreach (var v in function.Variables) {
if (v.Kind != IL.VariableKind.Parameter)
resolver = resolver.AddVariable(new DefaultVariable(v.Type, v.Name));
}
}
return new TypeSystemAstBuilder(resolver) {
AddResolveResultAnnotations = true,
UseAliases = true
};
@ -201,24 +196,55 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -201,24 +196,55 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
}
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
{
Visit(methodDeclaration, base.VisitMethodDeclaration);
}
public override void VisitAccessor(Accessor accessor)
{
Visit(accessor, base.VisitAccessor);
}
public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
{
Visit(constructorDeclaration, base.VisitConstructorDeclaration);
}
public override void VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration)
{
Visit(destructorDeclaration, base.VisitDestructorDeclaration);
}
public override void VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration)
{
Visit(operatorDeclaration, base.VisitOperatorDeclaration);
}
void Visit<T>(T entityDeclaration, Action<T> baseCall) where T : EntityDeclaration
{
if (ignoreUsingScope) {
base.VisitMethodDeclaration(methodDeclaration);
baseCall(entityDeclaration);
return;
}
if (methodDeclaration.GetSymbol() is IMethod method && CSharpDecompiler.IsWindowsFormsInitializeComponentMethod(method)) {
if (entityDeclaration.GetSymbol() is IMethod method) {
var previousContext = context.Peek();
var currentContext = new CSharpTypeResolveContext(previousContext.CurrentModule);
CSharpTypeResolveContext currentContext;
if (CSharpDecompiler.IsWindowsFormsInitializeComponentMethod(method)) {
currentContext = new CSharpTypeResolveContext(previousContext.CurrentModule);
} else {
currentContext = previousContext.WithCurrentMember(method);
}
context.Push(currentContext);
try {
astBuilder = CreateAstBuilder(currentContext);
base.VisitMethodDeclaration(methodDeclaration);
var function = entityDeclaration.Annotation<IL.ILFunction>();
astBuilder = CreateAstBuilder(currentContext, function);
baseCall(entityDeclaration);
} finally {
astBuilder = CreateAstBuilder(previousContext);
context.Pop();
}
} else {
base.VisitMethodDeclaration(methodDeclaration);
baseCall(entityDeclaration);
}
}

8
ILSpy/Languages/CSharpHighlightingTokenWriter.cs

@ -318,8 +318,14 @@ namespace ICSharpCode.ILSpy @@ -318,8 +318,14 @@ namespace ICSharpCode.ILSpy
public override void WriteIdentifier(Identifier identifier)
{
HighlightingColor color = null;
if (identifier.Name == "value" && identifier.Ancestors.OfType<Accessor>().FirstOrDefault() is Accessor accessor && accessor.Role != PropertyDeclaration.GetterRole)
if (identifier.Name == "value"
&& identifier.Parent?.GetResolveResult() is ILVariableResolveResult rr
&& rr.Variable.Kind == Decompiler.IL.VariableKind.Parameter
&& identifier.Ancestors.OfType<Accessor>().FirstOrDefault() is Accessor accessor
&& accessor.Role != PropertyDeclaration.GetterRole)
{
color = valueKeywordColor;
}
if ((identifier.Name == "dynamic" || identifier.Name == "var") && identifier.Parent is AstType)
color = queryKeywordsColor;
switch (GetCurrentDefinition()) {

Loading…
Cancel
Save