Loading...
Searching...
No Matches
Line

Definition

Standard-compliant handling of the #line directive.

This module implements support for the ISO C preprocessor #line directive, allowing the logical source location used by the preprocessor to be modified during preprocessing.

The directive affects the information stored in the active context object and therefore influences:

  • Diagnostic messages and source locations,
  • Expansions of predefined macros such as __LINE__,
  • Expansions of __FILE__ and __FILENAME__,
  • The apparent origin of generated or transformed source code.

This functionality is particularly useful for:

  • Source-to-source translators,
  • Code generators,
  • Literate programming systems,
  • Template engines,
  • Tools that emit Fortran intended to preserve original source locations.

The following standard forms are supported:

  • #line <number>
  • #line <number> "<filename>"

When encountered, the directive immediately updates the logical source position used for all subsequent processing.

Note
The specified line number refers to the line immediately following the directive itself, matching the behaviour of the ISO C preprocessor.
Malformed directives generate warnings and are ignored.

Examples

  1. Reset the logical line number:
    #line 100
    print *,
  2. Change both line number and filename:
    #line 42
    integer :: x
    ! Diagnostics now refer to generated.f90:42
  3. Improve diagnostics in generated code:
    #line 215
    call generated_procedure()

Methods

◆ handle_line()

subroutine, public handle_line ( type(context), intent(inout) ctx,
character(*), intent(in) token )

Process a #line directive.

Parses the directive arguments, validates the requested logical line number, and updates the active context object.

Supported forms are:

#line <number>
#line <number>

The specified line number becomes the number associated with the source line immediately following the directive.

If a filename is supplied, subsequent diagnostics and predefined file-related macros use the new filename.

Invalid directives produce warnings and leave the current context unchanged.

Parameters
[in,out]ctxCurrent source context. Its logical line number and optional filename are updated in place.
[in]tokenDirective keyword used to identify the #line directive, typically "line".

Definition at line 104 of file line.f90.