static bool IsGetterSetterPair(object getterOperand, object setterOperand) { MethodReference getter = getterOperand as MethodReference; MethodReference setter = setterOperand as MethodReference; if (getter == null || setter == null) { return(false); } if (!TypeAnalysis.IsSameType(getter.DeclaringType, setter.DeclaringType)) { return(false); } MethodDefinition getterDef = getter.Resolve(); MethodDefinition setterDef = setter.Resolve(); if (getterDef == null || setterDef == null) { return(false); } foreach (PropertyDefinition prop in getterDef.DeclaringType.Properties) { if (prop.GetMethod == getterDef) { return(prop.SetMethod == setterDef); } } return(false); }
public static async Task ComputeRefactoringsAsync( RefactoringContext context, DeclarationExpressionSyntax declarationExpression) { if (declarationExpression.Type?.Span.Contains(context.Span) == true && context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeExplicitTypeToVar, RefactoringIdentifiers.ChangeVarToExplicitType)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); TypeAnalysis analysis = CSharpTypeAnalysis.AnalyzeType(declarationExpression, semanticModel, context.CancellationToken); if (analysis.IsExplicit) { if (analysis.SupportsImplicit && context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeExplicitTypeToVar)) { context.RegisterRefactoring(CodeActionFactory.ChangeTypeToVar(context.Document, declarationExpression.Type, equivalenceKey: RefactoringIdentifiers.ChangeExplicitTypeToVar)); } } else if (analysis.SupportsExplicit && context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeVarToExplicitType)) { TypeSyntax type = declarationExpression.Type; var localSymbol = (ILocalSymbol)semanticModel.GetDeclaredSymbol(declarationExpression.Designation, context.CancellationToken); ITypeSymbol typeSymbol = localSymbol.Type; context.RegisterRefactoring(CodeActionFactory.ChangeType(context.Document, type, typeSymbol, semanticModel, equivalenceKey: RefactoringIdentifiers.ChangeVarToExplicitType)); } } }
internal static async Task ChangeTypeAsync( RefactoringContext context, ForEachStatementSyntax forEachStatement) { TypeSyntax type = forEachStatement.Type; if (type?.Span.Contains(context.Span) != true) { return; } SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); TypeAnalysis analysis = CSharpTypeAnalysis.AnalyzeType(forEachStatement, semanticModel); if (analysis.IsExplicit) { if (analysis.SupportsImplicit && context.IsRefactoringEnabled(RefactoringDescriptors.UseImplicitType)) { context.RegisterRefactoring(CodeActionFactory.ChangeTypeToVar(context.Document, type, equivalenceKey: EquivalenceKey.Create(RefactoringDescriptors.UseImplicitType))); } if (!forEachStatement.ContainsDiagnostics && context.IsRefactoringEnabled(RefactoringDescriptors.ChangeTypeAccordingToExpression)) { ChangeTypeAccordingToExpression(context, forEachStatement, semanticModel); } } else if (analysis.SupportsExplicit && context.IsRefactoringEnabled(RefactoringDescriptors.UseExplicitType)) { context.RegisterRefactoring(CodeActionFactory.UseExplicitType(context.Document, type, analysis.Symbol, semanticModel, equivalenceKey: EquivalenceKey.Create(RefactoringDescriptors.UseExplicitType))); } }
private static async Task ChangeTypeAsync( RefactoringContext context, VariableDeclarationSyntax variableDeclaration) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); TypeAnalysis analysis = TypeAnalysis.AnalyzeType(variableDeclaration, semanticModel, context.CancellationToken); if (analysis.IsExplicit) { if (analysis.SupportsImplicit && context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeExplicitTypeToVar)) { context.RegisterRefactoring( "Change type to 'var'", cancellationToken => { return(ChangeTypeRefactoring.ChangeTypeToVarAsync( context.Document, variableDeclaration.Type, cancellationToken)); }, RefactoringIdentifiers.ChangeExplicitTypeToVar); } } else if (analysis.SupportsExplicit && context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeVarToExplicitType)) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(variableDeclaration.Type, context.CancellationToken); ChangeType(context, variableDeclaration, typeSymbol, semanticModel, context.CancellationToken); } }
bool ForwardScanInitializeArrayRuntimeHelper(List <ILNode> body, int pos, ILVariable array, TypeReference arrayType, int arrayLength, out ILExpression[] values, out int foundPos) { ILVariable v2; MethodReference methodRef; ILExpression methodArg1; ILExpression methodArg2; FieldReference fieldRef; if (body.ElementAtOrDefault(pos).Match(ILCode.Call, out methodRef, out methodArg1, out methodArg2) && methodRef.DeclaringType.FullName == "System.Runtime.CompilerServices.RuntimeHelpers" && methodRef.Name == "InitializeArray" && methodArg1.Match(ILCode.Ldloc, out v2) && array == v2 && methodArg2.Match(ILCode.Ldtoken, out fieldRef)) { FieldDefinition fieldDef = fieldRef.ResolveWithinSameModule(); if (fieldDef != null && fieldDef.InitialValue != null) { ILExpression[] newArr = new ILExpression[arrayLength]; if (DecodeArrayInitializer(TypeAnalysis.GetTypeCode(arrayType.GetElementType()), fieldDef.InitialValue, newArr)) { values = newArr; foundPos = pos; return(true); } } } values = null; foundPos = -1; return(false); }
internal static async Task ChangeTypeAsync( RefactoringContext context, ForEachStatementSyntax forEachStatement) { TypeSyntax type = forEachStatement.Type; if (type?.Span.Contains(context.Span) != true) { return; } SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); TypeAnalysis analysis = TypeAnalysis.AnalyzeType(forEachStatement, semanticModel); if (analysis.IsExplicit) { if (analysis.SupportsImplicit && context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeExplicitTypeToVar)) { context.RegisterRefactoring( "Change type to 'var'", cancellationToken => ChangeTypeRefactoring.ChangeTypeToVarAsync(context.Document, type, cancellationToken)); } } else if (analysis.SupportsExplicit && context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeVarToExplicitType)) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken); context.RegisterRefactoring( $"Change type to '{SymbolDisplay.ToMinimalDisplayString(typeSymbol, semanticModel, type.SpanStart, SymbolDisplayFormats.Default)}'", cancellationToken => ChangeTypeRefactoring.ChangeTypeAsync(context.Document, type, typeSymbol, cancellationToken)); } }
public EqualityThroughMembers(DataTypeInfo dataTypeInfo) : this( dataTypeInfo, TypeAnalysis .GenuineDataMembers(dataTypeInfo) .Where(m => m.IsStatic == false) .Select(m => m.Name)) { }
public override bool Match(PatternMatcher pm, ILExpression e) { if (e.Code != this.code) { return(false); } var m = (IMethod)e.Operand; return(m.Name == this.method && TypeAnalysis.IsNullableType(m.DeclaringType.ToTypeSig()) && base.Match(pm, e)); }
public override bool Match(PatternMatcher pm, ILExpression e) { if (e.Code != code) { return(false); } var m = (MethodReference)e.Operand; return(m.Name == method && TypeAnalysis.IsNullableType(m.DeclaringType) && base.Match(pm, e)); }
// This will negate a condition and optimize it // Tis will simplify negates that get out of control static ILExpression SimplifyLogicNot(ILExpression expr, ref bool modified) { ILExpression a; #if false // not sure we need this // "ceq(a, ldc.i4.0)" becomes "logicnot(a)" if the inferred type for expression "a" is boolean if (expr.Code == GMCode.Seq && TypeAnalysis.IsBoolean(expr.Arguments[0].InferredType) && (a = expr.Arguments[1]).Code == ILCode.Ldc_I4 && (int)a.Operand == 0) { expr.Code = ILCode.LogicNot; expr.ILRanges.AddRange(a.ILRanges); expr.Arguments.RemoveAt(1); modified = true; } #endif if (expr.Code == GMCode.Push && expr.Arguments.Count > 0) { return(SimplifyLogicNot(expr.Arguments[0], ref modified)); } ILExpression res = null; while (expr.Code == GMCode.Not && expr.Arguments.Count > 0) { Debug.Assert(expr.Arguments.Count == 1); a = expr.Arguments[0]; // remove double negation if (a.Code == GMCode.Not) { res = a.Arguments[0]; res.ILRanges.AddRange(expr.ILRanges); res.ILRanges.AddRange(a.ILRanges); expr = res; } else { if (SimplifyLogicLogicArguments(expr)) { res = expr = a; } break; } } for (int i = 0; i < expr.Arguments.Count; i++) { a = SimplifyLogicNot(expr.Arguments[i], ref modified); if (a != null) { expr.Arguments[i] = a; modified = true; } } // Debug.Assert(res != null); return(res); }
static void Main(string[] args) { List <CsNode <string, string> > nodes; List <List <Elem> > allTables = new List <List <Elem> >(); List <string> files = ProcessCommandline(args); allTables = TypeAnalysis.analyse(files); nodes = DependencyAnalysis.getTables(allTables, files); display(nodes); Console.Read(); }
List <string> typeAnalysis(List <string> files) { List <List <Elem> > allTables = new List <List <Elem> >(); List <string> checkFiles = new List <string>(); List <string> tableType = new List <string>(); checkFiles = filePath(files); allTables = TypeAnalysis.analyse(checkFiles); tableType = displayRequirement(allTables); return(tableType); }
private static void AnalyzeDeclarationExpression(SyntaxNodeAnalysisContext context) { var declarationExpression = (DeclarationExpressionSyntax)context.Node; if (TypeAnalysis.IsExplicitThatCanBeImplicit(declarationExpression, context.SemanticModel, context.CancellationToken)) { context.ReportDiagnostic( DiagnosticDescriptors.UseVarInsteadOfExplicitTypeWhenTypeIsNotObvious, declarationExpression.Type); } }
private static void AnalyzeForEachStatement(SyntaxNodeAnalysisContext context) { var forEachStatement = (ForEachStatementSyntax)context.Node; if (TypeAnalysis.IsImplicitThatCanBeExplicit(forEachStatement, context.SemanticModel)) { context.ReportDiagnostic( DiagnosticDescriptors.UseExplicitTypeInsteadOfVarInForEach, forEachStatement.Type); } }
public override ILExpression BuildNew(PatternMatcher pm) { var v = b ? pm.B : pm.A; var e = new ILExpression(ILCode.Ldloc, v, EmptyArguments); if (TypeAnalysis.IsNullableType(v.Type)) { e = new ILExpression(ILCode.ValueOf, null, e); } return(e); }
private static void AnalyzeVariableDeclaration(SyntaxNodeAnalysisContext context) { var variableDeclaration = (VariableDeclarationSyntax)context.Node; if (TypeAnalysis.IsImplicitThatCanBeExplicit(variableDeclaration, context.SemanticModel, TypeAppearance.NotObvious, context.CancellationToken)) { context.ReportDiagnostic( DiagnosticDescriptors.UseExplicitTypeInsteadOfVarWhenTypeIsNotObvious, variableDeclaration.Type); } }
private static void AnalyzeForEachStatement(SyntaxNodeAnalysisContext context) { var forEachStatement = (ForEachStatementSyntax)context.Node; TypeAnalysis analysis = CSharpTypeAnalysis.AnalyzeType(forEachStatement, context.SemanticModel); if (analysis.IsExplicit && analysis.SupportsImplicit) { DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UseVarInsteadOfExplicitTypeInForEach, forEachStatement.Type); } }
private static void AnalyzeForEachVariableStatement(SyntaxNodeAnalysisContext context) { var forEachStatement = (ForEachVariableStatementSyntax)context.Node; TypeAnalysis analysis = CSharpTypeAnalysis.AnalyzeType(forEachStatement, context.SemanticModel); if (analysis.IsExplicit && analysis.SupportsImplicit) { ReportDiagnostic(context, forEachStatement.Variable); } }
//---------< get the typetable for selected files >------------- List <string> getTypetable(List <string> files) { List <List <Elem> > typetable = new List <List <Elem> >(); List <string> selectedfiles = new List <string>(); List <string> typetableList = new List <string>(); selectedfiles = getfilePath(files); TypeAnalysis typetableobj = new TypeAnalysis(selectedfiles); typetable = typetableobj.generateTypeTable(); typetableList = Display.showTypetable(typetable); return(typetableList); }
List <string> depAnalysis(List <string> files) { List <string> depType = new List <string>(); List <List <Elem> > allTables = new List <List <Elem> >(); List <string> checkFiles = new List <string>(); List <string> tableType = new List <string>(); checkFiles = filePath(files); allTables = TypeAnalysis.analyse(checkFiles); List <CsNode <string, string> > nodes = DependencyAnalysis.getTables(allTables, checkFiles); depType = Display.showDependences(nodes); return(depType); }
public InvokeTypeInfo( Type type, TypeAnalysis typeAnalysis) : base( type, typeAnalysis.name, typeAnalysis.level, typeAnalysis.opcode, typeAnalysis.keywords, typeAnalysis.tags) { if (typeAnalysis.properties.Length != 0) this.properties = typeAnalysis.properties; }
public static TypeReference SubstituteTypeArgs(ITypeInfoSource typeInfo, TypeReference type, MemberReference member) { var gp = (type as GenericParameter); if (gp != null) { if (gp.Owner.GenericParameterType == GenericParameterType.Method) { var ownerIdentifier = new MemberIdentifier(typeInfo, (MethodReference)gp.Owner); var memberIdentifier = new MemberIdentifier(typeInfo, (dynamic)member); if (!ownerIdentifier.Equals(memberIdentifier, typeInfo)) { return(type); } if (!(member is GenericInstanceMethod)) { return(type); } } else { var declaringType = member.DeclaringType.Resolve(); // FIXME: Is this right? if (declaringType == null) { return(type); } var ownerResolved = ((TypeReference)gp.Owner).Resolve(); // FIXME: Is this right? if (ownerResolved == null) { return(type); } var ownerIdentifier = new TypeIdentifier(ownerResolved); var typeIdentifier = new TypeIdentifier(declaringType); if (!ownerIdentifier.Equals(typeIdentifier)) { return(type); } } } return(TypeAnalysis.SubstituteTypeArgs(type, member)); }
//---------< get the depedency table for selected files >------------ List <string> getDependency(List <string> files) { List <CsNode <string, string> > nodes = new List <CsNode <string, string> >(); List <List <Elem> > dependency = new List <List <Elem> >(); List <string> selectedfiles = new List <string>(); List <string> dependencyList = new List <string>(); selectedfiles = getfilePath(files); TypeAnalysis typeAnalysisObj = new TypeAnalysis(selectedfiles); dependency = typeAnalysisObj.generateTypeTable(); nodes = DependencyAnalysis.GetDependency(dependency, selectedfiles); dependencyList = Display.showDependencies(nodes); return(dependencyList); }
public static void demoRequirement4() { Console.WriteLine("Now demostrating requirement 4, evaluate all the dependencies between files in a specified file set\n"); Console.WriteLine("As a demostrating, I will run the analysis on this project dirtory,\n and all the .cs file under this dirtory and subdirtory will be analyzed"); Console.WriteLine("Build a TypeTable and display it"); Console.WriteLine("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"); TypeAnalysis analyzer = new TypeAnalysis(); String[] path_ = { Tester.path }; TypeTable table = TypeAnalysis.buildTypeTable(path_); // calling the wraped function to get the typetable table.display(); Console.WriteLine("finish demostrating requirement4 "); Console.WriteLine("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n\n"); }
static void Main(string[] args) { List <CsNode <string, string> > nodes = new List <CsNode <string, string> >(); List <List <Elem> > listOfTables = new List <List <Elem> >(); Console.WriteLine("<------------------------------ Dependency Analysis Test Stub ----------------------->"); Console.WriteLine(); List <string> files = ProcessCommandline(args); TypeAnalysis typeAnalysisObj = new TypeAnalysis(files); listOfTables = typeAnalysisObj.generateTypeTable(); nodes = DependencyAnalysis.GetDependency(listOfTables, files); displayRequirement2(nodes); Console.ReadLine(); }
static bool DecodeArrayInitializer(TypeSig elementTypeRef, byte[] initialValue, ILExpression[] output) { elementTypeRef = elementTypeRef.RemovePinnedAndModifiers(); TypeCode elementType = TypeAnalysis.GetTypeCode(elementTypeRef); switch (elementType) { case TypeCode.Boolean: case TypeCode.Byte: return(DecodeArrayInitializer(initialValue, output, elementType, (d, i) => (int)d[i])); case TypeCode.SByte: return(DecodeArrayInitializer(initialValue, output, elementType, (d, i) => (int)unchecked ((sbyte)d[i]))); case TypeCode.Int16: return(DecodeArrayInitializer(initialValue, output, elementType, (d, i) => (int)BitConverter.ToInt16(d, i))); case TypeCode.Char: case TypeCode.UInt16: return(DecodeArrayInitializer(initialValue, output, elementType, (d, i) => (int)BitConverter.ToUInt16(d, i))); case TypeCode.Int32: case TypeCode.UInt32: return(DecodeArrayInitializer(initialValue, output, elementType, BitConverter.ToInt32)); case TypeCode.Int64: case TypeCode.UInt64: return(DecodeArrayInitializer(initialValue, output, elementType, BitConverter.ToInt64)); case TypeCode.Single: return(DecodeArrayInitializer(initialValue, output, elementType, BitConverter.ToSingle)); case TypeCode.Double: return(DecodeArrayInitializer(initialValue, output, elementType, BitConverter.ToDouble)); case TypeCode.Object: var typeDef = elementTypeRef.ToTypeDefOrRef().ResolveWithinSameModule(); if (typeDef != null && typeDef.IsEnum) { return(DecodeArrayInitializer(typeDef.GetEnumUnderlyingType(), initialValue, output)); } return(false); default: return(false); } }
public InvokeTypeInfo( Type type, TypeAnalysis typeAnalysis) : base( type, typeAnalysis.name, typeAnalysis.level, typeAnalysis.opcode, typeAnalysis.keywords, typeAnalysis.tags) { if (typeAnalysis.properties.Length != 0) { this.properties = typeAnalysis.properties; } }
public void 判断各种类型1() { Assert.Equal(MemberType.Class, TypeAnalysis.GetMemberType(typeof(T1))); Assert.Equal(MemberType.Delegate, TypeAnalysis.GetMemberType(typeof(T2))); Assert.Equal(MemberType.Enum, TypeAnalysis.GetMemberType(typeof(T3))); Assert.Equal(MemberType.Interface, TypeAnalysis.GetMemberType(typeof(T4))); Assert.Equal(MemberType.Class, TypeAnalysis.GetMemberType(typeof(T5))); Assert.Equal(MemberType.Struct, TypeAnalysis.GetMemberType(typeof(T6))); Assert.Equal(MemberType.BaseValue, TypeAnalysis.GetMemberType(typeof(int))); }
public static async Task ComputeRefactoringsAsync( RefactoringContext context, DeclarationExpressionSyntax declarationExpression) { if (declarationExpression.Type?.Span.Contains(context.Span) == true && context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeExplicitTypeToVar, RefactoringIdentifiers.ChangeVarToExplicitType)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); TypeAnalysis analysis = CSharpTypeAnalysis.AnalyzeType(declarationExpression, semanticModel, context.CancellationToken); if (analysis.IsExplicit) { if (analysis.SupportsImplicit && context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeExplicitTypeToVar)) { context.RegisterRefactoring( "Change type to 'var'", cancellationToken => { return(ChangeTypeRefactoring.ChangeTypeToVarAsync( context.Document, declarationExpression.Type, cancellationToken)); }, RefactoringIdentifiers.ChangeExplicitTypeToVar); } } else if (analysis.SupportsExplicit && context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeVarToExplicitType)) { TypeSyntax type = declarationExpression.Type; var localSymbol = semanticModel.GetDeclaredSymbol(declarationExpression.Designation, context.CancellationToken) as ILocalSymbol; ITypeSymbol typeSymbol = localSymbol.Type; context.RegisterRefactoring( $"Change type to '{SymbolDisplay.ToMinimalDisplayString(typeSymbol, semanticModel, type.SpanStart, SymbolDisplayFormats.Default)}'", cancellationToken => ChangeTypeRefactoring.ChangeTypeAsync(context.Document, type, typeSymbol, cancellationToken), RefactoringIdentifiers.ChangeVarToExplicitType); } } }
//----------------<Analyze dependency>--------------------------------------- public void ConnectNode(TypeAnalysis typea, string fqf) { List <string> namestore = new List <string>(); string filename = fqf.Substring(fqf.LastIndexOf('\\') + 1); CsNode <string, string> node = csgraph.findNode(filename); Toker toker = new Toker(); toker.doReturnComments = false; if (!toker.open(fqf)) { Console.Write("\n can't open {0}\n", fqf); } //else //{ // Console.Write("\n processing file: {0}\n", fqf); //} while (!toker.isDone()) { Token tok = toker.getTok(); if (tok == null) { continue; } if (typea.typetable_.table.ContainsKey(tok))//the key exist in the type table. { if (typea.typetable_.table[tok][0].namesp == "") { namestore.Add(tok); } else { List <TypeItem> list_it = typea.typetable_.table[tok]; foreach (TypeItem it in list_it) { if (namestore.Contains(it.namesp)) { //connect the node node.addChild(csgraph.findNode(it.file), ""); } } } } } toker.close(); }
public InvokeTypeInfo( TypeAnalysis typeAnalysis) : base( typeAnalysis.name, typeAnalysis.level, typeAnalysis.opcode, typeAnalysis.keywords, typeAnalysis.tags) { if (typeAnalysis.properties.Length != 0) { this.properties = typeAnalysis.properties; this.accessors = new PropertyAccessor <ContainerType> [this.properties.Length]; for (int i = 0; i < this.accessors.Length; i++) { this.accessors[i] = PropertyAccessor <ContainerType> .Create(this.properties[i]); } } }