Loading...
Searching...
No Matches
context.f90
Go to the documentation of this file.
1!> @file
2!! @defgroup group_context Context
3!! Source context tracking for diagnostics and error reporting in fpx
4!!
5!! This small but critical module defines the `context` type used throughout the fpx
6!! preprocessor to attach precise location information to every diagnostic message,
7!! label, or error report.
8!!
9!! Every time the preprocessor encounters a problem (undefined macro, include not found,
10!! invalid `#if` expression, unmatched `#endif`, etc.), it creates or reuses a `context`
11!! object that carries:
12!! - The full line content (for showing source snippets)
13!! - The 1-based line number
14!! - The full file path (for `-->` arrows in diagnostics)
15!! - The base filename (for concise reporting when path is long)
16!!
17!! This information is then used by the `fpx_logging` module to produce modern,
18!! rustc/clang-style error messages with line numbers, caret highlights (^), and
19!! file references.
20!!
21!! Without accurate context, diagnostics would be vague ("macro not defined").
22!! With `context`, users see exactly where the problem happened:
23!!
24!! ```
25!! error: Undefined macro 'DEBUG'
26!! --> src/main.F90:42:10-15
27!! |
28!! 42 | #ifdef DEBUG
29!! | ^^^^^ 'DEBUG' not defined
30!! ```
31!!
32!! @par Typical usage examples
33!!
34!! 1. Creating context when reading a new file:
35!! @code{.f90}
36!! type(context) :: ctx
37!!
38!! ctx%path = "/home/user/project/src/utils.F90"
39!! ctx%line = 27
40!! ctx%content = "real :: x = PI * r**2"
41!!
42!! ! Now pass ctx to logging functions
43!! call report_undefined_macro("PI", ctx)
44!! @endcode
45!!
46!! 2. Updating context during recursive #include processing:
47!! @code{.f90}
48!! ! In handle_include
49!! type(context) :: included_ctx
50!!
51!! included_ctx%path = resolved_include_path
52!! included_ctx%line = 1 ! reset for new file
53!! included_ctx%content = first_line_of_included_file
54!!
55!! ! Then use included_ctx for diagnostics inside the included file
56!! @endcode
57!!
58!! 3. Attaching context to a diagnostic label:
59!! @code{.f90}
60!! type(label_type) :: lbl
61!!
62!! lbl = label_type(LEVEL_ERROR, "expected expression", &
63!! line=ctx%line, first=8, last=12, primary=.true.)
64!!
65!! diag = diagnostic_report(LEVEL_ERROR, "Invalid #if condition", ctx%path, [lbl])
66!! @endcode
67module fpx_context
68 implicit none; private
69
70 !> Source location and content snapshot for precise diagnostics
71 !! Instances of this type are created for every source file (including nested
72 !! #include files) and passed along with every warning, error, note, or info
73 !! message to provide accurate line numbers, file names, and code snippets.
74 !!
75 !! <h2 class="groupheader">Examples</h2>
76 !! @code{.f90}
77 !! type(context) :: ctx
78 !! ctx = context('lorem ipsum', __LINE__, 'myfile.f90')
79 !! ...
80 !! @endcode
81 !! <h2 class="groupheader">Remarks</h2>
82 !! - Usually one `context` exists per currently processed file.
83 !! - When entering an `#include`, a new context is created for the included file.
84 !! - Helps produce high-quality, IDE-friendly error messages.
85 !!
86 !! <h2 class="groupheader">Constructors</h2>
87 !! Initializes a new instance of the @ref context class
88 !! <h3>context(character(*), integer, character(*))</h3>
89 !! @verbatim type(context) function context(character(*) content, integer line, character(*) path) @endverbatim
90 !!
91 !! @param[in] content The actual line of source code where the issue occurred
92 !! @param[in] line 1-based line number in the current file
93 !! @param[in] path Full absolute or relative path to the file
94 !!
95 !! @return The constructed datetime object.
96 !!
97 !! <h2 class="groupheader">Remarks</h2>
98 !! @ingroup group_context
99 type, public :: context
100 character(:), allocatable :: content
101 integer :: line
102 character(:), allocatable :: path
103 end type
104
105end module
Source location and content snapshot for precise diagnostics Instances of this type are created for e...
Definition context.f90:99