Skip to content

[NFC] Extract helper for making integer literals #17282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,13 @@ class IntegerLiteralExpr : public NumberLiteralExpr {
Val, DigitsLoc, Implicit)
{}

/// Returns a new integer literal expression with the given value.
/// \p C The AST context.
/// \p value The integer value.
/// \return An implicit integer literal expression which evaluates to the value.
static IntegerLiteralExpr *
createFromUnsigned(ASTContext &C, unsigned value);

APInt getValue() const;
static APInt getValue(StringRef Text, unsigned BitWidth, bool Negative);

Expand Down
7 changes: 7 additions & 0 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,13 @@ LiteralExpr *LiteralExpr::shallowClone(
return Result;
}

IntegerLiteralExpr * IntegerLiteralExpr::createFromUnsigned(ASTContext &C, unsigned value) {
llvm::SmallString<8> Scratch;
llvm::APInt(sizeof(unsigned)*8, value).toString(Scratch, 10, /*signed*/ false);
auto Text = C.AllocateCopy(StringRef(Scratch));
return new (C) IntegerLiteralExpr(Text, SourceLoc(), /*implicit*/ true);
}

/// A wrapper around LLVM::getAsInteger that can be used on Swift interger
/// literals. It avoids misinterpreting decimal numbers prefixed with 0 as
/// octal numbers.
Expand Down
18 changes: 2 additions & 16 deletions lib/Sema/DerivedConformanceEquatableHashable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,6 @@ enumElementPayloadSubpattern(EnumElementDecl *enumElementDecl,
return pat;
}

/// Returns a new integer literal expression with the given value.
/// \p C The AST context.
/// \p value The integer value.
/// \return The integer literal expression.
static Expr *integerLiteralExpr(ASTContext &C, int64_t value) {
llvm::SmallString<8> integerVal;
APInt(32, value).toString(integerVal, 10, /*signed*/ false);
auto integerStr = C.AllocateCopy(integerVal);
auto integerExpr = new (C) IntegerLiteralExpr(
StringRef(integerStr.data(), integerStr.size()), SourceLoc(),
/*implicit*/ true);
return integerExpr;
}

/// Create AST statements which convert from an enum to an Int with a switch.
/// \p stmts The generated statements are appended to this vector.
/// \p parentDC Either an extension or the enum itself.
Expand Down Expand Up @@ -255,7 +241,7 @@ static DeclRefExpr *convertEnumToIndex(SmallVectorImpl<ASTNode> &stmts,
auto labelItem = CaseLabelItem(pat);

// generate: indexVar = <index>
auto indexExpr = integerLiteralExpr(C, index++);
auto indexExpr = IntegerLiteralExpr::createFromUnsigned(C, index++);
auto indexRef = new (C) DeclRefExpr(indexVar, DeclNameLoc(),
/*implicit*/true);
auto assignExpr = new (C) AssignExpr(indexRef, SourceLoc(),
Expand Down Expand Up @@ -932,7 +918,7 @@ deriveBodyHashable_enum_hasAssociatedValues_hashInto(

{
// Generate: hasher.combine(<ordinal>)
auto ordinalExpr = integerLiteralExpr(C, index++);
auto ordinalExpr = IntegerLiteralExpr::createFromUnsigned(C, index++);
auto combineExpr = createHasherCombineCall(C, hasherParam, ordinalExpr);
statements.emplace_back(ASTNode(combineExpr));
}
Expand Down
5 changes: 1 addition & 4 deletions lib/Sema/DerivedConformanceRawRepresentable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ deriveBodyRawRepresentable_init(AbstractFunctionDecl *initDecl) {
// In case of a string enum we are calling the _findStringSwitchCase
// function from the library and switching on the returned Int value.
stringExprs.push_back(litExpr);
llvm::SmallString<16> IdxAsStringBuffer;
APInt(64, Idx).toStringUnsigned(IdxAsStringBuffer);
StringRef IndexAsString(C.AllocateCopy(IdxAsStringBuffer.str()));
litExpr = new (C) IntegerLiteralExpr(IndexAsString, SourceLoc());
litExpr = IntegerLiteralExpr::createFromUnsigned(C, Idx);
}
auto litPat = new (C) ExprPattern(litExpr, /*isResolved*/ true,
nullptr, nullptr);
Expand Down
48 changes: 8 additions & 40 deletions lib/Sema/PCMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,26 +532,10 @@ class Instrumenter : InstrumenterBase {
std::pair<unsigned, unsigned> EndLC = Context.SourceMgr.getLineAndColumn(
Lexer::getLocForEndOfToken(Context.SourceMgr, SR.End));

const size_t buf_size = 8;

char *start_line_buf = (char *)Context.Allocate(buf_size, 1);
char *end_line_buf = (char *)Context.Allocate(buf_size, 1);
char *start_column_buf = (char *)Context.Allocate(buf_size, 1);
char *end_column_buf = (char *)Context.Allocate(buf_size, 1);

::snprintf(start_line_buf, buf_size, "%u", StartLC.first);
::snprintf(start_column_buf, buf_size, "%u", StartLC.second);
::snprintf(end_line_buf, buf_size, "%u", EndLC.first);
::snprintf(end_column_buf, buf_size, "%u", EndLC.second);

Expr *StartLine =
new (Context) IntegerLiteralExpr(start_line_buf, SR.End, true);
Expr *EndLine =
new (Context) IntegerLiteralExpr(end_line_buf, SR.End, true);
Expr *StartColumn =
new (Context) IntegerLiteralExpr(start_column_buf, SR.End, true);
Expr *EndColumn =
new (Context) IntegerLiteralExpr(end_column_buf, SR.End, true);
Expr *StartLine = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.first);
Expr *EndLine = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.first);
Expr *StartColumn = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.second);
Expr *EndColumn = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.second);

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

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

const size_t buf_size = 8;

char *start_line_buf = (char *)Context.Allocate(buf_size, 1);
char *end_line_buf = (char *)Context.Allocate(buf_size, 1);
char *start_column_buf = (char *)Context.Allocate(buf_size, 1);
char *end_column_buf = (char *)Context.Allocate(buf_size, 1);

::snprintf(start_line_buf, buf_size, "%u", StartLC.first);
::snprintf(start_column_buf, buf_size, "%u", StartLC.second);
::snprintf(end_line_buf, buf_size, "%u", EndLC.first);
::snprintf(end_column_buf, buf_size, "%u", EndLC.second);

Expr *StartLine =
new (Context) IntegerLiteralExpr(start_line_buf, SR.End, true);
Expr *EndLine =
new (Context) IntegerLiteralExpr(end_line_buf, SR.End, true);
Expr *StartColumn =
new (Context) IntegerLiteralExpr(start_column_buf, SR.End, true);
Expr *EndColumn =
new (Context) IntegerLiteralExpr(end_column_buf, SR.End, true);
Expr *StartLine = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.first);
Expr *EndLine = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.first);
Expr *StartColumn = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.second);
Expr *EndColumn = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.second);

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

Expand Down
29 changes: 5 additions & 24 deletions lib/Sema/PlaygroundTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,12 +771,9 @@ class Instrumenter : InstrumenterBase {
new (Context) StringLiteralExpr(NameInContext->c_str(), SourceRange());
NameExpr->setImplicit(true);

const size_t buf_size = 11;
char *const id_buf = (char *)Context.Allocate(buf_size, 1);
std::uniform_int_distribution<unsigned> Distribution(0, 0x7fffffffu);
const unsigned id_num = Distribution(RNG);
::snprintf(id_buf, buf_size, "%u", id_num);
Expr *IDExpr = new (Context) IntegerLiteralExpr(id_buf, SourceLoc(), true);
Expr *IDExpr = IntegerLiteralExpr::createFromUnsigned(Context, id_num);

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

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

const size_t buf_size = 8;

char *start_line_buf = (char *)Context.Allocate(buf_size, 1);
char *end_line_buf = (char *)Context.Allocate(buf_size, 1);
char *start_column_buf = (char *)Context.Allocate(buf_size, 1);
char *end_column_buf = (char *)Context.Allocate(buf_size, 1);

::snprintf(start_line_buf, buf_size, "%u", StartLC.first);
::snprintf(start_column_buf, buf_size, "%u", StartLC.second);
::snprintf(end_line_buf, buf_size, "%u", EndLC.first);
::snprintf(end_column_buf, buf_size, "%u", EndLC.second);

Expr *StartLine =
new (Context) IntegerLiteralExpr(start_line_buf, SR.End, true);
Expr *EndLine =
new (Context) IntegerLiteralExpr(end_line_buf, SR.End, true);
Expr *StartColumn =
new (Context) IntegerLiteralExpr(start_column_buf, SR.End, true);
Expr *EndColumn =
new (Context) IntegerLiteralExpr(end_column_buf, SR.End, true);
Expr *StartLine = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.first);
Expr *EndLine = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.first);
Expr *StartColumn = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.second);
Expr *EndColumn = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.second);

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

Expand Down