Loading...
Searching...
No Matches
Define

Definition

Processing of define and undef preprocessor directives This module implements the core logic for handling macro definition and removal during preprocessing in the fpx Fortran preprocessor. It supports:

  • Object-like macros: #define NAME value
  • Function-like macros: #define NAME(arg1, arg2, ...) replacement
  • Variadic macros using ... and automatic detection
  • Proper parameter parsing with whitespace handling
  • Macro redefinition (overwrites existing definition)
  • Safe #undef that removes a previously defined macro
  • Integration with global undef list (globalundef) to block redefinition
  • Comprehensive verbose logging of all definition actions

The routines are designed to be robust against malformed input and provide clear diagnostics when verbose = .true..

Examples

  1. Define simple object-like macros:
    #define PI 3.141592653589793
    #define DEBUG 1
    #define MAX_SIZE 1024
    ...
  2. Define function-like and variadic macros:
    #define SQR(x) ((x)*(x))
    #define LOG_MSG(level, ...) print *, '[LOG:', level, ']', __VA_ARGS__
    #define CONCAT(a,b) a ## _ ## b
    ...
  3. Undefine a macro:
    #undef DEBUG
    !> Subsequent #ifdef DEBUG will be false
  4. Using from a driver program:
    use fpx_global
    use fpx_logging, only: verbose
    verbose = .true.
    call preprocess('input.F90') ! Will show all macro definitions/undefs
    ...
    logical, public verbose
    Master switch for verbose diagnostic output Default value is .false. (quiet mode)....
    Definition logging.f90:108

Methods

◆ handle_define()

subroutine, public handle_define ( type(context), intent(in) ctx,
type(macro), dimension(:), intent(inout), allocatable macros,
character(*), intent(in) token )

Process a define directive and register or update a macro Parses the line after #define, distinguishes between object-like and function-like forms, handles variadic ..., extracts parameters correctly, and stores the macro in the active macro table. Existing macros are overwritten. Respects globalundef list – macros listed there are ignored.

Parameters
[in]ctxContext source line containing the define
[in,out]macrosCurrent macro table (updated in-place)
[in]tokenUsually 'DEFINE' – keyword matched in lowercase

Remarks

Definition at line 77 of file define.f90.

◆ handle_undef()

subroutine, public handle_undef ( type(context), intent(in) ctx,
type(macro), dimension(:), intent(inout), allocatable macros,
character(*), intent(in) token )

Process a undef directive and remove a macro from the table Finds the named macro in the current table and removes it. Issues a warning if the macro was not previously defined.

Parameters
[in]ctxContext source line containing the undef
[in,out]macrosCurrent macro table (updated in-place)
[in]tokenUsually 'UNDEF' – keyword matched in lowercase

Remarks

Definition at line 207 of file define.f90.