Loading...
Searching...
No Matches
context.f90
Go to the documentation of this file.
1!> @file
2!! @defgroup group_context Context
3!! Source context information used for diagnostics and error reporting.
4!!
5!! This module defines the lightweight @ref fpx_context::context type used throughout
6!! the fpx preprocessor to associate source-location information with
7!! diagnostics, warnings, notes, and error messages.
8!!
9!! Every diagnostic emitted by fpx is accompanied by a context object
10!! describing where the event occurred. This enables the generation of
11!! modern compiler-style messages containing file names, line numbers,
12!! source snippets, and caret annotations.
13!!
14!! A context captures:
15!!
16!! - the original source line,
17!! - the corresponding 1-based line number,
18!! - the path of the source file being processed.
19!!
20!! The information stored in a context is consumed primarily by the
21!! @link fpx_logging fpx_logging @endlink module to produce precise and user-friendly diagnostics.
22!!
23!! For example:
24!!
25!! @code
26!! error: Undefined macro 'DEBUG'
27!! --> src/main.F90:42
28!! |
29!! 42 | #ifdef DEBUG
30!! | ^^^^^ not defined
31!! @endcode
32!!
33!! Accurate context information becomes particularly important when
34!! processing nested #include files, evaluating conditional directives,
35!! or reporting errors originating from macro expansions.
36!!
37!! @section context_examples Examples
38!!
39!! 1. Creating a context object:
40!! @code{.f90}
41!! type(context) :: ctx
42!!
43!! ctx = context( &
44!! content='real :: x = PI*r**2', &
45!! line=27, &
46!! path='src/utils.F90')
47!! ...
48!! @endcode
49!!
50!! 2. Using context when reporting diagnostics:
51!! @code{.f90}
52!! call printf(render(diagnostic_report( &
53!! LEVEL_ERROR, &
54!! message='Undefined macro', &
55!! source=ctx%path), &
56!! ctx%content, ctx%line))
57!! ...
58!! @endcode
59!!
60!! 3. Updating context during #include processing:
61!! @code{.f90}
62!! included_ctx = context( &
63!! content=first_line, &
64!! line=1, &
65!! path=resolved_include_path)
66!! ...
67!! @endcode
68module fpx_context
69 implicit none; private
70
71 !> Snapshot of a source location within the preprocessing stream.
72 !!
73 !! Instances of this type accompany diagnostics throughout fpx and
74 !! provide the information required to identify where an event
75 !! occurred in the original source.
76 !!
77 !! The stored line content is typically displayed alongside
78 !! highlighted regions when rendering diagnostics.
79 !!
80 !! @section context_type_examples Examples
81 !! @code{.f90}
82 !! type(context) :: ctx
83 !!
84 !! ctx = context('lorem ipsum', 42, 'example.F90')
85 !! ...
86 !! @endcode
87 !!
88 !! @section context_type_remarks Remarks
89 !! - A new context is typically created for each processed source line.
90 !! - Entering an `#include` file naturally creates contexts referring
91 !! to the included file.
92 !! - Context objects are lightweight and inexpensive to copy.
93 !! - They form the foundation of fpx's compiler-style diagnostics.
94 !!
95 !! @section context_type_constructors Constructors
96 !!
97 !! Initializes a new instance of the @ref context type.
98 !!
99 !! @b Constructor
100 !! @code{.f90}
101 !! type(context) function context(character(*) content, integer line, character(*) path)
102 !! @endcode
103 !!
104 !! @param[in] content
105 !! Source line associated with the diagnostic.
106 !! @param[in] line
107 !! One-based line number within the source file.
108 !! @param[in] path
109 !! Relative or absolute path of the source file.
110 !!
111 !! @return Newly constructed context object.
112 !!
113 !! @ingroup group_context
114 type, public :: context
115 character(:), allocatable :: content
116 integer :: line
117 character(:), allocatable :: path
118 end type
119
120end module
Snapshot of a source location within the preprocessing stream.
Definition context.f90:114