Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Provisional API: This reference documents current compiler behavior. Compatibility is not yet promised.

src/preprocess.x

C preprocessor directives in x2c source.

Functions

FunctionSummary
preproc_branch_stateReturns a group’s hidden-arm state after its #elif or #else: 2 when the group’s state was 1, and 0 otherwise.
preproc_conditional_kindClassifies the preprocessor line text as a conditional directive: <open> for #if, #ifdef, and #ifndef, <branch> for #elif and #else forms, <close> for #endif, or 0 for any other line.
preproc_directiveReturns the preprocessor line text without its # and the blanks around the directive.
preproc_include_targetReturns the file named by the #include line text, or NULL for any other line.
preproc_open_stateReturns the hidden-arm state of the conditional group that text opens: 2 when C never takes its first arm, because the condition requires a never-defined name or is 0; 1 when C never takes the arms after its first #else, because the condition is exactly !defined(NAME); and 0 otherwise.
preproc_track_armsFollows the conditional groups open after the preprocessor line text.
preproc_visibilityReturns 1 when the preprocessor line text is #pragma private, 0 when it is #pragma public, and -1 otherwise.
preproc_within_armsReturns items inside the conditional arms arms tracked by preproc_track_arms: the directives that reopen each group, outermost first, then items, then one #endif per group.
Compiler.leading_preprocReturns source-ordered preprocessor nodes in the preceding trivia.
Compiler.note_object_macroRecords the name of the #define line content so a bare atom spelled the same way inside a literal can be flagged and a declaration prefix can be read.
Compiler.scan_conditionalsRecords the open groups after each conditional directive of the tokenized unit, and marks layout attributes where written or where a macro expands to one.
Compiler.update_source_visibilityApplies public and private pragma directives to source visibility state and records each object-like #define name, less those #undef drops, for the literal warning.

Functions

preproc_branch_state

int preproc_branch_state(int state)

Returns a group’s hidden-arm state after its #elif or #else: 2 when the group’s state was 1, and 0 otherwise.

Source: src/preprocess.x:79

preproc_conditional_kind

Symbol preproc_conditional_kind(String text)

Classifies the preprocessor line text as a conditional directive: <open> for #if, #ifdef, and #ifndef, <branch> for #elif and #else forms, <close> for #endif, or 0 for any other line.

Source: src/preprocess.x:29

preproc_directive

String preproc_directive(String text)

Returns the preprocessor line text without its # and the blanks around the directive.

Source: src/preprocess.x:22

preproc_include_target

String preproc_include_target(String text, int &angle)

Returns the file named by the #include line text, or NULL for any other line. angle is 1 for a <...> name and 0 otherwise. Text after the name, such as a comment, is ignored.

Source: src/preprocess.x:96

preproc_open_state

int preproc_open_state(String text)

Returns the hidden-arm state of the conditional group that text opens: 2 when C never takes its first arm, because the condition requires a never-defined name or is 0; 1 when C never takes the arms after its first #else, because the condition is exactly !defined(NAME); and 0 otherwise.

Source: src/preprocess.x:42

preproc_track_arms

List preproc_track_arms(List arms, String text)

Follows the conditional groups open after the preprocessor line text. arms holds one entry per open group, innermost first, listing the preproc nodes that select that group’s current arm.

Source: src/preprocess.x:417

preproc_visibility

int preproc_visibility(String text)

Returns 1 when the preprocessor line text is #pragma private, 0 when it is #pragma public, and -1 otherwise. A comment in the line reads as a blank, as it does in C.

Source: src/preprocess.x:84

preproc_within_arms

List preproc_within_arms(List arms, List items)

Returns items inside the conditional arms arms tracked by preproc_track_arms: the directives that reopen each group, outermost first, then items, then one #endif per group.

Source: src/preprocess.x:430

Compiler

Compiler.leading_preproc

List Compiler.leading_preproc(Compiler c)

Returns source-ordered preprocessor nodes in the preceding trivia.

Spaces and comments remain trivia rather than becoming AST nodes.

Source: src/preprocess.x:283

Compiler.note_object_macro

void Compiler.note_object_macro(Compiler c, String content)

Records the name of the #define line content so a bare atom spelled the same way inside a literal can be flagged and a declaration prefix can be read. The directive after #define is scanned as x2c tokens. An #undef drops the name, so later source reads it as an ordinary identifier.

Source: src/preprocess.x:316

Compiler.scan_conditionals

void Compiler.scan_conditionals(Compiler c)

Records the open groups after each conditional directive of the tokenized unit, and marks layout attributes where written or where a macro expands to one.

Source: src/preprocess.x:142

Compiler.update_source_visibility

void Compiler.update_source_visibility(Compiler c, List directives)

Applies public and private pragma directives to source visibility state and records each object-like #define name, less those #undef drops, for the literal warning.

A negative visibility state disables pragma tracking for this token stream; macro names are recorded regardless.

Source: src/preprocess.x:301

Design notes

Preprocessor lines stay in the token stream and become preproc nodes, so generated C keeps them where they were written. This module owns what x2c reads from those lines without running a preprocessor: which conditional arms C can reach, and what the directives before each form say about visibility, layout, and the unit’s #define names.