Loading...
Searching...
No Matches
global.f90
Go to the documentation of this file.
1!> @file
2!! @defgroup group_global Global
3!! Central global configuration and shared state for the fpx Fortran preprocessor
4!! This module defines a single global instance `global` of type `global_settings`
5!! that holds all persistent, user-configurable state used across the entire preprocessing session:
6!!
7!! - `macros(:)` : Dynamic table of all defined macros (object-like and function-like)
8!! - `undef(:)` : List of symbols explicitly undefined via `#undef`
9!! - `includedir(:)` : User-specified include search directories for `#include <...>`
10!! - `expand_macros` : Master switch to enable/disable macro expansion (default: .true.)
11!! - `exclude_comments` : When .true., strip C-style /*...*/ and Fortran ! comments from output
12!!
13!! The design uses a single public variable `global` so that all fpx modules can access
14!! and modify the same configuration without passing arguments everywhere.
15!! This is safe in single-threaded use (typical for preprocessing) and allows easy
16!! customization from driver programs or interactive sessions.
17!! <h2 class="groupheader">Examples</h2>
18!!
19!! 1. Add custom include paths before preprocessing:
20!! @code{.f90}
21!!
22!! global%includedir = [ './include', '/usr/local/include/fortran', '../common' ]
23!! call preprocess('main.F90', 'main.f90')
24!! !#include <file.h> will search these directories
25!! @endcode
26!!
27!! 2. Predefine common macros (e.g. for conditional compilation):
28!! @code{.f90}
29!!
30!! call add(global%macros, macro('DEBUG', '1'))
31!! call add(global%macros, macro('MPI_VERSION', '3'))
32!! call add(global%macros, macro('USE_OPENMP', '1'))
33!!
34!! call preprocess('src/app.F90')
35!! !> Code can now use #ifdef DEBUG, #if MPI_VERSION >= 3, etc.
36!! @endcode
37!!
38!! 3. Disable macro expansion temporarily (pass-through mode):
39!! @code{.f90}
40!! global%expand_macros = .false. ! Only handle #include and conditionals
41!! call preprocess('raw_source.F90', 'clean.F90')
42!! ...
43!! @endcode
44!!
45!! 4. Strip all comments from final output:
46!! @code{.f90}
47!! global%exclude_comments = .true.
48!! call preprocess('messy.F90', 'clean_no_comments.f90')
49!! ...
50!! @endcode
51module fpx_global
52 use fpx_constants
53 use fpx_string
54 use fpx_macro
55
56 implicit none; private
57
58 !> Global preprocessor configuration and shared runtime state
59 !! All components of fpx read from and write to this single instance.
60 !! Users can safely modify its public components at any time.
61 !! <h2 class="groupheader">Examples</h2>
62 !! @code{.f90}
63 !! use fpx_global
64 !!
65 !! call add(global%macros, macro('__LFORTRAN__','1'))
66 !! call add(global%macros, macro('__VERSION__'))
67 !! call add(global%macros, macro('__LFORTRAN_MAJOR__'))
68 !! call add(global%macros, macro('__LFORTRAN_MINOR__'))
69 !! call add(global%macros, macro('__LFORTRAN_PATCHLEVEL__'))
70 !! @endcode
71 !! <h2 class="groupheader">Remarks</h2>
72 !! @par
73 !! The global settings are accessed through the global variable
74 !! @ref global
75 !! @ingroup group_global
76 type, public :: global_settings
77 private
78 type(macro), allocatable, public :: macros(:) !< List of global macros
79 type(string), allocatable, public :: undef(:) !< List of undefined macros
80 type(string), allocatable, public :: includedir(:) !< List of include directories
81 logical, public :: expand_macros = .true. !< Boolean controlling the macro expansion. The macros are expanded by default.
82 logical, public :: exlude_comments = .false. !< Boolean controlling the inclusion/exclusion of comments. The comments are kept by default.
83 logical, public :: implicit_continuation = .false. !< Boolean controlling implicit continuation line.
84 logical, public :: line_break = .false. !< Boolean controlling line break with double backslash.
85 logical, public :: extra_macros = .false. !< Boolean controlling extra (non-standard macro definitions: __FILENAME__, __TIMESTAMP__).
86 logical, public :: interactive = .false. !< Boolean controlling whether the program is used in interactive mode (REPL) or not.
87 logical, public :: nocolor = .false. !< @brief Switch for controling the ANSI color output. Default value is `.true.` (color mode on). Set to `.false.` to get raw string output.
88 end type
89
90 !> @brief The single global instance used throughout fpx
91 !! Initialized automatically with sensible defaults values.
92 !! @ingroup group_global
93 type(global_settings), public :: global
94
95end module
type(global_settings), public global
The single global instance used throughout fpx Initialized automatically with sensible defaults value...
Definition global.f90:93
character(:) function, allocatable, public expand_macros(line, macros, stitch, implicit_conti, ctx)
Core recursive macro expander (handles function-like, variadic, #, ##).
Definition macro.f90:300
Global preprocessor configuration and shared runtime state All components of fpx read from and write ...
Definition global.f90:76
Derived type representing a single preprocessor macro Extends string with macro-specific fields: rep...
Definition macro.f90:94
Represents text as a sequence of ASCII code units. The derived type wraps an allocatable character ar...
Definition string.f90:110