# Guidelines

> How C++ is written in the framework and its examples: which rules decide, in what order, every rule with the tool that holds it and where, and what a game gets of them.

Source: https://omardev29.github.io/rmp-docs/guidelines/index.html

How C++ is written here: in the framework, in its examples, and in a game that wants to read like them. In the framework and the examples the rules on these pages are mandatory, and a tool holds every one a tool can tell apart from its mistake; in a game they are advice, and the game decides how much of the tooling it takes. When a question has more than one good answer, three sources decide it, in this order.

## Who decides

1. **The API is as simple as possible for whoever uses it.** This outranks every other rule. A main menu is three functions, and a rule that would make it longer loses, whatever else it buys. [The three-function menu](https://omardev29.github.io/rmp-docs/framework/why/the-three-function-menu.html) says why.
2. **The rules on these pages**, each with its reason: the [naming table](https://omardev29.github.io/rmp-docs/guidelines/naming.html), [the layout](https://omardev29.github.io/rmp-docs/guidelines/formatting.html), [`const`](https://omardev29.github.io/rmp-docs/guidelines/const.html), [no raw pointer, and nothing owned by hand](https://omardev29.github.io/rmp-docs/guidelines/pointers-and-raii.html), [text in and out](https://omardev29.github.io/rmp-docs/guidelines/strings.html), [the C++ written where C would also compile](https://omardev29.github.io/rmp-docs/guidelines/modern-cpp.html) and [macros](https://omardev29.github.io/rmp-docs/guidelines/macros.html) — and the framework's own decisions in [why it is built this way](https://omardev29.github.io/rmp-docs/framework/why/index.html), such as [one header per module](https://omardev29.github.io/rmp-docs/framework/why/one-header-per-module.html) and [no exceptions](https://omardev29.github.io/rmp-docs/framework/why/no-exceptions.html).
3. **[Google's C++ Style Guide](https://google.github.io/styleguide/cppguide.html)**, for everything the first two leave open. Where the framework has decided otherwise its decision holds, and [Google's guide, and where this one differs](https://omardev29.github.io/rmp-docs/guidelines/google.html) lists each place with its reason.

## Every rule, and what holds it

A rule written only in a document is kept until the day somebody has not read it. So wherever a mistake can be described precisely, a tool refuses it: a check of clang-tidy, which [`rmp lint`](https://omardev29.github.io/rmp-docs/reference/commands.html#lint) runs; a gate, which is a stage of [`rmp test`](https://omardev29.github.io/rmp-docs/reference/commands.html#test); clang-format; or the compiler. One line per page of these Guidelines, with every tool that page names and where it holds — in the framework (its `include/`, `src/` and `tests/`), in its examples, and in a game made with `rmp new`:

| Rules | Checked by | Framework | Examples | A game |
|---|---|---|---|---|
| [Naming](https://omardev29.github.io/rmp-docs/guidelines/naming.html): one spelling per kind, no prefix, no name hiding another, no constant named like a macro, no retired name | `readability-identifier-naming`, `clang-diagnostic-shadow`; `tools/naming_check.sh`, rules R1 to R8, in every branch of every `#if` | yes | yes | no |
| [Formatting](https://omardev29.github.io/rmp-docs/guidelines/formatting.html): 4 spaces, 90 columns, no alignment, `char *p`, `{ 1, 2 }` | `.clang-format`; `rmp fmt` applies it, `rmp fmt check` and `rmp test fmt` refuse a file it would change | yes | yes | yes: `rmp fmt` formats and `rmp fmt check` refuses, when the game runs them |
| [`const`](https://omardev29.github.io/rmp-docs/guidelines/const.html): on every local and method that changes nothing, not on a parameter in a declaration, `inline constexpr` in a header | `misc-const-correctness`, `readability-make-member-function-const`, `readability-avoid-const-params-in-decls`, `performance-unnecessary-value-param`, `custom-rmp-header-constant` | yes | yes, but for `readability-make-member-function-const` and `readability-avoid-const-params-in-decls` | no |
| [Pointers and RAII](https://omardev29.github.io/rmp-docs/guidelines/pointers-and-raii.html): no raw pointer, no C string a game can name, an optional asked before it is read, nothing owned by hand, raylib's resources loaded only by their owners | `custom-rmp-raw-pointer`, `cppcoreguidelines-owning-memory`, `cppcoreguidelines-no-malloc`, `bugprone-unchecked-optional-access`; `tools/pointer_check.py`; `tools/ownership_check.sh` `O1` `O2` | yes, with what `tools/lint_debt.txt` and `tools/pointer_ratchet.txt` still owe, and `tools/ownership_check.sh` in `include/rmp/` and `src/rmp/` only | yes, but for `tools/pointer_check.py` and `O1`, which read only the framework's own code, and with what `tools/lint_debt.txt` still owes | no |
| [Strings](https://omardev29.github.io/rmp-docs/guidelines/strings.html): a view in, a `std::string` out, never a view's `data()` to raylib, nothing newer than the oldest compiler | `tools/pointer_check.py`, `custom-rmp-raw-pointer`, `bugprone-dangling-handle`, `bugprone-suspicious-stringview-data-usage`; `tools/style_check.sh` `S4` `S5` | yes, but for `S4` in `tests/`, which build only on CI's compilers | yes, but for `tools/pointer_check.py`, which reads only `include/rmp/` | no |
| [Modern C++](https://omardev29.github.io/rmp-docs/guidelines/modern-cpp.html): `std::array`, `static_cast`, `enum class`, `nullptr`, no varargs, `int`, `explicit`, `++i`, designated initialisers in order, C++20 as GCC 10.5 has it | `modernize-avoid-c-arrays`, `modernize-avoid-c-style-cast`, `cppcoreguidelines-use-enum-class`, `modernize-use-nullptr`, `modernize-avoid-variadic-functions`, `google-runtime-int`, `google-runtime-float`, `google-explicit-constructor`, `cppcoreguidelines-special-member-functions`, `modernize-use-auto`, `modernize-use-using`, `modernize-use-std-numbers`, `google-build-using-namespace`, `google-default-arguments`; `tools/style_check.sh` `S1` `S2` `S4` `S5` `S6`; `-Werror=reorder-init-list` | yes, but for `S4` in `tests/` | yes, but for `modernize-use-auto` | only the order of designated initialisers, which the compiler refuses |
| [Macros](https://omardev29.github.io/rmp-docs/guidelines/macros.html): only for what a function cannot do, named `RMP_` | `cppcoreguidelines-macro-usage`, `readability-identifier-naming`; `tools/naming_check.sh` `R5` `R6` `R7` | yes | yes | no |
| [Where Google's guide and these agree](https://omardev29.github.io/rmp-docs/guidelines/google.html#agreement): no unnamed namespace or global using-declaration in a header, no overloaded unary `&`, no `TODO` | `google-build-namespaces`, `google-global-names-in-headers`, `google-runtime-operator`, `google-build-explicit-make-pair`; `tools/style_check.sh` `S3` | yes | yes | no |
| No finding of a second analyser, and a reason beside every suppression | `tools/cppcheck_check.sh` | yes: `src/rmp/` | no | no |
| Warnings on the code we write | `-Wall` `-Wextra` | yes, as errors in the framework's own checks | yes | yes, as warnings |

[What checks what](https://omardev29.github.io/rmp-docs/guidelines/enforcement.html) takes the same rules one by one, with each check's exact reach, the checks the framework switches off and why, and what the code still owes. The build of this site holds both tables to the framework's own configuration: a check said to run there runs there.

The rest of what the framework checks is not about how the code reads, and holds it together instead:

| Rule | Checked by | Framework | Examples | A game |
|---|---|---|---|---|
| Every public header compiles on its own and carries the `.toml` values | `tools/header_check.sh` | yes: `include/rmp/` | no | no |
| What each public header costs to include | `tools/header_cost.py`, against `tools/header_budget.txt` | yes: `include/rmp/` | no | no |
| Nothing in the framework reads the clock, the input or the random state on its own | `tools/seam_check.sh` | yes: `src/rmp/` | no | no |
| A mistake in the `.toml` stops before the build | `tools/configure.py`, and `tests/configure_test.py` for each refusal | yes | no: they are built with the framework's | yes |
| Every vendored component has a licence the project can ship | `tools/license_db.py` | yes | no | yes: `rmp test config` |
| No memory error and no undefined behaviour in a run | AddressSanitizer and UndefinedBehaviorSanitizer in every debug build, from `cmake/sanitize.cmake`; `tools/sanitize_check.sh` for a second compiler | yes | yes | no, until its `[dev] sanitize` lists them |
| Every release binary stays the size it was measured at, unless the growth is written down | `tools/size_check.py`, against `tools/size_budget.txt` | yes | no | yes, from the day it commits a budget of its own |
| No file of the framework falls below the share of its lines its tests are written down to run, and no public function arrives without a test that calls it | `tools/coverage_report.py`, against `tools/coverage_floor.txt` and `tools/untested_api.txt` | yes: `src/rmp/` and `include/rmp/` | no | no |
| Generated input looks for what no test was written for: properties against generated values, every committed fuzz input replayed, each module's mutation score held to a floor that only rises; and nothing in the framework throws | `tools/fuzz.sh`, `tools/mutation.sh` against `tests/mutation/floors/`, RapidCheck and Hypothesis, all held together by `tests/campaign_test.py` | yes | no | no |

Each is a stage of `rmp test` or a step of the framework's CI, and [Testing](https://omardev29.github.io/rmp-docs/framework/testing.html) has where and when each one runs.

## Shown, and checked like the code

Each rule is shown in code, and the code on these pages is checked the way the framework's own is. A block that shows the right way is compiled against the framework's headers, and passes, with nothing to say, clang-tidy with the framework's `.clang-tidy`, clang-format with its `.clang-format`, and its text gates: `tools/naming_check.sh`, `tools/style_check.sh`, `tools/ownership_check.sh` and, for a block that is a public header, `tools/pointer_check.py`. A block that shows the wrong way says what refuses it, and the build of this site runs that tool on it and fails if it stays quiet; what the tool printed is under the block, from that run, never typed. A block that does not compile at all is shown with what the compiler said.

## What a game gets

A game made with `rmp new` gets two things from these pages, and the rest is advice:

- **`.clang-format`**, so `rmp fmt` lays out the game's code the framework's way, and `rmp fmt check` refuses a file that is not. Neither runs unless the game runs it: its `rmp test` and its CI leave the layout alone.
- **`-Werror=reorder-init-list`**, which the framework's library hands every target that links it: under Clang, designated initialisers out of declaration order are an error, as they already are under GCC and MSVC.

Its own code is also compiled with the warnings the framework's is: `-Wall -Wextra` under GCC and Clang, with `-Wno-missing-field-initializers`, because leaving fields of an options struct out — `{ .width = 8 }` — is how the API is written. That is every build CMake makes, the desktop's and the web's; Android's and iOS's own projects build without them. They stay warnings: a newer compiler on its author's machine must not stop a build that was fine yesterday. A game can configure with `-DRMP_WERROR=ON` to make them errors, as the framework's own checks do. The vendored libraries are included as system headers, so the warnings are about the game's code and not about theirs.

It does not get the framework's `.clang-tidy`, its text gates or its cppcheck: `rmp lint` is a command of the framework's own repository, and in a game it says so and stops.

### Switching on the framework's clang-tidy

There is no setting in the `.toml` for it. A game that wants the framework's checks on its own code copies the framework's `.clang-tidy` into its root and runs clang-tidy itself. The game already has what clang-tidy needs: its `CMakePresets.json` is the framework's, and the `lint` preset writes the compile database without building anything.

```shell
cp ~/.local/share/rmp/.clang-tidy .
cmake --preset lint
clang-tidy -p build/lint --experimental-custom-checks --warnings-as-errors='*' \
    --header-filter='.*/(include|src|tests)/.*' \
    --exclude-header-filter='.*/(include/rmp|src/rmp|thirdparty|generated|build)/.*' \
    $(find src tests -name '*.cpp' -not -path 'src/rmp/*')
```

- The `.clang-tidy` is the one of the framework `rmp` runs from: the clone `rmp install` made, in `~/.local/share/rmp` on Linux and macOS and in `%LOCALAPPDATA%\rmp` on Windows.
- `--experimental-custom-checks` runs the two queries of the framework's own, the raw pointer and the constant in a header; without it clang-tidy skips them and says nothing. The framework pins clang-tidy 22.1.8, in `thirdparty/FROZEN_VERSIONS.md`, and its file is written for that one.
- `--warnings-as-errors='*'` makes a finding fail the run, which is what a check is for. The framework's file says `WarningsAsErrors: ''` because `rmp lint` counts its warnings against a list of what the framework still owes.
- `--header-filter` reports the game's own headers in `include/`, `src/` and `tests/`. The framework's file names the folders it reports by the framework's layout, and a header in a game's `include/`, where the examples keep theirs, is not among them: without the flag, a C cast there passes in silence.
- `--exclude-header-filter` keeps the framework's copy in `include/rmp/` and `src/rmp/` out of the report: the framework answers for those files, and its lint still owes some of them a few raw pointers.
- The copy asks every macro defined to start with `RMP_`, the framework's prefix. A game whose own macros take the game's name instead, as [Macros](https://omardev29.github.io/rmp-docs/guidelines/macros.html#prefix) advises, writes that name in its copy's `MacroDefinitionPrefix`; with `'RMP_'` left there, clang-tidy refuses `#define PONG_SHOW(value)` in the game's code.

The last line hands it every source of the game's own — `src/` and `tests/`, without the framework's copy in `src/rmp/`. clang-tidy then says nothing about the game `rmp new` writes, its test included, and refuses a C cast added to its main menu, or to a header of its own in `include/`, the way the framework's lint would.
