On this page
  1. A constant is constexpr
  2. A function is a function
  3. When a macro is the right tool
  4. Every macro starts with RMP_
  5. A name that no longer exists is 0
  6. A constant is never named like a macro
  7. No header makes you write one
  8. What checks it

Macros

The preprocessor runs before the compiler knows what a type, a scope or a namespace is: a macro replaces its name on every line after it — in its file, in every file that includes it, and in every header included after it, raylib's and the C library's among them. So the rule is short. A macro is for what nothing else in the language can do, and everything else is a constexpr constant, a function or a template. Every macro the framework and its examples write is named RMP_.

This page is the rule as it applies to any code written here. The framework's own macros — the entry point, the values of the .toml, the said-once warning — are explained one by one in why the framework has the macros it has.

A constant is constexpr#

A constant made with #define has no type, so 3 is whatever the expression around it makes of it, and no scope, so it replaces that word everywhere after it — in your files and in every header included after it. A constexpr constant has both, and is just as known when the program compiles:

Compiles; rmp lint refuses it: cppcoreguidelines-macro-usage
#define RMP_LIVES 3
snippet.cpp:1:9: warning: macro 'RMP_LIVES' used to declare a constant; consider using a 'constexpr' constant [cppcoreguidelines-macro-usage]
    1 | #define RMP_LIVES 3
      |         ^
constexpr int LIVES = 3;

In a header, a constant at namespace scope is inline constexpr, one object for the whole program; const has why.

A function is a function#

A function-like macro pastes its arguments in as text: the one below, handed i++, increments twice; an argument of the wrong type is an error inside the expansion; and a debugger cannot step into it. A constexpr function does the same work at compile time when it can, and is a function the rest of the time:

Compiles; rmp lint refuses it: cppcoreguidelines-macro-usage
#define RMP_SQUARE(x) ((x) * (x))
snippet.cpp:1:9: warning: function-like macro 'RMP_SQUARE' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]
    1 | #define RMP_SQUARE(x) ((x) * (x))
      |         ^
template <class T> constexpr T square(const T x) { return x * x; }

static_assert(square(3) == 9);

When a macro is the right tool#

Four things no function can do, and they are the four the framework's own macros do.

Code at file scope. A program's entry point is a main() on a desktop, a function handed to the browser on the web and three extern "C" functions on iOS. No function can define a function, so the entry point is a macro, and a game writes it once:

RMP_GAME(MainMenuScene);

The call site. A function receives a value; it cannot see how the call was spelt, and it cannot give each place that calls it a variable of its own. A macro can do both. This one prints the expression it was handed as well as its value, through the # operator, which turns an argument into a string:

#define RMP_SHOW(value) TraceLog(LOG_INFO, "%s = %d", #value, value)
const int lives = 3;
RMP_SHOW(lives);

The framework's said-once warning is the other half of the same case: every place RMP_REPORT_ONCE is written gets a static variable of its own, whose address is the key that makes it say its line once, and only a macro can declare one there.

Conditional compilation. The same source is built for every target, and some of it only makes sense on one of them. That is an #if on a macro the platform defines:

#if defined(PLATFORM_ANDROID)
const int touch_target = 44;
#else
const int touch_target = 32;
#endif
DrawRectangle(0, 0, touch_target, touch_target, RED);

A value from the .toml. What raylib_multiplatform.toml says reaches the code as macros, RMP_WINDOW_WIDTH and the rest, because a game in plain C reads them too and because #if can test them. The .toml writes them; a game only reads them:

const Vector2 centre{ RMP_WINDOW_WIDTH / 2.0f, RMP_WINDOW_HEIGHT / 2.0f };
DrawCircleV(centre, 8, RED);

Checked by cppcoreguidelines-macro-usage, in the framework and in the examples. It refuses a macro whose body is only a literal, which could be a constant, and a macro with parameters, which could be a function; it lets through one that uses # or ##, which nothing else can do, and one that wraps a compiler's __attribute__, as the framework's check of printf formats does. The framework's own macros that it would refuse are named to it one by one, in its AllowedRegexp, so a new one in the framework is a question for whoever reviews it, not something that slips past. clang-tidy's suggestion to turn the .toml's values into an enum, modernize-macro-to-enum, is switched off for them: the C entry point reads them too.

Every macro starts with RMP_#

The preprocessor has one namespace, shared by raylib, the C library, every vendored library and the game, and two macros of the same name are one silently replacing the other. So every macro written here starts with RMP_; names that start with two underscores, or with one and a capital letter, belong to the compiler and the C library:

Compiles; rmp test naming refuses it: R5
#define NAME_OF(x) #x
FAIL  snippet.cpp:1: R5 #define NAME_OF -- a macro without the RMP_ prefix
#define RMP_NAME_OF(x) #x

Checked by readability-identifier-naming, which is given RMP_ as the macros' prefix, in the framework and in the examples, and by rule R5 of tools/naming_check.sh in every branch of every #if. A vendored library's own switches keep their names, in the one file of the framework that configures it, and the script lists each of those by file.

A game's own macros are a different case: RMP_ is the framework's prefix, and a game's macro that took it could one day meet one of the framework's. Google's rule decides there — the project's own name, in capitals, as the prefix. Nothing in a game checks it, and a game that switches the framework's clang-tidy on writes its own prefix in the copy, which asks for RMP_.

A name that no longer exists is 0#

An #if on a macro nobody defines is not an error: the name is 0. When a macro is renamed, every test of the old name keeps compiling and quietly takes the other branch. The CMake option is PRODUCTION_BUILD, and what the code sees is RMP_PRODUCTION_BUILD; written the CMake way, this draws the frame rate in every build, a release included:

Compiles; rmp test naming refuses it: R7
#if !PRODUCTION_BUILD
DrawFPS(10, 10);
#endif
FAIL  snippet.cpp:1: R7 PRODUCTION_BUILD -- a retired name
#if !RMP_PRODUCTION_BUILD
DrawFPS(10, 10);
#endif

Checked by rule R7 of tools/naming_check.sh, which keeps the list of retired names, in every branch of the framework and the examples. For RMP_PRODUCTION_BUILD the framework goes further: every build system defines it, and src/rmp/app/app.cpp refuses to compile if one forgets.

A constant is never named like a macro#

The other direction of the same collision: a constant called PI compiles until the file includes raylib.h, which defines PI as a number, and then the declaration is a number followed by an equals sign:

Does not compile
constexpr float PI = 3.14159265f;
snippet.cpp:1:17: error: expected unqualified-id
    1 | constexpr float PI = 3.14159265f;
      |                 ^

Here the error is immediate. With a macro that only some platforms define — MIN and MAX on the BSDs, DEBUG in Android's debug build — it compiles everywhere except there. Naming has the rule and the list; rule R6 of tools/naming_check.sh refuses a constant named like a macro of the vendored headers, or like one of the C library's and the BSDs' that are known to collide.

No header makes you write one#

Outside the entry point, nothing in the API asks for a macro. rmp::ui sits on Clay, whose API is macros, and hands a game functions that take a lambda instead; the rule has how. A game that wants Clay's macros can include Clay and use them.

What checks it#

RuleChecked byWhere
A macro only for what nothing else can docppcoreguidelines-macro-usagethe framework and the examples
Every macro starts with RMP_readability-identifier-naming, and R5 of tools/naming_check.sh in every branchthe framework and the examples
No retired name in an #ifR7 of tools/naming_check.shthe framework and the examples
No constant named like a macroR6 of tools/naming_check.shthe framework and the examples

A game gets none of these checks. The two of clang-tidy's hold its code if it switches the framework's clang-tidy on; tools/naming_check.sh stays in the framework. The rules are the same advice for its code, and what checks what has the whole list.