Table of Contents
preprocessor.h
The C Preprocessor, with Every Token Mapped Back to the File.
Copyright (c) 2016-2026 Michael Grey (liquibyte) liquibyte@gmail.com SPDX-License-Identifier: Artistic-2.0 Distributed under the Artistic License 2.0; see LICENSE for terms.
The preprocessor runs translation phase 4 over the tokens of a C or C++ file, as the compiler does, and keeps for every token that comes out the place in the file where it is written.
Features:
- 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 and include_next; embed; _Pragma
- Every token that comes out knows where it is written in the main file (spelled) and which macro use in the main file made it (expanded_at), so a brace that comes from a macro can be shown at the macro's use
- An included file adds its macros only: its own tokens are not part of the main file and are left out, unless
preprocess_with_headers()asks for them - An if on a macro that nothing defines (a configuration switch such as FEATURE_X) is reported as undecided, and a caller can force any decision to see the other configurations
- A run has limits, because a file is never trusted: 4 million tokens made by macros, 50,000 included files, 1 GB of included text, and includes 200 deep. Past the first three the run stops and fails
Internal design notes:
- Expansion follows Prosser's algorithm with hide sets, kept as persistent lists (a set is a node that points at the rest)
- It works from tokens, never from the raw text, so a token whose length is 0 (one that a program inserted) still has a spelling, from its kind
- The compiler's own macros come in as text (the output of cc -dM -E), so the configuration is the compiler's and not a guess
#include "preprocessor.h"
Types: PREPROCESSOR_FLAG_SPACE_BEFORE and the other values, PreprocessorToken, PreprocessorDecision, PreprocessorOptions, Preprocessed
Constants: PREPROCESSOR_NONE
Functions: preprocess, preprocess_with_headers, preprocessed_token_origin, preprocessed_free, preprocessed_macro_has_brace
Types
PREPROCESSOR_FLAG_SPACE_BEFORE and the other values
The bits of PreprocessorToken.flags.
| Name | Value | Description |
|---|---|---|
PREPROCESSOR_FLAG_SPACE_BEFORE |
1 << 0 |
white space comes before the token (for # and for printing) |
PREPROCESSOR_FLAG_LINE_START |
1 << 1 |
the token is the first of a line in its file |
PreprocessorToken
One token after phase 4, with where it comes from.
| Field | Type | Description |
|---|---|---|
kind |
uint8_t |
a TokenKind (TOKEN_IDENTIFIER, TOKEN_LEFT_BRACE ...) |
flags |
uint8_t |
PREPROCESSOR_FLAG_* bits. |
spelled |
uint32_t |
the main file's token it is written as, or PREPROCESSOR_NONE (made by pasting or stringizing, or written in another file) |
expanded_at |
uint32_t |
the main file's token of the outermost macro use that made it, or PREPROCESSOR_NONE (it is written as it is) |
text |
const char * |
its spelling, with no line splices (not NUL-terminated) |
length |
uint32_t |
the spelling's length in bytes |
PreprocessorDecision
A decision on one if, elif, ifdef ... of the main file, named by the index of its # token.
| Field | Type | Description |
|---|---|---|
directive |
uint32_t |
the main file's token: the # of the directive |
taken |
uint8_t |
1 when its branch is taken, 0 when it is not |
PreprocessorOptions
What the preprocessor is told about the build. Every field may be NULL or 0, and a NULL pointer to it means all of them are.
| Field | Type | Description |
|---|---|---|
predefines |
const char * |
the define lines the compiler starts with (the output of cc -dM -E), or NULL for a small built-in set (GCC on x86-64 Linux, C23) |
defines |
const char * |
the build's -D and -U, one per line: "+NAME=value", "+NAME" (defined as 1) or "-NAME"; or NULL |
include_directories |
const char * |
the directories include looks in, one per line, in order; a quoted name is first looked for beside the file that includes it |
file_directory |
const char * |
the main file's directory, for its quoted include; with NULL the main file is in no directory, and its quoted names are looked for in include_directories only |
cplusplus |
int |
1 when the file is C++: the built-in set then defines __cplusplus, and included files are lexed as C++ |
forced |
const PreprocessorDecision * |
decisions to make instead of the preprocessor's own, or NULL |
forced_count |
size_t |
how many decisions forced has |
Preprocessed
What preprocess() gives: the tokens the compiler reads, and what was decided.
| Field | Type | Description |
|---|---|---|
tokens |
PreprocessorToken * |
the main file after phase 4 |
count |
size_t |
how many tokens there are |
live |
uint8_t * |
one per main-file token: 1 when the token is in a branch that was taken (a comment outside a directive, and the line that ends a branch not taken, are 0) |
undecided |
PreprocessorDecision * |
the decisions that rested on a macro nothing defines, as they were made |
undecided_count |
size_t |
how many undecided decisions there are |
failed |
int |
1 after a live error, 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 reached |
failure[160] |
char |
which of those it was, for people ("#error at line 12") |
internal |
void * |
the storage behind the token texts; preprocessed_free() frees it |
Constants
PREPROCESSOR_NONE
#define PREPROCESSOR_NONE UINT32_MAX
No token: the value of a field that names none.
Functions
preprocess
int preprocess(
const Token *tokens,
size_t token_count,
const char *source,
const PreprocessorOptions *options,
Preprocessed *out);
Run phase 4 over the main file's tokens.
| Parameter | Description |
|---|---|
tokens |
The main file's tokens (lexer_run()) |
token_count |
How many tokens there are |
source |
The main file's text, which the tokens point into |
options |
What is known of the build, or NULL for nothing known (a C file) |
out |
Filled in; free it with preprocessed_free(), also when the call fails |
Returns: 0 (out->failed tells whether the file preprocesses cleanly), or -1 when out of memory or when a limit stopped the run (then out->failure says which)
preprocess_with_headers
int preprocess_with_headers(
const Token *tokens,
size_t token_count,
const char *source,
const PreprocessorOptions *options,
Preprocessed *out);
preprocess(), with the tokens of the included files in the output too, where they are included
This is the file as a compiler sees it, for reading what the headers declare (including what their macros make: glibc makes its math functions so). The main file's own answers should come from preprocess(). A header's token has spelled == PREPROCESSOR_NONE; preprocessed_token_origin() says where it is written.
| Parameter | Description |
|---|---|
tokens |
The main file's tokens |
token_count |
How many tokens there are |
source |
The main file's text |
options |
What is known of the build, or NULL for nothing known (a C file) |
out |
Filled in; free it with preprocessed_free(), also when the call fails |
Returns: As preprocess()
preprocessed_token_origin
int preprocessed_token_origin(
const Preprocessed *out,
const PreprocessorToken *token,
const char **path,
const char **text,
size_t *offset);
Which file a token's text is written in, and where.
| Parameter | Description |
|---|---|
out |
What preprocess() or preprocess_with_headers() made |
token |
One of out's tokens |
path |
Set to the file's full path, which out owns |
text |
Set to that file's text |
offset |
Set to the token's byte offset in that text |
Returns: 1 when the token is written in an included file; 0 for the main file, for text the preprocessor made itself (the predefines and -D lines), for a token made by pasting or stringizing, and for a header token whose spelling was rebuilt (a line splice, a trigraph or a \u in it) or that is empty; the three outputs are then not set
preprocessed_free
void preprocessed_free(Preprocessed *out);
Free what preprocess() or preprocess_with_headers() made, and clear it.
| Parameter | Description |
|---|---|
out |
The result, or NULL (nothing is done) |
preprocessed_macro_has_brace
int preprocessed_macro_has_brace(
const Preprocessed *out,
const char *name,
size_t length);
Whether a macro's body holds a brace: such a macro changes the blocks where it is used.
| Parameter | Description |
|---|---|
out |
What preprocess() made |
name |
The macro's name |
length |
The name's length in bytes |
Returns: 1 when the run defined a macro of that name and its last definition holds a { or a }, else 0
Kami API
The whole library
Reading C and C++
Braces
Any language