Skip to content
Snippets Groups Projects
Commit cf975fc3 authored by Christian Kamm's avatar Christian Kamm
Browse files

Quickfix: Use existing parentheses and negate by removing ! if possible.

parent 583531c1
No related branches found
No related tags found
No related merge requests found
......@@ -97,19 +97,17 @@ protected:
/*
Rewrite
a op b
As
!(a invop b)
a op b -> !(a invop b)
(a op b) -> !(a invop b)
!(a op b) -> (a invob b)
*/
class UseInverseOp: public QuickFixOperation
{
public:
UseInverseOp()
: binary(0)
: binary(0), nested(0), negation(0)
{}
virtual QString description() const
{
return QLatin1String("Rewrite using ") + replacement; // ### tr?
......@@ -151,18 +149,40 @@ public:
CPlusPlus::Token tok;
tok.f.kind = invertToken;
replacement = QLatin1String(tok.spell());
// check for enclosing nested expression
if (index - 1 >= 0)
nested = path[index - 1]->asNestedExpression();
// check for ! before parentheses
if (nested && index - 2 >= 0) {
negation = path[index - 2]->asUnaryExpression();
if (negation && ! tokenAt(negation->unary_op_token).is(T_EXCLAIM))
negation = 0;
}
return index;
}
virtual void createChangeSet()
{
insert(startOf(binary), "!(");
insert(endOf(binary), ")");
if (negation) {
// can't remove parentheses since that might break precedence
remove(negation->unary_op_token);
} else if (nested) {
insert(startOf(nested), "!");
} else {
insert(startOf(binary), "!(");
insert(endOf(binary), ")");
}
replace(binary->binary_op_token, replacement);
}
private:
BinaryExpressionAST *binary;
NestedExpressionAST *nested;
UnaryExpressionAST *negation;
QString replacement;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment