Exemple #1
0
        private static void AnalyzeInvocationOp(OperationAnalysisContext context)
        {
            var invocation = (IInvocationOperation)context.Operation;

            if (invocation.TargetMethod.Kind != SymbolKind.Method || !invocation.TargetMethod.IsStatic)
            {
                return;
            }
            if (!IsCodeContractToReplace(context.Compilation, invocation))
            {
                return;
            }

            DiagnosticDescriptor rule = Rule;

            if (ContractStatementAnalyzer.IsContractClass(context.ContainingSymbol))
            {
                rule = RuleWithinCC;
            }


            var invocationSyntax = (InvocationExpressionSyntax)invocation.Syntax;

            if (invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccess)
            {
                context.ReportDiagnostic(Diagnostic.Create(rule, memberAccess.Expression.GetLocation()));
            }
        }
Exemple #2
0
        private static void AnalyzeInvocationOp(OperationAnalysisContext context)
        {
            var statement  = (IExpressionStatementOperation)context.Operation;
            var invocation = statement.Operation as IInvocationOperation;

            if (invocation == null)
            {
                return;
            }

            if (!IsCodeContractToRemove(context.Compilation, invocation))
            {
                return;
            }
            if (ContractStatementAnalyzer.IsInvariantMethod(context.ContainingSymbol) || ContractStatementAnalyzer.IsContractClass(context.ContainingSymbol))
            {
                return;
            }


            var invocationSyntax = (InvocationExpressionSyntax)invocation.Syntax;

            if (invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccess)
            {
                context.ReportDiagnostic(Diagnostic.Create(Rule, statement.Syntax.GetLocation()));
            }
        }
Exemple #3
0
        private static void AnalyzeMethodDeclaration(CodeBlockAnalysisContext context)
        {
            MethodDeclarationSyntax methodSyntax = context.CodeBlock as MethodDeclarationSyntax;
            IMethodSymbol           method       = context.OwningSymbol as IMethodSymbol;

            if (method == null || methodSyntax == null)
            {
                return;
            }
            if (!ContractStatementAnalyzer.IsInvariantMethod(method))
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Rule, methodSyntax.Identifier.GetLocation()));
        }
Exemple #4
0
        private static bool IsEquivalentRequireStatements(StatementSyntax a, StatementSyntax b, bool smart)
        {
            if (!smart)
            {
                return(a.IsEquivalentTo(b));
            }

            var aCond = ContractStatementAnalyzer.ParseInvocation(a, out var aInv) ? aInv.Condition : null;
            var bCond = ContractStatementAnalyzer.ParseInvocation(b, out var bInv) ? bInv.Condition : null;

            if (aCond == null || bCond == null)
            {
                return(false);
            }

            return(aCond.IsEquivalentTo(bCond));
        }
Exemple #5
0
        internal static void ReplaceWithEnumerable(DocumentEditor editor, SyntaxNode nodeToReplace)
        {
            ContractInvocationInfo contractCallInfo = null;

            if (!ContractStatementAnalyzer.ParseInvocation(nodeToReplace.Parent as ExpressionStatementSyntax, out contractCallInfo))
            {
                if (!ContractStatementAnalyzer.ParseInvocation(nodeToReplace as InvocationExpressionSyntax, out contractCallInfo))
                {
                    return;
                }
            }

            if (!contractCallInfo.IsContractType || !ContractForAllToEnumerableAnalyzer.MethodNamesToFix.Contains(contractCallInfo.MethodNameAsString))
            {
                return;
            }

            var generator = editor.Generator;

            var trailingTrivia = nodeToReplace.GetTrailingTrivia();
            var leadingTrivia  = nodeToReplace.GetLeadingTrivia();

            SyntaxNode newCallNode = null;

            if (contractCallInfo.MethodNameAsString == nameof(System.Diagnostics.Contracts.Contract.ForAll) && contractCallInfo.AllArguments.Arguments.Count == 2)
            {
                newCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(contractCallInfo.AllArguments.Arguments[0].Expression, nameof(Enumerable.All)),
                    contractCallInfo.AllArguments.Arguments[1]);
            }
            else if (contractCallInfo.MethodNameAsString == nameof(System.Diagnostics.Contracts.Contract.Exists) && contractCallInfo.AllArguments.Arguments.Count == 2)
            {
                newCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(contractCallInfo.AllArguments.Arguments[0].Expression, nameof(Enumerable.Any)),
                    contractCallInfo.AllArguments.Arguments[1]);
            }


            if (newCallNode != null)
            {
                newCallNode = newCallNode.WithTrailingTrivia(trailingTrivia).WithLeadingTrivia(leadingTrivia);
                editor.ReplaceNode(nodeToReplace, newCallNode);
            }
        }
Exemple #6
0
        private static async Task <Document> ReplaceWithTurboContract(Document document, SyntaxNode nodeToReplace, string stringText, CancellationToken cancellationToken)
        {
            var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);

            var generator = editor.Generator;

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

            ContractInvocationInfo contractCallInfo = null;

            if (!ContractStatementAnalyzer.ParseInvocation(nodeToReplace.Parent as ExpressionStatementSyntax, out contractCallInfo) ||
                !contractCallInfo.IsSpecialContractType || !TurboContractConditionStringNotInSyncAnalyzer.MethodNamesToFix.Contains(contractCallInfo.MethodNameAsString))
            {
                return(document);
            }

            var trailingTrivia = nodeToReplace.GetTrailingTrivia();
            var leadingTrivia  = nodeToReplace.GetLeadingTrivia();

            SyntaxNode debugAssertCallNode = null;


            if (contractCallInfo.Message == null)
            {
                debugAssertCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(generator.IdentifierName(ContractStatementAnalyzer.SpecialContractClass), contractCallInfo.MethodName),
                    contractCallInfo.Condition,
                    generator.Argument("conditionString", RefKind.None, generator.LiteralExpression(contractCallInfo.Condition.ToString())));
            }
            else
            {
                debugAssertCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(generator.IdentifierName(ContractStatementAnalyzer.SpecialContractClass), contractCallInfo.MethodName),
                    contractCallInfo.Condition,
                    contractCallInfo.Message,
                    generator.Argument("conditionString", RefKind.None, generator.LiteralExpression(contractCallInfo.Condition.ToString())));
            }

            debugAssertCallNode = debugAssertCallNode.WithTrailingTrivia(trailingTrivia).WithLeadingTrivia(leadingTrivia);

            editor.ReplaceNode(nodeToReplace, debugAssertCallNode);
            return(editor.GetChangedDocument());
        }
Exemple #7
0
        private static void AnalyzeClassDeclaration(SymbolAnalysisContext context)
        {
            var namedType = context.Symbol as INamedTypeSymbol;

            if (namedType == null || namedType.TypeKind != TypeKind.Class)
            {
                return;
            }
            if (!ContractStatementAnalyzer.IsContractClass(namedType))
            {
                return;
            }

            foreach (var syntax in namedType.DeclaringSyntaxReferences)
            {
                var syntaxNode = syntax.GetSyntax(context.CancellationToken);
                if (syntaxNode is ClassDeclarationSyntax classDecl)
                {
                    context.ReportDiagnostic(Diagnostic.Create(Rule, classDecl.Identifier.GetLocation()));
                }
            }
        }
Exemple #8
0
        private static void AnalyzeInvocationOp(OperationAnalysisContext context)
        {
            var invocation = (IInvocationOperation)context.Operation;

            if (invocation.TargetMethod.Kind != SymbolKind.Method || !invocation.TargetMethod.IsStatic)
            {
                return;
            }
            if (invocation.Parent.Kind != OperationKind.ExpressionStatement)
            {
                return;
            }
            if (!IsCodeContractToReplace(invocation))
            {
                return;
            }
            if (ContractStatementAnalyzer.IsContractClass(context.ContainingSymbol))
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Rule, invocation.Parent.Syntax.GetLocation()));
        }
Exemple #9
0
        internal static void ReplaceWithDebugAssert(DocumentEditor editor, SyntaxNode nodeToReplace)
        {
            ContractInvocationInfo contractCallInfo = null;

            if (!ContractStatementAnalyzer.ParseInvocation(nodeToReplace.Parent as ExpressionStatementSyntax, out contractCallInfo) ||
                !contractCallInfo.IsContractType || !ContractToDebugAssertAnalyzer.MethodNamesToFix.Contains(contractCallInfo.MethodNameAsString))
            {
                return;
            }

            var generator = editor.Generator;

            var trailingTrivia = nodeToReplace.GetTrailingTrivia();
            var leadingTrivia  = nodeToReplace.GetLeadingTrivia();

            SyntaxNode debugAssertCallNode = null;

            if (contractCallInfo.Message == null)
            {
                debugAssertCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(generator.IdentifierName(nameof(Debug)), nameof(Debug.Assert)),
                    contractCallInfo.Condition,
                    generator.LiteralExpression(contractCallInfo.Condition.ToString()));
            }
            else
            {
                debugAssertCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(generator.IdentifierName(nameof(Debug)), nameof(Debug.Assert)),
                    contractCallInfo.Condition,
                    generator.LiteralExpression(contractCallInfo.Condition.ToString()),
                    contractCallInfo.Message);
            }

            debugAssertCallNode = debugAssertCallNode.WithTrailingTrivia(trailingTrivia).WithLeadingTrivia(leadingTrivia);

            editor.ReplaceNode(nodeToReplace, debugAssertCallNode);
        }
Exemple #10
0
 private static bool IsDebugAssertStatement(StatementSyntax statement)
 {
     return(ContractStatementAnalyzer.ParseInvocation(statement, out var invocationInfo) &&
            invocationInfo.MethodNameAsString == "Assert" &&
            invocationInfo.IsDebugType);
 }
Exemple #11
0
 private static bool IsTurboRequiresStatement(StatementSyntax statement)
 {
     return(ContractStatementAnalyzer.ParseInvocation(statement, out var invocationInfo) &&
            invocationInfo.MethodNameAsString == "Requires" &&
            invocationInfo.IsSpecialContractType);
 }