gbdev/rgbds

Refactoring and style cleanup with C++

Rangi42 opened this issue · 3 comments

  • Don't use redundant struct keyword in types.
    This regex will fix the single-lines ones: perl -pe 's/\bstruct (\w+)\b(?! \{)(?=.+[;{)]$)/\1/g'; just double-check for wrapped for (...; lines.
  • Use nullptr instead of NULL.
  • Use () instead of (void).
  • Use references instead of NONNULL pointers (C++ doesn't support [static 1])
  • Use std::variant instead of union
  • Replace functions with methods: foo_Bar(struct Foo *foo, ...) into Foo::bar(...)
  • Get rid of ((m|c|re)alloc|strdup)
  • Define constructors with default field values (and optimize some pushes to emplacement)
  • Get rid of new (std::nothrow) in favor of automatic allocation with RAII
    • This will require the rgbasm parser to use variants and complete symbols like the linker script one does
    • Re-enable ASan for rgbasm if this succeeds
    src/asm/lexer.cpp:2206:  lexerState->captureBuf = new (std::nothrow) std::vector<char>();
    src/asm/symbol.cpp:503:  std::string_view *macro = new (std::nothrow) std::string_view(body, size);
    

We should look into updating NONNULL to use [[gnu::nonnull]] instead.

(Also, in general, replacing __attribute__((ATTR)) with [[gnu::ATTR]] instead. This would make less code platform-specific!)

Most if not all of the NONNULLs should be references, as should many other functions which take pointers assumed not to be null.

After the last few weeks of refactoring, RAII, smart pointers, etc, I'm declaring this done. There are two new (std::nothrow)s left, both involved with how the lexer's files/mmaps/EQUs/interpolations/macro args can "leak as a feature", and fixing that (and re-enabling ASan) is a matter for #709.