/// <summary> /// Actions the specified t. /// </summary> /// <param name="element"> /// The element. /// </param> private void ReplaceReturnValue(IElement element) { var returnStatement = element as IReturnStatement; if (returnStatement == null) { return; } var value = returnStatement.Value; if (value == null) { return; } var factory = CSharpElementFactory.GetInstance(element.GetPsiModule()); if (factory == null) { return; } ICSharpExpression expression; var text = value.GetText(); if (text == "true") { expression = factory.CreateExpression("false"); } else if (text == "false") { expression = factory.CreateExpression("true"); } else if (IsNotExpression(value)) { var unaryOperatorExpression = (IUnaryOperatorExpression)value; text = unaryOperatorExpression.Operand.GetText(); if (text.StartsWith("(") && text.EndsWith(")")) { text = text.Substring(1, text.Length - 2); } expression = factory.CreateExpression(text); } else { if (text.StartsWith("(") && text.EndsWith(")")) { text = text.Substring(1, text.Length - 2); } expression = factory.CreateExpression("!(" + text + ")"); } if (expression == null) { return; } value.ReplaceBy(expression); }
/// <summary> /// Inserts the assert. /// </summary> /// <param name="element"> /// The element. /// </param> /// <param name="anchor"> /// The anchor. /// </param> /// <param name="name"> /// The name. /// </param> private void CheckAssignment(IElement element, IStatement anchor, string name) { var functionDeclaration = anchor.GetContainingTypeMemberDeclaration() as IMethodDeclaration; if (functionDeclaration == null) { return; } var body = functionDeclaration.Body; if (body == null) { return; } var factory = CSharpElementFactory.GetInstance(element.GetPsiModule()); var csharpElement = element as ICSharpElement; if (csharpElement == null) { return; } var code = string.Format("if({0} == null) {{ }}", name); var statement = factory.CreateStatement(code); var result = body.AddStatementAfter(statement, anchor); var range = result.GetDocumentRange(); var codeFormatter = new CodeFormatter(); codeFormatter.Format(this.Solution, range); }
/// <summary> /// Executes this instance. /// </summary> /// <param name="element"> /// The element. /// </param> protected override void Execute(IElement element) { var factory = CSharpElementFactory.GetInstance(element.GetPsiModule()); if (factory == null) { return; } var parent = element.ToTreeNode().Parent; if (parent == null) { return; } var classDeclaration = parent.Parent as IClassDeclaration; if (classDeclaration == null) { return; } classDeclaration.SetAbstract(false); var methodDeclarations = classDeclaration.MethodDeclarations; foreach (var methodDeclaration in methodDeclarations) { if (!methodDeclaration.IsAbstract) { continue; } methodDeclaration.SetAbstract(false); methodDeclaration.SetVirtual(true); if (methodDeclaration.Body != null) { continue; } var block = factory.CreateEmptyBlock(); methodDeclaration.SetBody(block); CSharpMemberBodyUtil.Instance.SetBodyToDefault(methodDeclaration); } var propertyDeclarations = classDeclaration.PropertyDeclarations; foreach (var propertyDeclaration in propertyDeclarations) { if (!propertyDeclaration.IsAbstract) { continue; } propertyDeclaration.SetAbstract(false); propertyDeclaration.SetVirtual(true); foreach (var accessorDeclaration in propertyDeclaration.AccessorDeclarations) { if (accessorDeclaration.Body != null) { continue; } var block = factory.CreateEmptyBlock(); accessorDeclaration.SetBody(block); CSharpPropertyBodyUtil.SetDefaultBody(accessorDeclaration); } } }
/// <summary>Inserts the assert.</summary> /// <param name="assertion">The assertion.</param> /// <param name="element">The element.</param> private void InsertAssertionCode(string assertion, IElement element) { IStatement anchor = null; string name; var assignmentExpression = element as IAssignmentExpression; if (assignmentExpression != null) { anchor = assignmentExpression.GetContainingStatement(); var referenceExpression = assignmentExpression.Dest as IReferenceExpression; if (referenceExpression == null) { return; } name = referenceExpression.Reference.GetName(); } else { var treeNode = element.ToTreeNode(); while (treeNode != null) { anchor = treeNode as IStatement; if (anchor != null) { break; } treeNode = treeNode.Parent; } var localVariable = element as ILocalVariable; if (localVariable == null) { return; } name = localVariable.ShortName; } if (anchor == null) { return; } var functionDeclaration = anchor.GetContainingTypeMemberDeclaration() as IMethodDeclaration; if (functionDeclaration == null) { return; } var body = functionDeclaration.Body; if (body == null) { return; } var factory = CSharpElementFactory.GetInstance(element.GetPsiModule()); var csharpElement = element as ICSharpElement; if (csharpElement == null) { return; } var code = string.Format(assertion, name); var statement = factory.CreateStatement(code); if (statement == null) { return; } var result = body.AddStatementAfter(statement, anchor); var range = result.GetDocumentRange(); var codeFormatter = new CodeFormatter(); codeFormatter.Format(this.Solution, range); }
/// <summary> /// Gets the state. /// </summary> /// <param name="element"> /// The element. /// </param> /// <param name="name"> /// The variable name. /// </param> /// <returns> /// The null reference state. /// </returns> private static CSharpControlFlowNullReferenceState GetExpressionNullReferenceState(IElement element, string name) { const CSharpControlFlowNullReferenceState UNKNOWN = CSharpControlFlowNullReferenceState.UNKNOWN; var factory = CSharpElementFactory.GetInstance(element.GetPsiModule()); var statement = factory.CreateStatement("if(" + name + " == null) { }"); var anchorSatement = StatementUtil.GetPreviousStatement(element); var block = element.GetContainingElement(typeof(IBlock), true) as IBlock; if (block == null) { return UNKNOWN; } var ifStatement = block.AddStatementAfter(statement, anchorSatement) as IIfStatement; if (ifStatement == null) { return UNKNOWN; } var equalityExpression = ifStatement.Condition as IEqualityExpression; if (equalityExpression == null) { return UNKNOWN; } var referenceExpression = equalityExpression.LeftOperand as IReferenceExpression; if (referenceExpression == null) { return UNKNOWN; } var functionDeclaration = ifStatement.GetContainingElement<ICSharpFunctionDeclaration>(true); if (functionDeclaration == null) { return UNKNOWN; } var graf = CSharpControlFlowBuilder.Build(functionDeclaration); var inspect = graf.Inspect(ValueAnalysisMode.OPTIMISTIC); return inspect.GetExpressionNullReferenceState(referenceExpression); }
/// <summary> /// Reverses the specified element. /// </summary> /// <param name="element"> /// The element. /// </param> private static void Negate(IElement element) { var ifStatement = element.GetContainingElement<IIfStatement>(true); if (ifStatement == null) { return; } IExpression condition = ifStatement.Condition; if (condition == null) { return; } var factory = CSharpElementFactory.GetInstance(element.GetPsiModule()); if (factory == null) { return; } var equalityExpression = ifStatement.Condition as IEqualityExpression; if (equalityExpression != null) { NegateEqualityExpression(factory, equalityExpression); return; } var relationalExpression = ifStatement.Condition as IRelationalExpression; if (relationalExpression != null) { NegateRelationalExpression(factory, relationalExpression); return; } var unaryOperatorExpression = ifStatement.Condition as IUnaryOperatorExpression; if (unaryOperatorExpression != null) { NegateUnaryExpression(factory, unaryOperatorExpression); return; } var invocationExpression = ifStatement.Condition as IInvocationExpression; if (invocationExpression != null) { NegateInvocationExpression(factory, invocationExpression); return; } var literalExpression = ifStatement.Condition as ILiteralExpression; if (literalExpression != null) { NegateLiteralExpression(factory, literalExpression); return; } NegateExpression(factory, ifStatement.Condition); }
/// <summary> /// Executes the specified element. /// </summary> /// <param name="element"> /// The element. /// </param> /// <returns> /// Returns the text range. /// </returns> private static global::JetBrains.Util.TextRange Execute(IElement element) { var typeMemberDeclaration = element.ToTreeNode().Parent as ICSharpTypeMemberDeclaration; if (typeMemberDeclaration == null) { return global::JetBrains.Util.TextRange.InvalidRange; } var classDeclaration = typeMemberDeclaration.GetContainingTypeDeclaration() as IClassDeclaration; if (classDeclaration == null) { return global::JetBrains.Util.TextRange.InvalidRange; } var text = typeMemberDeclaration.GetText(); var factory = CSharpElementFactory.GetInstance(element.GetPsiModule()); if (factory == null) { return global::JetBrains.Util.TextRange.InvalidRange; } var declaration = factory.CreateTypeMemberDeclaration(text) as IClassMemberDeclaration; if (declaration == null) { return global::JetBrains.Util.TextRange.InvalidRange; } var anchor = typeMemberDeclaration as IClassMemberDeclaration; if (anchor == null) { return global::JetBrains.Util.TextRange.InvalidRange; } var after = classDeclaration.AddClassMemberDeclarationAfter(declaration, anchor); if (after != null) { var treeTextRange = after.GetNameRange(); return new global::JetBrains.Util.TextRange(treeTextRange.StartOffset.Offset, treeTextRange.EndOffset.Offset); } return global::JetBrains.Util.TextRange.InvalidRange; }
/// <summary> /// Executes this instance. /// </summary> /// <param name="element"> /// The element. /// </param> protected override void Execute(IElement element) { var factory = CSharpElementFactory.GetInstance(element.GetPsiModule()); if (factory == null) { return; } var statement = element.ToTreeNode().Parent as IForStatement; if (statement == null) { return; } var localVariableDeclarations = statement.InitializerDeclarations; if (localVariableDeclarations.Count != 1) { return; } var localVariableDeclaration = localVariableDeclarations[0]; if (localVariableDeclaration == null) { return; } var expressionInitializer = localVariableDeclaration.Initial as IExpressionInitializer; if (expressionInitializer == null) { return; } var from = expressionInitializer.Value; var relationalExpression = statement.Condition as IRelationalExpression; if (relationalExpression == null) { return; } var to = relationalExpression.RightOperand; var iterators = statement.IteratorExpressions; if (iterators == null) { return; } if (iterators.Count != 1) { return; } var postfixOperatorExpression = iterators[0] as IPostfixOperatorExpression; if (postfixOperatorExpression == null) { return; } if (postfixOperatorExpression.PostfixOperatorType == PostfixOperatorType.INVALID) { return; } if (postfixOperatorExpression.PostfixOperatorType == PostfixOperatorType.PLUSPLUS) { var expression = AddToExpression(factory, to, '-'); expressionInitializer.SetValue(expression); var condition = GetCondition(factory, relationalExpression.LeftOperand, relationalExpression.OperatorSign.GetText(), from); relationalExpression.ReplaceBy(condition); var dec = factory.CreateExpression(string.Format("{0}--", postfixOperatorExpression.Operand.GetText())); postfixOperatorExpression.ReplaceBy(dec); } else { var expression = AddToExpression(factory, from, '+'); expressionInitializer.SetValue(to); var condition = GetCondition(factory, relationalExpression.LeftOperand, relationalExpression.OperatorSign.GetText(), expression); relationalExpression.ReplaceBy(condition); var inc = factory.CreateExpression(string.Format("{0}++", postfixOperatorExpression.Operand.GetText())); postfixOperatorExpression.ReplaceBy(inc); } }
/// <summary> /// Creates a DocCommentBlockNode with the text provided. /// </summary> /// <param name="element"> /// The element to create for. /// </param> /// <param name="text"> /// The text to use to create the doc. /// </param> /// <returns> /// An IDocCommentBlockNode that has been created. /// </returns> public static IDocCommentBlockNode CreateDocCommentBlockNode(IElement element, string text) { // Fix up the xml terminators to remove the extra space. text = text.Replace(" />", "/>"); StringBuilder builder = new StringBuilder(); foreach (string line in text.Split('\n')) { string outputLine = line; if (line.StartsWith(" ")) { outputLine = line.Substring(1); } builder.Append("/// " + outputLine + Environment.NewLine); } builder.Append("void fec();"); IDocCommentBlockOwnerNode declaration = (IDocCommentBlockOwnerNode)CSharpElementFactory.GetInstance(element.GetPsiModule()).CreateTypeMemberDeclaration(builder.ToString()); return declaration.GetDocCommentBlockNode(); }