Skip to content

Commit fcf95ea

Browse files
author
Nathan Hawes
authored
Merge pull request #30187 from nathawes/indentation
[SourceKit/CodeFormat] Re-work and improve the indentation implementation
2 parents 2e13428 + 2a93296 commit fcf95ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3693
-886
lines changed

include/swift/AST/Expr.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,14 @@ class alignas(8) Expr {
566566
/// typically this will have an ErrorType.
567567
class ErrorExpr : public Expr {
568568
SourceRange Range;
569+
Expr *OriginalExpr;
569570
public:
570-
ErrorExpr(SourceRange Range, Type Ty = Type())
571-
: Expr(ExprKind::Error, /*Implicit=*/true, Ty), Range(Range) {}
571+
ErrorExpr(SourceRange Range, Type Ty = Type(), Expr *OriginalExpr = nullptr)
572+
: Expr(ExprKind::Error, /*Implicit=*/true, Ty), Range(Range),
573+
OriginalExpr(OriginalExpr) {}
572574

573575
SourceRange getSourceRange() const { return Range; }
576+
Expr *getOriginalExpr() const { return OriginalExpr; }
574577

575578
static bool classof(const Expr *E) {
576579
return E->getKind() == ExprKind::Error;

include/swift/AST/Stmt.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class alignas(8) PoundAvailableInfo final :
321321
friend TrailingObjects;
322322

323323
SourceLoc PoundLoc;
324+
SourceLoc LParenLoc;
324325
SourceLoc RParenLoc;
325326

326327
// The number of queries tail allocated after this object.
@@ -338,17 +339,18 @@ class alignas(8) PoundAvailableInfo final :
338339
/// This is filled in by Sema.
339340
VersionRange VariantAvailableRange;
340341

341-
PoundAvailableInfo(SourceLoc PoundLoc, ArrayRef<AvailabilitySpec *> queries,
342-
SourceLoc RParenLoc)
343-
: PoundLoc(PoundLoc), RParenLoc(RParenLoc), NumQueries(queries.size()),
344-
AvailableRange(VersionRange::empty()),
342+
PoundAvailableInfo(SourceLoc PoundLoc, SourceLoc LParenLoc,
343+
ArrayRef<AvailabilitySpec *> queries, SourceLoc RParenLoc)
344+
: PoundLoc(PoundLoc), LParenLoc(LParenLoc), RParenLoc(RParenLoc),
345+
NumQueries(queries.size()), AvailableRange(VersionRange::empty()),
345346
VariantAvailableRange(VersionRange::empty()) {
346347
std::uninitialized_copy(queries.begin(), queries.end(),
347348
getTrailingObjects<AvailabilitySpec *>());
348349
}
349350

350351
public:
351352
static PoundAvailableInfo *create(ASTContext &ctx, SourceLoc PoundLoc,
353+
SourceLoc LParenLoc,
352354
ArrayRef<AvailabilitySpec *> queries,
353355
SourceLoc RParenLoc);
354356

@@ -357,6 +359,9 @@ class alignas(8) PoundAvailableInfo final :
357359
NumQueries);
358360
}
359361

362+
SourceLoc getLParenLoc() const { return LParenLoc; }
363+
SourceLoc getRParenLoc() const { return RParenLoc; }
364+
360365
SourceLoc getStartLoc() const { return PoundLoc; }
361366
SourceLoc getEndLoc() const;
362367
SourceLoc getLoc() const { return PoundLoc; }
@@ -771,6 +776,7 @@ class WhileStmt : public LabeledConditionalStmt {
771776

772777
SourceLoc getStartLoc() const { return getLabelLocOrKeywordLoc(WhileLoc); }
773778
SourceLoc getEndLoc() const { return Body->getEndLoc(); }
779+
SourceLoc getWhileLoc() const { return WhileLoc; }
774780

775781
Stmt *getBody() const { return Body; }
776782
void setBody(Stmt *s) { Body = s; }
@@ -792,9 +798,10 @@ class RepeatWhileStmt : public LabeledStmt {
792798
getDefaultImplicitFlag(implicit, RepeatLoc),
793799
LabelInfo),
794800
RepeatLoc(RepeatLoc), WhileLoc(WhileLoc), Body(Body), Cond(Cond) {}
795-
801+
796802
SourceLoc getStartLoc() const { return getLabelLocOrKeywordLoc(RepeatLoc); }
797803
SourceLoc getEndLoc() const;
804+
SourceLoc getRepeatLoc() const { return RepeatLoc; }
798805

799806
Stmt *getBody() const { return Body; }
800807
void setBody(Stmt *s) { Body = s; }
@@ -819,6 +826,7 @@ class ForEachStmt : public LabeledStmt {
819826
Pattern *Pat;
820827
SourceLoc InLoc;
821828
Expr *Sequence;
829+
SourceLoc WhereLoc;
822830
Expr *WhereExpr = nullptr;
823831
BraceStmt *Body;
824832

@@ -831,12 +839,12 @@ class ForEachStmt : public LabeledStmt {
831839

832840
public:
833841
ForEachStmt(LabeledStmtInfo LabelInfo, SourceLoc ForLoc, Pattern *Pat,
834-
SourceLoc InLoc, Expr *Sequence, Expr *WhereExpr, BraceStmt *Body,
835-
Optional<bool> implicit = None)
842+
SourceLoc InLoc, Expr *Sequence, SourceLoc WhereLoc,
843+
Expr *WhereExpr, BraceStmt *Body, Optional<bool> implicit = None)
836844
: LabeledStmt(StmtKind::ForEach, getDefaultImplicitFlag(implicit, ForLoc),
837845
LabelInfo),
838846
ForLoc(ForLoc), Pat(nullptr), InLoc(InLoc), Sequence(Sequence),
839-
WhereExpr(WhereExpr), Body(Body) {
847+
WhereLoc(WhereLoc), WhereExpr(WhereExpr), Body(Body) {
840848
setPattern(Pat);
841849
}
842850

@@ -864,6 +872,9 @@ class ForEachStmt : public LabeledStmt {
864872

865873
/// getInLoc - Retrieve the location of the 'in' keyword.
866874
SourceLoc getInLoc() const { return InLoc; }
875+
876+
/// getWhereLoc - Retrieve the location of the 'where' keyword.
877+
SourceLoc getWhereLoc() const { return WhereLoc; }
867878

868879
/// getPattern - Retrieve the pattern describing the iteration variables.
869880
/// These variables will only be visible within the body of the loop.

lib/AST/ASTWalker.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,31 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
327327
return false;
328328
}
329329

330+
bool visitParamDecl(ParamDecl *P) {
331+
// Don't walk into the type if the decl is implicit, or if the type is
332+
// implicit.
333+
if (!P->isImplicit()) {
334+
if (auto *repr = P->getTypeRepr()) {
335+
if (doIt(repr)) {
336+
return true;
337+
}
338+
}
339+
}
340+
if (auto *E = P->getStructuralDefaultExpr()) {
341+
auto res = doIt(E);
342+
if (!res) return true;
343+
P->setDefaultExpr(res, /*isTypeChecked*/ (bool)res->getType());
344+
}
345+
346+
if (!Walker.shouldWalkAccessorsTheOldWay()) {
347+
for (auto *AD : P->getAllAccessors())
348+
if (doIt(AD))
349+
return true;
350+
}
351+
352+
return false;
353+
}
354+
330355
bool visitSubscriptDecl(SubscriptDecl *SD) {
331356
bool WalkGenerics = visitGenericParamListIfNeeded(SD);
332357

@@ -1142,22 +1167,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
11421167
// Walk each parameter's decl and typeloc and default value.
11431168
if (doIt(P))
11441169
return true;
1145-
1146-
// Don't walk into the type if the decl is implicit, or if the type is
1147-
// implicit.
1148-
if (!P->isImplicit()) {
1149-
if (auto *repr = P->getTypeRepr()) {
1150-
if (doIt(repr)) {
1151-
return true;
1152-
}
1153-
}
1154-
}
1155-
1156-
if (auto *E = P->getStructuralDefaultExpr()) {
1157-
auto res = doIt(E);
1158-
if (!res) return true;
1159-
P->setDefaultExpr(res, /*isTypeChecked*/ (bool)res->getType());
1160-
}
11611170
}
11621171

11631172
return Walker.walkToParameterListPost(PL);

lib/AST/Stmt.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,21 @@ bool CatchStmt::isSyntacticallyExhaustive() const {
305305

306306
PoundAvailableInfo *PoundAvailableInfo::create(ASTContext &ctx,
307307
SourceLoc PoundLoc,
308+
SourceLoc LParenLoc,
308309
ArrayRef<AvailabilitySpec *> queries,
309310
SourceLoc RParenLoc) {
310311
unsigned size = totalSizeToAlloc<AvailabilitySpec *>(queries.size());
311312
void *Buffer = ctx.Allocate(size, alignof(PoundAvailableInfo));
312-
return ::new (Buffer) PoundAvailableInfo(PoundLoc, queries, RParenLoc);
313+
return ::new (Buffer) PoundAvailableInfo(PoundLoc, LParenLoc, queries,
314+
RParenLoc);
313315
}
314316

315317
SourceLoc PoundAvailableInfo::getEndLoc() const {
316318
if (RParenLoc.isInvalid()) {
317319
if (NumQueries == 0) {
318-
return PoundLoc;
320+
if (LParenLoc.isInvalid())
321+
return PoundLoc;
322+
return LParenLoc;
319323
}
320324
return getQueries()[NumQueries - 1]->getSourceRange().End;
321325
}

0 commit comments

Comments
 (0)