The Preprocessor
Before the C grammar can be checked, a C or C++ file has to be preprocessed: macros expanded, #if decided, headers read. Kami's preprocessor does this as the compiler does, and keeps for every token that comes out the place in the file where it is written. Most programs never call it directly, because semantic_build() and brace_match_proven() call it; this page is for a program that wants the compiler's view of a file itself. The declarations are in preprocessor.h.
1. What it does
preprocess() runs translation phase 4 over the tokens of the main file. It understands #define and #undef with object-like and function-like macros (variadic, __VA_OPT__, # and ##), #if and #elif expressions with defined, __has_include and the other __has_ tests, the #ifdef and #elifdef families, #include, #include_next and #import (read as #include), #embed in the main file (with limit, prefix, suffix and if_empty; an #embed in a header is passed over), #error, and _Pragma. __FILE__, __LINE__ and __COUNTER__ are built in. Expansion follows Prosser's algorithm with hide sets, so the result is the same as the compiler's, token for token. One difference: __FILE__ in the main file is the file directory followed by /<main> (or "" with no file_directory), since Kami is not told the file's name.
An included file adds its macros to the run, but its own tokens are not part of the output: the output is the main file as the compiler sees it. preprocess_with_headers() is the same call with the included files' tokens in the output too, where they are included (see Where each token comes from).
One run of the preprocessor. Free the result with preprocessed_free(), whatever the call returned.
2. Calling it
Lexer lexer;
lexer_run(&lexer, text, length, 0);
PreprocessorOptions options = { .file_directory = "/home/me/project/src" };
Preprocessed out;
if (preprocess(lexer.tokens, lexer.token_count, text, &options, &out) == 0) {
for (size_t index = 0; index < out.count; index++) {
const PreprocessorToken *token = &out.tokens[index];
/* token->text for token->length bytes; token->spelled, token->expanded_at */
}
if (out.failed) { /* out.failure says why, for people */ }
}
preprocessed_free(&out);
lexer_free(&lexer);
preprocess() returns 0 when it ran to the end, whether or not the file has faults; out.failed tells. It returns -1 when it ran out of memory, and also when a limit stopped the run (see Limits); then out.failure says which limit. In both cases preprocessed_free() must still be called. The options may be NULL, which is the same as options whose every field is NULL or 0.
3. The options
| Field | What it says |
|---|---|
predefines |
The #define lines the compiler starts with, as text: the output of cc -dM -E - </dev/null. With them, #if is decided exactly as that compiler decides it. NULL uses a small built-in set: __STDC__, __STDC_VERSION__ (C23), __STDC_HOSTED__, __GNUC__ 14.2, __x86_64__, __linux__, __unix__, the bits of a char (__CHAR_BIT__), the sizes of int, long and pointers, __INT_MAX__ and __LONG_MAX__, and __cplusplus (C++20) when cplusplus is set. |
defines |
The build's -D and -U, one per line: +NAME=value defines NAME as value, +NAME defines it as 1, and -NAME undefines it. Other lines are ignored. They are read after the predefines, in order. |
include_directories |
The directories that #include looks in, one per line, in order (as the compiler's -I and system directories). Empty lines are skipped. |
file_directory |
The main file's directory. A quoted #include "x.h" of the main file is looked for there first. When it is NULL, the main file is in no directory, so its quoted names are looked for only in the include directories (never in the program's current directory). |
cplusplus |
1 for a C++ file: the built-in set defines __cplusplus, and included files are lexed as C++. |
forced, forced_count |
Decisions to make instead of the preprocessor's own (see Decisions). |
Finding a header: a quoted name is first looked for beside the file that includes it (for the main file, in file_directory, when it is set), then in each include directory in order; a name in < > only in the include directories. #include_next goes on from the directory after the one where the current file was found. A file that holds a NUL byte is not text and is not read. A file already marked with #pragma once is read once (up to 4,096 such files are remembered). A <...> header that is not found is passed over without a fault, so a program that gives no include directories still gets the main file's own macros; a quoted header of the main file that is not found sets failed ("#include "x.h": not found"), and so does an #embed whose file is not found.
4. The result
| Field | What it holds |
|---|---|
tokens, count |
The tokens the compiler reads, in order. Each PreprocessorToken has a kind (a TokenKind), flags (PREPROCESSOR_FLAG_SPACE_BEFORE when white space came before it, PREPROCESSOR_FLAG_LINE_START when it is the first of a line in its file), its spelling (text and length, with no splices and no NUL), and the two fields below. |
spelled |
The index of the main file's token that this token is written as, or PREPROCESSOR_NONE when it is not written in the main file: made by ## or #, by __FILE__, __LINE__ or __COUNTER__, by #embed, or by a __has_ test in code, or written in a header or in the predefines. A token from the body of a macro defined in the main file is spelled at its token in that #define line. |
expanded_at |
The index of the main file's token of the outermost macro use that made this token, or PREPROCESSOR_NONE when the token is written as it is. A brace that comes from a macro can be shown at the macro's use this way. |
live |
One byte for each token of the main file: 1 when the token is in a branch that was taken (and so is compiled, or is a directive that was read), 0 in a branch that was not. A comment outside a directive is always 0, and so is the #endif (or #else) line that ends a branch that was not taken. |
undecided, undecided_count |
The decisions that rested on a macro nothing defines, as they were made (see below). |
failed, failure |
1 after a live #error ("#error at line 12"), a quoted #include of the main file that was not found, an #embed whose file was not found, an #if with no #endif, or a limit; failure says which, for people. |
preprocessed_macro_has_brace(out, name, length) tells whether the run defined a macro of that name whose last definition holds a { or a }. Such a macro changes the blocks where it is used, which brace matching needs to know.
5. Decisions on unknown macros
Code often holds switches that nothing in the build defines: #ifdef FEATURE_X, #if DEBUG. The compiler takes the branch for "not defined", and so does Kami, but such a decision is only one of the configurations the code has. Every #if, #elif, #ifdef and the like of the main file whose decision rested on a macro that nothing defines (a reserved name such as __x or _X does not count), or on a __has_attribute, __has_builtin, __has_embed or __has_feature test, or that was forced, is listed in undecided, as a PreprocessorDecision: the index of the directive's # token (directive) and whether its branch was taken (taken). An #ifndef X right before #define X is an include guard and is not listed.
To see another configuration, run the preprocessor again with forced holding the decisions to make the other way: each forced decision names a directive by its # token and says whether its branch is taken. This is how brace_match_proven() checks every configuration that can change the braces.
6. Where each token comes from
preprocess_with_headers() puts the tokens of every included file into the output, where they are included, expanded as the main file's are. This is the whole translation unit as a compiler sees it, for a program that wants to know what the headers declare, including what their macros make (glibc makes its math functions so). A header's token has spelled set to PREPROCESSOR_NONE. preprocessed_token_origin(out, token, &path, &text, &offset) returns 1 and says in which file a token's text is written (a full path, which out owns), that file's text, and the token's byte offset in it. It returns 0 for a token of the main file, for the predefines and the -D lines, for a token made by pasting or stringizing, and for a header token whose spelling had to be rebuilt (one with a line splice, a trigraph or a \u in a name) or that is empty.
7. Limits
Every file is untrusted: ten macros that each use the last one ten times, or a header that includes itself twice, would otherwise take all the memory there is. One run stops, with failed set, the reason in failure ("preprocessing stopped: ..."), and a return value of -1, when it makes more than 4 million tokens by macro expansion (each byte of an #embed counts as 2: "#embed file too large"), includes more than 50,000 files, or reads more than 1 GB of included text. Includes nested more than 200 deep are passed over without a fault. Real code is far below these limits: the macros of sqlite3.c make about 84 thousand tokens.
Kami API
The whole library
Reading C and C++
Braces
Any language
