Skip to content

Commit

Permalink
Merge pull request #3568 from yebblies/valisttype
Browse files Browse the repository at this point in the history
Use correct type for va_list
  • Loading branch information
ibuclaw committed May 23, 2014
2 parents 3cd89e0 + c662153 commit fa12d5b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/mtype.c
Expand Up @@ -325,7 +325,7 @@ void Type::init()

tvoidptr = tvoid->pointerTo();
tstring = tchar->immutableOf()->arrayOf();
tvalist = tvoid->pointerTo();
tvalist = Target::va_listType();

if (global.params.isLP64)
{
Expand Down
27 changes: 27 additions & 0 deletions src/target.c
Expand Up @@ -163,3 +163,30 @@ unsigned Target::critsecsize()
return 0;
}

Type *Target::va_listType()
{
if (global.params.isWindows)
{
return Type::tchar->pointerTo();
}
else if (global.params.isLinux ||
global.params.isFreeBSD ||
global.params.isOpenBSD ||
global.params.isSolaris ||
global.params.isOSX)
{
if (global.params.is64bit)
{
return (new TypeIdentifier(Loc(), Lexer::idPool("__va_list_tag")))->pointerTo();
}
else
{
return Type::tchar->pointerTo();
}
}
else
{
assert(0);
return NULL;
}
}
1 change: 1 addition & 0 deletions src/target.h
Expand Up @@ -29,6 +29,7 @@ struct Target
static unsigned alignsize(Type* type);
static unsigned fieldalign(Type* type);
static unsigned critsecsize();
static Type *va_listType(); // get type of va_list
};

#endif
33 changes: 33 additions & 0 deletions test/runnable/cppa.d
@@ -1,6 +1,7 @@
// EXTRA_CPP_SOURCES: cppb.cpp

import std.c.stdio;
import core.stdc.stdarg;

extern (C++)
int foob(int i, int j, int k);
Expand Down Expand Up @@ -278,6 +279,37 @@ void test11()
}
/****************************************/

char[100] valistbuffer;

extern(C++) void myvprintfx(const(char)* format, va_list va)
{
vsprintf(valistbuffer.ptr, format, va);
}
extern(C++) void myvprintf(const(char)*, va_list);
extern(C++) void myprintf(const(char)* format, ...)
{
va_list ap;
version(X86_64)
{
version(Windows)
va_start(ap, format);
else
va_start(ap, __va_argsave);
}
else
va_start(ap, format);
myvprintf(format, ap);
va_end(ap);
}

void testvalist()
{
myprintf("hello %d", 999);
assert(valistbuffer[0..9] == "hello 999");
}

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

void main()
{
test1();
Expand All @@ -292,6 +324,7 @@ void main()
test9();
test10();
test11();
testvalist();

printf("Success\n");
}
8 changes: 8 additions & 0 deletions test/runnable/extra-files/cppb.cpp
Expand Up @@ -212,3 +212,11 @@ namespace N11 { namespace M { void bar11() { } } }

namespace A11 { namespace B { namespace C { void bar() { } } } }

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

void myvprintfx(const char* format, va_list);

void myvprintf(const char* format, va_list va)
{
myvprintfx(format, va);
}
2 changes: 1 addition & 1 deletion test/runnable/imports/argufile.d
Expand Up @@ -7,7 +7,7 @@ import std.stdio;
import std.format;
import std.utf;

dstring formatstring(TypeInfo[] arguments, void *argptr)
dstring formatstring(TypeInfo[] arguments, va_list argptr)
{

dstring message = null;
Expand Down

0 comments on commit fa12d5b

Please sign in to comment.