Skip to content

Commit bb08e60

Browse files
authored
Merge pull request #17282 from brentdax/literally-unreadable
2 parents 5132fc7 + 6323a51 commit bb08e60

File tree

6 files changed

+30
-84
lines changed

6 files changed

+30
-84
lines changed

include/swift/AST/Expr.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,13 @@ class IntegerLiteralExpr : public NumberLiteralExpr {
836836
Val, DigitsLoc, Implicit)
837837
{}
838838

839+
/// Returns a new integer literal expression with the given value.
840+
/// \p C The AST context.
841+
/// \p value The integer value.
842+
/// \return An implicit integer literal expression which evaluates to the value.
843+
static IntegerLiteralExpr *
844+
createFromUnsigned(ASTContext &C, unsigned value);
845+
839846
APInt getValue() const;
840847
static APInt getValue(StringRef Text, unsigned BitWidth, bool Negative);
841848

lib/AST/Expr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,13 @@ LiteralExpr *LiteralExpr::shallowClone(
10051005
return Result;
10061006
}
10071007

1008+
IntegerLiteralExpr * IntegerLiteralExpr::createFromUnsigned(ASTContext &C, unsigned value) {
1009+
llvm::SmallString<8> Scratch;
1010+
llvm::APInt(sizeof(unsigned)*8, value).toString(Scratch, 10, /*signed*/ false);
1011+
auto Text = C.AllocateCopy(StringRef(Scratch));
1012+
return new (C) IntegerLiteralExpr(Text, SourceLoc(), /*implicit*/ true);
1013+
}
1014+
10081015
/// A wrapper around LLVM::getAsInteger that can be used on Swift interger
10091016
/// literals. It avoids misinterpreting decimal numbers prefixed with 0 as
10101017
/// octal numbers.

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,20 +194,6 @@ enumElementPayloadSubpattern(EnumElementDecl *enumElementDecl,
194194
return pat;
195195
}
196196

197-
/// Returns a new integer literal expression with the given value.
198-
/// \p C The AST context.
199-
/// \p value The integer value.
200-
/// \return The integer literal expression.
201-
static Expr *integerLiteralExpr(ASTContext &C, int64_t value) {
202-
llvm::SmallString<8> integerVal;
203-
APInt(32, value).toString(integerVal, 10, /*signed*/ false);
204-
auto integerStr = C.AllocateCopy(integerVal);
205-
auto integerExpr = new (C) IntegerLiteralExpr(
206-
StringRef(integerStr.data(), integerStr.size()), SourceLoc(),
207-
/*implicit*/ true);
208-
return integerExpr;
209-
}
210-
211197
/// Create AST statements which convert from an enum to an Int with a switch.
212198
/// \p stmts The generated statements are appended to this vector.
213199
/// \p parentDC Either an extension or the enum itself.
@@ -255,7 +241,7 @@ static DeclRefExpr *convertEnumToIndex(SmallVectorImpl<ASTNode> &stmts,
255241
auto labelItem = CaseLabelItem(pat);
256242

257243
// generate: indexVar = <index>
258-
auto indexExpr = integerLiteralExpr(C, index++);
244+
auto indexExpr = IntegerLiteralExpr::createFromUnsigned(C, index++);
259245
auto indexRef = new (C) DeclRefExpr(indexVar, DeclNameLoc(),
260246
/*implicit*/true);
261247
auto assignExpr = new (C) AssignExpr(indexRef, SourceLoc(),
@@ -932,7 +918,7 @@ deriveBodyHashable_enum_hasAssociatedValues_hashInto(
932918

933919
{
934920
// Generate: hasher.combine(<ordinal>)
935-
auto ordinalExpr = integerLiteralExpr(C, index++);
921+
auto ordinalExpr = IntegerLiteralExpr::createFromUnsigned(C, index++);
936922
auto combineExpr = createHasherCombineCall(C, hasherParam, ordinalExpr);
937923
statements.emplace_back(ASTNode(combineExpr));
938924
}

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ deriveBodyRawRepresentable_init(AbstractFunctionDecl *initDecl) {
217217
// In case of a string enum we are calling the _findStringSwitchCase
218218
// function from the library and switching on the returned Int value.
219219
stringExprs.push_back(litExpr);
220-
llvm::SmallString<16> IdxAsStringBuffer;
221-
APInt(64, Idx).toStringUnsigned(IdxAsStringBuffer);
222-
StringRef IndexAsString(C.AllocateCopy(IdxAsStringBuffer.str()));
223-
litExpr = new (C) IntegerLiteralExpr(IndexAsString, SourceLoc());
220+
litExpr = IntegerLiteralExpr::createFromUnsigned(C, Idx);
224221
}
225222
auto litPat = new (C) ExprPattern(litExpr, /*isResolved*/ true,
226223
nullptr, nullptr);

lib/Sema/PCMacro.cpp

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -532,26 +532,10 @@ class Instrumenter : InstrumenterBase {
532532
std::pair<unsigned, unsigned> EndLC = Context.SourceMgr.getLineAndColumn(
533533
Lexer::getLocForEndOfToken(Context.SourceMgr, SR.End));
534534

535-
const size_t buf_size = 8;
536-
537-
char *start_line_buf = (char *)Context.Allocate(buf_size, 1);
538-
char *end_line_buf = (char *)Context.Allocate(buf_size, 1);
539-
char *start_column_buf = (char *)Context.Allocate(buf_size, 1);
540-
char *end_column_buf = (char *)Context.Allocate(buf_size, 1);
541-
542-
::snprintf(start_line_buf, buf_size, "%u", StartLC.first);
543-
::snprintf(start_column_buf, buf_size, "%u", StartLC.second);
544-
::snprintf(end_line_buf, buf_size, "%u", EndLC.first);
545-
::snprintf(end_column_buf, buf_size, "%u", EndLC.second);
546-
547-
Expr *StartLine =
548-
new (Context) IntegerLiteralExpr(start_line_buf, SR.End, true);
549-
Expr *EndLine =
550-
new (Context) IntegerLiteralExpr(end_line_buf, SR.End, true);
551-
Expr *StartColumn =
552-
new (Context) IntegerLiteralExpr(start_column_buf, SR.End, true);
553-
Expr *EndColumn =
554-
new (Context) IntegerLiteralExpr(end_column_buf, SR.End, true);
535+
Expr *StartLine = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.first);
536+
Expr *EndLine = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.first);
537+
Expr *StartColumn = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.second);
538+
Expr *EndColumn = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.second);
555539

556540
llvm::SmallVector<Expr *, 5> ArgsWithSourceRange{};
557541

@@ -615,26 +599,10 @@ class Instrumenter : InstrumenterBase {
615599
std::pair<unsigned, unsigned> EndLC = Context.SourceMgr.getLineAndColumn(
616600
Lexer::getLocForEndOfToken(Context.SourceMgr, SR.End));
617601

618-
const size_t buf_size = 8;
619-
620-
char *start_line_buf = (char *)Context.Allocate(buf_size, 1);
621-
char *end_line_buf = (char *)Context.Allocate(buf_size, 1);
622-
char *start_column_buf = (char *)Context.Allocate(buf_size, 1);
623-
char *end_column_buf = (char *)Context.Allocate(buf_size, 1);
624-
625-
::snprintf(start_line_buf, buf_size, "%u", StartLC.first);
626-
::snprintf(start_column_buf, buf_size, "%u", StartLC.second);
627-
::snprintf(end_line_buf, buf_size, "%u", EndLC.first);
628-
::snprintf(end_column_buf, buf_size, "%u", EndLC.second);
629-
630-
Expr *StartLine =
631-
new (Context) IntegerLiteralExpr(start_line_buf, SR.End, true);
632-
Expr *EndLine =
633-
new (Context) IntegerLiteralExpr(end_line_buf, SR.End, true);
634-
Expr *StartColumn =
635-
new (Context) IntegerLiteralExpr(start_column_buf, SR.End, true);
636-
Expr *EndColumn =
637-
new (Context) IntegerLiteralExpr(end_column_buf, SR.End, true);
602+
Expr *StartLine = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.first);
603+
Expr *EndLine = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.first);
604+
Expr *StartColumn = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.second);
605+
Expr *EndColumn = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.second);
638606

639607
llvm::SmallVector<Expr *, 4> ArgsWithSourceRange{};
640608

lib/Sema/PlaygroundTransform.cpp

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -771,12 +771,9 @@ class Instrumenter : InstrumenterBase {
771771
new (Context) StringLiteralExpr(NameInContext->c_str(), SourceRange());
772772
NameExpr->setImplicit(true);
773773

774-
const size_t buf_size = 11;
775-
char *const id_buf = (char *)Context.Allocate(buf_size, 1);
776774
std::uniform_int_distribution<unsigned> Distribution(0, 0x7fffffffu);
777775
const unsigned id_num = Distribution(RNG);
778-
::snprintf(id_buf, buf_size, "%u", id_num);
779-
Expr *IDExpr = new (Context) IntegerLiteralExpr(id_buf, SourceLoc(), true);
776+
Expr *IDExpr = IntegerLiteralExpr::createFromUnsigned(Context, id_num);
780777

781778
Expr *LoggerArgExprs[] = {*E, NameExpr, IDExpr};
782779

@@ -814,26 +811,10 @@ class Instrumenter : InstrumenterBase {
814811
std::pair<unsigned, unsigned> EndLC = Context.SourceMgr.getLineAndColumn(
815812
Lexer::getLocForEndOfToken(Context.SourceMgr, SR.End));
816813

817-
const size_t buf_size = 8;
818-
819-
char *start_line_buf = (char *)Context.Allocate(buf_size, 1);
820-
char *end_line_buf = (char *)Context.Allocate(buf_size, 1);
821-
char *start_column_buf = (char *)Context.Allocate(buf_size, 1);
822-
char *end_column_buf = (char *)Context.Allocate(buf_size, 1);
823-
824-
::snprintf(start_line_buf, buf_size, "%u", StartLC.first);
825-
::snprintf(start_column_buf, buf_size, "%u", StartLC.second);
826-
::snprintf(end_line_buf, buf_size, "%u", EndLC.first);
827-
::snprintf(end_column_buf, buf_size, "%u", EndLC.second);
828-
829-
Expr *StartLine =
830-
new (Context) IntegerLiteralExpr(start_line_buf, SR.End, true);
831-
Expr *EndLine =
832-
new (Context) IntegerLiteralExpr(end_line_buf, SR.End, true);
833-
Expr *StartColumn =
834-
new (Context) IntegerLiteralExpr(start_column_buf, SR.End, true);
835-
Expr *EndColumn =
836-
new (Context) IntegerLiteralExpr(end_column_buf, SR.End, true);
814+
Expr *StartLine = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.first);
815+
Expr *EndLine = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.first);
816+
Expr *StartColumn = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.second);
817+
Expr *EndColumn = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.second);
837818

838819
llvm::SmallVector<Expr *, 6> ArgsWithSourceRange(Args.begin(), Args.end());
839820

0 commit comments

Comments
 (0)