@@ -1809,38 +1809,27 @@ class FormatWalker : public ASTWalker {
1809
1809
1810
1810
auto *CS = dyn_cast<CaseStmt>(S);
1811
1811
if (CS && CS->getParentKind () == CaseParentKind::Switch) {
1812
- if (TrailingTarget && !TrailingTarget->isEmpty ())
1813
- return None;
1814
-
1815
1812
SourceLoc CaseLoc = CS->getLoc ();
1816
1813
if (!SM.isBeforeInBuffer (CaseLoc, TargetLocation))
1817
1814
return None;
1818
1815
1819
1816
SourceRange LabelItemsRange = CS->getLabelItemsRange ();
1820
1817
SourceLoc ColonLoc = getLocIfKind (SM, LabelItemsRange.End , tok::colon);
1821
1818
1822
- if (isTargetContext (CaseLoc, ColonLoc)) {
1823
- ListAligner Aligner (SM, TargetLocation, CaseLoc, CaseLoc, ColonLoc);
1824
- for (auto &Elem: CS->getCaseLabelItems ()) {
1825
- SourceRange ElemRange = Elem.getSourceRange ();
1826
- Aligner.updateAlignment (ElemRange, CS);
1827
- if (isTargetContext (ElemRange)) {
1828
- Aligner.setAlignmentIfNeeded (CtxOverride);
1829
- return IndentContext {
1830
- ElemRange.Start ,
1831
- !OutdentChecker::hasOutdent (SM, ElemRange, CS)
1832
- };
1833
- }
1834
- }
1835
- return Aligner.getContextAndSetAlignment (CtxOverride);
1836
- }
1837
- if (ColonLoc.isValid () && isTargetContext (ColonLoc, SourceLoc ())) {
1819
+ if (auto Ctx = getIndentContextFromCaseItems (CS, /* CloseLoc=*/ ColonLoc))
1820
+ return Ctx;
1821
+
1822
+ if (ColonLoc.isValid () && isTargetContext (ColonLoc, SourceLoc ()) &&
1823
+ (!TrailingTarget || TrailingTarget->isEmpty ())) {
1838
1824
SourceRange ColonToEnd = SourceRange (ColonLoc, CS->getEndLoc ());
1839
1825
return IndentContext {
1840
1826
CaseLoc,
1841
1827
!OutdentChecker::hasOutdent (SM, ColonToEnd, CS)
1842
1828
};
1843
1829
}
1830
+
1831
+ if (TrailingTarget)
1832
+ return None;
1844
1833
return IndentContext {CaseLoc, false };
1845
1834
}
1846
1835
@@ -1858,13 +1847,21 @@ class FormatWalker : public ASTWalker {
1858
1847
}
1859
1848
1860
1849
if (CS && CS->getParentKind () == CaseParentKind::DoCatch) {
1861
- if (auto *BS = dyn_cast<BraceStmt>(CS->getBody ())) {
1862
- if (auto Ctx = getIndentContextFrom (BS, CS->getStartLoc ()))
1863
- return Ctx;
1864
- }
1850
+ SourceLoc CatchLoc = CS->getLoc ();
1851
+ SourceLoc L;
1852
+
1853
+ auto *BS = dyn_cast<BraceStmt>(CS->getBody ());
1854
+ if (BS) L = getLocIfKind (SM, BS->getLBraceLoc (), tok::l_brace);
1855
+
1856
+ if (auto Ctx = getIndentContextFromCaseItems (CS, /* CloseLoc=*/ L))
1857
+ return Ctx;
1858
+
1859
+ if (auto Ctx = getIndentContextFrom (BS, CS->getStartLoc ()))
1860
+ return Ctx;
1861
+
1865
1862
if (TrailingTarget)
1866
1863
return None;
1867
- return IndentContext {CS-> getStartLoc (), true };
1864
+ return IndentContext {CatchLoc, false };
1868
1865
}
1869
1866
1870
1867
if (auto *IS = dyn_cast<IfStmt>(S)) {
@@ -2086,6 +2083,33 @@ class FormatWalker : public ASTWalker {
2086
2083
return Bounds;
2087
2084
}
2088
2085
2086
+ Optional<IndentContext>
2087
+ getIndentContextFromCaseItems (CaseStmt *CS, SourceLoc CloseLoc) {
2088
+ SourceLoc IntroducerLoc = CS->getLoc ();
2089
+ if (!isTargetContext (IntroducerLoc, CloseLoc))
2090
+ return None;
2091
+
2092
+ ListAligner Aligner (SM, TargetLocation, IntroducerLoc, IntroducerLoc,
2093
+ CloseLoc);
2094
+ for (auto &Elem: CS->getCaseLabelItems ()) {
2095
+ // Skip the implicit 'error' pattern for empty catch CaseStmts.
2096
+ if ((!Elem.getPattern () || Elem.getPattern ()->isImplicit ()) &&
2097
+ Elem.getWhereLoc ().isInvalid ())
2098
+ continue ;
2099
+
2100
+ SourceRange ElemRange = Elem.getSourceRange ();
2101
+ Aligner.updateAlignment (ElemRange, CS);
2102
+ if (isTargetContext (ElemRange)) {
2103
+ Aligner.setAlignmentIfNeeded (CtxOverride);
2104
+ return IndentContext {
2105
+ ElemRange.Start ,
2106
+ !OutdentChecker::hasOutdent (SM, ElemRange, CS)
2107
+ };
2108
+ }
2109
+ }
2110
+ return Aligner.getContextAndSetAlignment (CtxOverride);
2111
+ }
2112
+
2089
2113
#pragma mark Expression indent contexts
2090
2114
2091
2115
Optional<IndentContext>
0 commit comments