1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "rewritecontrolstatements.h"
#include "../cppcodestylesettings.h"
#include "../cppeditortr.h"
#include "../cppeditorwidget.h"
#include "../cpprefactoringchanges.h"
#include "cppquickfix.h"
#include <cplusplus/Overview.h>
#include <cplusplus/TypeOfExpression.h>
#include <functional>
#ifdef WITH_TESTS
#include "cppquickfix_test.h"
#endif
using namespace CPlusPlus;
using namespace TextEditor;
using namespace Utils;
namespace CppEditor::Internal {
namespace {
template<typename Statement> Statement *asControlStatement(AST *node)
{
if constexpr (std::is_same_v<Statement, IfStatementAST>)
return node->asIfStatement();
if constexpr (std::is_same_v<Statement, WhileStatementAST>)
return node->asWhileStatement();
if constexpr (std::is_same_v<Statement, ForStatementAST>)
return node->asForStatement();
if constexpr (std::is_same_v<Statement, RangeBasedForStatementAST>)
return node->asRangeBasedForStatement();
if constexpr (std::is_same_v<Statement, DoStatementAST>)
return node->asDoStatement();
return nullptr;
}
template<typename Statement>
QList<int> triggerTokens(const Statement *statement)
{
if constexpr (std::is_same_v<Statement, IfStatementAST>)
return {statement->if_token, statement->else_token};
if constexpr (std::is_same_v<Statement, WhileStatementAST>)
return {statement->while_token};
if constexpr (std::is_same_v<Statement, DoStatementAST>)
return {statement->do_token};
if constexpr (std::is_same_v<Statement, ForStatementAST>
|| std::is_same_v<Statement, RangeBasedForStatementAST>) {
return {statement->for_token};
}
}
template<typename Statement>
int tokenToInsertOpeningBraceAfter(const Statement *statement)
{
if constexpr (std::is_same_v<Statement, DoStatementAST>)
return statement->do_token;
return statement->rparen_token;
}
template<typename Statement> class AddBracesToControlStatementOp : public CppQuickFixOperation
{
public:
AddBracesToControlStatementOp(const CppQuickFixInterface &interface,
const QList<Statement *> &statements,
StatementAST *elseStatement,
int elseToken)
: CppQuickFixOperation(interface, 0)
, m_statements(statements), m_elseStatement(elseStatement), m_elseToken(elseToken)
{
setDescription(Tr::tr("Add Curly Braces"));
}
void perform() override
{
ChangeSet changes;
for (Statement * const statement : m_statements) {
const int start = currentFile()->endOf(tokenToInsertOpeningBraceAfter(statement));
changes.insert(start, QLatin1String(" {"));
if constexpr (std::is_same_v<Statement, DoStatementAST>) {
const int end = currentFile()->startOf(statement->while_token);
changes.insert(end, QLatin1String("} "));
} else if constexpr (std::is_same_v<Statement, IfStatementAST>) {
if (statement->else_statement) {
changes.insert(currentFile()->startOf(statement->else_token), "} ");
} else {
changes.insert(currentFile()->endOf(statement->statement->lastToken() - 1),
"\n}");
}
} else {
const int end = currentFile()->endOf(statement->statement->lastToken() - 1);
changes.insert(end, QLatin1String("\n}"));
}
}
if (m_elseStatement) {
changes.insert(currentFile()->endOf(m_elseToken), " {");
changes.insert(currentFile()->endOf(m_elseStatement->lastToken() - 1), "\n}");
}
currentFile()->setChangeSet(changes);
currentFile()->apply();
}
private:
const QList<Statement *> m_statements;
StatementAST * const m_elseStatement;
const int m_elseToken;
};
template<typename Statement> class RemoveBracesFromControlStatementOp : public CppQuickFixOperation
{
public:
RemoveBracesFromControlStatementOp(const CppQuickFixInterface &interface,
const QList<Statement *> &statements,
StatementAST *elseStatement,
int elseToken)
: CppQuickFixOperation(interface, 0)
, m_statements(statements), m_elseStatement(elseStatement)
{
Q_UNUSED(elseToken)
setDescription(Tr::tr("Remove Curly Braces"));
}
void perform() override
{
ChangeSet changes;
const auto findNewline = [&](int bracePos, int diff, int &newlinePos, int *nextNonSpacePos) {
for (int i = bracePos + diff; true; i += diff) {
const QChar &c = currentFile()->charAt(i);
if (c == '\n' || c == QChar::ParagraphSeparator) {
newlinePos = i;
break;
}
if (!c.isSpace())
break;
if (nextNonSpacePos)
++*nextNonSpacePos;
}
};
const auto removeBraceAndPossiblyLine = [&](int braceToken, bool removeTrailingSpace) {
const int bracePos = currentFile()->startOf(braceToken);
int prevNewline = -1;
int nextNewline = -1;
int start = bracePos;
int end = bracePos + 1;
findNewline(bracePos, -1, prevNewline, nullptr);
findNewline(bracePos, 1, nextNewline, removeTrailingSpace ? &end : nullptr);
if (prevNewline != -1 && nextNewline != -1) {
start = prevNewline;
end = nextNewline;
}
changes.remove(start, end);
};
const auto apply = [&](const CompoundStatementAST *stmt) {
QTC_ASSERT(stmt, return);
removeBraceAndPossiblyLine(stmt->lbrace_token, false);
removeBraceAndPossiblyLine(stmt->rbrace_token,
std::is_same_v<Statement, DoStatementAST>
|| std::is_same_v<Statement, IfStatementAST>);
if (!stmt->statement_list)
changes.insert(currentFile()->endOf(stmt), "\n;");
};
for (Statement * const statement : m_statements)
apply(statement->statement->asCompoundStatement());
if (m_elseStatement)
apply(m_elseStatement->asCompoundStatement());
currentFile()->setChangeSet(changes);
currentFile()->apply();
}
private:
const QList<Statement *> m_statements;
StatementAST * const m_elseStatement;
};
using StmtConstraint = std::function<bool(AST *, bool &)>;
template<template<typename> typename Op, typename Statement>
bool checkControlStatementsHelper(
const CppQuickFixInterface &interface,
const StmtConstraint &constraint,
QuickFixOperations &result)
{
Statement * const statement = asControlStatement<Statement>(interface.path().last());
if (!statement)
return false;
QList<Statement *> statements;
if (!Utils::anyOf(triggerTokens(statement), [&](int tok) { return interface.isCursorOn(tok); }))
return false;
bool abort = false;
if (statement->statement && constraint(statement->statement, abort))
statements << statement;
if (abort)
return false;
StatementAST *elseStmt = nullptr;
int elseToken = 0;
if constexpr (std::is_same_v<Statement, IfStatementAST>) {
IfStatementAST *currentIfStmt = statement;
for (elseStmt = currentIfStmt->else_statement, elseToken = currentIfStmt->else_token;
elseStmt && (currentIfStmt = elseStmt->asIfStatement());
elseStmt = currentIfStmt->else_statement, elseToken = currentIfStmt->else_token) {
if (currentIfStmt->statement && constraint(currentIfStmt->statement, abort))
statements << currentIfStmt;
if (abort)
return false;
}
if (elseStmt && (elseStmt->asIfStatement() || !constraint(elseStmt, abort))) {
if (abort)
return false;
elseStmt = nullptr;
elseToken = 0;
}
}
if (!statements.isEmpty() || elseStmt) {
result << new Op<Statement>(interface, statements, elseStmt, elseToken);
return false;
}
return true;
}
template<template<typename> typename Op, typename... Statements>
void checkControlStatements(
const CppQuickFixInterface &interface,
const StmtConstraint &constraint,
QuickFixOperations &result)
{
(... || checkControlStatementsHelper<Op, Statements>(interface, constraint, result));
}
class MoveDeclarationOutOfIfOp: public CppQuickFixOperation
{
public:
MoveDeclarationOutOfIfOp(const CppQuickFixInterface &interface)
: CppQuickFixOperation(interface)
{
setDescription(Tr::tr("Move Declaration out of Condition"));
reset();
}
void reset()
{
condition = mk.Condition();
pattern = mk.IfStatement(condition);
}
void perform() override
{
ChangeSet changes;
changes.copy(currentFile()->range(core), currentFile()->startOf(condition));
int insertPos = currentFile()->startOf(pattern);
changes.move(currentFile()->range(condition), insertPos);
changes.insert(insertPos, QLatin1String(";\n"));
currentFile()->apply(changes);
}
ASTMatcher matcher;
ASTPatternBuilder mk;
ConditionAST *condition = nullptr;
IfStatementAST *pattern = nullptr;
CoreDeclaratorAST *core = nullptr;
};
class MoveDeclarationOutOfWhileOp: public CppQuickFixOperation
{
public:
MoveDeclarationOutOfWhileOp(const CppQuickFixInterface &interface)
: CppQuickFixOperation(interface)
{
setDescription(Tr::tr("Move Declaration out of Condition"));
reset();
}
void reset()
{
condition = mk.Condition();
pattern = mk.WhileStatement(condition);
}
void perform() override
{
ChangeSet changes;
changes.insert(currentFile()->startOf(condition), QLatin1String("("));
changes.insert(currentFile()->endOf(condition), QLatin1String(") != 0"));
int insertPos = currentFile()->startOf(pattern);
const int conditionStart = currentFile()->startOf(condition);
changes.move(conditionStart, currentFile()->startOf(core), insertPos);
changes.copy(currentFile()->range(core), insertPos);
changes.insert(insertPos, QLatin1String(";\n"));
currentFile()->apply(changes);
}
ASTMatcher matcher;
ASTPatternBuilder mk;
ConditionAST *condition = nullptr;
WhileStatementAST *pattern = nullptr;
CoreDeclaratorAST *core = nullptr;
};
class SplitIfStatementOp: public CppQuickFixOperation
{
public:
SplitIfStatementOp(const CppQuickFixInterface &interface, int priority,
IfStatementAST *pattern, BinaryExpressionAST *condition)
: CppQuickFixOperation(interface, priority)
, pattern(pattern)
, condition(condition)
{
setDescription(Tr::tr("Split if Statement"));
}
void perform() override
{
const Token binaryToken = currentFile()->tokenAt(condition->binary_op_token);
if (binaryToken.is(T_AMPER_AMPER))
splitAndCondition();
else
splitOrCondition();
}
void splitAndCondition() const
{
ChangeSet changes;
int startPos = currentFile()->startOf(pattern);
changes.insert(startPos, QLatin1String("if ("));
changes.move(currentFile()->range(condition->left_expression), startPos);
changes.insert(startPos, QLatin1String(") {\n"));
const int lExprEnd = currentFile()->endOf(condition->left_expression);
changes.remove(lExprEnd, currentFile()->startOf(condition->right_expression));
changes.insert(currentFile()->endOf(pattern), QLatin1String("\n}"));
currentFile()->apply(changes);
}
void splitOrCondition() const
{
ChangeSet changes;
StatementAST *ifTrueStatement = pattern->statement;
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
int insertPos = currentFile()->endOf(ifTrueStatement);
if (compoundStatement)
changes.insert(insertPos, QLatin1String(" "));
else
changes.insert(insertPos, QLatin1String("\n"));
changes.insert(insertPos, QLatin1String("else if ("));
const int rExprStart = currentFile()->startOf(condition->right_expression);
changes.move(rExprStart, currentFile()->startOf(pattern->rparen_token), insertPos);
changes.insert(insertPos, QLatin1String(")"));
const int rParenEnd = currentFile()->endOf(pattern->rparen_token);
changes.copy(rParenEnd, currentFile()->endOf(pattern->statement), insertPos);
const int lExprEnd = currentFile()->endOf(condition->left_expression);
changes.remove(lExprEnd, currentFile()->startOf(condition->right_expression));
currentFile()->apply(changes);
}
private:
IfStatementAST *pattern;
BinaryExpressionAST *condition;
};
class OptimizeForLoopOperation: public CppQuickFixOperation
{
public:
OptimizeForLoopOperation(const CppQuickFixInterface &interface, const ForStatementAST *forAst,
const bool optimizePostcrement, const ExpressionAST *expression,
const FullySpecifiedType &type)
: CppQuickFixOperation(interface)
, m_forAst(forAst)
, m_optimizePostcrement(optimizePostcrement)
, m_expression(expression)
, m_type(type)
{
setDescription(Tr::tr("Optimize for-Loop"));
}
void perform() override
{
QTC_ASSERT(m_forAst, return);
const CppRefactoringFilePtr file = currentFile();
ChangeSet change;
// Optimize post (in|de)crement operator to pre (in|de)crement operator
if (m_optimizePostcrement && m_forAst->expression) {
PostIncrDecrAST *incrdecr = m_forAst->expression->asPostIncrDecr();
if (incrdecr && incrdecr->base_expression && incrdecr->incr_decr_token) {
change.flip(file->range(incrdecr->base_expression),
file->range(incrdecr->incr_decr_token));
}
}
// Optimize Condition
int renamePos = -1;
if (m_expression) {
QString varName = QLatin1String("total");
if (file->textOf(m_forAst->initializer).size() == 1) {
Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview();
const QString typeAndName = oo.prettyType(m_type, varName);
renamePos = file->endOf(m_forAst->initializer) - 1 + typeAndName.size();
change.insert(file->endOf(m_forAst->initializer) - 1, // "-1" because of ";"
typeAndName + QLatin1String(" = ") + file->textOf(m_expression));
} else {
// Check if varName is already used
if (DeclarationStatementAST *ds = m_forAst->initializer->asDeclarationStatement()) {
if (DeclarationAST *decl = ds->declaration) {
if (SimpleDeclarationAST *sdecl = decl->asSimpleDeclaration()) {
for (;;) {
bool match = false;
for (DeclaratorListAST *it = sdecl->declarator_list; it;
it = it->next) {
if (file->textOf(it->value->core_declarator) == varName) {
varName += QLatin1Char('X');
match = true;
break;
}
}
if (!match)
break;
}
}
}
}
renamePos = file->endOf(m_forAst->initializer) + 1;
change.insert(file->endOf(m_forAst->initializer) - 1, // "-1" because of ";"
QLatin1String(", ") + varName + QLatin1String(" = ")
+ file->textOf(m_expression));
}
ChangeSet::Range exprRange(file->startOf(m_expression), file->endOf(m_expression));
change.replace(exprRange, varName);
}
file->apply(change);
// Select variable name and trigger symbol rename
if (renamePos != -1) {
QTextCursor c = file->cursor();
c.setPosition(renamePos);
editor()->setTextCursor(c);
editor()->renameSymbolUnderCursor();
c.select(QTextCursor::WordUnderCursor);
editor()->setTextCursor(c);
}
}
private:
const ForStatementAST *m_forAst;
const bool m_optimizePostcrement;
const ExpressionAST *m_expression;
const FullySpecifiedType m_type;
};
/*!
Replace
if (Type name = foo()) {...}
With
Type name = foo();
if (name) {...}
Activates on: the name of the introduced variable
*/
class MoveDeclarationOutOfIf: public CppQuickFixFactory
{
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
{
const QList<AST *> &path = interface.path();
using Ptr = QSharedPointer<MoveDeclarationOutOfIfOp>;
Ptr op(new MoveDeclarationOutOfIfOp(interface));
int index = path.size() - 1;
for (; index != -1; --index) {
if (IfStatementAST *statement = path.at(index)->asIfStatement()) {
if (statement->match(op->pattern, &op->matcher) && op->condition->declarator) {
DeclaratorAST *declarator = op->condition->declarator;
op->core = declarator->core_declarator;
if (!op->core)
return;
if (interface.isCursorOn(op->core)) {
op->setPriority(index);
result.append(op);
return;
}
op->reset();
}
}
}
}
};
/*!
Replace
while (Type name = foo()) {...}
With
Type name;
while ((name = foo()) != 0) {...}
Activates on: the name of the introduced variable
*/
class MoveDeclarationOutOfWhile: public CppQuickFixFactory
{
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
{
const QList<AST *> &path = interface.path();
QSharedPointer<MoveDeclarationOutOfWhileOp> op(new MoveDeclarationOutOfWhileOp(interface));
int index = path.size() - 1;
for (; index != -1; --index) {
if (WhileStatementAST *statement = path.at(index)->asWhileStatement()) {
if (statement->match(op->pattern, &op->matcher) && op->condition->declarator) {
DeclaratorAST *declarator = op->condition->declarator;
op->core = declarator->core_declarator;
if (!op->core)
return;
if (!declarator->equal_token)
return;
if (!declarator->initializer)
return;
if (interface.isCursorOn(op->core)) {
op->setPriority(index);
result.append(op);
return;
}
op->reset();
}
}
}
}
};
/*!
Replace
if (something && something_else) {
}
with
if (something)
if (something_else) {
}
}
and
if (something || something_else)
x;
with
if (something)
x;
else if (something_else)
x;
Activates on: && or ||
*/
class SplitIfStatement: public CppQuickFixFactory
{
#ifdef WITH_TESTS
public:
static QObject *createTest() { return new QObject; }
#endif
private:
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
{
IfStatementAST *pattern = nullptr;
const QList<AST *> &path = interface.path();
int index = path.size() - 1;
for (; index != -1; --index) {
AST *node = path.at(index);
if (IfStatementAST *stmt = node->asIfStatement()) {
pattern = stmt;
break;
}
}
if (!pattern || !pattern->statement)
return;
unsigned splitKind = 0;
for (++index; index < path.size(); ++index) {
AST *node = path.at(index);
BinaryExpressionAST *condition = node->asBinaryExpression();
if (!condition)
return;
Token binaryToken = interface.currentFile()->tokenAt(condition->binary_op_token);
// only accept a chain of ||s or &&s - no mixing
if (!splitKind) {
splitKind = binaryToken.kind();
if (splitKind != T_AMPER_AMPER && splitKind != T_PIPE_PIPE)
return;
// we can't reliably split &&s in ifs with an else branch
if (splitKind == T_AMPER_AMPER && pattern->else_statement)
return;
} else if (splitKind != binaryToken.kind()) {
return;
}
if (interface.isCursorOn(condition->binary_op_token)) {
result << new SplitIfStatementOp(interface, index, pattern, condition);
return;
}
}
}
};
/*!
Add curly braces to a control statement that doesn't already contain a
compound statement. I.e.
if (a)
b;
becomes
if (a) {
b;
}
Activates on: the keyword
*/
class AddBracesToControlStatement : public CppQuickFixFactory
{
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
{
if (interface.path().isEmpty())
return;
const auto constraint = [](AST *ast, bool &) { return !ast->asCompoundStatement(); };
checkControlStatements<AddBracesToControlStatementOp,
IfStatementAST,
WhileStatementAST,
ForStatementAST,
RangeBasedForStatementAST,
DoStatementAST>(interface, constraint, result);
}
};
/*!
* The reverse of AddBracesToControlStatement
*/
class RemoveBracesFromControlStatement : public CppQuickFixFactory
{
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
{
if (interface.path().isEmpty())
return;
const auto constraint = [&](AST *ast, bool &abort) {
if (const auto compoundStmt = ast->asCompoundStatement()) {
if (!compoundStmt->statement_list || !compoundStmt->statement_list->value)
return true; // No statements.
if (compoundStmt->statement_list->next) {
abort = true;
return false; // More than one statement.
}
// We have exactly one statement. Check whether it spans more than one line.
const CppRefactoringFilePtr file = interface.currentFile();
const ChangeSet::Range stmtRange = file->range(compoundStmt->statement_list->value);
int startLine, startColumn, endLine, endColumn;
file->lineAndColumn(stmtRange.start, &startLine, &startColumn);
file->lineAndColumn(stmtRange.end, &endLine, &endColumn);
if (startLine == endLine)
return true;
abort = true;
return false;
}
return false;
};
checkControlStatements<RemoveBracesFromControlStatementOp,
IfStatementAST,
WhileStatementAST,
ForStatementAST,
RangeBasedForStatementAST,
DoStatementAST>(interface, constraint, result);
}
};
/*!
Optimizes a for loop to avoid permanent condition check and forces to use preincrement
or predecrement operators in the expression of the for loop.
*/
class OptimizeForLoop : public CppQuickFixFactory
{
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
{
const QList<AST *> path = interface.path();
ForStatementAST *forAst = nullptr;
if (!path.isEmpty())
forAst = path.last()->asForStatement();
if (!forAst || !interface.isCursorOn(forAst))
return;
// Check for optimizing a postcrement
const CppRefactoringFilePtr file = interface.currentFile();
bool optimizePostcrement = false;
if (forAst->expression) {
if (PostIncrDecrAST *incrdecr = forAst->expression->asPostIncrDecr()) {
const Token t = file->tokenAt(incrdecr->incr_decr_token);
if (t.is(T_PLUS_PLUS) || t.is(T_MINUS_MINUS))
optimizePostcrement = true;
}
}
// Check for optimizing condition
bool optimizeCondition = false;
FullySpecifiedType conditionType;
ExpressionAST *conditionExpression = nullptr;
if (forAst->initializer && forAst->condition) {
if (BinaryExpressionAST *binary = forAst->condition->asBinaryExpression()) {
// Get the expression against which we should evaluate
IdExpressionAST *conditionId = binary->left_expression->asIdExpression();
if (conditionId) {
conditionExpression = binary->right_expression;
} else {
conditionId = binary->right_expression->asIdExpression();
conditionExpression = binary->left_expression;
}
if (conditionId && conditionExpression
&& !(conditionExpression->asNumericLiteral()
|| conditionExpression->asStringLiteral()
|| conditionExpression->asIdExpression()
|| conditionExpression->asUnaryExpression())) {
// Determine type of for initializer
FullySpecifiedType initializerType;
if (DeclarationStatementAST *stmt = forAst->initializer->asDeclarationStatement()) {
if (stmt->declaration) {
if (SimpleDeclarationAST *decl = stmt->declaration->asSimpleDeclaration()) {
if (decl->symbols) {
if (Symbol *symbol = decl->symbols->value)
initializerType = symbol->type();
}
}
}
}
// Determine type of for condition
TypeOfExpression typeOfExpression;
typeOfExpression.init(interface.semanticInfo().doc, interface.snapshot(),
interface.context().bindings());
typeOfExpression.setExpandTemplates(true);
Scope *scope = file->scopeAt(conditionId->firstToken());
const QList<LookupItem> conditionItems = typeOfExpression(
conditionId, interface.semanticInfo().doc, scope);
if (!conditionItems.isEmpty())
conditionType = conditionItems.first().type();
if (conditionType.isValid()
&& (file->textOf(forAst->initializer) == QLatin1String(";")
|| initializerType == conditionType)) {
optimizeCondition = true;
}
}
}
}
if (optimizePostcrement || optimizeCondition) {
result << new OptimizeForLoopOperation(interface, forAst, optimizePostcrement,
optimizeCondition ? conditionExpression : nullptr,
conditionType);
}
}
};
#ifdef WITH_TESTS
class AddBracesToControlStatementTest : public Tests::CppQuickFixTestObject
{
Q_OBJECT
public:
using CppQuickFixTestObject::CppQuickFixTestObject;
};
class RemoveBracesFromControlStatementTest : public Tests::CppQuickFixTestObject
{
Q_OBJECT
public:
using CppQuickFixTestObject::CppQuickFixTestObject;
};
class MoveDeclarationOutOfIfTest : public Tests::CppQuickFixTestObject
{
Q_OBJECT
public:
using CppQuickFixTestObject::CppQuickFixTestObject;
};
class MoveDeclarationOutOfWhileTest : public Tests::CppQuickFixTestObject
{
Q_OBJECT
public:
using CppQuickFixTestObject::CppQuickFixTestObject;
};
class OptimizeForLoopTest : public Tests::CppQuickFixTestObject
{
Q_OBJECT
public:
using CppQuickFixTestObject::CppQuickFixTestObject;
};
#endif
} // namespace
void registerRewriteControlStatementQuickfixes()
{
REGISTER_QUICKFIX_FACTORY_WITH_STANDARD_TEST(AddBracesToControlStatement);
REGISTER_QUICKFIX_FACTORY_WITH_STANDARD_TEST(RemoveBracesFromControlStatement);
REGISTER_QUICKFIX_FACTORY_WITH_STANDARD_TEST(MoveDeclarationOutOfIf);
REGISTER_QUICKFIX_FACTORY_WITH_STANDARD_TEST(MoveDeclarationOutOfWhile);
REGISTER_QUICKFIX_FACTORY_WITH_STANDARD_TEST(OptimizeForLoop);
CppQuickFixFactory::registerFactory<SplitIfStatement>();
}
} // namespace CppEditor::Internal
#ifdef WITH_TESTS
#include <rewritecontrolstatements.moc>
#endif
|