On this page
  1. Who runs it, and where
  2. Four spaces, 90 columns, braces on the line
  3. No alignment
  4. A guard on one line
  5. Classes, namespaces and switches
  6. int &x, and { 1, 2 }
  7. What clang-format leaves alone
  8. Files

Formatting

Nobody formats the framework's code by hand. .clang-format says how it is laid out, rmp fmt lays it out that way, and rmp test fmt refuses a file that is not. What follows is what that file decides, why, and what clang-format says about code written any other way — so that you can write it right the first time, and read the diff it makes when you do not.

Who runs it, and where#

CommandWhat it doesWhere
rmp fmtformats every file in placein the framework: include/, src/, tests/ and examples/; in a game: its own include/, src/ and tests/, and not the files that came with the framework: src/rmp/, include/rmp/ and tests/smoke_test.h
rmp fmt checkfails on a file clang-format would change, and changes nothingthe same files
rmp test fmtthe same, as a stage of rmp testthe framework, and its CI's lint job

A game made with rmp new gets the framework's .clang-format, so rmp fmt lays its code out the same way and rmp fmt check refuses a file that is not; neither the game's rmp test nor its CI runs them. It leaves the framework's own sources alone because a different clang-format would rewrite them for no reason of the game's.

The version is pinned, and it matters: clang-format one minor version out reformats files that were already right. The framework pins clang-format 22.1.8 in thirdparty/FROZEN_VERSIONS.md, and its tools/versions_check.sh says so when the one on your PATH is another. Each block on this page shown refused is what that version says about it.

Four spaces, 90 columns, braces on the line#

Four spaces and no tab, where LLVM's style, which the file starts from, has two. A brace opens on the line of what it belongs to.

Compiles; rmp test fmt refuses it
struct Ship {
  int hp = 3;
  float speed = 120.0f;
};
snippet.cpp:1:14: warning: code should be clang-formatted [-Wclang-format-violations]
struct Ship {
             ^
snippet.cpp:2:14: warning: code should be clang-formatted [-Wclang-format-violations]
  int hp = 3;
             ^
struct Ship {
    int hp = 3;
    float speed = 120.0f;
};

A line is at most 90 columns, not LLVM's 80: wide enough for a declaration of the public headers and the comment after it, which is a shape nearly every line of them has. A longer one is broken where clang-format breaks it:

Compiles; rmp test fmt refuses it
void announce(const int level, const int lives) { DrawText(TextFormat("Level %d, %d lives left", level, lives), 10, 10, 20, WHITE); }
snippet.cpp:1:50: warning: code should be clang-formatted [-Wclang-format-violations]
void announce(const int level, const int lives) { DrawText(TextFormat("Level %d, %d lives left", level, lives), 10, 10, 20, WHITE); }
                                                 ^
snippet.cpp:1:132: warning: code should be clang-formatted [-Wclang-format-violations]
void announce(const int level, const int lives) { DrawText(TextFormat("Level %d, %d lives left", level, lives), 10, 10, 20, WHITE); }
                                                                                                                                   ^
void announce(const int level, const int lives) {
    DrawText(TextFormat("Level %d, %d lives left", level, lives), 10, 10, 20, WHITE);
}

No alignment#

No column of = signs, no comments padded out to line up, no table squared with spaces. A column lined up with a ruler looks tidy in the commit that wrote it and costs something every time after: renaming one field moves twenty lines, the diff of a one-line change is a paragraph, and git blame stops pointing at whoever wrote the code and points at whoever moved the spaces. Code takes its shape from its structure, not from padding.

Compiles; rmp test fmt refuses it
struct Theme {
    Color background    = { 18, 18, 22, 255 }; // behind everything
    Color surface_hover = { 60, 60, 76, 255 };  // a button under the pointer
};
snippet.cpp:2:21: warning: code should be clang-formatted [-Wclang-format-violations]
    Color background    = { 18, 18, 22, 255 }; // behind everything
                    ^
snippet.cpp:3:47: warning: code should be clang-formatted [-Wclang-format-violations]
    Color surface_hover = { 60, 60, 76, 255 };  // a button under the pointer
                                              ^
struct Theme {
    Color background = { 18, 18, 22, 255 }; // behind everything
    Color surface_hover = { 60, 60, 76, 255 }; // a button under the pointer
};

One column is left, and clang-format keeps it: the backslashes that carry a macro over several lines line up just past the macro's longest line, so a new longest line moves every one of them.

A guard on one line#

An if that leaves early stays on one line, and so does a short loop or a short function. if (!ready) return; reads as a guard; a guard spread over three lines stops looking like one. Without braces: a block gets lines of its own.

Compiles; rmp test fmt refuses it
void step(const bool ready) {
    if (!ready)
        return;
    jump();
}
snippet.cpp:2:16: warning: code should be clang-formatted [-Wclang-format-violations]
    if (!ready)
               ^
void step(const bool ready) {
    if (!ready) return;
    jump();
}

A function that fits on a line is written on one, and a block does not start with an empty line:

Compiles; rmp test fmt refuses it
class Ship {
public:
    [[nodiscard]] int hp() const {

        return _hp;
    }

private:
    int _hp = 3;
};
snippet.cpp:3:35: warning: code should be clang-formatted [-Wclang-format-violations]
    [[nodiscard]] int hp() const {
                                  ^
snippet.cpp:5:20: warning: code should be clang-formatted [-Wclang-format-violations]
        return _hp;
                   ^
class Ship {
public:
    [[nodiscard]] int hp() const { return _hp; }

private:
    int _hp = 3;
};

Classes, namespaces and switches#

An access specifier stands at the column of its class, so public: and private: read as the headings they are:

Compiles; rmp test fmt refuses it
class Coin {
    public:
    int value = 10;
};
snippet.cpp:1:13: warning: code should be clang-formatted [-Wclang-format-violations]
class Coin {
            ^
class Coin {
public:
    int value = 10;
};

A namespace does not indent what is in it, and its closing brace says which namespace it closes, however short it is:

Compiles; rmp test fmt refuses it
namespace layer {
    constexpr unsigned PLAYER = 1U << 0;
}
snippet.cpp:1:18: warning: code should be clang-formatted [-Wclang-format-violations]
namespace layer {
                 ^
snippet.cpp:3:2: warning: code should be clang-formatted [-Wclang-format-violations]
}
 ^
namespace layer {
constexpr unsigned PLAYER = 1U << 0;
} // namespace layer

A case is indented inside its switch:

Compiles; rmp test fmt refuses it
float speed_for(const State state) {
    switch (state) {
    case State::IDLE:
        return 0.0f;
    case State::RUNNING:
        return 120.0f;
    }
    return 0.0f;
}
snippet.cpp:2:21: warning: code should be clang-formatted [-Wclang-format-violations]
    switch (state) {
                    ^
snippet.cpp:3:22: warning: code should be clang-formatted [-Wclang-format-violations]
    case State::IDLE:
                     ^
snippet.cpp:4:21: warning: code should be clang-formatted [-Wclang-format-violations]
        return 0.0f;
                    ^
snippet.cpp:5:25: warning: code should be clang-formatted [-Wclang-format-violations]
    case State::RUNNING:
                        ^
float speed_for(const State state) {
    switch (state) {
        case State::IDLE:
            return 0.0f;
        case State::RUNNING:
            return 120.0f;
    }
    return 0.0f;
}

int &x, and { 1, 2 }#

The & of a reference sits against the name, not the type — as the * of a pointer does, which is how raylib writes it: const char *text. There is no pointer in the framework's own code to show it on (Pointers and RAII says why), but every call into raylib's C has one.

Compiles; rmp test fmt refuses it
void heal(int& hp, int amount);
snippet.cpp:1:14: warning: code should be clang-formatted [-Wclang-format-violations]
void heal(int& hp, int amount);
             ^
snippet.cpp:1:15: warning: code should be clang-formatted [-Wclang-format-violations]
void heal(int& hp, int amount);
              ^
void heal(int &hp, int amount);

A braced list keeps a space inside each brace, as raylib's colours are written. LLVM's style closes them up, and every colour of the theme would change.

Compiles; rmp test fmt refuses it
constexpr Color SKY = Color{135, 206, 235, 255};
snippet.cpp:1:29: warning: code should be clang-formatted [-Wclang-format-violations]
constexpr Color SKY = Color{135, 206, 235, 255};
                            ^
snippet.cpp:1:47: warning: code should be clang-formatted [-Wclang-format-violations]
constexpr Color SKY = Color{135, 206, 235, 255};
                                              ^
constexpr Color SKY = { 135, 206, 235, 255 };

What clang-format leaves alone#

It does not sort the includes. They are in the order they were written, and each one may say after it what it is for; sorting them would scramble that, and in code that speaks to C the order of includes can matter.

#include <string_view> // a name on the way in
#include <vector> // the enemies on screen

It never rewraps a comment. The framework's file headers carry trees, tables and paragraphs wrapped by hand, and a formatter that rewraps them destroys the part of the code that took longest to write.

And it does not touch the entry-point macros of rmp/app.h, which sit between // clang-format off and // clang-format on: their bodies hold block comments, and every formatter mangles a /* */ inside a macro's continued lines. They are the only code of the framework's own it is told to leave alone; the vendored libraries and the headers generated from the .toml are left out whole, by .clang-format-ignore.

Files#

A header is .h and a source file .cpp, and a header starts with #pragma once: every compiler the framework builds with understands it, and it cannot get out of step with the file's name the way an include guard can. clang-tidy has a check that would refuse it, portability-avoid-pragma-once, and the framework's .clang-tidy turns it off. Nothing checks that a header has it, and every header of the framework does but one: tests/smoke_test.h, which the plain C example includes too, has an include guard.

#pragma once

namespace game {
inline constexpr int START_LIVES = 3;
} // namespace game