File: Extract.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (2017 lines) | stat: -rw-r--r-- 78,015 bytes parent folder | download
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
//===--- Extract.cpp -  ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Implements the "extract" refactoring operation.
//
//===----------------------------------------------------------------------===//

#include "ExtractionUtils.h"
#include "RefactoringOperations.h"
#include "SourceLocationUtilities.h"
#include "StmtUtils.h"
#include "TypeUtils.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/Refactor/RefactoringOptions.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Support/Path.h"
#include <algorithm>

using namespace clang;
using namespace clang::tooling;

namespace {

struct CompoundStatementRange {
  CompoundStmt::const_body_iterator First, Last;

  const Stmt *getFirst() const {
    // We must have selected just the child of the case, since a selection that
    // includes the case is treated like a selection of the entire switch.
    if (const auto *Case = dyn_cast<SwitchCase>(*First)) {
      if (const Stmt *S = Case->getSubStmt())
        return S;
    }
    return *First;
  }

  const Stmt *getLast() const { return *Last; }

  // TODO: We might not want to iterate over the switch case if we've just
  // selected its child. We should switch over to an array of nodes instead of
  // an iterator pair instead.
  CompoundStmt::const_body_iterator begin() const { return First; }
  CompoundStmt::const_body_iterator end() const { return Last + 1; }
};

enum class ExtractionKind { Function, Method, Expression };

class ExtractOperation : public RefactoringOperation {
public:
  struct CandidateInfo {
    CandidateInfo(SourceRange Range, StringRef PreInsertedText = "",
                  const Stmt *AnalyzedStatement = nullptr)
        : Range(Range), PreInsertedText(PreInsertedText),
          AnalyzedStatement(AnalyzedStatement) {}

    /// The candidate token range, i.e. the end location is the starting
    /// location of the last token.
    SourceRange Range;
    /// The text that should be inserted before the call to the extracted
    /// function.
    StringRef PreInsertedText;
    /// The expression that should be analyzed for captured variables and the
    /// return value.
    const Stmt *AnalyzedStatement;
  };

  ExtractOperation(const Stmt *S, const Stmt *ParentStmt,
                   const Decl *FunctionLikeParentDecl,
                   std::vector<std::string> Candidates,
                   std::optional<CompoundStatementRange> ExtractedStmtRange,
                   std::optional<CandidateInfo> FirstCandidateInfo,
                   ExtractionKind Kind)
      : S(S), ParentStmt(ParentStmt),
        FunctionLikeParentDecl(FunctionLikeParentDecl),
        Candidates(std::move(Candidates)),
        ExtractedStmtRange(ExtractedStmtRange), Kind(Kind) {
    if (FirstCandidateInfo)
      CandidateExtractionInfo.push_back(*FirstCandidateInfo);
  }

  const Stmt *getTransformedStmt() const override {
    if (ExtractedStmtRange)
      return ExtractedStmtRange->getFirst();
    return S;
  }

  const Stmt *getLastTransformedStmt() const override {
    if (ExtractedStmtRange)
      return ExtractedStmtRange->getLast();
    return nullptr;
  }

  std::vector<std::string> getRefactoringCandidates() override {
    return Candidates;
  }

  std::vector<RefactoringActionType> getAvailableSubActions() override {
    std::vector<RefactoringActionType> SubActions;
    if (isa<CXXMethodDecl>(FunctionLikeParentDecl) ||
        isa<ObjCMethodDecl>(FunctionLikeParentDecl))
      SubActions.push_back(RefactoringActionType::Extract_Method);
    if (isLexicalExpression(S, ParentStmt))
      SubActions.push_back(RefactoringActionType::Extract_Expression);
    return SubActions;
  }

  bool isMethodExtraction() const { return Kind == ExtractionKind::Method; }

  bool isExpressionExtraction() const {
    return Kind == ExtractionKind::Expression;
  }

  llvm::Expected<RefactoringResult> perform(ASTContext &Context, const Preprocessor &ThePreprocessor,
          const RefactoringOptionSet &Options,
          unsigned SelectedCandidateIndex) override;

  llvm::Expected<RefactoringResult>
  performExpressionExtraction(ASTContext &Context, PrintingPolicy &PP);

  const Stmt *S, *ParentStmt;
  const Decl *FunctionLikeParentDecl;
  std::vector<std::string> Candidates;
  /// A set of extraction candidates that correspond to the extracted code.
  SmallVector<CandidateInfo, 2> CandidateExtractionInfo;
  std::optional<CompoundStatementRange> ExtractedStmtRange;
  ExtractionKind Kind;
};

} // end anonymous namespace

bool isSimpleExpression(const Expr *E) {
  switch (E->IgnoreParenCasts()->getStmtClass()) {
  case Stmt::DeclRefExprClass:
  case Stmt::PredefinedExprClass:
  case Stmt::IntegerLiteralClass:
  case Stmt::FloatingLiteralClass:
  case Stmt::ImaginaryLiteralClass:
  case Stmt::CharacterLiteralClass:
  case Stmt::StringLiteralClass:
    return true;
  default:
    return false;
  }
}

static bool isMultipleCandidateBinOp(BinaryOperatorKind Op) {
  return Op == BO_Add || Op == BO_Sub;
}

/// Searches for the selected statement in the given CompoundStatement, looking
/// through things like PseudoObjectExpressions.
static CompoundStmt::const_body_iterator
findSelectedStmt(CompoundStmt::body_const_range Statements,
                 const Stmt *Target) {
  return llvm::find_if(Statements, [=](const Stmt *S) {
    if (S == Target)
      return true;
    if (const auto *POE = dyn_cast<PseudoObjectExpr>(S)) {
      if (POE->getSyntacticForm() == Target)
        return true;
    }
    return false;
  });
}

/// Returns the first and the last statements that should be extracted from a
/// compound statement.
std::optional<CompoundStatementRange>
getExtractedStatements(const CompoundStmt *CS, const Stmt *Begin,
                       const Stmt *End) {
  if (CS->body_empty())
    return std::nullopt;
  assert(Begin && End);
  CompoundStatementRange Result;
  Result.First = findSelectedStmt(CS->body(), Begin);
  if (Result.First == CS->body_end())
    return std::nullopt;
  Result.Last = findSelectedStmt(
      CompoundStmt::body_const_range(Result.First, CS->body_end()), End);
  if (Result.Last == CS->body_end())
    return std::nullopt;
  return Result;
}

static RefactoringOperationResult
initiateAnyExtractOperation(ASTSlice &Slice, ASTContext &Context,
                            SourceLocation Location, SourceRange SelectionRange,
                            bool CreateOperation,
                            ExtractionKind Kind = ExtractionKind::Function) {
  auto SelectedStmtsOpt = Slice.getSelectedStmtSet();
  if (!SelectedStmtsOpt)
    return std::nullopt;
  SelectedStmtSet Stmts = *SelectedStmtsOpt;
  // The selection range is contained entirely within this statement (without
  // taking leading/trailing comments and whitespace into account).
  const Stmt *Selected = Stmts.containsSelectionRange;

  // We only want to perform the extraction if the selection range is entirely
  // within a body of a function or method.
  if (!Selected)
    return std::nullopt;
  const Decl *ParentDecl =
      Slice.parentDeclForIndex(*Stmts.containsSelectionRangeIndex);

  if (!ParentDecl ||
      (!Stmts.isCompoundStatementPartiallySelected() &&
       !Slice.isContainedInCompoundStmt(*Stmts.containsSelectionRangeIndex)))
    return RefactoringOperationResult(
        "the selected expression is not in a function");

  if (isa<Expr>(Selected) && isSimpleExpression(cast<Expr>(Selected)))
    return RefactoringOperationResult("the selected expression is too simple");
  if (const auto *PRE = dyn_cast<ObjCPropertyRefExpr>(Selected)) {
    if (!PRE->isMessagingGetter())
      return RefactoringOperationResult("property setter can't be extracted");
  }

  const Stmt *ParentStmt =
      Slice.parentStmtForIndex(*Stmts.containsSelectionRangeIndex);
  if (Kind == ExtractionKind::Expression &&
      !isLexicalExpression(Selected, ParentStmt))
    return std::nullopt;

  RefactoringOperationResult Result;
  Result.Initiated = true;
  if (!CreateOperation)
    return Result;

  std::optional<CompoundStatementRange> ExtractedStmtRange;

  // Check if there are multiple candidates that can be extracted.
  std::vector<std::string> Candidates;
  std::optional<ExtractOperation::CandidateInfo> FirstCandidateInfo;
  if (const auto *BinOp = dyn_cast<BinaryOperator>(Selected)) {
    // Binary '+' and '-' operators allow multiple candidates when the
    // selection range starts after the LHS expression but still overlaps
    // with the RHS.
    if (isMultipleCandidateBinOp(BinOp->getOpcode()) &&
        (!Stmts.containsSelectionRangeStart ||
         getPreciseTokenLocEnd(
             BinOp->getLHS()->getEndLoc(), Context.getSourceManager(),
             Context.getLangOpts()) == SelectionRange.getBegin()) &&
        Stmts.containsSelectionRangeEnd) {
      SourceRange FirstCandidateRange =
          SourceRange(SelectionRange.getBegin(), BinOp->getEndLoc());
      if (FirstCandidateRange.getEnd().isMacroID())
        FirstCandidateRange.setEnd(Context.getSourceManager().getExpansionLoc(
            FirstCandidateRange.getEnd()));
      FirstCandidateInfo = ExtractOperation::CandidateInfo(
          FirstCandidateRange, "+ ",
          /*AnalyzedStatement=*/BinOp->getRHS());
      Candidates.push_back(
          std::string(Lexer::getSourceText(
                          CharSourceRange::getTokenRange(FirstCandidateRange),
                          Context.getSourceManager(), Context.getLangOpts())
                          .trim()));
      Candidates.push_back(std::string(Lexer::getSourceText(
          CharSourceRange::getTokenRange(BinOp->getSourceRange()),
          Context.getSourceManager(), Context.getLangOpts())));
    }
  } else if (const auto *CS = dyn_cast<CompoundStmt>(Selected)) {
    // We want to extract some child statements from a compound statement unless
    // we've selected the entire compound statement including the opening and
    // closing brace.
    if (Stmts.containsSelectionRangeStart)
      ExtractedStmtRange =
          getExtractedStatements(CS, Stmts.containsSelectionRangeStart,
                                 Stmts.containsSelectionRangeEnd);
  }

  auto Operation = std::make_unique<ExtractOperation>(
      Selected, ParentStmt, ParentDecl, std::move(Candidates),
      ExtractedStmtRange, FirstCandidateInfo, Kind);
  auto &CandidateExtractionInfo = Operation->CandidateExtractionInfo;
  SourceRange Range;
  if (ExtractedStmtRange)
    Range = SourceRange(ExtractedStmtRange->getFirst()->getBeginLoc(),
                        ExtractedStmtRange->getLast()->getEndLoc());
  else
    Range = Selected->getSourceRange();
  bool IsBeginMacroArgument = false;
  if (Range.getBegin().isMacroID()) {
    if (Context.getSourceManager().isMacroArgExpansion(Range.getBegin())) {
      Range.setBegin(
          Context.getSourceManager().getSpellingLoc(Range.getBegin()));
      IsBeginMacroArgument = true;
    } else {
      Range.setBegin(
          Context.getSourceManager().getExpansionLoc(Range.getBegin()));
    }
  }
  if (Range.getEnd().isMacroID()) {
    if (IsBeginMacroArgument &&
        Context.getSourceManager().isMacroArgExpansion(Range.getEnd()))
      Range.setEnd(Context.getSourceManager().getSpellingLoc(Range.getEnd()));
    else
      Range.setEnd(Context.getSourceManager()
                       .getExpansionRange(Range.getEnd())
                       .getEnd());
  }
  CandidateExtractionInfo.push_back(ExtractOperation::CandidateInfo(Range));
  Result.RefactoringOp = std::move(Operation);
  return Result;
}

RefactoringOperationResult clang::tooling::initiateExtractOperation(
    ASTSlice &Slice, ASTContext &Context, SourceLocation Location,
    SourceRange SelectionRange, bool CreateOperation) {
  return initiateAnyExtractOperation(Slice, Context, Location, SelectionRange,
                                     CreateOperation);
}

RefactoringOperationResult clang::tooling::initiateExtractMethodOperation(
    ASTSlice &Slice, ASTContext &Context, SourceLocation Location,
    SourceRange SelectionRange, bool CreateOperation) {
  // TODO: Verify that method extraction is actually possible.
  return initiateAnyExtractOperation(Slice, Context, Location, SelectionRange,
                                     CreateOperation, ExtractionKind::Method);
}

RefactoringOperationResult clang::tooling::initiateExtractExpressionOperation(
    ASTSlice &Slice, ASTContext &Context, SourceLocation Location,
    SourceRange SelectionRange, bool CreateOperation) {
  RefactoringOperationResult R =
      initiateAnyExtractOperation(Slice, Context, Location, SelectionRange,
                                  CreateOperation, ExtractionKind::Expression);
  return R;
}

using ReferencedEntity =
    llvm::PointerUnion<const DeclRefExpr *, const FieldDecl *>;

/// Iterate over the entities (variables/instance variables) that are directly
/// referenced by the given expression \p E.
///
/// Note: Objective-C ivars are always captured via 'self'.
static void findEntitiesDirectlyReferencedInExpr(
    const Expr *E,
    llvm::function_ref<void(const ReferencedEntity &Entity)> Handler) {
  E = E->IgnoreParenCasts();
  if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
    return Handler(DRE);

  if (const auto *ME = dyn_cast<MemberExpr>(E)) {
    if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) {
      if (const auto *FD = dyn_cast_or_null<FieldDecl>(ME->getMemberDecl()))
        Handler(FD);
      return;
    }
    if (const auto *MD = ME->getMemberDecl()) {
      if (isa<FieldDecl>(MD) || isa<IndirectFieldDecl>(MD))
        findEntitiesDirectlyReferencedInExpr(ME->getBase(), Handler);
    }
    return;
  }

  if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
    findEntitiesDirectlyReferencedInExpr(CO->getTrueExpr(), Handler);
    findEntitiesDirectlyReferencedInExpr(CO->getFalseExpr(), Handler);
    return;
  }

  if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
    if (BO->getOpcode() == BO_Comma)
      return findEntitiesDirectlyReferencedInExpr(BO->getRHS(), Handler);
  }
}

template <typename T, typename Matcher>
static void
findMatchingParameters(Matcher &ParameterMatcher, const Stmt *S,
                       ASTContext &Context, StringRef Node,
                       llvm::function_ref<void(const T *E)> Handler) {
  using namespace clang::ast_matchers;
  auto Matches = match(findAll(callExpr(ParameterMatcher)), *S, Context);
  for (const auto &Match : Matches)
    Handler(Match.template getNodeAs<T>(Node));
  Matches = match(findAll(cxxConstructExpr(ParameterMatcher)), *S, Context);
  for (const auto &Match : Matches)
    Handler(Match.template getNodeAs<T>(Node));
}

static void
findUseOfConstThis(const Stmt *S, ASTContext &Context,
                   llvm::function_ref<void(const CXXThisExpr *E)> Handler) {
  using namespace clang::ast_matchers;
  // Check the receiver in method call and member operator calls.
  auto This = cxxThisExpr().bind("this");
  auto ThisReceiver = ignoringParenCasts(
      anyOf(This, unaryOperator(hasOperatorName("*"),
                                hasUnaryOperand(ignoringParenCasts(This)))));
  auto ConstMethodCallee = callee(cxxMethodDecl(isConst()));
  auto Matches = match(
      findAll(expr(anyOf(cxxMemberCallExpr(ConstMethodCallee, on(ThisReceiver)),
                         cxxOperatorCallExpr(ConstMethodCallee,
                                             hasArgument(0, ThisReceiver))))),
      *S, Context);
  for (const auto &Match : Matches)
    Handler(Match.getNodeAs<CXXThisExpr>("this"));
  // Check parameters in calls.
  auto ConstPointee = pointee(qualType(isConstQualified()));
  auto RefParameter = forEachArgumentWithParam(
      ThisReceiver,
      parmVarDecl(hasType(qualType(referenceType(ConstPointee)))));
  findMatchingParameters(RefParameter, S, Context, "this", Handler);
  auto PtrParameter = forEachArgumentWithParam(
      ignoringParenCasts(This),
      parmVarDecl(hasType(qualType(pointerType(ConstPointee)))));
  findMatchingParameters(PtrParameter, S, Context, "this", Handler);
}

static void findArgumentsPassedByNonConstReference(
    const Stmt *S, ASTContext &Context,
    llvm::function_ref<void(const Expr *E)> Handler) {
  using namespace clang::ast_matchers;
  // Check the receiver in method call and member operator calls.
  auto NonPointerReceiver =
      expr(unless(hasType(qualType(pointerType())))).bind("arg");
  auto NonConstMethodCallee = callee(cxxMethodDecl(unless(isConst())));
  auto Matches = match(
      traverse(
          TK_AsIs,
          findAll(expr(anyOf(
              cxxMemberCallExpr(NonConstMethodCallee, on(NonPointerReceiver)),
              cxxOperatorCallExpr(NonConstMethodCallee,
                                  hasArgument(0, NonPointerReceiver)))))),
      *S, Context);
  for (const auto &Match : Matches)
    Handler(Match.getNodeAs<Expr>("arg"));
  // Check parameters in calls.
  auto RefParameter = forEachArgumentWithParam(
      expr().bind("arg"), parmVarDecl(hasType(qualType(referenceType(unless(
                              pointee(qualType(isConstQualified()))))))));
  Matches =
      match(traverse(TK_AsIs, findAll(callExpr(RefParameter))), *S, Context);
  for (const auto &Match : Matches)
    Handler(Match.getNodeAs<Expr>("arg"));
  Matches = match(traverse(TK_AsIs, findAll(cxxConstructExpr(RefParameter))),
                  *S, Context);
  for (const auto &Match : Matches)
    Handler(Match.getNodeAs<Expr>("arg"));
}

static void findAddressExpressionsPassedByConstPointer(
    const Stmt *S, ASTContext &Context,
    llvm::function_ref<void(const UnaryOperator *E)> Handler) {
  using namespace clang::ast_matchers;
  auto ConstPtrParameter = forEachArgumentWithParam(
      ignoringParenImpCasts(unaryOperator(hasOperatorName("&")).bind("arg")),
      parmVarDecl(hasType(
          qualType(pointerType(pointee(qualType(isConstQualified())))))));
  auto Matches = match(traverse(TK_AsIs, findAll(callExpr(ConstPtrParameter))),
                       *S, Context);
  for (const auto &Match : Matches)
    Handler(Match.getNodeAs<UnaryOperator>("arg"));
  Matches =
      match(traverse(TK_AsIs, findAll(cxxConstructExpr(ConstPtrParameter))), *S,
            Context);
  for (const auto &Match : Matches)
    Handler(Match.getNodeAs<UnaryOperator>("arg"));
}

static bool isImplicitInitializer(const VarDecl *VD) {
  assert(VD->hasInit());
  const auto *E = VD->getInit();
  if (isa<ExprWithCleanups>(E))
    return false;
  const auto *Construct = dyn_cast<CXXConstructExpr>(E);
  if (!Construct)
    return E->getBeginLoc() == VD->getLocation();
  return Construct->getParenOrBraceRange().isInvalid();
}

static const Expr *getInitializerExprWithLexicalRange(const Expr *E) {
  if (const auto *EWC = dyn_cast<ExprWithCleanups>(E)) {
    if (const auto *Construct = dyn_cast<CXXConstructExpr>(EWC->getSubExpr())) {
      if (Construct->getNumArgs() == 1) {
        if (const auto *ME =
                dyn_cast<MaterializeTemporaryExpr>(Construct->getArg(0)))
          return ME;
      }
    }
  }
  return E;
}

namespace {

class ExtractedCodeVisitor : public RecursiveASTVisitor<ExtractedCodeVisitor> {
  int DefineOrdering = 0;

public:
  struct CaptureInfo {
    bool IsMutated = false;
    bool IsDefined = false;
    bool IsAddressTaken = false;
    bool IsConstAddressTaken = false;
    bool IsFieldCapturedWithThis = false;
    bool IsUsed = false;
    int DefineOrderingPriority = 0;

    bool isPassedByRefOrPtr() const {
      return IsMutated || IsAddressTaken || IsConstAddressTaken;
    }
    bool isRefOrPtrConst() const {
      return IsConstAddressTaken && !IsMutated && !IsAddressTaken;
    }
  };

  const ImplicitParamDecl *SelfDecl;

  ExtractedCodeVisitor(const ImplicitParamDecl *SelfDecl)
      : SelfDecl(SelfDecl) {}

  bool HasReturnInExtracted = false;

  CaptureInfo &captureVariable(const VarDecl *VD) {
    CaptureInfo &Result = CapturedVariables[VD];
    Result.IsUsed = true;
    return Result;
  }

  CaptureInfo &captureField(const FieldDecl *FD) { return CapturedFields[FD]; }

  bool VisitDeclRefExpr(const DeclRefExpr *E) {
    const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
    if (!VD)
      return true;
    if (VD == SelfDecl) {
      CaptureSelf = true;
      SelfType = VD->getType();
      return true;
    }
    if (!VD->isLocalVarDeclOrParm())
      return true;
    captureVariable(VD);
    return true;
  }

  void captureThisWithoutConstConcerns(const CXXThisExpr *E) {
    CaptureThis = true;
    ThisRecordType = E->getType()->getPointeeType();
  }

  bool VisitCXXThisExpr(const CXXThisExpr *E) {
    captureThisWithoutConstConcerns(E);
    ThisUsesWithUnknownConstness.insert(E);
    return true;
  }

  bool TraverseMemberExpr(MemberExpr *E) {
    const auto *Base = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts());
    if (!Base)
      return RecursiveASTVisitor::TraverseMemberExpr(E);
    const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(E->getMemberDecl());
    if (!FD)
      return RecursiveASTVisitor::TraverseMemberExpr(E);
    CaptureInfo &Info = captureField(FD);
    // Don't capture the implicit 'this' for private fields as we don't want to
    // capture this if we only use the private fields.
    if (FD->getAccess() == AS_public || !Base->isImplicit()) {
      Info.IsFieldCapturedWithThis = true;
      // The member might have an effect on the constness of the captured 'this'
      // but this is checked via mutation/const tracking for the field itself,
      // so we just capture 'this' without worrying about checking if it's used
      // in a 'const' manner here.
      captureThisWithoutConstConcerns(Base);
    }
    return true;
  }

  void captureSuper(QualType T) {
    if (CaptureSuper)
      return;
    SuperType = T;
    CaptureSuper = true;
  }

  bool TraverseObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
    if (E->isSuperReceiver())
      captureSuper(E->getSuperReceiverType());
    // Base might be an opaque expression, so we have to visit it manually as
    // we don't necessarily visit the setter/getter message sends if just the
    // property was selected.
    if (E->isObjectReceiver()) {
      if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E->getBase()))
        TraverseStmt(OVE->getSourceExpr());
    }
    return RecursiveASTVisitor::TraverseObjCPropertyRefExpr(E);
  }

  bool TraverseBinaryOperator(BinaryOperator *S) {
    if (S->getOpcode() != BO_Assign)
      return RecursiveASTVisitor::TraverseBinaryOperator(S);
    // RHS might be an opaque expression, if this is a property assignment. We
    // have to visit it manually as we don't necessarily visit the setter/getter
    // message sends if just the property was selected.
    if (const auto *OVE = dyn_cast<OpaqueValueExpr>(S->getRHS()))
      TraverseStmt(OVE->getSourceExpr());
    return RecursiveASTVisitor::TraverseBinaryOperator(S);
  }

  void findCapturedVariableOrFieldsInExpression(
      const Expr *E, llvm::function_ref<void(CaptureInfo &)> Handler) {
    findEntitiesDirectlyReferencedInExpr(
        E, [&Handler, this](const ReferencedEntity &Entity) {
          if (const auto *DRE = Entity.dyn_cast<const DeclRefExpr *>()) {
            const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl());
            if (!VD || !VD->isLocalVarDeclOrParm() || VD->isImplicit())
              return;
            return Handler(captureVariable(VD));
          }
          return Handler(captureField(Entity.get<const FieldDecl *>()));
        });
  }

  void
  markDirectlyReferencedVariableOrFieldInExpressionAsMutated(const Expr *E) {
    findCapturedVariableOrFieldsInExpression(
        E, [](CaptureInfo &Capture) { Capture.IsMutated = true; });
  }

  bool VisitBinaryOperator(const BinaryOperator *E) {
    if (E->isAssignmentOp())
      markDirectlyReferencedVariableOrFieldInExpressionAsMutated(E->getLHS());
    return true;
  }

  bool VisitUnaryOperator(const UnaryOperator *E) {
    auto Op = E->getOpcode();
    if (Op != UO_PreInc && Op != UO_PostInc && Op != UO_PreDec &&
        Op != UO_PostDec) {
      if (Op == UO_AddrOf) {
        // Capture the entity with 'const' reference/pointer when its address is
        // passed into a function that takes a 'const' pointer and no other
        // mutations or non-const address/reference acquisitions occur.
        if (AddressExpressionsPassedToConstPointerParameter.count(E))
          findCapturedVariableOrFieldsInExpression(
              E->getSubExpr(),
              [](CaptureInfo &Capture) { Capture.IsConstAddressTaken = true; });
        else
          captureVariableOrFieldInExpressionByReference(E->getSubExpr());
      }
      return true;
    }
    markDirectlyReferencedVariableOrFieldInExpressionAsMutated(E->getSubExpr());
    return true;
  }

  /// If the given expression refers to a local/instance variable or a
  /// a member of such variable that variable is marked as captured by
  /// reference.
  void captureVariableOrFieldInExpressionByReference(const Expr *E) {
    findCapturedVariableOrFieldsInExpression(
        E, [](CaptureInfo &Capture) { Capture.IsAddressTaken = true; });
  }

  bool VisitObjCMessageExpr(const ObjCMessageExpr *E) {
    if (E->getSuperLoc().isValid())
      captureSuper(E->getSuperType());
    const ObjCMethodDecl *MD = E->getMethodDecl();
    if (!MD)
      return true;
    for (const auto &Param : llvm::enumerate(MD->parameters())) {
      QualType T = Param.value()->getType();
      if (Param.index() >= E->getNumArgs())
        break;
      if (T->isReferenceType() && !T->getPointeeType().isConstQualified())
        captureVariableOrFieldInExpressionByReference(E->getArg(Param.index()));
      if (T->isPointerType() && T->getPointeeType().isConstQualified()) {
        // Check if this is an '&' passed into a const pointer parameter.
        const Expr *Arg = E->getArg(Param.index());
        if (const auto *Op =
                dyn_cast<UnaryOperator>(Arg->IgnoreParenImpCasts())) {
          if (Op->getOpcode() == UO_AddrOf)
            AddressExpressionsPassedToConstPointerParameter.insert(Op);
        }
      }
    }
    return true;
  }

  bool VisitVarDecl(const VarDecl *VD) {
    // Don't capture using the captureVariable method as we don't want to mark
    // the declaration as a 'use'. This allows us to avoid passing in variables
    // that are defined in extracted code, used afterwards, but never actually
    // used in the extracted code.
    CaptureInfo &Capture = CapturedVariables[VD];
    Capture.IsDefined = true;
    Capture.DefineOrderingPriority = ++DefineOrdering;
    // Ensure the capture is marked as 'used' when the variable declaration has
    // an explicit initialization expression. This allows us to pass it by
    // reference when it's defined in extracted code, used afterwards, but never
    // actually used in the extracted code. The main reason why we want to try
    // to keep this initialization in the extracted code is to preserve
    // semantics as the initialization expression might have side-effects.
    if (!Capture.IsUsed && VD->hasInit() && !isImplicitInitializer(VD))
      Capture.IsUsed = true;
    QualType T = VD->getType();
    if (T->isReferenceType() && !T->getPointeeType().isConstQualified() &&
        VD->hasInit())
      captureVariableOrFieldInExpressionByReference(VD->getInit());
    return true;
  }

  bool VisitReturnStmt(const ReturnStmt *S) {
    HasReturnInExtracted = true;
    return true;
  }

  void InspectExtractedStmt(Stmt *S, ASTContext &Context) {
    findAddressExpressionsPassedByConstPointer(
        S, Context, [this](const UnaryOperator *Arg) {
          AddressExpressionsPassedToConstPointerParameter.insert(Arg);
        });
    TraverseStmt(S);
    findArgumentsPassedByNonConstReference(S, Context, [this](const Expr *Arg) {
      captureVariableOrFieldInExpressionByReference(Arg);
    });
    if (CaptureThis && !ThisUsesWithUnknownConstness.empty()) {
      // Compare the definite 'const' uses of 'this' to all the seen uses
      // (except for the known field uses).
      findUseOfConstThis(S, Context, [this](const CXXThisExpr *Arg) {
        ThisUsesWithUnknownConstness.erase(Arg);
      });
      IsThisConstForNonCapturedFieldUses = ThisUsesWithUnknownConstness.empty();
    }
  }

  llvm::DenseMap<const VarDecl *, CaptureInfo> CapturedVariables;
  llvm::DenseMap<const FieldDecl *, CaptureInfo> CapturedFields;
  llvm::SmallPtrSet<const UnaryOperator *, 8>
      AddressExpressionsPassedToConstPointerParameter;
  llvm::SmallPtrSet<const CXXThisExpr *, 16> ThisUsesWithUnknownConstness;
  bool CaptureThis = false;
  bool IsThisConstForNonCapturedFieldUses = true;
  QualType ThisRecordType;
  bool CaptureSelf = false, CaptureSuper = false;
  QualType SelfType, SuperType;
};

/// Traverses the extracted code and finds the uses of captured variables
/// that are passed into the extracted function using a pointer.
class VariableDefinedInExtractedCodeUseAfterExtractionFinder
    : public RecursiveASTVisitor<
          VariableDefinedInExtractedCodeUseAfterExtractionFinder> {
  bool IsAfterExtracted = false;

public:
  const Stmt *LastExtractedStmt;
  const llvm::SmallPtrSetImpl<const VarDecl *> &VariablesDefinedInExtractedCode;
  llvm::SmallPtrSet<const VarDecl *, 4> VariablesUsedAfterExtraction;

  VariableDefinedInExtractedCodeUseAfterExtractionFinder(
      const Stmt *LastExtractedStmt,
      const llvm::SmallPtrSetImpl<const VarDecl *>
          &VariablesDefinedInExtractedCode)
      : LastExtractedStmt(LastExtractedStmt),
        VariablesDefinedInExtractedCode(VariablesDefinedInExtractedCode) {}

  bool TraverseStmt(Stmt *S) {
    RecursiveASTVisitor::TraverseStmt(S);
    if (S == LastExtractedStmt)
      IsAfterExtracted = true;
    return true;
  }

  bool VisitDeclRefExpr(const DeclRefExpr *E) {
    if (!IsAfterExtracted)
      return true;
    const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
    if (!VD)
      return true;
    if (VariablesDefinedInExtractedCode.count(VD))
      VariablesUsedAfterExtraction.insert(VD);
    return true;
  }
};

class PossibleShadowingVariableFinder
    : public RecursiveASTVisitor<PossibleShadowingVariableFinder> {
  const VarDecl *TargetVD;

  PossibleShadowingVariableFinder(const VarDecl *TargetVD)
      : TargetVD(TargetVD) {}

public:
  bool VisitVarDecl(const VarDecl *VD) {
    if (VD == TargetVD || VD->getName() != TargetVD->getName())
      return true;
    return false;
  }

  /// Returns true if the given statement \p S has a variable declaration whose
  /// name is identical to the given variable declaration \p VD.
  static bool hasShadowingVar(const VarDecl *VD, const Stmt *S) {
    return !PossibleShadowingVariableFinder(VD).TraverseStmt(
        const_cast<Stmt *>(S));
  }
};

/// Traverses the extracted code and rewrites the 'return' statements to ensure
/// that they now return some value.
class ReturnRewriter : public RecursiveASTVisitor<ReturnRewriter> {
  Rewriter &SourceRewriter;
  std::string Text;

public:
  ReturnRewriter(Rewriter &SourceRewriter, StringRef Text)
      : SourceRewriter(SourceRewriter), Text(std::string(" ") + Text.str()) {}

  bool VisitReturnStmt(const ReturnStmt *S) {
    SourceRewriter.InsertText(
        getPreciseTokenLocEnd(S->getEndLoc(), SourceRewriter.getSourceMgr(),
                              SourceRewriter.getLangOpts()),
        Text);
    return true;
  }
};

/// Prints the given initializer expression using the original source code if
/// possible.
static void printInitializerExpressionUsingOriginalSyntax(
    const VarDecl *VD, const Expr *E, bool IsDeclaration, const ASTContext &Ctx,
    llvm::raw_ostream &OS, const PrintingPolicy &PP) {
  E = getInitializerExprWithLexicalRange(E);
  SourceRange Range = E->getSourceRange();
  bool UseEquals = true;
  bool UseTypeName = false;
  if (const auto *Construct = dyn_cast<CXXConstructExpr>(E)) {
    SourceRange SubRange = Construct->getParenOrBraceRange();
    if (SubRange.isValid()) {
      UseEquals = false;
      UseTypeName = true;
      Range = SubRange;
    }
  }
  if (Range.getBegin().isMacroID())
    Range.setBegin(Ctx.getSourceManager().getExpansionLoc(Range.getBegin()));
  if (Range.getEnd().isMacroID())
    Range.setEnd(Ctx.getSourceManager().getExpansionLoc(Range.getEnd()));
  bool IsInvalid = false;
  StringRef Text = Lexer::getSourceText(CharSourceRange::getTokenRange(Range),
                                        Ctx.getSourceManager(),
                                        Ctx.getLangOpts(), &IsInvalid);
  if (IsDeclaration && UseEquals)
    OS << " = ";
  else if (!IsDeclaration && UseTypeName)
    VD->getType().print(OS, PP);
  if (IsInvalid)
    E->printPretty(OS, nullptr, PP);
  else
    OS << Text;
}

/// Traverses the extracted code and rewrites the declaration statements that
/// declare variables that are used after the extracted code.
class DefinedInExtractedCodeDeclStmtRewriter
    : public RecursiveASTVisitor<DefinedInExtractedCodeDeclStmtRewriter> {
public:
  Rewriter &SourceRewriter;
  const llvm::SmallPtrSetImpl<const VarDecl *> &VariablesUsedAfterExtraction;
  const PrintingPolicy &PP;

  DefinedInExtractedCodeDeclStmtRewriter(
      Rewriter &SourceRewriter, const llvm::SmallPtrSetImpl<const VarDecl *>
                                    &VariablesUsedAfterExtraction,
      const PrintingPolicy &PP)
      : SourceRewriter(SourceRewriter),
        VariablesUsedAfterExtraction(VariablesUsedAfterExtraction), PP(PP) {}

  /// When a declaration statement declares variables that are all used
  /// after extraction, we can rewrite it completely into a set of assignments
  /// while still preserving the original initializer expressions when we
  /// can.
  void rewriteAllVariableDeclarationsToAssignments(const DeclStmt *S) {
    SourceLocation StartLoc = S->getBeginLoc();
    for (const Decl *D : S->decls()) {
      const auto *VD = dyn_cast<VarDecl>(D);
      if (!VD || !VariablesUsedAfterExtraction.count(VD))
        continue;
      if (!VD->hasInit() || isImplicitInitializer(VD)) {
        // Remove the variable declarations without explicit initializers.
        // This can affect the semantics of the program if the implicit
        // initialization expression has side effects.
        SourceRange Range = SourceRange(
            StartLoc, S->isSingleDecl() ? S->getEndLoc() : VD->getLocation());
        SourceRewriter.RemoveText(Range);
        continue;
      }
      std::string Str;
      llvm::raw_string_ostream OS(Str);
      if (StartLoc != S->getBeginLoc())
        OS << "; ";
      const ASTContext &Ctx = D->getASTContext();
      // Dereference the variable unless the source uses C++.
      if (!Ctx.getLangOpts().CPlusPlus)
        OS << '*';
      OS << VD->getName() << " = ";
      const Expr *Init = getInitializerExprWithLexicalRange(VD->getInit());
      SourceLocation End = Init->getBeginLoc();
      if (const auto *Construct = dyn_cast<CXXConstructExpr>(Init)) {
        SourceRange SubRange = Construct->getParenOrBraceRange();
        if (SubRange.isValid()) {
          End = SubRange.getBegin();
          VD->getType().print(OS, PP);
        }
      }
      if (End.isMacroID())
        End = Ctx.getSourceManager().getExpansionLoc(End);
      auto Range = CharSourceRange::getCharRange(StartLoc, End);
      SourceRewriter.ReplaceText(StartLoc, SourceRewriter.getRangeSize(Range),
                                 OS.str());
      StartLoc = getPreciseTokenLocEnd(D->getEndLoc(), Ctx.getSourceManager(),
                                       Ctx.getLangOpts());
    }
  }

  /// When a declaration statement has variables that are both used after
  /// extraction and not used after extraction, we create new declaration
  /// statements that declare the unused variables, while creating assignment
  /// statements that "initialize" the variables that are used after the
  /// extraction. This way we can preserve the order of
  /// initialization/assignment from the original declaration statement.
  void rewriteMixedDeclarations(const DeclStmt *S) {
    // Completely rewrite the declaration statement.
    std::string Str;
    llvm::raw_string_ostream OS(Str);
    for (const Decl *D : S->decls()) {
      const ASTContext &Ctx = D->getASTContext();
      const VarDecl *VD = dyn_cast<VarDecl>(D);
      bool IsLast = D == S->decl_end()[-1];
      if (!VD) {
        OS << "<<unsupported declaration>>;";
        continue;
      }

      auto PrintInit = [&](bool IsDeclaration) {
        printInitializerExpressionUsingOriginalSyntax(
            VD, VD->getInit(), IsDeclaration, Ctx, OS, PP);
      };
      if (!VariablesUsedAfterExtraction.count(VD)) {
        VD->getType().print(OS, PP);
        OS << " " << VD->getName();
        if (VD->hasInit() && !isImplicitInitializer(VD))
          PrintInit(/*IsDeclaration=*/true);
        OS << ";";
        if (!IsLast)
          OS << ' ';
        continue;
      }
      if (VD->hasInit() && !isImplicitInitializer(VD)) {
        // Dereference the variable unless the source uses C++.
        if (!Ctx.getLangOpts().CPlusPlus)
          OS << '*';
        OS << VD->getName() << " = ";
        PrintInit(/*IsDeclaration=*/false);
        OS << ";";
        if (!IsLast)
          OS << ' ';
      }
    }
    SourceRewriter.ReplaceText(S->getSourceRange(), OS.str());
  }

  bool VisitDeclStmt(const DeclStmt *S) {
    bool AreAllUsed = true;
    bool AreNoneUsed = true;
    for (const Decl *D : S->decls()) {
      const auto *VD = dyn_cast<VarDecl>(D);
      if (!VD || !VariablesUsedAfterExtraction.count(VD)) {
        AreAllUsed = false;
        continue;
      }
      AreNoneUsed = false;
      // Exit early when both flags were set in the loop.
      if (!AreAllUsed)
        break;
    }
    if (AreNoneUsed)
      return true;

    if (AreAllUsed)
      rewriteAllVariableDeclarationsToAssignments(S);
    else
      rewriteMixedDeclarations(S);
    return true;
  }
};

/// Takes care of pseudo object expressions and Objective-C properties to avoid
/// duplicate rewrites and missing rewrites.
template <typename T>
class PseudoObjectRewriter : public RecursiveASTVisitor<T> {
  typedef RecursiveASTVisitor<T> Base;

public:
  bool TraversePseudoObjectExpr(PseudoObjectExpr *E) {
    return Base::TraverseStmt(E->getSyntacticForm());
  }

  bool TraverseObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
    // Base might be an opaque expression, so we have to visit it manually as
    // we don't necessarily visit the setter/getter message sends if just the
    // property was selected.
    if (E->isObjectReceiver()) {
      if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E->getBase()))
        Base::TraverseStmt(OVE->getSourceExpr());
    }
    return Base::TraverseObjCPropertyRefExpr(E);
  }

  bool TraverseBinaryOperator(BinaryOperator *S) {
    if (S->getOpcode() != BO_Assign)
      return Base::TraverseBinaryOperator(S);
    // RHS might be an opaque expression, if this is a property assignment. We
    // have to visit it manually as we don't necessarily visit the setter/getter
    // message sends if just the property was selected.
    if (const auto *OVE = dyn_cast<OpaqueValueExpr>(S->getRHS()))
      Base::TraverseStmt(OVE->getSourceExpr());
    return Base::TraverseBinaryOperator(S);
  }
};

/// Traverses the extracted code and rewrites the uses of captured variables
/// that are passed into the extracted function using a pointer.
class CapturedVariableCaptureByPointerRewriter
    : public PseudoObjectRewriter<CapturedVariableCaptureByPointerRewriter> {
public:
  const VarDecl *TargetVD;
  Rewriter &SourceRewriter;

  CapturedVariableCaptureByPointerRewriter(const VarDecl *VD,
                                           Rewriter &SourceRewriter)
      : TargetVD(VD), SourceRewriter(SourceRewriter) {}

  bool isTargetDeclRefExpr(const Expr *E) {
    const auto *DRE = dyn_cast<DeclRefExpr>(E);
    if (!DRE)
      return false;
    return dyn_cast<VarDecl>(DRE->getDecl()) == TargetVD;
  }

  void dereferenceTargetVar(const Expr *E, bool WrapInParens = false) {
    SourceRewriter.InsertTextBefore(E->getBeginLoc(),
                                    WrapInParens ? "(*" : "*");
    if (WrapInParens)
      SourceRewriter.InsertTextAfterToken(E->getEndLoc(), ")");
  }

  bool VisitDeclRefExpr(const DeclRefExpr *E) {
    const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
    if (VD != TargetVD)
      return true;
    dereferenceTargetVar(E);
    return true;
  }

  bool TraverseUnaryOperator(UnaryOperator *E) {
    if (E->getOpcode() != UO_AddrOf)
      return RecursiveASTVisitor::TraverseUnaryOperator(E);
    if (const auto *DRE =
            dyn_cast<DeclRefExpr>(E->getSubExpr()->IgnoreParenCasts())) {
      const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl());
      if (VD == TargetVD) {
        // Remove the '&' as the variable is now a pointer.
        SourceRewriter.RemoveText(
            CharSourceRange::getTokenRange(E->getBeginLoc(), E->getBeginLoc()));
        return true;
      }
    }
    return RecursiveASTVisitor::TraverseUnaryOperator(E);
  }

  bool TraverseMemberExpr(MemberExpr *E) {
    if (!E->isArrow()) {
      if (const auto *DRE =
              dyn_cast<DeclRefExpr>(E->getBase()->IgnoreParenCasts())) {
        const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl());
        if (VD == TargetVD) {
          // Replace '.' with '->'.
          SourceRewriter.ReplaceText(E->getOperatorLoc(), 1, "->");
          return true;
        }
      }
    } else if (isTargetDeclRefExpr(E->getBase()->IgnoreImpCasts())) {
      // Ensure the variable is wrapped in parenthesis when it's the base of
      // '->' operator.
      dereferenceTargetVar(E->getBase(), /*WrapInParens=*/true);
      return true;
    }
    return RecursiveASTVisitor::TraverseMemberExpr(E);
  }
};

/// Traverses the extracted code and rewrites the uses of 'this' that can be
/// rewritten as references.
class CapturedThisReferenceRewriter
    : public PseudoObjectRewriter<CapturedThisReferenceRewriter> {
public:
  Rewriter &SourceRewriter;
  llvm::SmallPtrSet<const CXXThisExpr *, 8> RewrittenExpressions;

  CapturedThisReferenceRewriter(Rewriter &SourceRewriter)
      : SourceRewriter(SourceRewriter) {}

  void rewriteThis(const CXXThisExpr *E) {
    RewrittenExpressions.insert(E);
    if (!E->isImplicit())
      SourceRewriter.ReplaceText(E->getBeginLoc(), 4, "object");
    else
      SourceRewriter.InsertText(E->getBeginLoc(), "object");
  }

  bool VisitMemberExpr(const MemberExpr *E) {
    const auto *This =
        dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts());
    if (This) {
      rewriteThis(This);
      if (!This->isImplicit() && E->isArrow())
        SourceRewriter.ReplaceText(E->getOperatorLoc(), 2, ".");
      else
        SourceRewriter.InsertText(E->getBase()->getEndLoc(), ".");
    }
    return true;
  }
};

/// Traverses the extracted code and rewrites the uses of 'this' into '&object'.
class CapturedThisPointerRewriter
    : public PseudoObjectRewriter<CapturedThisPointerRewriter> {
public:
  Rewriter &SourceRewriter;
  const llvm::SmallPtrSetImpl<const CXXThisExpr *> &RewrittenExpressions;

  CapturedThisPointerRewriter(
      Rewriter &SourceRewriter,
      const llvm::SmallPtrSetImpl<const CXXThisExpr *> &RewrittenExpressions)
      : SourceRewriter(SourceRewriter),
        RewrittenExpressions(RewrittenExpressions) {}

  void replace(const CXXThisExpr *E, StringRef Text) {
    SourceRewriter.ReplaceText(E->getBeginLoc(), 4, Text);
  }

  bool VisitCXXThisExpr(const CXXThisExpr *E) {
    if (RewrittenExpressions.count(E))
      return true;
    if (!E->isImplicit())
      replace(E, "&object");
    return true;
  }

  bool TraverseUnaryOperator(UnaryOperator *E) {
    if (E->getOpcode() != UO_Deref)
      return RecursiveASTVisitor::TraverseUnaryOperator(E);
    if (const auto *This =
            dyn_cast<CXXThisExpr>(E->getSubExpr()->IgnoreParenImpCasts())) {
      if (!This->isImplicit()) {
        // Remove the '*' as the variable is now a reference.
        SourceRewriter.RemoveText(
            CharSourceRange::getTokenRange(E->getBeginLoc(), E->getBeginLoc()));
        replace(This, "object");
        return true;
      }
    }
    return RecursiveASTVisitor::TraverseUnaryOperator(E);
  }
};

/// Traverses the extracted code and rewrites the uses of 'self' into 'object'.
class CapturedSelfRewriter : public PseudoObjectRewriter<CapturedSelfRewriter> {
public:
  Rewriter &SourceRewriter;
  const ImplicitParamDecl *SelfDecl;

  CapturedSelfRewriter(Rewriter &SourceRewriter,
                       const ImplicitParamDecl *SelfDecl)
      : SourceRewriter(SourceRewriter), SelfDecl(SelfDecl) {
    assert(SelfDecl);
  }

  bool VisitDeclRefExpr(const DeclRefExpr *E) {
    const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
    if (!VD || VD != SelfDecl)
      return true;
    if (E->getBeginLoc().isInvalid())
      return true;
    SourceRewriter.ReplaceText(E->getBeginLoc(), 4, "object");
    return true;
  }

  void insertObjectForImplicitSelf(const Expr *E, SourceLocation Loc,
                                   StringRef Text) {
    const auto *DRE = dyn_cast<DeclRefExpr>(E);
    if (!DRE)
      return;
    const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl());
    if (!VD || VD != SelfDecl || DRE->getBeginLoc().isValid())
      return;
    SourceRewriter.InsertText(Loc, Text);
  }

  bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) {
    insertObjectForImplicitSelf(E->getBase()->IgnoreImpCasts(),
                                E->getBeginLoc(), "object->");
    return true;
  }
};

/// Traverses the extracted code and rewrites the uses of 'self' into the name
/// of the class.
class CapturedClassSelfRewriter
    : public PseudoObjectRewriter<CapturedClassSelfRewriter> {
public:
  Rewriter &SourceRewriter;
  StringRef ClassName;
  const ImplicitParamDecl *SelfDecl;

  CapturedClassSelfRewriter(Rewriter &SourceRewriter, StringRef ClassName,
                            const ImplicitParamDecl *SelfDecl)
      : SourceRewriter(SourceRewriter), ClassName(ClassName),
        SelfDecl(SelfDecl) {

    assert(SelfDecl);
  }

  bool VisitDeclRefExpr(const DeclRefExpr *E) {
    const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
    if (!VD || VD != SelfDecl || E->getBeginLoc().isInvalid())
      return true;
    SourceRewriter.ReplaceText(E->getBeginLoc(), 4, ClassName);
    return true;
  }
};

/// Traverses the extracted code and rewrites the uses of 'super' into
/// 'superObject' or the name of the super class.
class CapturedSuperRewriter
    : public PseudoObjectRewriter<CapturedSuperRewriter> {
public:
  Rewriter &SourceRewriter;
  StringRef ReplacementString;

  CapturedSuperRewriter(Rewriter &SourceRewriter, StringRef ReplacementString)
      : SourceRewriter(SourceRewriter), ReplacementString(ReplacementString) {}

  void rewriteSuper(SourceLocation Loc) {
    SourceRewriter.ReplaceText(Loc, strlen("super"), ReplacementString);
  }

  bool VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *E) {
    if (E->isSuperReceiver())
      rewriteSuper(E->getReceiverLocation());
    return true;
  }

  bool VisitObjCMessageExpr(const ObjCMessageExpr *E) {
    if (E->getSuperLoc().isValid())
      rewriteSuper(E->getSuperLoc());
    return true;
  }
};

struct ExtractionSemicolonPolicy {
  bool IsNeededInExtractedFunction;
  bool IsNeededInOriginalFunction;

  static ExtractionSemicolonPolicy neededInExtractedFunction() {
    return {true, false};
  }
  static ExtractionSemicolonPolicy neededInOriginalFunction() {
    return {false, true};
  }
  static ExtractionSemicolonPolicy neededInBoth() { return {true, true}; }
};

} // end anonymous namespace

ExtractionSemicolonPolicy
computeSemicolonExtractionPolicy(const Stmt *S, SourceRange &ExtractedRange,
                                 const SourceManager &SM,
                                 const LangOptions &LangOpts) {
  if (isa<Expr>(S))
    return ExtractionSemicolonPolicy::neededInExtractedFunction();
  bool NeedsSemi = isSemicolonRequiredAfter(S);
  if (!NeedsSemi)
    return ExtractionSemicolonPolicy::neededInOriginalFunction();
  SourceLocation End = ExtractedRange.getEnd();
  if (isSemicolonAtLocation(End, SM, LangOpts))
    return ExtractionSemicolonPolicy::neededInOriginalFunction();
  SourceLocation NextTokenLoc =
      Lexer::findNextTokenLocationAfterTokenAt(End, SM, LangOpts);
  if (NextTokenLoc.isValid() &&
      isSemicolonAtLocation(NextTokenLoc, SM, LangOpts) &&
      areOnSameLine(NextTokenLoc, End, SM)) {
    ExtractedRange.setEnd(NextTokenLoc);
    return ExtractionSemicolonPolicy::neededInOriginalFunction();
  }
  return ExtractionSemicolonPolicy::neededInBoth();
}

PrintingPolicy getPrintingPolicy(const ASTContext &Context,
                                 const Preprocessor &PP) {
  PrintingPolicy Policy = Context.getPrintingPolicy();
  // Our printing policy is copied over the ASTContext printing policy whenever
  // a diagnostic is emitted, so recompute it.
  Policy.Bool = Context.getLangOpts().Bool;
  // FIXME: This is duplicated with Sema.cpp. When upstreaming this should be
  // cleaned up.
  if (!Policy.Bool) {
    if (const MacroInfo *BoolMacro = PP.getMacroInfo(Context.getBoolName())) {
      Policy.Bool = BoolMacro->isObjectLike() &&
                    BoolMacro->getNumTokens() == 1 &&
                    BoolMacro->getReplacementToken(0).is(tok::kw__Bool);
    }
  }
  return Policy;
}

static QualType getFunctionLikeParentDeclReturnType(const Decl *D) {
  // FIXME: might need to handle ObjC blocks in the future.
  if (const auto *M = dyn_cast<ObjCMethodDecl>(D))
    return M->getReturnType();
  return cast<FunctionDecl>(D)->getReturnType();
}

static const Stmt *getEnclosingDeclBody(const Decl *D) {
  // FIXME: might need to handle ObjC blocks in the future.
  if (const auto *M = dyn_cast<ObjCMethodDecl>(D))
    return M->getBody();
  return cast<FunctionDecl>(D)->getBody();
}

static bool isEnclosingMethodConst(const Decl *D) {
  if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
    return MD->isConst();
  return false;
}

static bool isEnclosingMethodStatic(const Decl *D) {
  if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
    return MD->isStatic();
  return false;
}

static bool isEnclosingMethodOutOfLine(const Decl *D) {
  const auto *MD = dyn_cast<CXXMethodDecl>(D);
  if (!MD)
    return false;
  return MD->isOutOfLine();
}

static void printEnclosingMethodScope(const Decl *D, llvm::raw_ostream &OS,
                                      const PrintingPolicy &PP) {
  const auto *MD = dyn_cast<CXXMethodDecl>(D);
  if (!MD)
    return;
  if (!MD->isOutOfLine() || !MD->getQualifier())
    return;
  MD->getQualifier()->print(OS, PP);
}

static SourceLocation
computeFunctionExtractionLocation(const Decl *D, bool IsMethodExtraction) {
  if (!IsMethodExtraction && isa<CXXMethodDecl>(D)) {
    // Code from methods that defined in class bodies should be extracted to a
    // function defined just before the class.
    while (const auto *RD = dyn_cast<CXXRecordDecl>(D->getLexicalDeclContext()))
      D = RD;
  }
  return D->getBeginLoc();
}

namespace {
enum class MethodDeclarationPlacement { After, Before };

/// \brief Represents an entity captured from the original function that's
/// passed into the new function/method.
struct CapturedVariable {
  const VarDecl *VD;
  const FieldDecl *FD;
  QualType ThisType;
  bool PassByRefOrPtr;
  bool IsRefOrPtrConst;
  bool IsThisSelf = false;
  bool IsThisSuper = false;
  bool TakeAddress = false;
  QualType ParameterType;

  CapturedVariable(const VarDecl *VD, bool PassByRefOrPtr, bool IsRefOrPtrConst)
      : VD(VD), FD(nullptr), PassByRefOrPtr(PassByRefOrPtr),
        IsRefOrPtrConst(IsRefOrPtrConst) {}
  CapturedVariable(const FieldDecl *FD, bool PassByRefOrPtr,
                   bool IsRefOrPtrConst)
      : VD(nullptr), FD(FD), PassByRefOrPtr(PassByRefOrPtr),
        IsRefOrPtrConst(IsRefOrPtrConst) {}
  CapturedVariable(QualType ThisType, bool PassByRefOrPtr, bool IsConst)
      : VD(nullptr), FD(nullptr), ThisType(ThisType),
        PassByRefOrPtr(PassByRefOrPtr), IsRefOrPtrConst(IsConst) {}

  static CapturedVariable getThis(QualType T, bool IsConst) {
    return CapturedVariable(T, /*PassByRefOrPtr=*/true, /*IsConst*/ IsConst);
  }

  static CapturedVariable getSelf(QualType T) {
    auto Result =
        CapturedVariable(T, /*PassByRefOrPtr=*/false, /*IsConst*/ false);
    Result.IsThisSelf = true;
    return Result;
  }

  static CapturedVariable getSuper(QualType T) {
    auto Result =
        CapturedVariable(T, /*PassByRefOrPtr=*/false, /*IsConst*/ false);
    Result.IsThisSuper = true;
    return Result;
  }

  StringRef getName() const {
    return VD ? VD->getName()
              : FD ? FD->getName() : IsThisSuper ? "superObject" : "object";
  }
  StringRef getExpr() const {
    return ThisType.isNull()
               ? getName()
               : IsThisSelf ? "self" : IsThisSuper ? "super.self" : "*this";
  }
  QualType getType() const {
    return VD ? VD->getType() : FD ? FD->getType() : ThisType;
  }
};
} // end anonymous namespace

static std::pair<SourceLocation, MethodDeclarationPlacement>
computeAppropriateExtractionLocationForMethodDeclaration(
    const CXXMethodDecl *D) {
  const CXXRecordDecl *RD = D->getParent();
  // Try to put the new declaration after the last method, or just before the
  // end of the class.
  SourceLocation Loc;
  for (const CXXMethodDecl *M : RD->methods()) {
    if (M->isImplicit())
      continue;
    Loc = M->getEndLoc();
  }
  return Loc.isValid() ? std::make_pair(Loc, MethodDeclarationPlacement::After)
                       : std::make_pair(RD->getEndLoc(),
                                        MethodDeclarationPlacement::Before);
}

static bool isInHeader(SourceLocation Loc, const SourceManager &SM) {
  // Base the header decision on the filename.
  StringRef Extension = llvm::sys::path::extension(SM.getFilename(Loc));
  if (Extension.empty())
    return false;
  return llvm::StringSwitch<bool>(Extension.drop_front())
      .Case("h", true)
      .Case("hpp", true)
      .Case("hh", true)
      .Case("h++", true)
      .Case("hxx", true)
      .Case("inl", true)
      .Case("def", true)
      .Default(false);
}

llvm::Expected<RefactoringResult>
ExtractOperation::performExpressionExtraction(ASTContext &Context,
                                              PrintingPolicy &PP) {
  assert(isExpressionExtraction() && "Not an expression extraction");
  std::vector<RefactoringReplacement> Replacements;
  const Expr *E = cast<Expr>(S);
  QualType VarType = findExpressionLexicalType(FunctionLikeParentDecl, E,
                                               E->getType(), PP, Context);
  StringRef VarName = "extractedExpr";
  auto CreatedSymbol = std::make_unique<RefactoringResultAssociatedSymbol>(
      SymbolName(VarName, /*IsObjectiveCSelector=*/false));

  SourceRange ExtractedTokenRange = CandidateExtractionInfo[0].Range;
  SourceRange ExtractedCharRange = SourceRange(
      ExtractedTokenRange.getBegin(),
      getPreciseTokenLocEnd(ExtractedTokenRange.getEnd(),
                            Context.getSourceManager(), Context.getLangOpts()));

  // Create the variable that will hold the value of the duplicate expression.
  std::string VariableDeclarationString;
  llvm::raw_string_ostream OS(VariableDeclarationString);
  VarType.print(OS, PP, /*PlaceHolder*/ VarName);
  // FIXME: We should hook into the TypePrinter when moving over to llvm.org
  // instead and get the offset from it.
  unsigned NameOffset = StringRef(OS.str()).find(VarName);
  OS << " = ";
  OS << Lexer::getSourceText(CharSourceRange::getCharRange(ExtractedCharRange),
                             Context.getSourceManager(), Context.getLangOpts());
  OS << ";\n";

  // Variable declaration.
  SourceLocation InsertionLoc =
      extract::locationForExtractedVariableDeclaration(
          E, FunctionLikeParentDecl, Context.getSourceManager());
  Replacements.push_back(RefactoringReplacement(
      SourceRange(InsertionLoc, InsertionLoc), OS.str(), CreatedSymbol.get(),
      RefactoringReplacement::AssociatedSymbolLocation(
          ArrayRef(NameOffset), /*IsDeclaration=*/true)));
  // Replace the expression with the variable.
  Replacements.push_back(
      RefactoringReplacement(ExtractedCharRange, VarName, CreatedSymbol.get(),
                             /*NameOffset=*/ArrayRef(unsigned(0))));

  RefactoringResult Result(std::move(Replacements));
  Result.AssociatedSymbols.push_back(std::move(CreatedSymbol));
  return std::move(Result);
}

llvm::Expected<RefactoringResult> ExtractOperation::perform(
    ASTContext &Context, const Preprocessor &ThePreprocessor,
    const RefactoringOptionSet &Options, unsigned SelectedCandidateIndex) {
  std::vector<RefactoringReplacement> Replacements;
  SourceManager &SM = Context.getSourceManager();
  const LangOptions &LangOpts = Context.getLangOpts();
  Rewriter SourceRewriter(SM, LangOpts);
  PrintingPolicy PP = getPrintingPolicy(Context, ThePreprocessor);
  PP.UseStdFunctionForLambda = true;
  PP.SuppressStrongLifetime = true;
  PP.SuppressLifetimeQualifiers = true;
  PP.SuppressUnwrittenScope = true;

  if (isExpressionExtraction())
    return performExpressionExtraction(Context, PP);

  const Stmt *S =
      CandidateExtractionInfo[SelectedCandidateIndex].AnalyzedStatement
          ? CandidateExtractionInfo[SelectedCandidateIndex].AnalyzedStatement
          : this->S;

  const auto *EnclosingObjCMethod =
      dyn_cast<ObjCMethodDecl>(FunctionLikeParentDecl);

  // Find the variables that are captured by the extracted code.
  ExtractedCodeVisitor Visitor(/*SelfDecl=*/EnclosingObjCMethod
                                   ? EnclosingObjCMethod->getSelfDecl()
                                   : nullptr);
  if (ExtractedStmtRange) {
    for (const Stmt *S : *ExtractedStmtRange)
      Visitor.InspectExtractedStmt(const_cast<Stmt *>(S), Context);
  } else
    Visitor.InspectExtractedStmt(const_cast<Stmt *>(S), Context);
  // Compute the return type.
  bool IsExpr = isLexicalExpression(S, ParentStmt);
  QualType ReturnType;
  if (IsExpr || Visitor.HasReturnInExtracted) {
    if (const auto *E = dyn_cast<Expr>(S)) {
      assert(!ExtractedStmtRange);
      ReturnType = findExpressionLexicalType(FunctionLikeParentDecl, E,
                                             E->getType(), PP, Context);
    } else
      ReturnType = getFunctionLikeParentDeclReturnType(FunctionLikeParentDecl);
  } else
    ReturnType = Context.VoidTy;
  // Sort the captured variables.
  std::vector<CapturedVariable> CapturedVariables;
  llvm::SmallPtrSet<const VarDecl *, 4> VariablesDefinedInExtractedCode;
  CapturedVariables.reserve(Visitor.CapturedVariables.size() +
                            Visitor.CapturedFields.size());
  for (const auto &I : Visitor.CapturedVariables) {
    if (I.getSecond().IsDefined) {
      VariablesDefinedInExtractedCode.insert(I.getFirst());
      continue;
    }
    CapturedVariables.push_back(
        CapturedVariable(I.getFirst(), I.getSecond().isPassedByRefOrPtr(),
                         I.getSecond().isRefOrPtrConst()));
  }
  // Take a look at the variables that are defined in the extracted code.
  VariableDefinedInExtractedCodeUseAfterExtractionFinder
      UsedAfterExtractionFinder(ExtractedStmtRange ? *ExtractedStmtRange->Last
                                                   : S,
                                VariablesDefinedInExtractedCode);
  UsedAfterExtractionFinder.TraverseStmt(
      const_cast<Stmt *>(getEnclosingDeclBody(FunctionLikeParentDecl)));
  struct RedeclaredVariable {
    const VarDecl *VD;
    int OrderingPriority;
  };
  llvm::SmallVector<RedeclaredVariable, 4> RedeclaredVariables;
  bool CanUseReturnForVariablesUsedAfterwards =
      !isa<Expr>(S) && ReturnType->isVoidType() &&
      UsedAfterExtractionFinder.VariablesUsedAfterExtraction.size() == 1;
  if (CanUseReturnForVariablesUsedAfterwards) {
    // Avoid using the return value for the variable that's used afterwards as
    // another variable might shadow it at the point of a 'return' that we
    // have to rewrite to 'return var'.
    const VarDecl *VD =
        *UsedAfterExtractionFinder.VariablesUsedAfterExtraction.begin();
    if (ExtractedStmtRange) {
      for (const Stmt *S : *ExtractedStmtRange) {
        if (PossibleShadowingVariableFinder::hasShadowingVar(VD, S)) {
          CanUseReturnForVariablesUsedAfterwards = false;
          break;
        }
      }
    } else
      CanUseReturnForVariablesUsedAfterwards =
          !PossibleShadowingVariableFinder::hasShadowingVar(VD, S);
  }
  if (CanUseReturnForVariablesUsedAfterwards) {
    for (const auto &I : Visitor.CapturedVariables) {
      if (!I.getSecond().IsDefined ||
          !UsedAfterExtractionFinder.VariablesUsedAfterExtraction.count(
              I.getFirst()))
        continue;
      RedeclaredVariables.push_back(
          {I.getFirst(), I.getSecond().DefineOrderingPriority});
      ReturnType = I.getFirst()->getType();
      // Const qualifier can be dropped as we don't want to declare the return
      // type as 'const'.
      if (ReturnType.isConstQualified())
        ReturnType.removeLocalConst();
      break;
    }
    if (Visitor.HasReturnInExtracted) {
      ReturnRewriter ReturnsRewriter(SourceRewriter,
                                     RedeclaredVariables.front().VD->getName());
      if (ExtractedStmtRange) {
        for (const Stmt *S : *ExtractedStmtRange)
          ReturnsRewriter.TraverseStmt(const_cast<Stmt *>(S));
      } else
        ReturnsRewriter.TraverseStmt(const_cast<Stmt *>(S));
    }
  } else {
    for (const auto &I : Visitor.CapturedVariables) {
      if (!I.getSecond().IsDefined ||
          !UsedAfterExtractionFinder.VariablesUsedAfterExtraction.count(
              I.getFirst()))
        continue;
      RedeclaredVariables.push_back(
          {I.getFirst(), I.getSecond().DefineOrderingPriority});
      if (!I.getSecond().IsUsed)
        continue;
      // Pass the variable that's defined in the extracted code but used
      // afterwards as a parameter only when it's actually used in the extracted
      // code.
      CapturedVariables.push_back(CapturedVariable(I.getFirst(),
                                                   /*PassByRefOrPtr=*/true,
                                                   /*IsRefOrPtrConst=*/false));
    }
    std::sort(RedeclaredVariables.begin(), RedeclaredVariables.end(),
              [](const RedeclaredVariable &X, const RedeclaredVariable &Y) {
                return X.OrderingPriority < Y.OrderingPriority;
              });
    DefinedInExtractedCodeDeclStmtRewriter DeclRewriter(
        SourceRewriter, UsedAfterExtractionFinder.VariablesUsedAfterExtraction,
        PP);
    if (ExtractedStmtRange) {
      for (const Stmt *S : *ExtractedStmtRange)
        DeclRewriter.TraverseStmt(const_cast<Stmt *>(S));
    } else
      DeclRewriter.TraverseStmt(const_cast<Stmt *>(S));
  }
  // Capture any fields if necessary.
  bool IsThisConstInCapturedFieldUses = true;
  if (!isMethodExtraction()) {
    for (const auto &I : Visitor.CapturedFields) {
      if (I.getSecond().isPassedByRefOrPtr() &&
          !I.getSecond().isRefOrPtrConst())
        IsThisConstInCapturedFieldUses = false;
      // Private fields that use explicit 'this' should be captured using 'this'
      // even if they might end up being inaccessible in the extracted function.
      if (I.getSecond().IsFieldCapturedWithThis)
        continue;
      CapturedVariables.push_back(
          CapturedVariable(I.getFirst(), I.getSecond().isPassedByRefOrPtr(),
                           I.getSecond().isRefOrPtrConst()));
    }
  }
  std::sort(CapturedVariables.begin(), CapturedVariables.end(),
            [](const CapturedVariable &X, const CapturedVariable &Y) {
              return X.getName() < Y.getName();
            });
  // 'This'/'self' should be passed-in first.
  if (!isMethodExtraction() && Visitor.CaptureThis) {
    CapturedVariables.insert(
        CapturedVariables.begin(),
        CapturedVariable::getThis(
            Visitor.ThisRecordType,
            IsThisConstInCapturedFieldUses &&
                Visitor.IsThisConstForNonCapturedFieldUses));
    CapturedThisReferenceRewriter ThisRewriter(SourceRewriter);
    if (ExtractedStmtRange) {
      for (const Stmt *S : *ExtractedStmtRange)
        ThisRewriter.TraverseStmt(const_cast<Stmt *>(S));
    } else
      ThisRewriter.TraverseStmt(const_cast<Stmt *>(S));
    CapturedThisPointerRewriter PtrThisRewriter(
        SourceRewriter, ThisRewriter.RewrittenExpressions);
    if (ExtractedStmtRange) {
      for (const Stmt *S : *ExtractedStmtRange)
        PtrThisRewriter.TraverseStmt(const_cast<Stmt *>(S));
    } else
      PtrThisRewriter.TraverseStmt(const_cast<Stmt *>(S));
  } else if (!isMethodExtraction() && Visitor.CaptureSelf &&
             EnclosingObjCMethod) {
    if (EnclosingObjCMethod->isInstanceMethod()) {
      // Instance methods rewrite 'self' into an 'object' parameter.
      CapturedVariables.insert(CapturedVariables.begin(),
                               CapturedVariable::getSelf(Visitor.SelfType));
      CapturedSelfRewriter SelfRewriter(SourceRewriter,
                                        EnclosingObjCMethod->getSelfDecl());
      if (ExtractedStmtRange) {
        for (const Stmt *S : *ExtractedStmtRange)
          SelfRewriter.TraverseStmt(const_cast<Stmt *>(S));
      } else
        SelfRewriter.TraverseStmt(const_cast<Stmt *>(S));
    } else {
      // Class methods rewrite 'self' into the class name and don't pass 'self'
      // as a parameter.
      CapturedClassSelfRewriter SelfRewriter(
          SourceRewriter, EnclosingObjCMethod->getClassInterface()->getName(),
          EnclosingObjCMethod->getSelfDecl());
      if (ExtractedStmtRange) {
        for (const Stmt *S : *ExtractedStmtRange)
          SelfRewriter.TraverseStmt(const_cast<Stmt *>(S));
      } else
        SelfRewriter.TraverseStmt(const_cast<Stmt *>(S));
    }
  }
  if (!isMethodExtraction() && Visitor.CaptureSuper && EnclosingObjCMethod) {
    if (EnclosingObjCMethod->isInstanceMethod())
      // Instance methods rewrite 'super' into an 'superObject' parameter.
      CapturedVariables.insert(Visitor.CaptureSelf
                                   ? CapturedVariables.begin() + 1
                                   : CapturedVariables.begin(),
                               CapturedVariable::getSuper(Visitor.SuperType));
    CapturedSuperRewriter SuperRewriter(
        SourceRewriter, EnclosingObjCMethod->isInstanceMethod()
                            ? "superObject"
                            : EnclosingObjCMethod->getClassInterface()
                                  ->getSuperClass()
                                  ->getName());
    if (ExtractedStmtRange) {
      for (const Stmt *S : *ExtractedStmtRange)
        SuperRewriter.TraverseStmt(const_cast<Stmt *>(S));
    } else
      SuperRewriter.TraverseStmt(const_cast<Stmt *>(S));
  }

  // Compute the parameter types.
  for (auto &Var : CapturedVariables) {
    QualType T = Var.getType();

    // Array types are passed into the extracted function using a pointer.
    if (const auto *AT = Context.getAsArrayType(T))
      T = Context.getPointerType(AT->getElementType());

    // Captured records and other mutated variables are passed into the
    // extracted function either using a reference (C++) or a pointer.
    if ((T->isRecordType() || Var.PassByRefOrPtr) && !T->isReferenceType()) {
      // Add a 'const' qualifier to the record when it's not mutated in the
      // extracted code or when we are taking the address of the captured
      // variable for just a 'const' use.
      if (!Var.PassByRefOrPtr || Var.IsRefOrPtrConst)
        T.addConst();

      if (LangOpts.CPlusPlus)
        T = Context.getLValueReferenceType(T);
      else {
        T = Context.getPointerType(T);
        CapturedVariableCaptureByPointerRewriter UseRewriter(Var.VD,
                                                             SourceRewriter);
        if (ExtractedStmtRange) {
          for (const Stmt *S : *ExtractedStmtRange)
            UseRewriter.TraverseStmt(const_cast<Stmt *>(S));
        } else
          UseRewriter.TraverseStmt(const_cast<Stmt *>(S));
        Var.TakeAddress = true;
      }
    }
    // Const qualifier can be dropped as we don't want to declare the parameter
    // as 'const'.
    else if (T.isLocalConstQualified())
      T.removeLocalConst();

    Var.ParameterType = T;
  }

  // TODO: Choose a better name if there are collisions.
  StringRef ExtractedName = "extracted";
  llvm::SmallVector<StringRef, 4> ExtractedNamePieces;
  ExtractedNamePieces.push_back(ExtractedName);
  if (isMethodExtraction() && EnclosingObjCMethod &&
      !CapturedVariables.empty()) {
    for (const auto &Var : ArrayRef(CapturedVariables).drop_front())
      ExtractedNamePieces.push_back(Var.getName());
  }
  std::unique_ptr<RefactoringResultAssociatedSymbol> CreatedSymbol =
      std::make_unique<RefactoringResultAssociatedSymbol>(
          SymbolName(ExtractedNamePieces));

  SourceLocation FunctionExtractionLoc = computeFunctionExtractionLocation(
      FunctionLikeParentDecl, isMethodExtraction());
  FunctionExtractionLoc =
      getLocationOfPrecedingComment(FunctionExtractionLoc, SM, LangOpts);

  // Create the replacement that contains the new function.
  auto PrintFunctionHeader =
      [&](llvm::raw_string_ostream &OS,
          bool IsDefinition =
              true) -> RefactoringReplacement::AssociatedSymbolLocation {
    if (isMethodExtraction() && EnclosingObjCMethod) {
      OS << (EnclosingObjCMethod->isClassMethod() ? '+' : '-') << " (";
      ReturnType.print(OS, PP);
      OS << ')';
      llvm::SmallVector<unsigned, 4> NameOffsets;
      NameOffsets.push_back(OS.str().size());
      OS << ExtractedName;
      bool IsFirst = true;
      for (const auto &Var : CapturedVariables) {
        if (!IsFirst) {
          OS << ' ';
          NameOffsets.push_back(OS.str().size());
          OS << Var.getName();
        }
        IsFirst = false;
        OS << ":(";
        Var.ParameterType.print(OS, PP);
        OS << ')' << Var.getName();
      }
      return RefactoringReplacement::AssociatedSymbolLocation(
          NameOffsets, /*IsDeclaration=*/true);
    }
    auto *FD = dyn_cast<FunctionDecl>(FunctionLikeParentDecl);
    if (isMethodExtraction() && IsDefinition &&
        !FD->getDescribedFunctionTemplate()) {
      // Print the class template parameter lists for an out-of-line method.
      for (unsigned I = 0,
                    NumTemplateParams = FD->getNumTemplateParameterLists();
           I < NumTemplateParams; ++I) {
        FD->getTemplateParameterList(I)->print(OS, Context, PP);
        OS << "\n";
      }
    }
    if (isMethodExtraction() && isEnclosingMethodStatic(FunctionLikeParentDecl))
      OS << "static ";
    else if (!isMethodExtraction())
      OS << (isInHeader(FunctionExtractionLoc, SM) ? "inline " : "static ");
    std::string QualifiedName;
    llvm::raw_string_ostream NameOS(QualifiedName);
    if (isMethodExtraction() && IsDefinition)
      printEnclosingMethodScope(FunctionLikeParentDecl, NameOS, PP);
    NameOS << ExtractedName;
    NameOS << '(';
    bool IsFirst = true;
    for (const auto &Var : CapturedVariables) {
      if (!IsFirst)
        NameOS << ", ";
      IsFirst = false;
      Var.ParameterType.print(NameOS, PP, /*PlaceHolder=*/Var.getName());
    }
    NameOS << ')';
    ReturnType.print(OS, PP, NameOS.str());
    unsigned NameOffset = OS.str().find(std::string(ExtractedName));
    if (isMethodExtraction() && isEnclosingMethodConst(FunctionLikeParentDecl))
      OS << " const";
    return RefactoringReplacement::AssociatedSymbolLocation(
        NameOffset, /*IsDeclaration=*/true);
    ;
  };

  if (isMethodExtraction() &&
      isEnclosingMethodOutOfLine(FunctionLikeParentDecl)) {
    // The location of the declaration should be either before the original
    // declararation, or, if this method has not declaration, somewhere
    // appropriate in the class.
    MethodDeclarationPlacement Placement;
    SourceLocation DeclarationLoc;
    if (FunctionLikeParentDecl->getCanonicalDecl() != FunctionLikeParentDecl) {
      DeclarationLoc = computeFunctionExtractionLocation(
          FunctionLikeParentDecl->getCanonicalDecl(), isMethodExtraction());
      Placement = MethodDeclarationPlacement::Before;
    } else {
      auto LocAndPlacement =
          computeAppropriateExtractionLocationForMethodDeclaration(
              cast<CXXMethodDecl>(FunctionLikeParentDecl));
      DeclarationLoc = LocAndPlacement.first;
      Placement = LocAndPlacement.second;
    }
    if (Placement == MethodDeclarationPlacement::Before)
      DeclarationLoc =
          getLocationOfPrecedingComment(DeclarationLoc, SM, LangOpts);
    else
      DeclarationLoc = getLastLineLocationUnlessItHasOtherTokens(
          getPreciseTokenLocEnd(DeclarationLoc, SM, LangOpts), SM, LangOpts);
    // Add a replacement for the method declaration if necessary.
    std::string DeclarationString;
    llvm::raw_string_ostream OS(DeclarationString);
    if (Placement == MethodDeclarationPlacement::After)
      OS << "\n\n";
    RefactoringReplacement::AssociatedSymbolLocation SymbolLoc =
        PrintFunctionHeader(OS, /*IsDefinition=*/false);
    OS << ";\n";
    if (Placement == MethodDeclarationPlacement::Before)
      OS << "\n";
    Replacements.push_back(RefactoringReplacement(
        SourceRange(DeclarationLoc, DeclarationLoc), std::move(OS.str()),
        CreatedSymbol.get(), SymbolLoc));
  }
  std::string ExtractedCode;
  llvm::raw_string_ostream ExtractedOS(ExtractedCode);
  RefactoringReplacement::AssociatedSymbolLocation SymbolLoc =
      PrintFunctionHeader(ExtractedOS);
  ExtractedOS << " {\n";
  if (IsExpr && !ReturnType->isVoidType())
    ExtractedOS << "return ";
  SourceRange ExtractedTokenRange =
      CandidateExtractionInfo[SelectedCandidateIndex].Range;
  auto Semicolons = computeSemicolonExtractionPolicy(
      ExtractedStmtRange ? *(ExtractedStmtRange->Last) : S, ExtractedTokenRange,
      SM, LangOpts);
  bool ShouldCopyBlock = false;
  if (IsExpr && !LangOpts.ObjCAutoRefCount &&
      ReturnType->isBlockPointerType()) {
    // We can't return local blocks directly without ARC; they should be copied.
    // FIXME: This is overly pessimistic, as we only need the copy for local
    // blocks.
    ExtractedOS << "[(";
    ShouldCopyBlock = true;
  }
  ExtractedOS << SourceRewriter.getRewrittenText(ExtractedTokenRange);
  if (ShouldCopyBlock)
    ExtractedOS << ") copy]";
  if (Semicolons.IsNeededInExtractedFunction)
    ExtractedOS << ';';
  if (CanUseReturnForVariablesUsedAfterwards)
    ExtractedOS << "\nreturn " << RedeclaredVariables.front().VD->getName()
                << ";";
  ExtractedOS << "\n}\n\n";
  Replacements.push_back(RefactoringReplacement(
      SourceRange(FunctionExtractionLoc, FunctionExtractionLoc),
      std::move(ExtractedOS.str()), CreatedSymbol.get(), SymbolLoc));

  // Create a replacements that removes the extracted code in favor of the
  // function call.
  std::string InsertedCode;
  llvm::raw_string_ostream InsertedOS(InsertedCode);
  // We might have to declare variables that were declared in the extracted code
  // but still used afterwards.
  if (CanUseReturnForVariablesUsedAfterwards) {
    const auto &Var = RedeclaredVariables.front();
    Var.VD->getType().print(InsertedOS, PP);
    InsertedOS << ' ' << Var.VD->getName() << " = ";
  } else {
    for (const auto &Var : RedeclaredVariables) {
      Var.VD->getType().print(InsertedOS, PP);
      InsertedOS << ' ' << Var.VD->getName() << ";\n";
    }
  }
  InsertedOS << CandidateExtractionInfo[SelectedCandidateIndex].PreInsertedText;
  llvm::SmallVector<unsigned, 4> NameOffsets;
  if (isMethodExtraction() && EnclosingObjCMethod) {
    InsertedOS << "[self ";
    NameOffsets.push_back(InsertedOS.str().size());
    InsertedOS << ExtractedName;
    bool IsFirst = true;
    for (const auto &Var : CapturedVariables) {
      if (!IsFirst) {
        InsertedOS << ' ';
        NameOffsets.push_back(InsertedOS.str().size());
        InsertedOS << Var.getName();
      }
      IsFirst = false;
      InsertedOS << ':';
      if (Var.TakeAddress)
        InsertedOS << '&';
      InsertedOS << Var.getExpr();
    }
    InsertedOS << ']';
  } else {
    NameOffsets.push_back(InsertedOS.str().size());
    InsertedOS << ExtractedName << '(';
    bool IsFirst = true;
    for (const auto &Var : CapturedVariables) {
      if (!IsFirst)
        InsertedOS << ", ";
      IsFirst = false;
      if (Var.TakeAddress)
        InsertedOS << '&';
      InsertedOS << Var.getExpr();
    }
    InsertedOS << ')';
  }
  if (Semicolons.IsNeededInOriginalFunction)
    InsertedOS << ';';
  SourceRange ExtractedCharRange = SourceRange(
      ExtractedTokenRange.getBegin(),
      getPreciseTokenLocEnd(ExtractedTokenRange.getEnd(), SM, LangOpts));
  Replacements.push_back(RefactoringReplacement(
      ExtractedCharRange, std::move(InsertedOS.str()), CreatedSymbol.get(),
      ArrayRef(NameOffsets)));

  RefactoringResult Result(std::move(Replacements));
  Result.AssociatedSymbols.push_back(std::move(CreatedSymbol));
  return std::move(Result);
}