Skip to content

Commit

Permalink
Merge pull request #3868 from 9rnsr/fix13281
Browse files Browse the repository at this point in the history
Issue 13281 - Print type suffix of real/complex literals in pragma(msg) and error diagnostic
  • Loading branch information
yebblies committed Aug 11, 2014
2 parents 07067a3 + 92db02c commit 5ce80cf
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 74 deletions.
53 changes: 0 additions & 53 deletions src/expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -2623,11 +2623,6 @@ bool IntegerExp::equals(RootObject *o)
return false;
}

char *IntegerExp::toChars()
{
return Expression::toChars();
}

void IntegerExp::setInteger(dinteger_t value)
{
this->value = value;
Expand Down Expand Up @@ -2751,24 +2746,6 @@ RealExp::RealExp(Loc loc, real_t value, Type *type)
this->type = type;
}

char *RealExp::toChars()
{
/** sizeof(value)*3 is because each byte of mantissa is max
of 256 (3 characters). The string will be "-M.MMMMe-4932".
(ie, 8 chars more than mantissa). Plus one for trailing \0.
Plus one for rounding. */
const size_t BUFFER_LEN = sizeof(value) * 3 + 8 + 1 + 1;
char buffer[BUFFER_LEN];

ld_sprint(buffer, 'g', value);

if (type->isimaginary())
strcat(buffer, "i");

assert(strlen(buffer) < BUFFER_LEN);
return mem.strdup(buffer);
}

dinteger_t RealExp::toInteger()
{
return (sinteger_t) toReal();
Expand Down Expand Up @@ -2902,21 +2879,6 @@ ComplexExp::ComplexExp(Loc loc, complex_t value, Type *type)
//printf("ComplexExp::ComplexExp(%s)\n", toChars());
}

char *ComplexExp::toChars()
{
const size_t BUFFER_LEN = sizeof(value) * 3 + 8 + 1 + 1;
char buffer[BUFFER_LEN];

char buf1[BUFFER_LEN];
char buf2[BUFFER_LEN];

ld_sprint(buf1, 'g', creall(value));
ld_sprint(buf2, 'g', cimagl(value));
sprintf(buffer, "(%s+%si)", buf1, buf2);
assert(strlen(buffer) < BUFFER_LEN);
return mem.strdup(buffer);
}

dinteger_t ComplexExp::toInteger()
{
return (sinteger_t) toReal();
Expand Down Expand Up @@ -3122,11 +3084,6 @@ Expression *IdentifierExp::semantic(Scope *sc)
return new ErrorExp();
}

char *IdentifierExp::toChars()
{
return ident->toChars();
}

int IdentifierExp::isLvalue()
{
return 1;
Expand Down Expand Up @@ -3345,11 +3302,6 @@ Expression *DsymbolExp::semantic(Scope *sc)
return new ErrorExp();
}

char *DsymbolExp::toChars()
{
return s->toChars();
}

int DsymbolExp::isLvalue()
{
return 1;
Expand Down Expand Up @@ -5231,11 +5183,6 @@ Expression *VarExp::semantic(Scope *sc)
return this;
}

char *VarExp::toChars()
{
return var->toChars();
}

void VarExp::checkEscape()
{
VarDeclaration *v = var->isVarDeclaration();
Expand Down
6 changes: 0 additions & 6 deletions src/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ class IntegerExp : public Expression
IntegerExp(dinteger_t value);
bool equals(RootObject *o);
Expression *semantic(Scope *sc);
char *toChars();
dinteger_t toInteger();
real_t toReal();
real_t toImaginary();
Expand Down Expand Up @@ -283,7 +282,6 @@ class RealExp : public Expression
RealExp(Loc loc, real_t value, Type *type);
bool equals(RootObject *o);
Expression *semantic(Scope *sc);
char *toChars();
dinteger_t toInteger();
uinteger_t toUInteger();
real_t toReal();
Expand All @@ -302,7 +300,6 @@ class ComplexExp : public Expression
ComplexExp(Loc loc, complex_t value, Type *type);
bool equals(RootObject *o);
Expression *semantic(Scope *sc);
char *toChars();
dinteger_t toInteger();
uinteger_t toUInteger();
real_t toReal();
Expand All @@ -322,7 +319,6 @@ class IdentifierExp : public Expression
IdentifierExp(Loc loc, Identifier *ident);
static IdentifierExp *create(Loc loc, Identifier *ident);
Expression *semantic(Scope *sc);
char *toChars();
int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
void accept(Visitor *v) { v->visit(this); }
Expand All @@ -343,7 +339,6 @@ class DsymbolExp : public Expression

DsymbolExp(Loc loc, Dsymbol *s, bool hasOverloads = false);
Expression *semantic(Scope *sc);
char *toChars();
int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
void accept(Visitor *v) { v->visit(this); }
Expand Down Expand Up @@ -645,7 +640,6 @@ class VarExp : public SymbolExp
static VarExp *create(Loc loc, Declaration *var, bool hasOverloads = false);
bool equals(RootObject *o);
Expression *semantic(Scope *sc);
char *toChars();
void checkEscape();
void checkEscapeRef();
int checkModifiable(Scope *sc, int flag);
Expand Down
23 changes: 11 additions & 12 deletions src/hdrgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1981,22 +1981,21 @@ class PrettyPrintVisitor : public Visitor

void floatToBuffer(Type *type, real_t value)
{
/* In order to get an exact representation, try converting it
* to decimal then back again. If it matches, use it.
* If it doesn't, fall back to hex, which is
* always exact.
* Longest string is for -real.max:
* "-1.18973e+4932\0".length == 17
* "-0xf.fffffffffffffffp+16380\0".length == 28
*/
const size_t BUFFER_LEN = 32;
/** sizeof(value)*3 is because each byte of mantissa is max
of 256 (3 characters). The string will be "-M.MMMMe-4932".
(ie, 8 chars more than mantissa). Plus one for trailing \0.
Plus one for rounding. */
const size_t BUFFER_LEN = sizeof(value) * 3 + 8 + 1 + 1;
char buffer[BUFFER_LEN];
ld_sprint(buffer, 'g', value);
assert(strlen(buffer) < BUFFER_LEN);

real_t r = Port::strtold(buffer, NULL);
if (r != value) // if exact duplication
ld_sprint(buffer, 'a', value);
if (hgs->hdrgen)
{
real_t r = Port::strtold(buffer, NULL);
if (r != value) // if exact duplication
ld_sprint(buffer, 'a', value);
}
buf->writestring(buffer);

if (type)
Expand Down
47 changes: 47 additions & 0 deletions test/compilable/test13281.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// REQUIRED_ARGS: -o-
// PERMUTE_ARGS:
/*
TEST_OUTPUT:
---
123
123u
123L
123LU
123.4
123.4F
123.4L
123.4i
123.4Fi
123.4Li
(123.4+5.6i)
(123.4F+5.6Fi)
(123.4L+5.6Li)
---
*/
pragma(msg, 123);
pragma(msg, 123u);
pragma(msg, 123L);
pragma(msg, 123uL);
pragma(msg, 123.4);
pragma(msg, 123.4f);
pragma(msg, 123.4L);
pragma(msg, 123.4i);
pragma(msg, 123.4fi);
pragma(msg, 123.4Li);
pragma(msg, 123.4 +5.6i);
pragma(msg, 123.4f+5.6fi);
pragma(msg, 123.4L+5.6Li);

static assert((123 ).stringof == "123");
static assert((123u ).stringof == "123u");
static assert((123L ).stringof == "123L");
static assert((123uL).stringof == "123LU");
static assert((123.4 ).stringof == "123.4");
static assert((123.4f ).stringof == "123.4F");
static assert((123.4L ).stringof == "123.4L");
static assert((123.4i ).stringof == "123.4i");
static assert((123.4fi).stringof == "123.4Fi");
static assert((123.4Li).stringof == "123.4Li");
static assert((123.4 +5.6i ).stringof == "123.4 + 5.6i");
static assert((123.4f+5.6fi).stringof == "123.4F + 5.6Fi");
static assert((123.4L+5.6Li).stringof == "123.4L + 5.6Li");
32 changes: 32 additions & 0 deletions test/fail_compilation/diag13281.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag13281.d(20): Error: cannot implicitly convert expression (123) of type int to string
fail_compilation/diag13281.d(21): Error: cannot implicitly convert expression (123u) of type uint to string
fail_compilation/diag13281.d(22): Error: cannot implicitly convert expression (123L) of type long to string
fail_compilation/diag13281.d(23): Error: cannot implicitly convert expression (123LU) of type ulong to string
fail_compilation/diag13281.d(24): Error: cannot implicitly convert expression (123.4) of type double to int
fail_compilation/diag13281.d(25): Error: cannot implicitly convert expression (123.4F) of type float to int
fail_compilation/diag13281.d(26): Error: cannot implicitly convert expression (123.4L) of type real to int
fail_compilation/diag13281.d(27): Error: cannot implicitly convert expression (123.4i) of type idouble to int
fail_compilation/diag13281.d(28): Error: cannot implicitly convert expression (123.4Fi) of type ifloat to int
fail_compilation/diag13281.d(29): Error: cannot implicitly convert expression (123.4Li) of type ireal to int
fail_compilation/diag13281.d(30): Error: cannot implicitly convert expression ((123.4+5.6i)) of type cdouble to int
fail_compilation/diag13281.d(31): Error: cannot implicitly convert expression ((123.4F+5.6Fi)) of type cfloat to int
fail_compilation/diag13281.d(32): Error: cannot implicitly convert expression ((123.4L+5.6Li)) of type creal to int
---
*/

string x1 = 123;
string x2 = 123u;
string x3 = 123L;
string x4 = 123uL;
int y1 = 123.4;
int y2 = 123.4f;
int y3 = 123.4L;
int y4 = 123.4i;
int y5 = 123.4fi;
int y6 = 123.4Li;
int y7 = 123.4 +5.6i;
int y8 = 123.4f+5.6fi;
int y9 = 123.4L+5.6Li;
4 changes: 2 additions & 2 deletions test/fail_compilation/diag9357.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ fail_compilation/diag9357.d(14): Error: cannot implicitly convert expression (1.
fail_compilation/diag9357.d(15): Error: cannot implicitly convert expression (10.0000) of type double to int
fail_compilation/diag9357.d(16): Error: cannot implicitly convert expression (11.0000) of type double to int
fail_compilation/diag9357.d(17): Error: cannot implicitly convert expression (99.0000) of type double to int
fail_compilation/diag9357.d(18): Error: cannot implicitly convert expression (1.04858e+06) of type real to int
fail_compilation/diag9357.d(19): Error: cannot implicitly convert expression (1.04858e+06) of type real to int
fail_compilation/diag9357.d(18): Error: cannot implicitly convert expression (1.04858e+06L) of type real to int
fail_compilation/diag9357.d(19): Error: cannot implicitly convert expression (1.04858e+06L) of type real to int
---
*/
void main()
Expand Down
2 changes: 1 addition & 1 deletion test/runnable/functype.d
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void testti()
static assert(StringOf!(typeof(test)) == "int[](int[])");

float function(float x = 0F) fp = x => x;
static assert(typeof(fp).stringof == "float function(float x = " ~ (0F).stringof ~ "F)");
static assert(typeof(fp).stringof == "float function(float x = " ~ (0F).stringof ~ ")");
static assert(StringOf!(typeof(fp)) == "float function(float)");

double delegate(double x = 0.0) dg = x => x;
Expand Down

0 comments on commit 5ce80cf

Please sign in to comment.