Added -Wpointer-arith to CFLAGS, read more

I attempted to add '-Wpedantic' to CFLAGS, but it seems that my usage of
the variable length arrays within unions is causing an error that can't
be selectively disabled:

error: invalid use of structure with flexible array member [-Werror=pedantic]

This is the offending code: /source/toy_string.h#L9-L37

It seems that tagged unions, with VLAs within, is simply not allowed.
Unfortunately, my whole string system depends on it. I'll have to find some way
around it.

I've also updated the debugging output in repl/main.c.
This commit is contained in:
2024-12-12 18:14:08 +11:00
parent cf9affe190
commit 7be63c8ccc
11 changed files with 79 additions and 150 deletions

View File

@@ -1,6 +1,6 @@
#compiler settings reference
#CC=gcc
#CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2
#CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
#LIBS+=-lm
#LDFLAGS+=

View File

@@ -42,7 +42,7 @@ unsigned char* readFile(char* path, int* size) {
}
//read the file
if (fread(buffer, sizeof(unsigned char), *size, file) < *size) {
if (fread(buffer, sizeof(unsigned char), *size, file) < (unsigned int)(*size)) {
fclose(file);
*size = -2; //singal a read error
return NULL;
@@ -130,10 +130,12 @@ static void assertFailureAndContinueCallback(const char* msg) {
static void noOpCallback(const char* msg) {
//NO-OP
(void)msg;
}
static void silentExitCallback(const char* msg) {
//NO-OP
(void)msg;
exit(-1);
}
@@ -151,6 +153,7 @@ typedef struct CmdLine {
} CmdLine;
void usageCmdLine(int argc, const char* argv[]) {
(void)argc;
printf("Usage: %s [ -h | -v | -f source.toy ]\n\n", argv[0]);
}
@@ -169,6 +172,8 @@ void helpCmdLine(int argc, const char* argv[]) {
}
void versionCmdLine(int argc, const char* argv[]) {
(void)argc;
(void)argv;
printf("The Toy Programming Language, Version %d.%d.%d %s\n\n", TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD);
//copy/pasted from the license file - there's a way to include it directly, but it's too finnicky to bother
@@ -344,72 +349,37 @@ int repl(const char* filepath) {
static void debugStackPrint(Toy_Stack* stack) {
//DEBUG: if there's anything on the stack, print it
if (stack->count > 0) {
printf("Stack Dump\n-------------------------\ntype\tvalue\n");
for (int i = 0; i < stack->count; i++) {
Toy_Value v = ((Toy_Value*)(stack + 1))[i];
Toy_Bucket* stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
printf("Stack Dump\n-------------------------\ntype\tvalue\n");
for (unsigned int i = 0; i < stack->count; i++) {
Toy_Value v = ((Toy_Value*)(stack + 1))[i]; //'stack + 1' is a naughty trick
//print type
printf("%s\t", Toy_private_getValueTypeAsCString(v.type));
v = Toy_unwrapValue(v);
switch(v.type) {
case TOY_VALUE_NULL:
printf("null");
break;
case TOY_VALUE_BOOLEAN:
printf("%s", TOY_VALUE_AS_BOOLEAN(v) ? "true" : "false");
break;
case TOY_VALUE_INTEGER:
printf("%d", TOY_VALUE_AS_INTEGER(v));
break;
case TOY_VALUE_FLOAT:
printf("%f", TOY_VALUE_AS_FLOAT(v));
break;
case TOY_VALUE_STRING: {
Toy_String* str = TOY_VALUE_AS_STRING(v);
//print based on type
if (str->type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
//print value
Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v));
char* buffer = Toy_getStringRawBuffer(string);
printf("%s", buffer);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
printf("%s", str->as.leaf.data);
}
else if (str->type == TOY_STRING_NAME) {
printf("%s", str->as.name.data);
}
break;
}
case TOY_VALUE_ARRAY:
case TOY_VALUE_TABLE:
case TOY_VALUE_FUNCTION:
case TOY_VALUE_OPAQUE:
case TOY_VALUE_TYPE:
case TOY_VALUE_ANY:
case TOY_VALUE_REFERENCE:
case TOY_VALUE_UNKNOWN:
printf("???");
break;
}
Toy_freeString(string);
printf("\n");
}
Toy_freeBucket(&stringBucket);
}
}
static void debugScopePrint(Toy_Scope* scope, int depth) {
//DEBUG: if there's anything in the scope, print it
if (scope->table->count > 0) {
Toy_Bucket* stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
printf("Scope %d Dump\n-------------------------\ntype\tname\tvalue\n", depth);
for (int i = 0; i < scope->table->capacity; i++) {
if ( (TOY_VALUE_IS_STRING(scope->table->data[i].key) && TOY_VALUE_AS_STRING(scope->table->data[i].key)->type == TOY_STRING_NAME) == false) {
for (unsigned int i = 0; i < scope->table->capacity; i++) {
if ( (TOY_VALUE_IS_STRING(scope->table->data[i].key) && TOY_VALUE_AS_STRING(scope->table->data[i].key)->type == TOY_STRING_NAME) != true) {
continue;
}
@@ -418,58 +388,17 @@ static void debugScopePrint(Toy_Scope* scope, int depth) {
printf("%s\t%s\t", Toy_private_getValueTypeAsCString(v.type), TOY_VALUE_AS_STRING(k)->as.name.data);
k = Toy_unwrapValue(k);
v = Toy_unwrapValue(v);
switch(v.type) {
case TOY_VALUE_NULL:
printf("null");
break;
case TOY_VALUE_BOOLEAN:
printf("%s", TOY_VALUE_AS_BOOLEAN(v) ? "true" : "false");
break;
case TOY_VALUE_INTEGER:
printf("%d", TOY_VALUE_AS_INTEGER(v));
break;
case TOY_VALUE_FLOAT:
printf("%f", TOY_VALUE_AS_FLOAT(v));
break;
case TOY_VALUE_STRING: {
Toy_String* str = TOY_VALUE_AS_STRING(v);
//print based on type
if (str->type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
//print value
Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v));
char* buffer = Toy_getStringRawBuffer(string);
printf("%s", buffer);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
printf("%s", str->as.leaf.data);
}
else if (str->type == TOY_STRING_NAME) {
printf("%s\nWarning: The above value is a name string", str->as.name.data);
}
break;
}
case TOY_VALUE_ARRAY:
case TOY_VALUE_TABLE:
case TOY_VALUE_FUNCTION:
case TOY_VALUE_OPAQUE:
case TOY_VALUE_TYPE:
case TOY_VALUE_ANY:
case TOY_VALUE_REFERENCE:
case TOY_VALUE_UNKNOWN:
printf("???");
break;
}
Toy_freeString(string);
printf("\n");
}
Toy_freeBucket(&stringBucket);
}
if (scope->next != NULL) {

View File

@@ -1,6 +1,6 @@
#compiler settings
CC=gcc
#CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
LIBS+=-lm -lToy
LDFLAGS+=-Wl,-rpath,'$$ORIGIN'

View File

@@ -1,6 +1,6 @@
#compiler settings
CC=gcc
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
LIBS+=-lm
LDFLAGS+=

View File

@@ -10,7 +10,7 @@
#include <string.h>
//utils
static void expand(void** handle, unsigned int* capacity, unsigned int* count, unsigned int amount) {
static void expand(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned int amount) {
if ((*count) + amount > (*capacity)) {
while ((*count) + amount > (*capacity)) {
(*capacity) = (*capacity) < 8 ? 8 : (*capacity) * 2;
@@ -24,12 +24,12 @@ static void expand(void** handle, unsigned int* capacity, unsigned int* count, u
}
}
static void emitByte(void** handle, unsigned int* capacity, unsigned int* count, unsigned char byte) {
static void emitByte(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned char byte) {
expand(handle, capacity, count, 1);
((unsigned char*)(*handle))[(*count)++] = byte;
}
static void emitInt(void** handle, unsigned int* capacity, unsigned int* count, unsigned int bytes) {
static void emitInt(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned int bytes) {
char* ptr = (char*)&bytes;
emitByte(handle, capacity, count, *(ptr++));
emitByte(handle, capacity, count, *(ptr++));
@@ -37,7 +37,7 @@ static void emitInt(void** handle, unsigned int* capacity, unsigned int* count,
emitByte(handle, capacity, count, *(ptr++));
}
static void emitFloat(void** handle, unsigned int* capacity, unsigned int* count, float bytes) {
static void emitFloat(unsigned char** handle, unsigned int* capacity, unsigned int* count, float bytes) {
char* ptr = (char*)&bytes;
emitByte(handle, capacity, count, *(ptr++));
emitByte(handle, capacity, count, *(ptr++));
@@ -47,11 +47,11 @@ static void emitFloat(void** handle, unsigned int* capacity, unsigned int* count
//write instructions based on the AST types
#define EMIT_BYTE(rt, part, byte) \
emitByte((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), byte)
emitByte((&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), byte)
#define EMIT_INT(rt, part, bytes) \
emitInt((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes)
emitInt((&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes)
#define EMIT_FLOAT(rt, part, bytes) \
emitFloat((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes)
emitFloat((&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes)
//skip bytes, but return the address
#define SKIP_BYTE(rt, part) (EMIT_BYTE(rt, part, 0), ((*rt)->part##Count - 1))
@@ -59,7 +59,7 @@ static void emitFloat(void** handle, unsigned int* capacity, unsigned int* count
//overwrite a pre-existing position
#define OVERWRITE_INT(rt, part, addr, bytes) \
emitInt((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &(addr), bytes);
emitInt((&((*rt)->part)), &((*rt)->part##Capacity), &(addr), bytes);
//simply get the address (always an integer)
#define CURRENT_ADDRESS(rt, part) ((*rt)->part##Count)
@@ -80,7 +80,7 @@ static unsigned int emitString(Toy_Routine** rt, Toy_String* str) {
unsigned int startAddr = (*rt)->dataCount;
//move the string into the data section
expand((void**)(&((*rt)->data)), &((*rt)->dataCapacity), &((*rt)->dataCount), length);
expand((&((*rt)->data)), &((*rt)->dataCapacity), &((*rt)->dataCount), length);
if (str->type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
@@ -750,7 +750,7 @@ static void* writeRoutine(Toy_Routine* rt, Toy_Ast* ast) {
}
//write the header and combine the parts
void* buffer = NULL;
unsigned char* buffer = NULL;
unsigned int capacity = 0, count = 0;
// int paramAddr = 0, subsAddr = 0;
int codeAddr = 0;
@@ -766,23 +766,23 @@ static void* writeRoutine(Toy_Routine* rt, Toy_Ast* ast) {
//generate blank spaces, cache their positions in the *Addr variables (for storing the start positions)
if (rt->paramCount > 0) {
// paramAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //params
emitInt(&buffer, &capacity, &count, 0); //params
}
if (rt->codeCount > 0) {
codeAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //code
emitInt(&buffer, &capacity, &count, 0); //code
}
if (rt->jumpsCount > 0) {
jumpsAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //jumps
emitInt(&buffer, &capacity, &count, 0); //jumps
}
if (rt->dataCount > 0) {
dataAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //data
emitInt(&buffer, &capacity, &count, 0); //data
}
if (rt->subsCount > 0) {
// subsAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //subs
emitInt(&buffer, &capacity, &count, 0); //subs
}
//append various parts to the buffer

View File

@@ -13,7 +13,7 @@ typedef struct Toy_Routine {
unsigned int codeCapacity;
unsigned int codeCount;
unsigned int* jumps; //each 'jump' is the starting address of an element within 'data'
unsigned char* jumps; //each 'jump' is the starting address of an element within 'data'
unsigned int jumpsCapacity;
unsigned int jumpsCount;

View File

@@ -1,6 +1,6 @@
#compiler settings
CC=gcc
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
LIBS+=-lm
LDFLAGS+=

View File

@@ -1,6 +1,6 @@
#compiler settings
CC=gcc
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
LIBS+=-lm
LDFLAGS+=

View File

@@ -18,7 +18,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) {
Toy_private_emitAstPass(bucketHandle, &ast);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -66,7 +66,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -114,7 +114,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -166,7 +166,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -218,7 +218,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -271,7 +271,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -324,7 +324,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* header = (int*)buffer;
@@ -350,7 +350,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) {
return -1;
}
void* code = buffer + 32; //8 values in the header, each 4 bytes
unsigned char* code = buffer + 32; //8 values in the header, each 4 bytes
//check code
if (
@@ -374,7 +374,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) {
return -1;
}
void* jumps = code + 12;
unsigned char* jumps = code + 12;
//check jumps
if (
@@ -430,7 +430,7 @@ int test_routine_binary(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -495,7 +495,7 @@ int test_routine_binary(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -560,7 +560,7 @@ int test_routine_binary(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -625,7 +625,7 @@ int test_routine_binary(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -720,7 +720,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -778,7 +778,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -841,7 +841,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -931,7 +931,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -1045,7 +1045,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* ptr = (int*)buffer;
@@ -1102,7 +1102,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* header = (int*)buffer;
@@ -1128,7 +1128,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
return -1;
}
void* code = buffer + 32; //8 values in the header, each 4 bytes
unsigned char* code = buffer + 32; //8 values in the header, each 4 bytes
//check code
if (
@@ -1161,7 +1161,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
return -1;
}
void* jumps = code + 20;
unsigned char* jumps = code + 20;
//check jumps
if (
@@ -1209,7 +1209,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser);
//run
void* buffer = Toy_compileRoutine(ast);
unsigned char* buffer = Toy_compileRoutine(ast);
//check header
int* header = (int*)buffer;
@@ -1235,7 +1235,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
return -1;
}
void* code = buffer + 32; //8 values in the header, each 4 bytes
unsigned char* code = buffer + 32; //8 values in the header, each 4 bytes
//check code
if (
@@ -1268,7 +1268,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
return -1;
}
void* jumps = code + 20;
unsigned char* jumps = code + 20;
//check jumps
if (

View File

@@ -1,6 +1,6 @@
#compiler settings
CC=gcc
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
LIBS+=-lm
LDFLAGS+=

View File

@@ -1,6 +1,6 @@
#compiler settings
CC=gcc
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
LIBS+=-lm
LDFLAGS+=