From 043db49676d679b1ee4aeab338ff5a5a92f9a49a Mon Sep 17 00:00:00 2001 From: Sylvie <35663410+Rangi42@users.noreply.github.com> Date: Thu, 29 Feb 2024 15:06:33 -0500 Subject: [PATCH] Replace `NULL` with `nullptr` (#1321) --- include/asm/fstack.hpp | 2 +- include/asm/lexer.hpp | 2 +- include/link/patch.hpp | 2 +- include/link/section.hpp | 2 +- include/link/symbol.hpp | 2 +- src/asm/charmap.cpp | 4 +-- src/asm/fstack.cpp | 8 ++--- src/asm/lexer.cpp | 44 +++++++++++++-------------- src/asm/macro.cpp | 12 ++++---- src/asm/main.cpp | 58 ++++++++++++++++++------------------ src/asm/output.cpp | 2 +- src/asm/parser.y | 16 +++++----- src/asm/rpn.cpp | 14 ++++----- src/asm/section.cpp | 28 +++++++++--------- src/asm/symbol.cpp | 42 +++++++++++++------------- src/asm/warning.cpp | 4 +-- src/fix/main.cpp | 42 +++++++++++++------------- src/gfx/main.cpp | 64 ++++++++++++++++++++-------------------- src/link/assign.cpp | 26 ++++++++-------- src/link/main.cpp | 36 +++++++++++----------- src/link/object.cpp | 20 ++++++------- src/link/output.cpp | 4 +-- src/link/script.y | 4 +-- src/link/sdas_obj.cpp | 36 +++++++++++----------- src/link/section.cpp | 22 +++++++------- src/link/symbol.cpp | 2 +- test/gfx/randtilegen.cpp | 8 ++--- 27 files changed, 252 insertions(+), 254 deletions(-) diff --git a/include/asm/fstack.hpp b/include/asm/fstack.hpp index d7612950..db444241 100644 --- a/include/asm/fstack.hpp +++ b/include/asm/fstack.hpp @@ -53,7 +53,7 @@ void fstk_AddIncludePath(char const *s); void fstk_SetPreIncludeFile(char const *s); /* * @param path The user-provided file name - * @return A pointer to the `new`-allocated full path, or NULL if no path worked + * @return A pointer to the `new`-allocated full path, or `nullptr` if no path worked */ std::string *fstk_FindFile(char const *path); diff --git a/include/asm/lexer.hpp b/include/asm/lexer.hpp index 73b98c04..fbfa9dc6 100644 --- a/include/asm/lexer.hpp +++ b/include/asm/lexer.hpp @@ -77,7 +77,7 @@ struct LexerState { bool capturing; // Whether the text being lexed should be captured size_t captureSize; // Amount of text captured - char *captureBuf; // Buffer to send the captured text to if non-NULL + char *captureBuf; // Buffer to send the captured text to if non-null size_t captureCapacity; // Size of the buffer above bool disableMacroArgs; diff --git a/include/link/patch.hpp b/include/link/patch.hpp index 9914c07d..4607c2b4 100644 --- a/include/link/patch.hpp +++ b/include/link/patch.hpp @@ -15,7 +15,7 @@ struct Assertion { Patch patch; // Also used for its `.type` std::string message; - // This would be redundant with `.section->fileSymbols`... but `section` is sometimes NULL! + // This would be redundant with `.section->fileSymbols`, but `section` is sometimes `nullptr`! std::vector *fileSymbols; }; diff --git a/include/link/section.hpp b/include/link/section.hpp index 1c7b6af6..40609cf8 100644 --- a/include/link/section.hpp +++ b/include/link/section.hpp @@ -69,7 +69,7 @@ void sect_AddSection(Section *section); /* * Finds a section by its name. * @param name The name of the section to look for - * @return A pointer to the section, or NULL if it wasn't found + * @return A pointer to the section, or `nullptr` if it wasn't found */ Section *sect_GetSection(std::string const &name); diff --git a/include/link/symbol.hpp b/include/link/symbol.hpp index 2dbab52d..7dea7dcd 100644 --- a/include/link/symbol.hpp +++ b/include/link/symbol.hpp @@ -36,7 +36,7 @@ void sym_AddSymbol(Symbol *symbol); /* * Finds a symbol in all the defined symbols. * @param name The name of the symbol to look for - * @return A pointer to the symbol, or NULL if not found. + * @return A pointer to the symbol, or `nullptr` if not found. */ Symbol *sym_GetSymbol(std::string const &name); diff --git a/src/asm/charmap.cpp b/src/asm/charmap.cpp index a1872313..8fa7c16c 100644 --- a/src/asm/charmap.cpp +++ b/src/asm/charmap.cpp @@ -40,9 +40,9 @@ std::stack charmapStack; void charmap_New(char const *name, char const *baseName) { - Charmap *base = NULL; + Charmap *base = nullptr; - if (baseName != NULL) { + if (baseName != nullptr) { auto search = charmaps.find(baseName); if (search == charmaps.end()) diff --git a/src/asm/fstack.cpp b/src/asm/fstack.cpp index 87c3173d..9f490f35 100644 --- a/src/asm/fstack.cpp +++ b/src/asm/fstack.cpp @@ -104,7 +104,7 @@ void fstk_DumpCurrent(void) FileStackNode *fstk_GetFileStack(void) { if (contextStack.empty()) - return NULL; + return nullptr; FileStackNode *topNode = contextStack.top().fileInfo; @@ -185,7 +185,7 @@ std::string *fstk_FindFile(char const *path) errno = ENOENT; if (generatedMissingIncludes) printDep(path); - return NULL; + return nullptr; } bool yywrap(void) @@ -255,7 +255,7 @@ bool yywrap(void) // Make sure not to switch the lexer state before calling this, so the saved line no is correct. // BE CAREFUL! This modifies the file stack directly, you should have set up the file info first. -// Callers should set `contextStack.top().lexerState` after this so it is not NULL. +// Callers should set `contextStack.top().lexerState` after this so it is not `nullptr`. static Context &newContext(FileStackNode *fileInfo) { if (contextStack.size() > maxRecursionDepth) @@ -518,7 +518,7 @@ void fstk_Init(char const *mainPath, size_t maxDepth) fileInfo->type = NODE_FILE; fileInfo->data = lexer_GetFileName(); // lineNo and nbReptIters are unused on the top-level context - fileInfo->parent = NULL; + fileInfo->parent = nullptr; fileInfo->lineNo = 0; // This still gets written to the object file, so init it fileInfo->referenced = false; diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index be57df4e..15481065 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -42,16 +42,16 @@ # include // CreateFileMappingA # include // MapViewOfFile # include // CloseHandle -# define MAP_FAILED NULL +# define MAP_FAILED nullptr # define mapFile(ptr, fd, path, size) do { \ (ptr) = MAP_FAILED; \ - HANDLE file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \ - FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_RANDOM_ACCESS, NULL); \ + HANDLE file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, \ + FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_RANDOM_ACCESS, nullptr); \ HANDLE mappingObj; \ \ if (file == INVALID_HANDLE_VALUE) \ break; \ - mappingObj = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, NULL); \ + mappingObj = CreateFileMappingA(file, nullptr, PAGE_READONLY, 0, 0, nullptr); \ if (mappingObj != INVALID_HANDLE_VALUE) \ (ptr) = MapViewOfFile(mappingObj, FILE_MAP_READ, 0, 0, 0); \ CloseHandle(mappingObj); \ @@ -63,7 +63,7 @@ # include # define mapFile(ptr, fd, path, size) do { \ - (ptr) = mmap(NULL, (size), PROT_READ, MAP_PRIVATE, (fd), 0); \ + (ptr) = mmap(nullptr, (size), PROT_READ, MAP_PRIVATE, (fd), 0); \ \ if ((ptr) == MAP_FAILED && errno == ENOTSUP) { \ /* @@ -73,7 +73,7 @@ */ \ if (verbose) \ printf("mmap(%s, MAP_PRIVATE) failed, retrying with MAP_SHARED\n", path); \ - (ptr) = mmap(NULL, (size), PROT_READ, MAP_SHARED, (fd), 0); \ + (ptr) = mmap(nullptr, (size), PROT_READ, MAP_SHARED, (fd), 0); \ } \ } while (0) #endif // !( defined(_MSC_VER) || defined(__MINGW32__) ) @@ -298,8 +298,8 @@ static bool isWhitespace(int c) return c == ' ' || c == '\t'; } -LexerState *lexerState = NULL; -LexerState *lexerStateEOL = NULL; +LexerState *lexerState = nullptr; +LexerState *lexerStateEOL = nullptr; static void initState(LexerState &state) { @@ -310,7 +310,7 @@ static void initState(LexerState &state) state.ifStack.clear(); state.capturing = false; - state.captureBuf = NULL; + state.captureBuf = nullptr; state.disableMacroArgs = false; state.disableInterpolation = false; @@ -509,7 +509,7 @@ static void beginExpansion(char const *str, bool owned, char const *name) lexer_CheckRecursionDepth(); lexerState->expansions.push_front({ - .name = name ? strdup(name) : NULL, + .name = name ? strdup(name) : nullptr, .contents = { .unowned = str }, .size = size, .offset = 0, @@ -610,7 +610,7 @@ static uint32_t readBracketedMacroArgNum(void) static char const *readMacroArg(char name) { - char const *str = NULL; + char const *str = nullptr; if (name == '@') { str = macro_GetUniqueIDStr(); @@ -620,14 +620,14 @@ static char const *readMacroArg(char name) uint32_t num = readBracketedMacroArgNum(); if (num == 0) - return NULL; + return nullptr; str = macro_GetArg(num); if (!str) error("Macro argument '\\<%" PRIu32 ">' not defined\n", num); return str; } else if (name == '0') { error("Invalid macro argument '\\0'\n"); - return NULL; + return nullptr; } else { assert(name > '0' && name <= '9'); str = macro_GetArg(name - '0'); @@ -735,7 +735,7 @@ static int peek(void) if (!str || !str[0]) return peek(); - beginExpansion(str, c == '#', NULL); + beginExpansion(str, c == '#', nullptr); // Assuming macro args can't be recursive (I'll be damned if a way // is found...), then we mark the entire macro arg as scanned. @@ -822,7 +822,7 @@ static void handleCRLF(int c) char const *lexer_GetFileName(void) { - return lexerState ? lexerState->path : NULL; + return lexerState ? lexerState->path : nullptr; } uint32_t lexer_GetLineNo(void) @@ -1260,7 +1260,7 @@ static char const *readInterpolation(size_t depth) } else { error("Only numerical and string symbols can be interpolated\n"); } - return NULL; + return nullptr; } #define append_yylval_string(c) do { \ @@ -2259,7 +2259,7 @@ int yylex(void) { if (lexerState->atLineStart && lexerStateEOL) { lexer_SetState(lexerStateEOL); - lexerStateEOL = NULL; + lexerStateEOL = nullptr; } // `lexer_SetState` updates `lexerState`, so check for EOF after it if (lexerState->lastToken == T_EOB && yywrap()) @@ -2300,22 +2300,22 @@ static void startCapture(CaptureBody *capture) capture->body = &lexerState->mmap.ptr[lexerState->mmap.offset]; } else { lexerState->captureCapacity = 128; // The initial size will be twice that - assert(lexerState->captureBuf == NULL); + assert(lexerState->captureBuf == nullptr); reallocCaptureBuf(); - capture->body = NULL; // Indicate to retrieve the capture buffer when done capturing + capture->body = nullptr; // Indicate to retrieve the capture buffer when done capturing } } static void endCapture(CaptureBody *capture) { - // This being NULL means we're capturing from the capture buf, which is `realloc`'d during - // the whole capture process, and so MUST be retrieved at the end + // This being `nullptr` means we're capturing from the capture buf, which is `realloc`ed + // during the whole capture process, and so MUST be retrieved at the end if (!capture->body) capture->body = lexerState->captureBuf; capture->size = lexerState->captureSize; lexerState->capturing = false; - lexerState->captureBuf = NULL; + lexerState->captureBuf = nullptr; lexerState->disableMacroArgs = false; lexerState->disableInterpolation = false; } diff --git a/src/asm/macro.cpp b/src/asm/macro.cpp index ac39b434..cf381436 100644 --- a/src/asm/macro.cpp +++ b/src/asm/macro.cpp @@ -18,14 +18,14 @@ struct MacroArgs { std::vector args; }; -static MacroArgs *macroArgs = NULL; +static MacroArgs *macroArgs = nullptr; static uint32_t uniqueID = 0; static uint32_t maxUniqueID = 0; // The initialization is somewhat harmful, since it is never used, but it // guarantees the size of the buffer will be correct. I was unable to find a // better solution, but if you have one, please feel free! static char uniqueIDBuf[] = "_u4294967295"; // UINT32_MAX -static char *uniqueIDPtr = NULL; +static char *uniqueIDPtr = nullptr; MacroArgs *macro_GetCurrentArgs(void) { @@ -66,17 +66,17 @@ void macro_FreeArgs(MacroArgs *args) char const *macro_GetArg(uint32_t i) { if (!macroArgs) - return NULL; + return nullptr; uint32_t realIndex = i + macroArgs->shift - 1; - return realIndex >= macroArgs->args.size() ? NULL : macroArgs->args[realIndex]; + return realIndex >= macroArgs->args.size() ? nullptr : macroArgs->args[realIndex]; } char const *macro_GetAllArgs(void) { if (!macroArgs) - return NULL; + return nullptr; size_t nbArgs = macroArgs->args.size(); @@ -128,7 +128,7 @@ void macro_SetUniqueID(uint32_t id) { uniqueID = id; if (id == 0 || id == (uint32_t)-1) { - uniqueIDPtr = NULL; + uniqueIDPtr = nullptr; } else { // The buffer is guaranteed to be the correct size // This is a valid label fragment, but not a valid numeric diff --git a/src/asm/main.cpp b/src/asm/main.cpp index 612cd883..bcd30455 100644 --- a/src/asm/main.cpp +++ b/src/asm/main.cpp @@ -51,7 +51,7 @@ char const *__asan_default_options(void) { return "detect_leaks=0"; } // Unfortunately, macOS still ships 2.3, which is from 2008... int yyparse(void); -FILE *dependfile = NULL; +FILE *dependfile = nullptr; bool generatedMissingIncludes = false; bool failedOnMissingInclude = false; bool generatePhonyDeps = false; @@ -98,31 +98,31 @@ static int depType; // Variants of `-M` // This is because long opt matching, even to a single char, is prioritized // over short opt matching static option const longopts[] = { - { "binary-digits", required_argument, NULL, 'b' }, - { "define", required_argument, NULL, 'D' }, - { "export-all", no_argument, NULL, 'E' }, - { "gfx-chars", required_argument, NULL, 'g' }, - { "nop-after-halt", no_argument, NULL, 'H' }, - { "halt-without-nop", no_argument, NULL, 'h' }, - { "include", required_argument, NULL, 'I' }, - { "preserve-ld", no_argument, NULL, 'L' }, - { "auto-ldh", no_argument, NULL, 'l' }, - { "dependfile", required_argument, NULL, 'M' }, + { "binary-digits", required_argument, nullptr, 'b' }, + { "define", required_argument, nullptr, 'D' }, + { "export-all", no_argument, nullptr, 'E' }, + { "gfx-chars", required_argument, nullptr, 'g' }, + { "nop-after-halt", no_argument, nullptr, 'H' }, + { "halt-without-nop", no_argument, nullptr, 'h' }, + { "include", required_argument, nullptr, 'I' }, + { "preserve-ld", no_argument, nullptr, 'L' }, + { "auto-ldh", no_argument, nullptr, 'l' }, + { "dependfile", required_argument, nullptr, 'M' }, { "MG", no_argument, &depType, 'G' }, { "MP", no_argument, &depType, 'P' }, { "MT", required_argument, &depType, 'T' }, - { "warning", required_argument, NULL, 'W' }, + { "warning", required_argument, nullptr, 'W' }, { "MQ", required_argument, &depType, 'Q' }, - { "output", required_argument, NULL, 'o' }, - { "preinclude", required_argument, NULL, 'P' }, - { "pad-value", required_argument, NULL, 'p' }, - { "q-precision", required_argument, NULL, 'Q' }, - { "recursion-depth", required_argument, NULL, 'r' }, - { "version", no_argument, NULL, 'V' }, - { "verbose", no_argument, NULL, 'v' }, - { "warning", required_argument, NULL, 'W' }, - { "max-errors", required_argument, NULL, 'X' }, - { NULL, no_argument, NULL, 0 } + { "output", required_argument, nullptr, 'o' }, + { "preinclude", required_argument, nullptr, 'P' }, + { "pad-value", required_argument, nullptr, 'p' }, + { "q-precision", required_argument, nullptr, 'Q' }, + { "recursion-depth", required_argument, nullptr, 'r' }, + { "version", no_argument, nullptr, 'V' }, + { "verbose", no_argument, nullptr, 'v' }, + { "warning", required_argument, nullptr, 'W' }, + { "max-errors", required_argument, nullptr, 'X' }, + { nullptr, no_argument, nullptr, 0 } }; static void printUsage(void) @@ -146,13 +146,13 @@ static void printUsage(void) int main(int argc, char *argv[]) { - time_t now = time(NULL); + time_t now = time(nullptr); char const *sourceDateEpoch = getenv("SOURCE_DATE_EPOCH"); // Support SOURCE_DATE_EPOCH for reproducible builds // https://reproducible-builds.org/docs/source-date-epoch/ if (sourceDateEpoch) - now = (time_t)strtoul(sourceDateEpoch, NULL, 0); + now = (time_t)strtoul(sourceDateEpoch, nullptr, 0); // Perform some init for below sym_Init(now); @@ -171,14 +171,14 @@ int main(int argc, char *argv[]) warnings = true; sym_SetExportAll(false); uint32_t maxDepth = DEFAULT_MAX_DEPTH; - char const *dependFileName = NULL; + char const *dependFileName = nullptr; std::string newTarget; // Maximum of 100 errors only applies if rgbasm is printing errors to a terminal. if (isatty(STDERR_FILENO)) maxErrors = 100; - for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, NULL)) != -1;) { + for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, nullptr)) != -1;) { switch (ch) { char *endptr; @@ -258,7 +258,7 @@ int main(int argc, char *argv[]) dependfile = fopen(musl_optarg, "w"); dependFileName = musl_optarg; } - if (dependfile == NULL) + if (dependfile == nullptr) err("Failed to open dependfile \"%s\"", dependFileName); break; @@ -392,7 +392,7 @@ int main(int argc, char *argv[]) fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), mainFileName); } - charmap_New(DEFAULT_CHARMAP_NAME, NULL); + charmap_New(DEFAULT_CHARMAP_NAME, nullptr); // Init lexer and file stack, providing file info fstk_Init(mainFileName, maxDepth); @@ -414,7 +414,7 @@ int main(int argc, char *argv[]) return 0; // If no path specified, don't write file - if (objectName != NULL) + if (objectName != nullptr) out_WriteObject(); return 0; } diff --git a/src/asm/output.cpp b/src/asm/output.cpp index e9983884..dec52513 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -51,7 +51,7 @@ static void putlong(uint32_t i, FILE *f) putc(i >> 24, f); } -// Write a NULL-terminated string to a file +// Write a NUL-terminated string to a file static void putstring(char const *s, FILE *f) { while (*s) diff --git a/src/asm/parser.y b/src/asm/parser.y index 457d85d6..79fbec5b 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -75,13 +75,13 @@ static const char *strrstr(char const *s1, char const *s2) size_t len2 = strlen(s2); if (len2 > len1) - return NULL; + return nullptr; for (char const *p = s1 + len1 - len2; p >= s1; p--) if (!strncmp(p, s2, len2)) return p; - return NULL; + return nullptr; } static void errorInvalidUTF8Byte(uint8_t byte, char const *functionName) @@ -172,7 +172,7 @@ static size_t charlenUTF8(char const *s) { size_t len; - for (len = 0; charmap_ConvertNext(&s, NULL); len++) + for (len = 0; charmap_ConvertNext(&s, nullptr); len++) ; return len; @@ -184,11 +184,11 @@ static void charsubUTF8(char *dest, char const *src, uint32_t pos) // Advance to starting position in source string. for (uint32_t curPos = 1; charLen && curPos < pos; curPos++) - charLen = charmap_ConvertNext(&src, NULL); + charLen = charmap_ConvertNext(&src, nullptr); char const *start = src; - if (!charmap_ConvertNext(&src, NULL)) + if (!charmap_ConvertNext(&src, nullptr)) warning(WARNING_BUILTIN_ARG, "CHARSUB: Position %" PRIu32 " is past the end of the string\n", pos); @@ -922,7 +922,7 @@ assignment : T_LABEL T_POP_EQUAL const { sym_AddVar($1, $3); } | T_LABEL compoundeq const { - const char *compoundEqOperator = NULL; + const char *compoundEqOperator = nullptr; switch ($2) { case RPN_ADD: compoundEqOperator = "+="; break; case RPN_SUB: compoundEqOperator = "-="; break; @@ -1287,7 +1287,7 @@ charmap : T_POP_CHARMAP string T_COMMA const_8bit { } ; -newcharmap : T_POP_NEWCHARMAP T_ID { charmap_New($2, NULL); } +newcharmap : T_POP_NEWCHARMAP T_ID { charmap_New($2, nullptr); } | T_POP_NEWCHARMAP T_ID T_COMMA T_ID { charmap_New($2, $4); } ; @@ -1509,7 +1509,7 @@ relocexpr_no_str : scoped_anon_id { rpn_Symbol(&$$, $1); } | T_OP_DEF { lexer_ToggleStringExpansion(false); } T_LPAREN scoped_anon_id T_RPAREN { - rpn_Number(&$$, sym_FindScopedValidSymbol($4) != NULL); + rpn_Number(&$$, sym_FindScopedValidSymbol($4) != nullptr); lexer_ToggleStringExpansion(true); } diff --git a/src/asm/rpn.cpp b/src/asm/rpn.cpp index 513f39da..59a7bfcf 100644 --- a/src/asm/rpn.cpp +++ b/src/asm/rpn.cpp @@ -49,10 +49,10 @@ static uint8_t *reserveSpace(Expression *expr, uint32_t size) // Init a RPN expression static void rpn_Init(Expression *expr) { - expr->reason = NULL; + expr->reason = nullptr; expr->isKnown = true; expr->isSymbol = false; - expr->rpn = NULL; + expr->rpn = nullptr; expr->rpnPatchSize = 0; } @@ -303,7 +303,7 @@ void rpn_LOGNOT(Expression *expr, const Expression *src) Symbol const *rpn_SymbolOf(Expression const *expr) { if (!rpn_isSymbol(expr)) - return NULL; + return nullptr; return sym_FindScopedSymbol((char const *)&(*expr->rpn)[1]); } @@ -527,7 +527,7 @@ void rpn_BinaryOp(enum RPNCommand op, Expression *expr, const Expression *src1, uint8_t bytes[] = {RPN_CONST, (uint8_t)lval, (uint8_t)(lval >> 8), (uint8_t)(lval >> 16), (uint8_t)(lval >> 24)}; expr->rpnPatchSize = sizeof(bytes); - expr->rpn = NULL; + expr->rpn = nullptr; memcpy(reserveSpace(expr, sizeof(bytes)), bytes, sizeof(bytes)); // Use the other expression's un-const reason @@ -542,7 +542,7 @@ void rpn_BinaryOp(enum RPNCommand op, Expression *expr, const Expression *src1, } // Now, merge the right expression into the left one - uint8_t const *ptr = NULL; + uint8_t const *ptr = nullptr; uint32_t len = 0; uint32_t patchSize = 0; @@ -563,11 +563,11 @@ void rpn_BinaryOp(enum RPNCommand op, Expression *expr, const Expression *src1, uint8_t *buf = reserveSpace(expr, len + 1); if (ptr) - // If there was none, `memcpy(buf, NULL, 0)` would be UB + // If there was none, `memcpy(buf, nullptr, 0)` would be UB memcpy(buf, ptr, len); buf[len] = op; - delete src2->rpn; // If there was none, this is `delete NULL` + delete src2->rpn; // If there was none, this is `delete nullptr` expr->rpnPatchSize += patchSize + 1; } } diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 9a6fd2c9..6385787b 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -45,9 +45,9 @@ std::stack currentUnionStack; std::deque sectionStack; std::deque
sectionList; uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset) -Section *currentSection = NULL; -static Section *currentLoadSection = NULL; -char const *currentLoadScope = NULL; +Section *currentSection = nullptr; +static Section *currentLoadSection = nullptr; +char const *currentLoadScope = nullptr; int32_t loadOffset; // Offset into the LOAD section's parent (see sect_GetOutputOffset) // A quick check to see if we have an initialized section @@ -116,7 +116,7 @@ Section *sect_FindSectionByName(char const *name) if (strcmp(name, sect.name) == 0) return § } - return NULL; + return nullptr; } #define mask(align) ((1U << (align)) - 1) @@ -271,7 +271,7 @@ static Section *createSection(char const *name, enum SectionType type, uint32_t Section § = sectionList.emplace_front(); sect.name = strdup(name); - if (sect.name == NULL) + if (sect.name == nullptr) fatalerror("Not enough memory for section name: %s\n", strerror(errno)); sect.type = type; @@ -372,7 +372,7 @@ static void changeSection(void) if (!currentUnionStack.empty()) fatalerror("Cannot change the section within a UNION\n"); - sym_SetCurrentSymbolScope(NULL); + sym_SetCurrentSymbolScope(nullptr); } // Set the current section by name and type @@ -441,7 +441,7 @@ void sect_EndLoadSection(void) changeSection(); curOffset += loadOffset; loadOffset = 0; - currentLoadSection = NULL; + currentLoadSection = nullptr; sym_SetCurrentSymbolScope(currentLoadScope); } @@ -790,7 +790,7 @@ void sect_BinaryFile(char const *s, int32_t startPos) return; std::string *fullPath = fstk_FindFile(s); - FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : NULL; + FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr; delete fullPath; @@ -861,7 +861,7 @@ void sect_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length) return; std::string *fullPath = fstk_FindFile(s); - FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : NULL; + FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr; delete fullPath; @@ -932,9 +932,9 @@ void sect_PushSection(void) }); // Reset the section scope - currentSection = NULL; - currentLoadSection = NULL; - sym_SetCurrentSymbolScope(NULL); + currentSection = nullptr; + currentLoadSection = nullptr; + sym_SetCurrentSymbolScope(nullptr); std::swap(currentUnionStack, sectionStack.front().unionStack); } @@ -970,8 +970,8 @@ void sect_EndSection(void) fatalerror("Cannot end the section within a UNION\n"); // Reset the section scope - currentSection = NULL; - sym_SetCurrentSymbolScope(NULL); + currentSection = nullptr; + sym_SetCurrentSymbolScope(nullptr); } bool sect_IsSizeKnown(Section const NONNULL(sect)) diff --git a/src/asm/symbol.cpp b/src/asm/symbol.cpp index c21a9576..53e9a9ba 100644 --- a/src/asm/symbol.cpp +++ b/src/asm/symbol.cpp @@ -91,8 +91,8 @@ static void dumpFilename(Symbol const *sym) // Set a symbol's definition filename and line static void setSymbolFilename(Symbol *sym) { - sym->src = fstk_GetFileStack(); - sym->fileLine = sym->src ? lexer_GetLineNo() : 0; // This is (NULL, 1) for built-ins + sym->src = fstk_GetFileStack(); // This is `nullptr` for built-ins + sym->fileLine = sym->src ? lexer_GetLineNo() : 0; // This is 1 for built-ins } // Update a symbol's definition filename and line @@ -118,7 +118,7 @@ static Symbol *createsymbol(char const *symName) sym.isExported = false; sym.isBuiltin = false; sym.hasCallback = false; - sym.section = NULL; + sym.section = nullptr; setSymbolFilename(&sym); sym.ID = -1; @@ -153,7 +153,7 @@ static void assignStringSymbol(Symbol *sym, char const *value) Symbol *sym_FindExactSymbol(char const *symName) { auto search = symbols.find(symName); - return search != symbols.end() ? &search->second : NULL; + return search != symbols.end() ? &search->second : nullptr; } Symbol *sym_FindScopedSymbol(char const *symName) @@ -179,11 +179,11 @@ Symbol *sym_FindScopedValidSymbol(char const *symName) // `@` has no value outside a section if (sym == PCSymbol && !sect_GetSymbolSection()) { - return NULL; + return nullptr; } // `_NARG` has no value outside a macro if (sym == _NARGSymbol && !macro_GetCurrentArgs()) { - return NULL; + return nullptr; } return sym; } @@ -212,7 +212,7 @@ void sym_Purge(std::string const &symName) } else { // Do not keep a reference to the label's name after purging it if (sym->name == labelScope) - sym_SetCurrentSymbolScope(NULL); + sym_SetCurrentSymbolScope(nullptr); // FIXME: this leaks sym->equs.value for SYM_EQUS and sym->macro.value for SYM_MACRO, // but this can't free either of them because the expansion may be purging itself. @@ -287,13 +287,13 @@ static Symbol *createNonrelocSymbol(char const *symName, bool numeric) error("'%s' already defined at ", symName); dumpFilename(sym); putc('\n', stderr); - return NULL; // Don't allow overriding the symbol, that'd be bad! + return nullptr; // Don't allow overriding the symbol, that'd be bad! } else if (!numeric) { // The symbol has already been referenced, but it's not allowed error("'%s' already referenced at ", symName); dumpFilename(sym); putc('\n', stderr); - return NULL; // Don't allow overriding the symbol, that'd be bad! + return nullptr; // Don't allow overriding the symbol, that'd be bad! } return sym; @@ -305,7 +305,7 @@ Symbol *sym_AddEqu(char const *symName, int32_t value) Symbol *sym = createNonrelocSymbol(symName, true); if (!sym) - return NULL; + return nullptr; sym->type = SYM_EQU; sym->value = value; @@ -324,10 +324,10 @@ Symbol *sym_RedefEqu(char const *symName, int32_t value) error("'%s' already defined as non-EQU at ", symName); dumpFilename(sym); putc('\n', stderr); - return NULL; + return nullptr; } else if (sym->isBuiltin) { error("Built-in symbol '%s' cannot be redefined\n", symName); - return NULL; + return nullptr; } updateSymbolFilename(sym); @@ -354,7 +354,7 @@ Symbol *sym_AddString(char const *symName, char const *value) Symbol *sym = createNonrelocSymbol(symName, false); if (!sym) - return NULL; + return nullptr; assignStringSymbol(sym, value); return sym; @@ -374,10 +374,10 @@ Symbol *sym_RedefString(char const *symName, char const *value) error("'%s' already referenced at ", symName); dumpFilename(sym); putc('\n', stderr); - return NULL; + return nullptr; } else if (sym->isBuiltin) { error("Built-in symbol '%s' cannot be redefined\n", symName); - return NULL; + return nullptr; } updateSymbolFilename(sym); @@ -427,7 +427,7 @@ static Symbol *addLabel(char const *symName) error("'%s' already defined at ", symName); dumpFilename(sym); putc('\n', stderr); - return NULL; + return nullptr; } else { updateSymbolFilename(sym); } @@ -468,7 +468,7 @@ Symbol *sym_AddLocalLabel(char const *symName) if (localName == symName) { if (!labelScope) { error("Unqualified local label '%s' in main scope\n", symName); - return NULL; + return nullptr; } // Expand `symName` to the full `labelScope.symName` name fullSymbolName(fullName, sizeof(fullName), symName, labelScope); @@ -496,7 +496,7 @@ Symbol *sym_AddAnonLabel(void) { if (anonLabelID == UINT32_MAX) { error("Only %" PRIu32 " anonymous labels can be created!", anonLabelID); - return NULL; + return nullptr; } char name[MAXSYMLEN + 1]; @@ -551,7 +551,7 @@ Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, size_t Symbol *sym = createNonrelocSymbol(symName, false); if (!sym) - return NULL; + return nullptr; sym->type = SYM_MACRO; sym->macro.size = size; @@ -599,7 +599,7 @@ static Symbol *createBuiltinSymbol(char const *symName) sym->isBuiltin = true; sym->hasCallback = true; - sym->src = NULL; + sym->src = nullptr; sym->fileLine = 1; // This is 0 for CLI-defined symbols return sym; @@ -671,6 +671,6 @@ void sym_Init(time_t now) #undef addString #undef addSym - sym_SetCurrentSymbolScope(NULL); + sym_SetCurrentSymbolScope(nullptr); anonLabelID = 0; } diff --git a/src/asm/warning.cpp b/src/asm/warning.cpp index 73a06aca..50b913cb 100644 --- a/src/asm/warning.cpp +++ b/src/asm/warning.cpp @@ -344,7 +344,7 @@ void error(char const *fmt, ...) va_list args; va_start(args, fmt); - printDiag(fmt, args, "error", ":", NULL); + printDiag(fmt, args, "error", ":", nullptr); va_end(args); // This intentionally makes 0 act as "unlimited" (or at least "limited to sizeof(unsigned)") @@ -359,7 +359,7 @@ void error(char const *fmt, ...) va_list args; va_start(args, fmt); - printDiag(fmt, args, "FATAL", ":", NULL); + printDiag(fmt, args, "FATAL", ":", nullptr); va_end(args); exit(1); diff --git a/src/fix/main.cpp b/src/fix/main.cpp index 691cad8f..cca38447 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -37,23 +37,23 @@ static const char *optstring = "Ccf:i:jk:l:m:n:Op:r:st:Vv"; * over short opt matching */ static option const longopts[] = { - { "color-only", no_argument, NULL, 'C' }, - { "color-compatible", no_argument, NULL, 'c' }, - { "fix-spec", required_argument, NULL, 'f' }, - { "game-id", required_argument, NULL, 'i' }, - { "non-japanese", no_argument, NULL, 'j' }, - { "new-licensee", required_argument, NULL, 'k' }, - { "old-licensee", required_argument, NULL, 'l' }, - { "mbc-type", required_argument, NULL, 'm' }, - { "rom-version", required_argument, NULL, 'n' }, - { "overwrite", no_argument, NULL, 'O' }, - { "pad-value", required_argument, NULL, 'p' }, - { "ram-size", required_argument, NULL, 'r' }, - { "sgb-compatible", no_argument, NULL, 's' }, - { "title", required_argument, NULL, 't' }, - { "version", no_argument, NULL, 'V' }, - { "validate", no_argument, NULL, 'v' }, - { NULL, no_argument, NULL, 0 } + { "color-only", no_argument, nullptr, 'C' }, + { "color-compatible", no_argument, nullptr, 'c' }, + { "fix-spec", required_argument, nullptr, 'f' }, + { "game-id", required_argument, nullptr, 'i' }, + { "non-japanese", no_argument, nullptr, 'j' }, + { "new-licensee", required_argument, nullptr, 'k' }, + { "old-licensee", required_argument, nullptr, 'l' }, + { "mbc-type", required_argument, nullptr, 'm' }, + { "rom-version", required_argument, nullptr, 'n' }, + { "overwrite", no_argument, nullptr, 'O' }, + { "pad-value", required_argument, nullptr, 'p' }, + { "ram-size", required_argument, nullptr, 'r' }, + { "sgb-compatible", no_argument, nullptr, 's' }, + { "title", required_argument, nullptr, 't' }, + { "version", no_argument, nullptr, 'V' }, + { "validate", no_argument, nullptr, 'v' }, + { nullptr, no_argument, nullptr, 0 } }; static void printUsage(void) @@ -761,10 +761,10 @@ static enum { DMG, BOTH, CGB } model = DMG; // If DMG, byte is left alone #define FIX_GLOBAL_SUM 0x08 #define TRASH_GLOBAL_SUM 0x04 static uint8_t fixSpec = 0; -static const char *gameID = NULL; +static const char *gameID = nullptr; static uint8_t gameIDLen; static bool japanese = true; -static const char *newLicensee = NULL; +static const char *newLicensee = nullptr; static uint8_t newLicenseeLen; static uint16_t oldLicensee = UNSPECIFIED; static enum MbcType cartridgeType = MBC_NONE; @@ -773,7 +773,7 @@ static bool overwriteRom = false; // If false, warn when overwriting non-zero no static uint16_t padValue = UNSPECIFIED; static uint16_t ramSize = UNSPECIFIED; static bool sgb = false; // If false, SGB flags are left alone -static const char *title = NULL; +static const char *title = nullptr; static uint8_t titleLen; static uint8_t maxTitleLen(void) @@ -1213,7 +1213,7 @@ int main(int argc, char *argv[]) { nbErrors = 0; - for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, NULL)) != -1;) { + for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, nullptr)) != -1;) { switch (ch) { size_t len; #define parseByte(output, name) \ diff --git a/src/gfx/main.cpp b/src/gfx/main.cpp index e3b03ec5..8a62deb2 100644 --- a/src/gfx/main.cpp +++ b/src/gfx/main.cpp @@ -111,36 +111,36 @@ static char const *optstring = "-Aa:b:Cc:Dd:FfhL:mN:n:Oo:Pp:Qq:r:s:Tt:U:uVvx:Z"; * over short opt matching */ static option const longopts[] = { - {"auto-attr-map", no_argument, NULL, 'A'}, - {"output-attr-map", no_argument, NULL, -'A'}, // Deprecated - {"attr-map", required_argument, NULL, 'a'}, - {"base-tiles", required_argument, NULL, 'b'}, - {"color-curve", no_argument, NULL, 'C'}, - {"colors", required_argument, NULL, 'c'}, - {"depth", required_argument, NULL, 'd'}, - {"slice", required_argument, NULL, 'L'}, - {"mirror-tiles", no_argument, NULL, 'm'}, - {"nb-tiles", required_argument, NULL, 'N'}, - {"nb-palettes", required_argument, NULL, 'n'}, - {"group-outputs", no_argument, NULL, 'O'}, - {"output", required_argument, NULL, 'o'}, - {"auto-palette", no_argument, NULL, 'P'}, - {"output-palette", no_argument, NULL, -'P'}, // Deprecated - {"palette", required_argument, NULL, 'p'}, - {"auto-palette-map", no_argument, NULL, 'Q'}, - {"output-palette-map", no_argument, NULL, -'Q'}, // Deprecated - {"palette-map", required_argument, NULL, 'q'}, - {"reverse", required_argument, NULL, 'r'}, - {"auto-tilemap", no_argument, NULL, 'T'}, - {"output-tilemap", no_argument, NULL, -'T'}, // Deprecated - {"tilemap", required_argument, NULL, 't'}, - {"unit-size", required_argument, NULL, 'U'}, - {"unique-tiles", no_argument, NULL, 'u'}, - {"version", no_argument, NULL, 'V'}, - {"verbose", no_argument, NULL, 'v'}, - {"trim-end", required_argument, NULL, 'x'}, - {"columns", no_argument, NULL, 'Z'}, - {NULL, no_argument, NULL, 0 } + {"auto-attr-map", no_argument, nullptr, 'A'}, + {"output-attr-map", no_argument, nullptr, -'A'}, // Deprecated + {"attr-map", required_argument, nullptr, 'a'}, + {"base-tiles", required_argument, nullptr, 'b'}, + {"color-curve", no_argument, nullptr, 'C'}, + {"colors", required_argument, nullptr, 'c'}, + {"depth", required_argument, nullptr, 'd'}, + {"slice", required_argument, nullptr, 'L'}, + {"mirror-tiles", no_argument, nullptr, 'm'}, + {"nb-tiles", required_argument, nullptr, 'N'}, + {"nb-palettes", required_argument, nullptr, 'n'}, + {"group-outputs", no_argument, nullptr, 'O'}, + {"output", required_argument, nullptr, 'o'}, + {"auto-palette", no_argument, nullptr, 'P'}, + {"output-palette", no_argument, nullptr, -'P'}, // Deprecated + {"palette", required_argument, nullptr, 'p'}, + {"auto-palette-map", no_argument, nullptr, 'Q'}, + {"output-palette-map", no_argument, nullptr, -'Q'}, // Deprecated + {"palette-map", required_argument, nullptr, 'q'}, + {"reverse", required_argument, nullptr, 'r'}, + {"auto-tilemap", no_argument, nullptr, 'T'}, + {"output-tilemap", no_argument, nullptr, -'T'}, // Deprecated + {"tilemap", required_argument, nullptr, 't'}, + {"unit-size", required_argument, nullptr, 'U'}, + {"unique-tiles", no_argument, nullptr, 'u'}, + {"version", no_argument, nullptr, 'V'}, + {"verbose", no_argument, nullptr, 'v'}, + {"trim-end", required_argument, nullptr, 'x'}, + {"columns", no_argument, nullptr, 'Z'}, + {nullptr, no_argument, nullptr, 0 } }; static void printUsage(void) { @@ -331,8 +331,8 @@ static std::vector readAtFile(std::string const &path, std::vector * Parses an arg vector, modifying `options` and `localOptions` as options are read. * The `localOptions` struct is for flags which must be processed after the option parsing finishes. * - * Returns NULL if the vector was fully parsed, or a pointer (which is part of the arg vector) to an - * "at-file" path if one is encountered. + * Returns `nullptr` if the vector was fully parsed, or a pointer (which is part of the arg vector) + * to an "at-file" path if one is encountered. */ static char *parseArgv(int argc, char **argv) { for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, nullptr)) != -1;) { diff --git a/src/link/assign.cpp b/src/link/assign.cpp index 0e9b51b5..6237d032 100644 --- a/src/link/assign.cpp +++ b/src/link/assign.cpp @@ -51,7 +51,7 @@ static void initFreeSpace(void) type, bank); memory[type][bank].next->address = sectionTypeInfo[type].startAddr; memory[type][bank].next->size = sectionTypeInfo[type].size; - memory[type][bank].next->next = NULL; + memory[type][bank].next->next = nullptr; memory[type][bank].next->prev = &memory[type][bank]; } } @@ -69,7 +69,7 @@ static void assignSection(Section *section, MemoryLocation const *location) // Propagate the assigned location to all UNIONs/FRAGMENTs // so `jr` patches in them will have the correct offset - for (Section *next = section->nextu; next != NULL; next = next->nextu) { + for (Section *next = section->nextu; next != nullptr; next = next->nextu) { next->org = section->org; next->bank = section->bank; } @@ -94,22 +94,20 @@ static bool isLocationSuitable(Section const *section, FreeSpace const *freeSpac if (section->isAddressFixed && section->org != location->address) return false; - if (section->isAlignFixed - && ((location->address - section->alignOfs) & section->alignMask)) + if (section->isAlignFixed && ((location->address - section->alignOfs) & section->alignMask)) return false; if (location->address < freeSpace->address) return false; - return location->address + section->size - <= freeSpace->address + freeSpace->size; + + return location->address + section->size <= freeSpace->address + freeSpace->size; } /* * Finds a suitable location to place a section at. * @param section The section to be placed * @param location A pointer to a memory location that will be filled - * @return A pointer to the free space encompassing the location, or NULL if - * none was found + * @return A pointer to the free space encompassing the location, or `nullptr` if none was found */ static FreeSpace *getPlacement(Section const *section, MemoryLocation *location) { @@ -159,7 +157,7 @@ static FreeSpace *getPlacement(Section const *section, MemoryLocation *location) location->address = section->org; else // Try again in next bank - space = NULL; + space = nullptr; } else if (section->isAlignFixed) { // Move to next aligned location // Move back to alignment boundary @@ -187,32 +185,32 @@ static FreeSpace *getPlacement(Section const *section, MemoryLocation *location) // Try scrambled banks in descending order until no bank in the scrambled range is available. // Otherwise, try in ascending order. if (section->isBankFixed) { - return NULL; + return nullptr; } else if (scrambleROMX && section->type == SECTTYPE_ROMX && location->bank <= scrambleROMX) { if (location->bank > sectionTypeInfo[section->type].firstBank) location->bank--; else if (scrambleROMX < sectionTypeInfo[section->type].lastBank) location->bank = scrambleROMX + 1; else - return NULL; + return nullptr; } else if (scrambleWRAMX && section->type == SECTTYPE_WRAMX && location->bank <= scrambleWRAMX) { if (location->bank > sectionTypeInfo[section->type].firstBank) location->bank--; else if (scrambleWRAMX < sectionTypeInfo[section->type].lastBank) location->bank = scrambleWRAMX + 1; else - return NULL; + return nullptr; } else if (scrambleSRAM && section->type == SECTTYPE_SRAM && location->bank <= scrambleSRAM) { if (location->bank > sectionTypeInfo[section->type].firstBank) location->bank--; else if (scrambleSRAM < sectionTypeInfo[section->type].lastBank) location->bank = scrambleSRAM + 1; else - return NULL; + return nullptr; } else if (location->bank < sectionTypeInfo[section->type].lastBank) { location->bank++; } else { - return NULL; + return nullptr; } #undef BANK_INDEX } diff --git a/src/link/main.cpp b/src/link/main.cpp index efcbcb2b..04bfd302 100644 --- a/src/link/main.cpp +++ b/src/link/main.cpp @@ -173,22 +173,22 @@ static const char *optstring = "dl:m:Mn:O:o:p:S:s:tVvWwx"; * over short opt matching */ static option const longopts[] = { - { "dmg", no_argument, NULL, 'd' }, - { "linkerscript", required_argument, NULL, 'l' }, - { "map", required_argument, NULL, 'm' }, - { "no-sym-in-map", no_argument, NULL, 'M' }, - { "sym", required_argument, NULL, 'n' }, - { "overlay", required_argument, NULL, 'O' }, - { "output", required_argument, NULL, 'o' }, - { "pad", required_argument, NULL, 'p' }, - { "scramble", required_argument, NULL, 'S' }, - { "smart", required_argument, NULL, 's' }, - { "tiny", no_argument, NULL, 't' }, - { "version", no_argument, NULL, 'V' }, - { "verbose", no_argument, NULL, 'v' }, - { "wramx", no_argument, NULL, 'w' }, - { "nopad", no_argument, NULL, 'x' }, - { NULL, no_argument, NULL, 0 } + { "dmg", no_argument, nullptr, 'd' }, + { "linkerscript", required_argument, nullptr, 'l' }, + { "map", required_argument, nullptr, 'm' }, + { "no-sym-in-map", no_argument, nullptr, 'M' }, + { "sym", required_argument, nullptr, 'n' }, + { "overlay", required_argument, nullptr, 'O' }, + { "output", required_argument, nullptr, 'o' }, + { "pad", required_argument, nullptr, 'p' }, + { "scramble", required_argument, nullptr, 'S' }, + { "smart", required_argument, nullptr, 's' }, + { "tiny", no_argument, nullptr, 't' }, + { "version", no_argument, nullptr, 'V' }, + { "verbose", no_argument, nullptr, 'v' }, + { "wramx", no_argument, nullptr, 'w' }, + { "nopad", no_argument, nullptr, 'x' }, + { nullptr, no_argument, nullptr, 0 } }; static void printUsage(void) @@ -345,7 +345,7 @@ next: int main(int argc, char *argv[]) { // Parse options - for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, NULL)) != -1;) { + for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, nullptr)) != -1;) { switch (ch) { case 'd': isDmgMode = true; @@ -399,7 +399,7 @@ int main(int argc, char *argv[]) break; case 's': // TODO: implement "smart linking" with `-s` - warning(NULL, 0, "Nobody has any idea what `-s` does"); + warning(nullptr, 0, "Nobody has any idea what `-s` does"); break; case 't': is32kMode = true; diff --git a/src/link/object.cpp b/src/link/object.cpp index 233238b7..8f8fa120 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -136,7 +136,7 @@ static void readFileStackNode(FILE *file, std::vector &fileNodes, tryReadlong(parentID, file, "%s: Cannot read node #%" PRIu32 "'s parent ID: %s", fileName, i); - node.parent = parentID != (uint32_t)-1 ? &fileNodes[parentID] : NULL; + node.parent = parentID != (uint32_t)-1 ? &fileNodes[parentID] : nullptr; tryReadlong(node.lineNo, file, "%s: Cannot read node #%" PRIu32 "'s line number: %s", fileName, i); tryGetc(enum FileStackNodeType, node.type, file, @@ -159,7 +159,7 @@ static void readFileStackNode(FILE *file, std::vector &fileNodes, "%s: Cannot read node #%" PRIu32 "'s iter #%" PRIu32 ": %s", fileName, i, k); if (!node.parent) - fatal(NULL, 0, "%s is not a valid object file: root node (#%" + fatal(nullptr, 0, "%s is not a valid object file: root node (#%" PRIu32 ") may not be REPT", fileName, i); } } @@ -248,7 +248,7 @@ static void readPatch(FILE *file, Patch *patch, char const *fileName, std::strin static void linkPatchToPCSect(Patch *patch, std::vector
const &fileSections) { patch->pcSection = patch->pcSectionID != (uint32_t)-1 ? fileSections[patch->pcSectionID] - : NULL; + : nullptr; } /* @@ -281,7 +281,7 @@ static void readSection(FILE *file, Section *section, char const *fileName, tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s org: %s", fileName, section->name.c_str()); section->isAddressFixed = tmp >= 0; if (tmp > UINT16_MAX) { - error(NULL, 0, "\"%s\"'s org is too large (%" PRId32 ")", section->name.c_str(), tmp); + error(nullptr, 0, "\"%s\"'s org is too large (%" PRId32 ")", section->name.c_str(), tmp); tmp = UINT16_MAX; } section->org = tmp; @@ -297,7 +297,7 @@ static void readSection(FILE *file, Section *section, char const *fileName, tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s alignment offset: %s", fileName, section->name.c_str()); if (tmp > UINT16_MAX) { - error(NULL, 0, "\"%s\"'s alignment offset is too large (%" PRId32 ")", + error(nullptr, 0, "\"%s\"'s alignment offset is too large (%" PRId32 ")", section->name.c_str(), tmp); tmp = UINT16_MAX; } @@ -391,7 +391,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) ungetc(c, file); // Guaranteed to work switch (c) { case EOF: - fatal(NULL, 0, "File \"%s\" is empty!", fileName); + fatal(nullptr, 0, "File \"%s\" is empty!", fileName); case 'R': break; @@ -400,7 +400,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) // Since SDCC does not provide line info, everything will be reported as coming from the // object file. It's better than nothing. nodes[fileID].push_back({ - .parent = NULL, + .parent = nullptr, .lineNo = 0, .type = NODE_FILE, .data = fileName @@ -463,7 +463,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) } // This file's sections, stored in a table to link symbols to them - std::vector
fileSections(nbSections, NULL); + std::vector
fileSections(nbSections, nullptr); verbosePrint("Reading %" PRIu32 " sections...\n", nbSections); for (uint32_t i = 0; i < nbSections; i++) { @@ -472,7 +472,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) if (!fileSections[i]) err("%s: Failed to create new section", fileName); - fileSections[i]->nextu = NULL; + fileSections[i]->nextu = nullptr; readSection(file, fileSections[i], fileName, nodes[fileID]); fileSections[i]->fileSymbols = &fileSymbols; fileSections[i]->symbols.reserve(nbSymPerSect[i]); @@ -493,7 +493,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) int32_t sectionID = fileSymbols[i].sectionID; if (sectionID == -1) { - fileSymbols[i].section = NULL; + fileSymbols[i].section = nullptr; } else { Section *section = fileSections[sectionID]; diff --git a/src/link/output.cpp b/src/link/output.cpp index 4b031338..6f3b10dd 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -96,7 +96,7 @@ Section const *out_OverlappingSection(Section const *section) if (ptr->org < section->org + section->size && section->org < ptr->org + ptr->size) return ptr; } - return NULL; + return nullptr; } /* @@ -222,7 +222,7 @@ static void writeROM(void) coverOverlayBanks(nbOverlayBanks); if (outputFile) { - writeBank(!sections[SECTTYPE_ROM0].empty() ? §ions[SECTTYPE_ROM0][0].sections : NULL, + writeBank(!sections[SECTTYPE_ROM0].empty() ? §ions[SECTTYPE_ROM0][0].sections : nullptr, sectionTypeInfo[SECTTYPE_ROM0].startAddr, sectionTypeInfo[SECTTYPE_ROM0].size); for (uint32_t i = 0 ; i < sections[SECTTYPE_ROMX].size(); i++) diff --git a/src/link/script.y b/src/link/script.y index 9cb4dad4..aad9b573 100644 --- a/src/link/script.y +++ b/src/link/script.y @@ -106,7 +106,7 @@ optional: %empty { $$ = false; } %% #define scriptError(context, fmt, ...) \ - ::error(NULL, 0, "%s(%" PRIu32 "): " fmt, \ + ::error(nullptr, 0, "%s(%" PRIu32 "): " fmt, \ context.path.c_str(), context.lineNo __VA_OPT__(,) __VA_ARGS__) // Lexer. @@ -577,7 +577,7 @@ void script_ProcessScript(char const *path) { auto &newContext = lexerStack.emplace_back(std::string(path)); if (!newContext.file.open(newContext.path, std::ios_base::in)) { - error(NULL, 0, "Failed to open linker script \"%s\"", newContext.path.c_str()); + error(nullptr, 0, "Failed to open linker script \"%s\"", newContext.path.c_str()); lexerStack.clear(); } else { yy::parser linkerScriptParser; diff --git a/src/link/sdas_obj.cpp b/src/link/sdas_obj.cpp index 0773e00b..18a80d6b 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -143,12 +143,12 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector fatal(where, lineNo, __VA_ARGS__); \ } while (0) #define expectEol(...) do { \ - token = strtok(NULL, delim); \ + token = strtok(nullptr, delim); \ if (token) \ fatal(where, lineNo, __VA_ARGS__); \ } while (0) #define expectToken(expected, lineType) do { \ - getToken(NULL, "'%c' line is too short", (lineType)); \ + getToken(nullptr, "'%c' line is too short", (lineType)); \ if (strcasecmp(token, (expected)) != 0) \ fatal(where, lineNo, "Malformed '%c' line: expected \"%s\", got \"%s\"", \ (lineType), (expected), token); \ @@ -204,7 +204,7 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector expectToken("areas", 'H'); - getToken(NULL, "'H' line is too short"); + getToken(nullptr, "'H' line is too short"); uint32_t expectedNbSymbols = parseNumber(where, lineNo, token, numberType); expectToken("global", 'H'); @@ -255,7 +255,7 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector expectToken("size", 'A'); - getToken(NULL, "'A' line is too short"); + getToken(nullptr, "'A' line is too short"); uint32_t tmp = parseNumber(where, lineNo, token, numberType); @@ -266,7 +266,7 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector expectToken("flags", 'A'); - getToken(NULL, "'A' line is too short"); + getToken(nullptr, "'A' line is too short"); tmp = parseNumber(where, lineNo, token, numberType); if (tmp & (1 << AREA_PAGING)) fatal(where, lineNo, "Internal error: paging is not supported"); @@ -283,7 +283,7 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector expectToken("addr", 'A'); - getToken(NULL, "'A' line is too short"); + getToken(nullptr, "'A' line is too short"); tmp = parseNumber(where, lineNo, token, numberType); curSection->org = tmp; // Truncation keeps the address portion only curSection->bank = tmp >> 16; @@ -319,7 +319,7 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector } curSection->isAlignFixed = false; // No such concept! curSection->fileSymbols = &fileSymbols; // IDs are instead per-section - curSection->nextu = NULL; + curSection->nextu = nullptr; break; } @@ -335,12 +335,12 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector symbol.lineNo = lineNo; // No need to set the `sectionID`, since we can directly set the pointer - symbol.section = !fileSections.empty() ? fileSections.back().section : NULL; + symbol.section = !fileSections.empty() ? fileSections.back().section : nullptr; getToken(line.data(), "'S' line is too short"); symbol.name = token; - getToken(NULL, "'S' line is too short"); + getToken(nullptr, "'S' line is too short"); // It might be an `offset`, but both types are the same so type punning is fine symbol.value = parseNumber(where, lineNo, &token[3], numberType); if (symbol.section && symbol.section->isAddressFixed) { @@ -394,7 +394,7 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector warning(where, lineNo, "Previous 'T' line had no 'R' line (ignored)"); data.clear(); - for (token = strtok(line.data(), delim); token; token = strtok(NULL, delim)) + for (token = strtok(line.data(), delim); token; token = strtok(nullptr, delim)) data.push_back(parseByte(where, lineNo, token, numberType)); if (data.size() < ADDR_SIZE) @@ -411,12 +411,12 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector // First two bytes are ignored getToken(line.data(), "'R' line is too short"); - getToken(NULL, "'R' line is too short"); + getToken(nullptr, "'R' line is too short"); uint16_t areaIdx; - getToken(NULL, "'R' line is too short"); + getToken(nullptr, "'R' line is too short"); areaIdx = parseByte(where, lineNo, token, numberType); - getToken(NULL, "'R' line is too short"); + getToken(nullptr, "'R' line is too short"); areaIdx |= (uint16_t)parseByte(where, lineNo, token, numberType) << 8; if (areaIdx >= fileSections.size()) fatal(where, lineNo, "'R' line references area #%" PRIu16 ", but there are only %zu (so far)", @@ -458,15 +458,15 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector // This all can be "translated" to RGBDS parlance by generating the // appropriate RPN expression (depending on flags), plus an addition for the // bytes being patched over. - while ((token = strtok(NULL, delim)) != NULL) { + while ((token = strtok(nullptr, delim)) != nullptr) { uint16_t flags = parseByte(where, lineNo, token, numberType); if ((flags & 0xF0) == 0xF0) { - getToken(NULL, "Incomplete relocation"); + getToken(nullptr, "Incomplete relocation"); flags = (flags & 0x0F) | (uint16_t)parseByte(where, lineNo, token, numberType) << 4; } - getToken(NULL, "Incomplete relocation"); + getToken(nullptr, "Incomplete relocation"); uint8_t offset = parseByte(where, lineNo, token, numberType); if (offset < ADDR_SIZE) @@ -476,10 +476,10 @@ void sdobj_ReadFile(FileStackNode const *where, FILE *file, std::vector fatal(where, lineNo, "Relocation index is out of bounds (%" PRIu16 " >= %zu)", offset, data.size()); - getToken(NULL, "Incomplete relocation"); + getToken(nullptr, "Incomplete relocation"); uint16_t idx = parseByte(where, lineNo, token, numberType); - getToken(NULL, "Incomplete relocation"); + getToken(nullptr, "Incomplete relocation"); idx |= (uint16_t)parseByte(where, lineNo, token, numberType); // Loudly fail on unknown flags diff --git a/src/link/section.cpp b/src/link/section.cpp index 11e50ba3..f27cc159 100644 --- a/src/link/section.cpp +++ b/src/link/section.cpp @@ -182,33 +182,33 @@ void sect_AddSection(Section *section) Section *sect_GetSection(std::string const &name) { auto search = sections.find(name); - return search != sections.end() ? search->second : NULL; + return search != sections.end() ? search->second : nullptr; } static void doSanityChecks(Section *section) { // Sanity check the section's type if (section->type < 0 || section->type >= SECTTYPE_INVALID) { - error(NULL, 0, "Section \"%s\" has an invalid type", section->name.c_str()); + error(nullptr, 0, "Section \"%s\" has an invalid type", section->name.c_str()); return; } if (is32kMode && section->type == SECTTYPE_ROMX) { if (section->isBankFixed && section->bank != 1) - error(NULL, 0, "%s: ROMX sections must be in bank 1 (if any) with option -t", + error(nullptr, 0, "%s: ROMX sections must be in bank 1 (if any) with option -t", section->name.c_str()); else section->type = SECTTYPE_ROM0; } if (isWRAM0Mode && section->type == SECTTYPE_WRAMX) { if (section->isBankFixed && section->bank != 1) - error(NULL, 0, "%s: WRAMX sections must be in bank 1 with options -w or -d", + error(nullptr, 0, "%s: WRAMX sections must be in bank 1 with options -w or -d", section->name.c_str()); else section->type = SECTTYPE_WRAM0; } if (isDmgMode && section->type == SECTTYPE_VRAM && section->bank == 1) - error(NULL, 0, "%s: VRAM bank 1 can't be used with option -d", + error(nullptr, 0, "%s: VRAM bank 1 can't be used with option -d", section->name.c_str()); // Check if alignment is reasonable, this is important to avoid UB @@ -218,21 +218,21 @@ static void doSanityChecks(Section *section) // Too large an alignment may not be satisfiable if (section->isAlignFixed && (section->alignMask & sectionTypeInfo[section->type].startAddr)) - error(NULL, 0, "%s: %s sections cannot be aligned to $%04x bytes", + error(nullptr, 0, "%s: %s sections cannot be aligned to $%04x bytes", section->name.c_str(), sectionTypeInfo[section->type].name.c_str(), section->alignMask + 1); uint32_t minbank = sectionTypeInfo[section->type].firstBank, maxbank = sectionTypeInfo[section->type].lastBank; if (section->isBankFixed && section->bank < minbank && section->bank > maxbank) - error(NULL, 0, minbank == maxbank + error(nullptr, 0, minbank == maxbank ? "Cannot place section \"%s\" in bank %" PRIu32 ", it must be %" PRIu32 : "Cannot place section \"%s\" in bank %" PRIu32 ", it must be between %" PRIu32 " and %" PRIu32, section->name.c_str(), section->bank, minbank, maxbank); // Check if section has a chance to be placed if (section->size > sectionTypeInfo[section->type].size) - error(NULL, 0, "Section \"%s\" is bigger than the max size for that type: $%" + error(nullptr, 0, "Section \"%s\" is bigger than the max size for that type: $%" PRIx16 " > $%" PRIx16, section->name.c_str(), section->size, sectionTypeInfo[section->type].size); @@ -247,7 +247,7 @@ static void doSanityChecks(Section *section) // It doesn't make sense to have both org and alignment set if (section->isAlignFixed) { if ((section->org & section->alignMask) != section->alignOfs) - error(NULL, 0, "Section \"%s\"'s fixed address doesn't match its alignment", + error(nullptr, 0, "Section \"%s\"'s fixed address doesn't match its alignment", section->name.c_str()); section->isAlignFixed = false; } @@ -255,12 +255,12 @@ static void doSanityChecks(Section *section) // Ensure the target address is valid if (section->org < sectionTypeInfo[section->type].startAddr || section->org > endaddr(section->type)) - error(NULL, 0, "Section \"%s\"'s fixed address $%04" PRIx16 " is outside of range [$%04" + error(nullptr, 0, "Section \"%s\"'s fixed address $%04" PRIx16 " is outside of range [$%04" PRIx16 "; $%04" PRIx16 "]", section->name.c_str(), section->org, sectionTypeInfo[section->type].startAddr, endaddr(section->type)); if (section->org + section->size > endaddr(section->type) + 1) - error(NULL, 0, "Section \"%s\"'s end address $%04x is greater than last address $%04x", + error(nullptr, 0, "Section \"%s\"'s end address $%04x is greater than last address $%04x", section->name.c_str(), section->org + section->size, endaddr(section->type) + 1); } diff --git a/src/link/symbol.cpp b/src/link/symbol.cpp index efb66d65..5934faa4 100644 --- a/src/link/symbol.cpp +++ b/src/link/symbol.cpp @@ -32,5 +32,5 @@ void sym_AddSymbol(Symbol *symbol) Symbol *sym_GetSymbol(std::string const &name) { auto search = symbols.find(name); - return search != symbols.end() ? search->second : NULL; + return search != symbols.end() ? search->second : nullptr; } diff --git a/test/gfx/randtilegen.cpp b/test/gfx/randtilegen.cpp index da9ddaf0..9844afd9 100644 --- a/test/gfx/randtilegen.cpp +++ b/test/gfx/randtilegen.cpp @@ -165,7 +165,7 @@ static void write_image(char const *filename, uint16_t /* const */ palettes[MIN_ unsigned char /* const */ (*tileData)[8][8], Attributes const *attributes, uint8_t width, uint8_t height) { uint8_t const nbTiles = width * height; - png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); png_infop pngInfo = png_create_info_struct(png); if (setjmp(png_jmpbuf(png))) { @@ -174,7 +174,7 @@ static void write_image(char const *filename, uint16_t /* const */ palettes[MIN_ } FILE *file = fopen(filename, "wb"); - if (file == NULL) { + if (file == nullptr) { fprintf(stderr, "FATAL: Failed to open \"%s\": %s\n", filename, strerror(errno)); exit(1); } @@ -191,7 +191,7 @@ static void write_image(char const *filename, uint16_t /* const */ palettes[MIN_ assert(height != 0); uint8_t *data = (uint8_t *)malloc(height * 8 * width * 8 * SIZEOF_PIXEL); uint8_t **rowPtrs = (uint8_t **)malloc(height * 8 * sizeof(*rowPtrs)); - if (data == NULL || rowPtrs == NULL) { + if (data == nullptr || rowPtrs == nullptr) { fatal("Out of memory"); } for (uint8_t y = 0; y < height * 8; ++y) { @@ -214,7 +214,7 @@ static void write_image(char const *filename, uint16_t /* const */ palettes[MIN_ } png_set_rows(png, pngInfo, rowPtrs); - png_write_png(png, pngInfo, PNG_TRANSFORM_IDENTITY, NULL); + png_write_png(png, pngInfo, PNG_TRANSFORM_IDENTITY, nullptr); fclose(file); free(rowPtrs); free(data);