Use std::string_view for macro bodies (#1326)

This removes the last use of `strdup`

This required making a lot of related pointers be `const`.
That in turn conflicted with the need to `munmap()` a pointer
eventually, which was similar to the need to eventually `free()`
an `Expansion`'s contents, so I used the same solution of a
`union`. That lets us normally use the `const` pointer for
`const` correctness, and the non-`const` one for the not-really-
mutating destruction cases.
This commit is contained in:
Sylvie
2024-03-02 13:21:28 -05:00
committed by GitHub
parent 2069a95e0f
commit a71e4086a2
7 changed files with 47 additions and 59 deletions

View File

@@ -61,9 +61,9 @@ std::string *fstk_FindFile(char const *path);
bool yywrap();
void fstk_RunInclude(char const *path);
void fstk_RunMacro(char const *macroName, MacroArgs *args);
void fstk_RunRept(uint32_t count, int32_t reptLineNo, char *body, size_t size);
void fstk_RunRept(uint32_t count, int32_t reptLineNo, char const *body, size_t size);
void fstk_RunFor(char const *symName, int32_t start, int32_t stop, int32_t step,
int32_t reptLineNo, char *body, size_t size);
int32_t reptLineNo, char const *body, size_t size);
void fstk_StopRept();
bool fstk_Break();

View File

@@ -30,7 +30,7 @@ struct Expansion {
std::optional<std::string> name;
union {
char const *unowned;
char *owned;
char *owned; // Non-`const` only so it can be `free()`d
} contents;
size_t size; // Length of the contents
size_t offset; // Cursor into the contents
@@ -43,7 +43,10 @@ struct IfStackEntry {
};
struct MmappedLexerState {
char *ptr; // Technically `const` during the lexer's execution
union {
char const *unreferenced;
char *referenced; // Non-`const` only so it can be `munmap()`ped
} ptr;
size_t size;
size_t offset;
bool isReferenced; // If a macro in this file requires not unmapping it
@@ -121,7 +124,8 @@ static inline void lexer_SetGfxDigits(char const digits[4])
// `path` is referenced, but not held onto..!
bool lexer_OpenFile(LexerState &state, char const *path);
void lexer_OpenFileView(LexerState &state, char const *path, char *buf, size_t size, uint32_t lineNo);
void lexer_OpenFileView(LexerState &state, char const *path, char const *buf, size_t size,
uint32_t lineNo);
void lexer_RestartRept(uint32_t lineNo);
void lexer_CleanupState(LexerState &state);
void lexer_Init();
@@ -138,7 +142,7 @@ void lexer_ReachELSEBlock();
struct CaptureBody {
uint32_t lineNo;
char *body;
char const *body;
size_t size;
};

View File

@@ -6,6 +6,7 @@
#include <stdint.h>
#include <string>
#include <string.h>
#include <string_view>
#include <time.h>
#include "asm/section.hpp"
@@ -23,12 +24,6 @@ enum SymbolType {
SYM_REF // Forward reference to a label
};
// Only used in an anonymous union by `Symbol`
struct strValue {
size_t size;
char *value;
};
struct Symbol; // For the `sym_IsPC` forward declaration
bool sym_IsPC(Symbol const *sym); // For the inline `getSection` method
@@ -43,14 +38,11 @@ struct Symbol {
bool hasCallback;
union {
// If isNumeric()
int32_t value;
int32_t (*numCallback)(); // If hasCallback
// For SYM_MACRO
strValue macro;
// For SYM_EQUS
strValue equs;
char const *(*strCallback)(); // If hasCallback
int32_t value; // If isNumeric()
int32_t (*numCallback)(); // If isNumeric() and hasCallback
std::string_view *macro; // For SYM_MACRO
std::string *equs; // For SYM_EQUS
char const *(*strCallback)(); // For SYM_EQUS if hasCallback
};
uint32_t ID; // ID of the symbol in the object file (-1 if none)
@@ -71,7 +63,7 @@ struct Symbol {
int32_t getValue() const;
uint32_t getConstantValue() const;
char const *getStringValue() const { return hasCallback ? strCallback() : equs.value; }
char const *getStringValue() const { return hasCallback ? strCallback() : equs->c_str(); }
};
void sym_ForEach(void (*func)(Symbol *));
@@ -94,7 +86,7 @@ Symbol *sym_FindScopedSymbol(char const *symName);
// Find a scoped symbol by name; do not return `@` or `_NARG` when they have no value
Symbol *sym_FindScopedValidSymbol(char const *symName);
Symbol const *sym_GetPC();
Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, size_t size);
Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char const *body, size_t size);
Symbol *sym_Ref(char const *symName);
Symbol *sym_AddString(char const *symName, char const *value);
Symbol *sym_RedefString(char const *symName, char const *value);