No more anonymous structs (not standard C++) (#1305)

This is one step to restoring `-pedantic` builds
This commit is contained in:
Sylvie
2024-02-19 02:12:20 -05:00
committed by GitHub
parent a02687a83e
commit 9cdd0b8a02
8 changed files with 65 additions and 65 deletions

View File

@@ -39,9 +39,9 @@ struct Symbol {
int32_t (*numCallback)(void);
// For SYM_MACRO and SYM_EQUS; TODO: have separate fields
struct {
size_t macroSize;
char *macro;
};
size_t size;
char *value;
} macro;
// For SYM_EQUS
char const *(*strCallback)(void);
};
@@ -97,7 +97,7 @@ static inline char const *sym_GetStringValue(struct Symbol const *sym)
{
if (sym->hasCallback)
return sym->strCallback();
return sym->macro;
return sym->macro.value;
}
void sym_ForEach(void (*func)(struct Symbol *, void *), void *arg);

View File

@@ -37,9 +37,9 @@ struct FileStackNode {
union {
char *name; // NODE_FILE, NODE_MACRO
struct { // NODE_REPT
uint32_t reptDepth;
uint32_t depth;
uint32_t *iters;
};
} rept;
};
};

View File

@@ -56,7 +56,7 @@
#define restrict
// C++ doesn't support designated array initializers, but they're a gcc extension
#ifdef __GNUC__
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
# define AT(index) [index] =
#else
# define AT(index)

View File

@@ -455,7 +455,7 @@ void fstk_RunMacro(char const *macroName, struct MacroArgs *args)
memcpy(dest, macro->name, macroNameLen + 1);
newContext((struct FileStackNode *)fileInfo);
contextStack->lexerState = lexer_OpenFileView("MACRO", macro->macro, macro->macroSize,
contextStack->lexerState = lexer_OpenFileView("MACRO", macro->macro.value, macro->macro.size,
macro->fileLine);
if (!contextStack->lexerState)
fatalerror("Failed to set up lexer for macro invocation\n");

View File

@@ -319,13 +319,13 @@ struct LexerState {
size_t size;
size_t offset;
bool isReferenced; // If a macro in this file requires not unmapping it
};
} mmap;
struct { // Otherwise
int fd;
size_t index; // Read index into the buffer
char buf[LEXER_BUF_SIZE]; // Circular buffer
size_t nbChars; // Number of "fresh" chars in the buffer
};
} cbuf;
};
// Common state
@@ -454,8 +454,8 @@ struct LexerState *lexer_OpenFile(char const *path)
}
state->path = path;
state->isFile = true;
state->fd = isStdin ? STDIN_FILENO : open(path, O_RDONLY);
if (state->fd < 0) {
state->cbuf.fd = isStdin ? STDIN_FILENO : open(path, O_RDONLY);
if (state->cbuf.fd < 0) {
error("Failed to open file \"%s\": %s\n", path, strerror(errno));
free(state);
return NULL;
@@ -464,25 +464,25 @@ struct LexerState *lexer_OpenFile(char const *path)
if (!isStdin && fileInfo.st_size > 0) {
// Try using `mmap` for better performance
// Important: do NOT assign to `state->ptr` directly, to avoid a cast that may
// alter an eventual `MAP_FAILED` value. It would also invalidate `state->fd`,
// Important: do NOT assign to `state->mmap.ptr` directly, to avoid a cast that may
// alter an eventual `MAP_FAILED` value. It would also invalidate `state->cbuf.fd`,
// being on the other side of the union.
void *mappingAddr;
mapFile(mappingAddr, state->fd, state->path, fileInfo.st_size);
mapFile(mappingAddr, state->cbuf.fd, state->path, fileInfo.st_size);
if (mappingAddr == MAP_FAILED) {
// If mmap()ing failed, try again using another method (below)
state->isMmapped = false;
} else {
// IMPORTANT: the `union` mandates this is accessed before other members!
close(state->fd);
close(state->cbuf.fd);
state->isMmapped = true;
state->isReferenced = false; // By default, a state isn't referenced
state->ptr = (char *)mappingAddr;
state->mmap.isReferenced = false; // By default, a state isn't referenced
state->mmap.ptr = (char *)mappingAddr;
assert(fileInfo.st_size >= 0);
state->size = (size_t)fileInfo.st_size;
state->offset = 0;
state->mmap.size = (size_t)fileInfo.st_size;
state->mmap.offset = 0;
if (verbose)
printf("File %s successfully mmap()ped\n", path);
@@ -499,8 +499,8 @@ struct LexerState *lexer_OpenFile(char const *path)
printf("File %s opened as regular, errno reports \"%s\"\n",
path, strerror(errno));
}
state->index = 0;
state->nbChars = 0;
state->cbuf.index = 0;
state->cbuf.nbChars = 0;
}
initState(state);
@@ -520,9 +520,9 @@ struct LexerState *lexer_OpenFileView(char const *path, char *buf, size_t size,
state->path = path; // Used to report read errors in `peekInternal`
state->isFile = false;
state->isMmapped = true; // It's not *really* mmap()ed, but it behaves the same
state->ptr = buf;
state->size = size;
state->offset = 0;
state->mmap.ptr = buf;
state->mmap.size = size;
state->mmap.offset = 0;
initState(state);
state->lineNo = lineNo; // Will be incremented at first line start
@@ -531,7 +531,7 @@ struct LexerState *lexer_OpenFileView(char const *path, char *buf, size_t size,
void lexer_RestartRept(uint32_t lineNo)
{
lexerState->offset = 0;
lexerState->mmap.offset = 0;
initState(lexerState);
lexerState->lineNo = lineNo;
}
@@ -552,9 +552,9 @@ void lexer_DeleteState(struct LexerState *state)
assert(state != lexerStateEOL);
if (!state->isMmapped)
close(state->fd);
else if (state->isFile && !state->isReferenced)
munmap(state->ptr, state->size);
close(state->cbuf.fd);
else if (state->isFile && !state->mmap.isReferenced)
munmap(state->mmap.ptr, state->mmap.size);
free(state);
}
@@ -798,7 +798,7 @@ static size_t readInternal(size_t bufIndex, size_t nbChars)
{
// This buffer overflow made me lose WEEKS of my life. Never again.
assert(bufIndex + nbChars <= LEXER_BUF_SIZE);
ssize_t nbReadChars = read(lexerState->fd, &lexerState->buf[bufIndex], nbChars);
ssize_t nbReadChars = read(lexerState->cbuf.fd, &lexerState->cbuf.buf[bufIndex], nbChars);
if (nbReadChars == -1)
fatalerror("Error while reading \"%s\": %s\n", lexerState->path, strerror(errno));
@@ -824,25 +824,25 @@ static int peekInternal(uint8_t distance)
PRIu8 " >= %u)\n", distance, LEXER_BUF_SIZE);
if (lexerState->isMmapped) {
if (lexerState->offset + distance >= lexerState->size)
if (lexerState->mmap.offset + distance >= lexerState->mmap.size)
return EOF;
return (unsigned char)lexerState->ptr[lexerState->offset + distance];
return (unsigned char)lexerState->mmap.ptr[lexerState->mmap.offset + distance];
}
if (lexerState->nbChars <= distance) {
if (lexerState->cbuf.nbChars <= distance) {
// Buffer isn't full enough, read some chars in
size_t target = LEXER_BUF_SIZE - lexerState->nbChars; // Aim: making the buf full
size_t target = LEXER_BUF_SIZE - lexerState->cbuf.nbChars; // Aim: making the buf full
// Compute the index we'll start writing to
size_t writeIndex = (lexerState->index + lexerState->nbChars) % LEXER_BUF_SIZE;
size_t writeIndex = (lexerState->cbuf.index + lexerState->cbuf.nbChars) % LEXER_BUF_SIZE;
// If the range to fill passes over the buffer wrapping point, we need two reads
if (writeIndex + target > LEXER_BUF_SIZE) {
size_t nbExpectedChars = LEXER_BUF_SIZE - writeIndex;
size_t nbReadChars = readInternal(writeIndex, nbExpectedChars);
lexerState->nbChars += nbReadChars;
lexerState->cbuf.nbChars += nbReadChars;
writeIndex += nbReadChars;
if (writeIndex == LEXER_BUF_SIZE)
@@ -854,14 +854,14 @@ static int peekInternal(uint8_t distance)
target = 0;
}
if (target != 0)
lexerState->nbChars += readInternal(writeIndex, target);
lexerState->cbuf.nbChars += readInternal(writeIndex, target);
// If there aren't enough chars even after refilling, give up
if (lexerState->nbChars <= distance)
if (lexerState->cbuf.nbChars <= distance)
return EOF;
}
return (unsigned char)lexerState->buf[(lexerState->index + distance) % LEXER_BUF_SIZE];
return (unsigned char)lexerState->cbuf.buf[(lexerState->cbuf.index + distance) % LEXER_BUF_SIZE];
}
// forward declarations for peek
@@ -946,14 +946,14 @@ restart:
// Advance within the file contents
lexerState->colNo++;
if (lexerState->isMmapped) {
lexerState->offset++;
lexerState->mmap.offset++;
} else {
assert(lexerState->index < LEXER_BUF_SIZE);
lexerState->index++;
if (lexerState->index == LEXER_BUF_SIZE)
lexerState->index = 0; // Wrap around if necessary
assert(lexerState->nbChars > 0);
lexerState->nbChars--;
assert(lexerState->cbuf.index < LEXER_BUF_SIZE);
lexerState->cbuf.index++;
if (lexerState->cbuf.index == LEXER_BUF_SIZE)
lexerState->cbuf.index = 0; // Wrap around if necessary
assert(lexerState->cbuf.nbChars > 0);
lexerState->cbuf.nbChars--;
}
}
}
@@ -2458,7 +2458,7 @@ static void startCapture(struct CaptureBody *capture)
capture->lineNo = lexer_GetLineNo();
if (lexerState->isMmapped && !lexerState->expansions) {
capture->body = &lexerState->ptr[lexerState->offset];
capture->body = &lexerState->mmap.ptr[lexerState->mmap.offset];
} else {
lexerState->captureCapacity = 128; // The initial size will be twice that
assert(lexerState->captureBuf == NULL);
@@ -2545,7 +2545,7 @@ bool lexer_CaptureMacroBody(struct CaptureBody *capture)
// If the file is `mmap`ed, we need not to unmap it to keep access to the macro
if (lexerState->isMmapped)
lexerState->isReferenced = true;
lexerState->mmap.isReferenced = true;
int c = EOF;

View File

@@ -164,8 +164,8 @@ static void assignStringSymbol(struct Symbol *sym, char const *value)
fatalerror("No memory for string equate: %s\n", strerror(errno));
sym->type = SYM_EQUS;
sym->macro = string;
sym->macroSize = strlen(string);
sym->macro.value = string;
sym->macro.size = strlen(string);
}
struct Symbol *sym_FindExactSymbol(char const *symName)
@@ -233,8 +233,8 @@ void sym_Purge(char const *symName)
if (sym->name == labelScope)
sym_SetCurrentSymbolScope(NULL);
// FIXME: this leaks sym->macro for SYM_EQUS and SYM_MACRO, but this can't
// free(sym->macro) because the expansion may be purging itself.
// FIXME: this leaks sym->macro.value for SYM_EQUS and SYM_MACRO, but this can't
// free(sym->macro.value) because the expansion may be purging itself.
hash_RemoveElement(symbols, sym->name);
// TODO: ideally, also unref the file stack nodes
free(sym);
@@ -401,8 +401,8 @@ struct Symbol *sym_RedefString(char const *symName, char const *value)
}
updateSymbolFilename(sym);
// FIXME: this leaks the previous sym->macro value, but this can't
// free(sym->macro) because the expansion may be redefining itself.
// FIXME: this leaks the previous sym->macro.value value, but this can't
// free(sym->macro.value) because the expansion may be redefining itself.
assignStringSymbol(sym, value);
return sym;
@@ -574,8 +574,8 @@ struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body,
return NULL;
sym->type = SYM_MACRO;
sym->macroSize = size;
sym->macro = body;
sym->macro.size = size;
sym->macro.value = body;
setSymbolFilename(sym); // TODO: is this really necessary?
// The symbol is created at the line after the `endm`,
// override this with the actual definition line

View File

@@ -61,8 +61,8 @@ char const *dumpFileStack(struct FileStackNode const *node)
lastName = node->name;
fprintf(stderr, "(%" PRIu32 ") -> %s", node->lineNo, lastName);
if (node->type == NODE_REPT) {
for (uint32_t i = 0; i < node->reptDepth; i++)
fprintf(stderr, "::REPT~%" PRIu32, node->iters[i]);
for (uint32_t i = 0; i < node->rept.depth; i++)
fprintf(stderr, "::REPT~%" PRIu32, node->rept.iters[i]);
}
} else {
assert(node->type != NODE_REPT);

View File

@@ -180,15 +180,15 @@ static void readFileStackNode(FILE *file, struct FileStackNode fileNodes[], uint
break;
case NODE_REPT:
tryReadlong(fileNodes[i].reptDepth, file,
tryReadlong(fileNodes[i].rept.depth, file,
"%s: Cannot read node #%" PRIu32 "'s rept depth: %s", fileName, i);
fileNodes[i].iters =
(uint32_t *)malloc(sizeof(*fileNodes[i].iters) * fileNodes[i].reptDepth);
if (!fileNodes[i].iters)
fileNodes[i].rept.iters =
(uint32_t *)malloc(sizeof(*fileNodes[i].rept.iters) * fileNodes[i].rept.depth);
if (!fileNodes[i].rept.iters)
fatal(NULL, 0, "%s: Failed to alloc node #%" PRIu32 "'s iters: %s",
fileName, i, strerror(errno));
for (uint32_t k = 0; k < fileNodes[i].reptDepth; k++)
tryReadlong(fileNodes[i].iters[k], file,
for (uint32_t k = 0; k < fileNodes[i].rept.depth; k++)
tryReadlong(fileNodes[i].rept.iters[k], file,
"%s: Cannot read node #%" PRIu32 "'s iter #%" PRIu32 ": %s",
fileName, i, k);
if (!fileNodes[i].parent)
@@ -659,7 +659,7 @@ void obj_Setup(unsigned int nbFiles)
static void freeNode(struct FileStackNode *node)
{
if (node->type == NODE_REPT)
free(node->iters);
free(node->rept.iters);
else
free(node->name);
}