Classify every statement and compute its depth: the number of constructs enclosing it, and
how many of those are program units and procedures, so callers can choose not to indent
their bodies.
An opening statement is at the depth outside its construct, the statements inside are one
deeper, and middle (else, contains, ...) and closing statements are at the opening depth.
Non-code items get the depth of the code around them.
Branches of preprocessor conditionals (cpp #if/#ifdef/#elif/#else/#endif and
fypp #:if/#:elif/#:else/#:endif) are treated as alternatives: each branch starts
from the structure at the #if, and the structure at the end of the first branch carries
on after the #endif. That keeps code such as #ifdef X / do / #endif ... #ifdef X /
end do / #endif balanced.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(source_t), | intent(in) | :: | source |
the source |
||
| type(statement_view_t), | intent(in) | :: | views(:) |
the view of each item (see statement_views) |
||
| type(structure_t), | intent(out) | :: | st |
the structure |
||
| type(diagnostic_t), | intent(out), | allocatable | :: | diags(:) |
structure problems found, in the order found |
subroutine analyze_structure(source, views, st, diags) !! Classify every statement and compute its depth: the number of constructs enclosing it, and !! how many of those are program units and procedures, so callers can choose not to indent !! their bodies. !! An opening statement is at the depth outside its construct, the statements inside are one !! deeper, and middle (`else`, `contains`, ...) and closing statements are at the opening depth. !! Non-code items get the depth of the code around them. !! !! Branches of preprocessor conditionals (cpp `#if`/`#ifdef`/`#elif`/`#else`/`#endif` and !! fypp `#:if`/`#:elif`/`#:else`/`#:endif`) are treated as alternatives: each branch starts !! from the structure at the `#if`, and the structure at the end of the first branch carries !! on after the `#endif`. That keeps code such as `#ifdef X` / `do` / `#endif` ... `#ifdef X` / !! `end do` / `#endif` balanced. type(source_t), intent(in) :: source !! the source type(statement_view_t), intent(in) :: views(:) !! the view of each item (see [[statement_views]]) type(structure_t), intent(out) :: st !! the structure type(diagnostic_t), allocatable, intent(out) :: diags(:) !! structure problems found, in the order found type(scope_stack_t) :: stack type(saved_stack_t), allocatable :: saved(:) type(stmt_info_t), allocatable :: info(:) integer, allocatable :: depth(:), nunits(:), nprocs(:) integer :: k, line, nsaved logical :: in_interface associate (n => source%stmts%n) allocate (info(n), depth(n), nunits(n), nprocs(n), diags(0), saved(0)) end associate nsaved = 0 do k = 1, source%stmts%n depth(k) = stack%top if (source%stmts%s(k)%kind == SK_PREPROC) call preprocessor(k) if (source%stmts%s(k)%kind /= SK_CODE) then call count_units(k) cycle end if line = source%tokens%tok(source%stmts%s(k)%first)%line in_interface = .false. if (stack%top > 0) in_interface = stack%s(stack%top)%construct == C_INTERFACE info(k) = classify_statement(source%text, source%tokens, views(k), in_interface) select case (info(k)%role) case (ROLE_OPEN) call stack%push(scope_t(info(k)%construct, info(k)%do_label, line)) case (ROLE_MIDDLE) if (stack%top == 0 .and. info(k)%construct == C_CONTAINS) then ! a main program without a `program` statement call stack%push(scope_t(C_PROGRAM, 0, line)) else if (stack%top == 0) then call diagnose(line, construct_label(info(k)%construct)//' statement outside any construct') else if (.not. middle_fits(info(k)%construct, stack%s(stack%top)%construct)) then call diagnose(line, construct_label(info(k)%construct)//' statement inside '// & trim(opened(stack%s(stack%top)))) else depth(k) = stack%top - 1 end if case (ROLE_CLOSE) call close_scope(info(k)%construct, line) depth(k) = stack%top end select call count_units(k) ! the labeled statement that ends `do 10 ...` loops is part of the innermost loop body if (info(k)%label > 0) then do while (stack%top > 0) if (stack%s(stack%top)%construct /= C_DO .or. stack%s(stack%top)%do_label /= info(k)%label) exit stack%top = stack%top - 1 end do end if end do do k = stack%top, 1, -1 call diagnose(stack%s(k)%line, construct_label(stack%s(k)%construct)//' is not closed') end do call move_alloc(info, st%info) call move_alloc(depth, st%depth) call move_alloc(nunits, st%units) call move_alloc(nprocs, st%procedures) st%fypp = fypp_depths(source) contains subroutine count_units(k) !! Record how many of the scopes enclosing item `k` are program units, and procedures. integer, intent(in) :: k !! index into `stmts` integer :: j nunits(k) = 0 nprocs(k) = 0 do j = 1, depth(k) select case (stack%s(j)%construct) case (C_MODULE, C_SUBMODULE, C_PROGRAM) nunits(k) = nunits(k) + 1 case (C_SUBROUTINE, C_FUNCTION, C_PROCEDURE) ! interface bodies are declarations, not procedure definitions if (j == 1) then nprocs(k) = nprocs(k) + 1 else if (stack%s(j - 1)%construct /= C_INTERFACE) then nprocs(k) = nprocs(k) + 1 end if end select end do end subroutine count_units subroutine preprocessor(k) !! Save or restore the scope stack at the preprocessor conditionals in item `k`, and set !! the depth of the item to that of the restored stack. integer, intent(in) :: k !! index into `stmts` of a preprocessor item integer :: j do j = source%stmts%s(k)%first, source%stmts%s(k)%last if (source%tokens%tok(j)%kind /= TK_PREPROC) cycle select case (conditional_kind(token_lower(source%text, source%tokens%tok(j)))) case (PP_IF) nsaved = nsaved + 1 if (nsaved > size(saved)) saved = [saved, saved_stack_t()] saved(nsaved)%at_if = stack%snapshot() if (allocated(saved(nsaved)%after_first)) deallocate (saved(nsaved)%after_first) case (PP_ELSE) if (nsaved > 0) then if (.not. allocated(saved(nsaved)%after_first)) saved(nsaved)%after_first = stack%snapshot() call stack%restore(saved(nsaved)%at_if) depth(k) = stack%top end if case (PP_ENDIF) if (nsaved > 0) then if (allocated(saved(nsaved)%after_first)) call stack%restore(saved(nsaved)%after_first) depth(k) = stack%top nsaved = nsaved - 1 end if end select end do end subroutine preprocessor subroutine close_scope(construct, line) !! Pop the construct closed by an `end` statement, with a diagnostic if it does not match !! the innermost open construct. If an outer one matches, everything down to it is closed. integer, intent(in) :: construct !! construct named by the `end` (`C_ANY` for a bare `end`) integer, intent(in) :: line !! source line of the `end` statement integer :: j if (stack%top == 0) then ! `end` or `end program` may close a main program without a `program` statement if (construct /= C_ANY .and. construct /= C_PROGRAM) & call diagnose(line, 'end '//construct_label(construct)//' without an open construct') return end if if (closes(construct, stack%s(stack%top)%construct)) then stack%top = stack%top - 1 return end if ! recover from a missing `end`: close down to a matching construct if there is one do j = stack%top - 1, 1, -1 if (closes(construct, stack%s(j)%construct)) then call diagnose(line, 'end '//construct_label(construct)//' closes '// & trim(opened(stack%s(stack%top)))) stack%top = j - 1 return end if end do call diagnose(line, 'end '//construct_label(construct)//' does not match '// & trim(opened(stack%s(stack%top)))) end subroutine close_scope function opened(sc) result(text) !! `sc` for messages: `do (opened on line 12)`. type(scope_t), intent(in) :: sc !! an open construct character(len=64) :: text text = construct_label(sc%construct)//' (opened on line '//to_s(sc%line)//')' end function opened subroutine diagnose(line, message) !! Add a diagnostic to `diags`. integer, intent(in) :: line !! source line character(len=*), intent(in) :: message !! text of the warning diags = [diags, diagnostic_t(line, message)] end subroutine diagnose end subroutine analyze_structure