Skip to content
This repository has been archived by the owner on Jun 20, 2019. It is now read-only.

Commit

Permalink
Fix Bug 285: Wrong codegen on optimized strlen()
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuclaw committed Mar 11, 2018
1 parent e4c7d24 commit b8d4228
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
29 changes: 22 additions & 7 deletions gcc/d/expr.cc
Expand Up @@ -2593,24 +2593,39 @@ class ExprVisitor : public Visitor

/* All strings are null terminated except static arrays. */
const char *string = (const char *)(e->len ? e->string : "");
dinteger_t length = (e->len * e->sz);
tree value = build_string (length, string);
tree type = build_ctype (e->type);

if (tb->ty == Tsarray)
TREE_TYPE (value) = type;
{
/* Turn the string into a constructor for the static array. */
vec<constructor_elt, va_gc> *elms = NULL;
vec_safe_reserve (elms, e->len);
tree etype = TREE_TYPE (type);

for (size_t i = 0; i < e->len; i++)
{
tree value = build_integer_cst (e->charAt (i), etype);
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), value);
}

tree ctor = build_constructor (type, elms);
TREE_CONSTANT (ctor) = 1;
this->result_ = ctor;
}
else
{
/* Array type string length includes the null terminator. */
TREE_TYPE (value) = make_array_type (tb->nextOf (), length + 1);
dinteger_t length = (e->len * e->sz) + 1;
tree value = build_string (length, string);
TREE_TYPE (value) = make_array_type (tb->nextOf (), length);
value = build_address (value);

if (tb->ty == Tarray)
value = d_array_value (type, size_int (e->len), value);
}

TREE_CONSTANT (value) = 1;
this->result_ = d_convert (type, value);
TREE_CONSTANT (value) = 1;
this->result_ = d_convert (type, value);
}
}

/* Build a tuple literal. Just an argument list that may have
Expand Down
17 changes: 17 additions & 0 deletions gcc/testsuite/gdc.dg/runnable.d
Expand Up @@ -1531,6 +1531,22 @@ void test273()

/******************************************/

// Bug 285

inout(char)[] test285a(inout(char)* s) @nogc @system pure nothrow
{
import core.stdc.string : strlen;
return s ? s[0 .. strlen(s)] : null;
}

void test285()
{
assert(test285a(null) == null);
assert(test285a("foo") == "foo");
}

/******************************************/

void main()
{
test2();
Expand Down Expand Up @@ -1564,6 +1580,7 @@ void main()
test248();
test250();
test273();
test285();

printf("Success!\n");
}

0 comments on commit b8d4228

Please sign in to comment.