Mercurial > projects > ldc
comparison dmd/root.c @ 846:bc982f1ad106
Merged DMD 1.037 frontend
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Sat, 13 Dec 2008 13:15:31 +0100 |
parents | fa306ca8843b |
children | aa953cc960b6 |
comparison
equal
deleted
inserted
replaced
845:d128381e086e | 846:bc982f1ad106 |
---|---|
14 #include <stdint.h> | 14 #include <stdint.h> |
15 #include <assert.h> | 15 #include <assert.h> |
16 | 16 |
17 #if _MSC_VER ||__MINGW32__ | 17 #if _MSC_VER ||__MINGW32__ |
18 #include <malloc.h> | 18 #include <malloc.h> |
19 #include <string> | |
19 #endif | 20 #endif |
20 | 21 |
21 #if _WIN32 | 22 #if _WIN32 |
22 #include <windows.h> | 23 #include <windows.h> |
23 #include <direct.h> | 24 #include <direct.h> |
44 printf("Assert('%s','%s',%d)\n",e,f,line); | 45 printf("Assert('%s','%s',%d)\n",e,f,line); |
45 fflush(stdout); | 46 fflush(stdout); |
46 *(char *)0 = 0; | 47 *(char *)0 = 0; |
47 } | 48 } |
48 #endif | 49 #endif |
50 | |
49 | 51 |
50 /************************************* | 52 /************************************* |
51 * Convert wchar string to ascii string. | 53 * Convert wchar string to ascii string. |
52 */ | 54 */ |
53 | 55 |
1604 | 1606 |
1605 nbytes = ((offset + size - 1) & ~(size - 1)) - offset; | 1607 nbytes = ((offset + size - 1) & ~(size - 1)) - offset; |
1606 fill0(nbytes); | 1608 fill0(nbytes); |
1607 } | 1609 } |
1608 | 1610 |
1611 | |
1612 //////////////////////////////////////////////////////////////// | |
1613 // The compiler shipped with Visual Studio 2005 (and possible | |
1614 // other versions) does not support C99 printf format specfiers | |
1615 // such as %z and %j | |
1616 #if _MSC_VER | |
1617 using std::string; | |
1618 using std::wstring; | |
1619 | |
1620 template<typename S> | |
1621 inline void | |
1622 search_and_replace(S& str, const S& what, const S& replacement) | |
1623 { | |
1624 assert(!what.empty()); | |
1625 size_t pos = str.find(what); | |
1626 while (pos != S::npos) | |
1627 { | |
1628 str.replace(pos, what.size(), replacement); | |
1629 pos = str.find(what, pos + replacement.size()); | |
1630 } | |
1631 } | |
1632 #define WORKAROUND_C99_SPECIFIERS_BUG(S,tmp,f) \ | |
1633 S tmp = f; \ | |
1634 search_and_replace(fmt, S("%z"), S("%l")); \ | |
1635 search_and_replace(fmt, S("%j"), S("%i")); \ | |
1636 f = tmp.c_str(); | |
1637 #else | |
1638 #define WORKAROUND_C99_SPECIFIERS_BUG(S,tmp,f) | |
1639 #endif | |
1640 | |
1609 void OutBuffer::vprintf(const char *format, va_list args) | 1641 void OutBuffer::vprintf(const char *format, va_list args) |
1610 { | 1642 { |
1611 char buffer[128]; | 1643 char buffer[128]; |
1612 char *p; | 1644 char *p; |
1613 unsigned psize; | 1645 unsigned psize; |
1614 int count; | 1646 int count; |
1615 | 1647 |
1616 // On some platforms (i.e. x86_64) va_list is an array and thus passed by | 1648 WORKAROUND_C99_SPECIFIERS_BUG(string, fmt, format); |
1617 // reference. Copy the input list so we can copy it back before retrying. | |
1618 va_list orig_args; | |
1619 va_copy(orig_args, args); | |
1620 | 1649 |
1621 p = buffer; | 1650 p = buffer; |
1622 psize = sizeof(buffer); | 1651 psize = sizeof(buffer); |
1623 for (;;) | 1652 for (;;) |
1624 { | 1653 { |
1626 count = _vsnprintf(p,psize,format,args); | 1655 count = _vsnprintf(p,psize,format,args); |
1627 if (count != -1) | 1656 if (count != -1) |
1628 break; | 1657 break; |
1629 psize *= 2; | 1658 psize *= 2; |
1630 #elif POSIX | 1659 #elif POSIX |
1631 count = vsnprintf(p,psize,format,args); | 1660 va_list va; |
1661 va_copy(va, args); | |
1662 /* | |
1663 The functions vprintf(), vfprintf(), vsprintf(), vsnprintf() | |
1664 are equivalent to the functions printf(), fprintf(), sprintf(), | |
1665 snprintf(), respectively, except that they are called with a | |
1666 va_list instead of a variable number of arguments. These | |
1667 functions do not call the va_end macro. Consequently, the value | |
1668 of ap is undefined after the call. The application should call | |
1669 va_end(ap) itself afterwards. | |
1670 */ | |
1671 count = vsnprintf(p,psize,format,va); | |
1672 va_end(va); | |
1632 if (count == -1) | 1673 if (count == -1) |
1633 psize *= 2; | 1674 psize *= 2; |
1634 else if (count >= psize) | 1675 else if (count >= psize) |
1635 psize = count + 1; | 1676 psize = count + 1; |
1636 else | 1677 else |
1637 break; | 1678 break; |
1638 #endif | 1679 #endif |
1639 va_copy(args, orig_args); | |
1640 p = (char *) alloca(psize); // buffer too small, try again with larger size | 1680 p = (char *) alloca(psize); // buffer too small, try again with larger size |
1641 } | 1681 } |
1642 write(p,count); | 1682 write(p,count); |
1643 } | 1683 } |
1644 | 1684 |
1648 dchar buffer[128]; | 1688 dchar buffer[128]; |
1649 dchar *p; | 1689 dchar *p; |
1650 unsigned psize; | 1690 unsigned psize; |
1651 int count; | 1691 int count; |
1652 | 1692 |
1693 WORKAROUND_C99_SPECIFIERS_BUG(wstring, fmt, format); | |
1694 | |
1653 p = buffer; | 1695 p = buffer; |
1654 psize = sizeof(buffer) / sizeof(buffer[0]); | 1696 psize = sizeof(buffer) / sizeof(buffer[0]); |
1655 for (;;) | 1697 for (;;) |
1656 { | 1698 { |
1657 #if _WIN32 | 1699 #if _WIN32 |
1659 if (count != -1) | 1701 if (count != -1) |
1660 break; | 1702 break; |
1661 psize *= 2; | 1703 psize *= 2; |
1662 #endif | 1704 #endif |
1663 #if POSIX | 1705 #if POSIX |
1664 count = vsnwprintf(p,psize,format,args); | 1706 va_list va; |
1707 va_copy(va, args); | |
1708 count = vsnwprintf(p,psize,format,va); | |
1709 va_end(va); | |
1710 | |
1665 if (count == -1) | 1711 if (count == -1) |
1666 psize *= 2; | 1712 psize *= 2; |
1667 else if (count >= psize) | 1713 else if (count >= psize) |
1668 psize = count + 1; | 1714 psize = count + 1; |
1669 else | 1715 else |