C: reduce number of warnings, redirect console to debugger in Windows

This commit is contained in:
Alexey Andreev 2019-06-20 15:06:15 +03:00
parent 9e4c3010b0
commit a65f8fdcd9
7 changed files with 61 additions and 35 deletions

View File

@ -16,29 +16,31 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import java.io.IOException; import java.io.IOException;
import org.teavm.classlib.PlatformDetector;
import org.teavm.classlib.java.io.TOutputStream; import org.teavm.classlib.java.io.TOutputStream;
import org.teavm.interop.DelegateTo;
import org.teavm.interop.Import; import org.teavm.interop.Import;
import org.teavm.interop.Unmanaged; import org.teavm.interop.Unmanaged;
import org.teavm.interop.c.Include;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
class TConsoleOutputStreamStderr extends TOutputStream { class TConsoleOutputStreamStderr extends TOutputStream {
@Override @Override
@DelegateTo("writeLowLevel")
public void write(int b) throws IOException { public void write(int b) throws IOException {
writeJs(b);
}
@JSBody(params = "b", script = "$rt_putStderr(b);")
private static native void writeJs(int b);
private void writeLowLevel(int b) {
writeImpl(b); writeImpl(b);
} }
@Include("wchar.h") static void writeImpl(int b) {
@Import(name = "putwchar", module = "teavm") if (PlatformDetector.isC()) {
@Unmanaged writeC(b);
static native void writeImpl(int b); } else {
writeJs(b);
}
}
@JSBody(params = "b", script = "$rt_putStderr(b);")
@Import(name = "putwchar", module = "teavm")
private static native void writeJs(int b);
@Unmanaged
@Import(name = "teavm_logchar")
private static native void writeC(int b);
} }

View File

@ -240,6 +240,9 @@ public class ClassGenerator {
String poolName = "strings_" + context.getNames().forClassInstance(type); String poolName = "strings_" + context.getNames().forClassInstance(type);
codeWriter.println("TeaVM_String* " + poolName + "[];"); codeWriter.println("TeaVM_String* " + poolName + "[];");
codeWriter.println("#ifdef TEAVM_GET_STRING");
codeWriter.println("#undef TEAVM_GET_STRING");
codeWriter.println("#endif");
codeWriter.println("#define TEAVM_GET_STRING(i) " + poolName + "[i]"); codeWriter.println("#define TEAVM_GET_STRING(i) " + poolName + "[i]");
} }

View File

@ -600,7 +600,7 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
} }
if (resultTmp != null) { if (resultTmp != null) {
writer.print(resultTmp + " = "); writer.print(resultTmp + " = (" + typeToCType(method.getResultType()).text + ") ");
} }
writer.print(names.forMethod(method.getReference())); writer.print(names.forMethod(method.getReference()));
if (method.getAnnotations().get(Variable.class.getName()) == null) { if (method.getAnnotations().get(Variable.class.getName()) == null) {

View File

@ -48,13 +48,14 @@ int64_t teavm_date_timeToTimestamp(time_t t) {
} }
time_t teavm_date_timestampToTime(int64_t timestamp) { time_t teavm_date_timestampToTime(int64_t timestamp) {
int64_t seconds = (timestamp / 1000);
struct tm t = { struct tm t = {
.tm_year = 70, .tm_year = 70,
.tm_mon = 0, .tm_mon = 0,
.tm_mday = 1, .tm_mday = 1,
.tm_hour = 0, .tm_hour = (int) (seconds / 3600),
.tm_min = 0, .tm_min = (int) ((seconds / 60) % 60),
.tm_sec = timestamp / 1000, .tm_sec = (int) (seconds % 60),
.tm_isdst = -1 .tm_isdst = -1
}; };
return timegm(&t) + timestamp % 1000; return timegm(&t) + timestamp % 1000;
@ -62,7 +63,10 @@ time_t teavm_date_timestampToTime(int64_t timestamp) {
inline static struct tm* teavm_date_decompose(int64_t timestamp, struct tm *t) { inline static struct tm* teavm_date_decompose(int64_t timestamp, struct tm *t) {
*t = teavm_epochStartTm; *t = teavm_epochStartTm;
t->tm_sec += timestamp / 1000; int64_t seconds = (timestamp / 1000);
t->tm_sec += (int) (seconds % 60);
t->tm_min += (int) ((seconds / 60) % 60);
t->tm_hour += (int) (seconds / 3600);
mktime(t); mktime(t);
return t; return t;
} }
@ -179,7 +183,10 @@ int64_t teavm_date_setSeconds(int64_t time, int32_t seconds) {
char* teavm_date_format(int64_t time) { char* teavm_date_format(int64_t time) {
struct tm t; struct tm t;
t = teavm_epochStartTm; t = teavm_epochStartTm;
t.tm_sec += time / 1000; int64_t seconds = (time / 1000);
t.tm_sec += (int) (seconds % 60);
t.tm_min += (int) ((seconds / 60) % 60);
t.tm_hour += (int) (seconds / 3600);
mktime(&t); mktime(&t);
strftime(teavm_date_formatBuffer, 512, teavm_date_defaultFormat, &t); strftime(teavm_date_formatBuffer, 512, teavm_date_defaultFormat, &t);
return teavm_date_formatBuffer; return teavm_date_formatBuffer;

View File

@ -587,7 +587,7 @@ int32_t teavm_file_isWindows() {
} }
static TeaVM_StringList* teavm_file_addToList(TeaVM_StringList* strings, char16_t* data) { static TeaVM_StringList* teavm_file_addToList(TeaVM_StringList* strings, char16_t* data) {
int32_t size = wcslen(data); int32_t size = (int32_t) wcslen(data);
WCHAR* copy = malloc(size * sizeof(char16_t)); WCHAR* copy = malloc(size * sizeof(char16_t));
memcpy(copy, data, size * sizeof(char16_t)); memcpy(copy, data, size * sizeof(char16_t));
return teavm_appendString(strings, copy, size); return teavm_appendString(strings, copy, size);

View File

@ -62,7 +62,7 @@ static HANDLE teavm_queueTimer;
#endif #endif
void teavm_beforeInit() { void teavm_beforeInit() {
srand(time(NULL)); srand((unsigned int) time(NULL));
#ifdef __GNUC__ #ifdef __GNUC__
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
@ -100,7 +100,7 @@ void teavm_beforeInit() {
#ifdef __GNUC__ #ifdef __GNUC__
void teavm_initHeap(int64_t heapSize) { void teavm_initHeap(int64_t heapSize) {
long workSize = heapSize / 16; long workSize = (long) (heapSize / 16);
long regionsSize = (long) (heapSize / teavm_gc_regionSize); long regionsSize = (long) (heapSize / teavm_gc_regionSize);
long pageSize = sysconf(_SC_PAGE_SIZE); long pageSize = sysconf(_SC_PAGE_SIZE);
@ -166,7 +166,7 @@ static void* teavm_virtualAlloc(int size) {
} }
void teavm_initHeap(int64_t heapSize) { void teavm_initHeap(int64_t heapSize) {
long workSize = heapSize / 16; long workSize = (long) (heapSize / 16);
long regionsSize = (long) (heapSize / teavm_gc_regionSize); long regionsSize = (long) (heapSize / teavm_gc_regionSize);
SYSTEM_INFO systemInfo; SYSTEM_INFO systemInfo;
@ -206,9 +206,8 @@ int64_t teavm_currentTimeNano() {
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
#define gmtime_r(a, b) gmtime_s(b, a) #define gmtime_r(a, b) gmtime_s(a, b)
#define mktime_r(a, b) mktime_s(b, a) #define localtime_r(a, b) localtime_s(a, b)
#define localtime_r(a, b) localtime_s(b, a)
#endif #endif
int32_t teavm_timeZoneOffset() { int32_t teavm_timeZoneOffset() {
@ -216,7 +215,7 @@ int32_t teavm_timeZoneOffset() {
time_t t = time(NULL); time_t t = time(NULL);
time_t local = mktime(localtime_r(&t, &tm)); time_t local = mktime(localtime_r(&t, &tm));
time_t utc = mktime(gmtime_r(&t, &tm)); time_t utc = mktime(gmtime_r(&t, &tm));
return difftime(utc, local) / 60; return (int32_t) (difftime(utc, local) / 60);
} }
#ifdef __GNUC__ #ifdef __GNUC__
@ -245,7 +244,7 @@ void teavm_interrupt() {
#ifdef _MSC_VER #ifdef _MSC_VER
void teavm_waitFor(int64_t timeout) { void teavm_waitFor(int64_t timeout) {
WaitForSingleObject(teavm_queueTimer, timeout); WaitForSingleObject(teavm_queueTimer, (DWORD) timeout);
ResetEvent(teavm_queueTimer); ResetEvent(teavm_queueTimer);
} }
@ -320,7 +319,7 @@ size_t teavm_mbSize(char16_t* javaChars, int32_t javaCharsCount) {
mbstate_t state = {0}; mbstate_t state = {0};
for (int32_t i = 0; i < javaCharsCount; ++i) { for (int32_t i = 0; i < javaCharsCount; ++i) {
size_t result = c16rtomb(buffer, javaChars[i], &state); size_t result = c16rtomb(buffer, javaChars[i], &state);
if (result == -1) { if (result == (size_t) -1) {
break; break;
} }
sz += result; sz += result;
@ -333,7 +332,7 @@ int32_t teavm_c16Size(char* cstring, size_t count) {
int32_t sz = 0; int32_t sz = 0;
while (count > 0) { while (count > 0) {
size_t result = mbrtoc16(NULL, cstring, count, &state); size_t result = mbrtoc16(NULL, cstring, count, &state);
if (result == -1) { if (result == (size_t) -1) {
break; break;
} else if ((int) result >= 0) { } else if ((int) result >= 0) {
sz++; sz++;
@ -362,7 +361,7 @@ char* teavm_stringToC(void* obj) {
mbstate_t state = {0}; mbstate_t state = {0};
for (int32_t i = 0; i < charArray->size; ++i) { for (int32_t i = 0; i < charArray->size; ++i) {
size_t result = c16rtomb(dst, javaChars[i], &state); size_t result = c16rtomb(dst, javaChars[i], &state);
if (result == -1) { if (result == (size_t) -1) {
break; break;
} }
dst += result; dst += result;
@ -397,8 +396,8 @@ TeaVM_String* teavm_cToString(char* cstring) {
char16_t* javaChars = TEAVM_ARRAY_DATA(charArray, char16_t); char16_t* javaChars = TEAVM_ARRAY_DATA(charArray, char16_t);
mbstate_t state = {0}; mbstate_t state = {0};
for (int32_t i = 0; i < size; ++i) { for (int32_t i = 0; i < size; ++i) {
int32_t result = mbrtoc16(javaChars++, cstring, clen, &state); size_t result = mbrtoc16(javaChars++, cstring, clen, &state);
if (result == -1) { if (result == (size_t) -1) {
break; break;
} else if ((int) result >= 0) { } else if ((int) result >= 0) {
clen -= result; clen -= result;
@ -429,8 +428,8 @@ char16_t* teavm_mbToChar16(char* cstring, int32_t* length) {
char16_t* javaChars = malloc(sizeof(char16_t) * (size + 2)); char16_t* javaChars = malloc(sizeof(char16_t) * (size + 2));
mbstate_t state = {0}; mbstate_t state = {0};
for (int32_t i = 0; i < size; ++i) { for (int32_t i = 0; i < size; ++i) {
int32_t result = mbrtoc16(javaChars + i, cstring, clen, &state); size_t result = mbrtoc16(javaChars + i, cstring, clen, &state);
if (result == -1) { if (result == (size_t) -1) {
break; break;
} else if ((int) result >= 0) { } else if ((int) result >= 0) {
clen -= result; clen -= result;
@ -542,3 +541,14 @@ TeaVM_StringList* teavm_appendString(TeaVM_StringList* list, char16_t* data, int
entry->next = list; entry->next = list;
return entry; return entry;
} }
#ifndef TEAVM_WINDOWS_LOG
void teavm_logchar(char16_t c) {
putwchar(c);
}
#else
void teavm_logchar(int32_t c) {
char16_t buffer[2] = { (char16_t) c, 0 };
OutputDebugStringW(buffer);
}
#endif

View File

@ -12,6 +12,8 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#define alignas(n) __declspec(align(n)) #define alignas(n) __declspec(align(n))
#pragma comment (lib,"uuid.lib") #pragma comment (lib,"uuid.lib")
#pragma warning(disable:4116)
#pragma warning(disable:4102)
#ifdef WINAPI_FAMILY #ifdef WINAPI_FAMILY
#if WINAPI_FAMILY == 2 || WINAPI_FAMILY == 3 || WINAPI_FAMILY == 5 #if WINAPI_FAMILY == 2 || WINAPI_FAMILY == 3 || WINAPI_FAMILY == 5
@ -354,3 +356,5 @@ extern int32_t teavm_file_isWindows();
#ifdef _MSC_VER #ifdef _MSC_VER
extern int64_t teavm_unixTimeOffset; extern int64_t teavm_unixTimeOffset;
#endif #endif
extern void teavm_logchar(int32_t);