Loading...
Searching...
No Matches
Context

Definition

Source context tracking for diagnostics and error reporting in fpx.

This small but critical module defines the context type used throughout the fpx preprocessor to attach precise location information to every diagnostic message, label, or error report.

Every time the preprocessor encounters a problem (undefined macro, include not found, invalid #if expression, unmatched #endif, etc.), it creates or reuses a context object that carries:

  • The full line content (for showing source snippets)
  • The 1-based line number
  • The full file path (for --> arrows in diagnostics)
  • The base filename (for concise reporting when path is long)

This information is then used by the fpx_logging module to produce modern, rustc/clang-style error messages with line numbers, caret highlights (^), and file references.

Without accurate context, diagnostics would be vague ("macro not defined"). With context, users see exactly where the problem happened:

error: undefined macro 'DEBUG'
--> src/main.f90:42:10-15
|
42 | #ifdef DEBUG
| ^^^^^ 'DEBUG' not defined
Typical usage examples
  1. Creating context when reading a new file:
    type(context) :: ctx
    ctx%path =
    ctx%line = 27
    ctx%content =
    ! Now pass ctx to logging functions
    call report_undefined_macro(, ctx)
  2. Updating context during recursive include processing:
    ! In handle_include
    type(context) :: included_ctx
    included_ctx%path = resolved_include_path
    included_ctx%line = 1 ! reset for new file
    included_ctx%content = first_line_of_included_file
    ! Then use included_ctx for diagnostics inside the included file
  3. Attaching context to a diagnostic label:
    type(label_type) :: lbl
    lbl = label_type(level_error, , &
    line=ctx%line, first=8, last=12, primary=.true.)
    diag = diagnostic_report(level_error, , ctx%path, [lbl])

Data Types

type  context
 Source location and content snapshot for precise diagnostics Instances of this type are created for every source file (including nested include files) and passed along with every warning, error, note, or info message to provide accurate line numbers, file names, and code snippets. More...