The code tokens of src as one string that same_code compares when use statements may
be reordered: for each token its kind (operators all alike), whether it is glued to the
previous token, and its text as key gives it. The signatures of use statements in a run
are sorted, and so are those of the items of each only: list.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | src |
source text |
function code_signature(src) result(sig) !! The code tokens of `src` as one string that [[same_code]] compares when `use` statements may !! be reordered: for each token its kind (operators all alike), whether it is glued to the !! previous token, and its text as `key` gives it. The signatures of `use` statements in a run !! are sorted, and so are those of the items of each `only:` list. character(len=*), intent(in) :: src !! source text character(len=:), allocatable :: sig type(source_t) :: source type(statement_view_t) :: v type(string_builder_t) :: buf type(string_t), allocatable :: run(:) type(string_t) :: item integer :: k, j call parse_source(src, source) allocate (run(0)) associate (tokens => source%tokens, stmts => source%stmts) do k = 1, stmts%n if (stmts%s(k)%kind == SK_PREPROC) call flush_run() if (stmts%s(k)%kind /= SK_CODE) cycle call set_view(src, tokens, stmts%s(k), v) if (is_use_statement(v)) then ! through a variable: a gfortran 15 internal compiler error otherwise item%s = use_signature(stmts%s(k), v) run = [run, item] else call flush_run() do j = stmts%s(k)%first, stmts%s(k)%last if (is_code_kind(tokens%tok(j)%kind)) call buf%append(key(tokens%tok(j))) end do end if end do end associate call flush_run() sig = buf%get() contains subroutine flush_run() !! Append the signatures of the pending run of `use` statements, sorted, and empty the run. integer, allocatable :: order(:) integer :: p order = sorted_order(run) do p = 1, size(order) call buf%append(run(order(p))%s) end do deallocate (run) allocate (run(0)) end subroutine flush_run function use_signature(stmt, v) result(s) !! Signature of a `use` statement, with the items of its `only:` list sorted. Any `;` that !! ends it comes last. type(statement_t), intent(in) :: stmt !! the `use` statement type(statement_view_t), intent(in) :: v !! its view character(len=:), allocatable :: s integer, allocatable :: starts(:), ends(:), order(:) type(string_t), allocatable :: items(:) integer :: p, q, first starts = only_items(v) first = v%n + 1 if (size(starts) > 0) first = starts(1) s = '' do p = 1, first - 1 s = s//key(source%tokens%tok(v%code(p))) end do if (size(starts) > 0) then ends = [starts(2:) - 2, v%n] allocate (items(size(starts))) do p = 1, size(starts) items(p)%s = '' do q = starts(p), ends(p) items(p)%s = items(p)%s//key(source%tokens%tok(v%code(q))) end do end do order = sorted_order(items) do p = 1, size(order) s = s//items(order(p))%s if (p < size(order)) s = s//key(source%tokens%tok(v%code(starts(2) - 1))) ! the comma end do end if do p = stmt%first, stmt%last if (source%tokens%tok(p)%kind == TK_OP) then if (token_text(src, source%tokens%tok(p)) == ';') s = s//key(source%tokens%tok(p)) end if end do end function use_signature function key(t) result(k) !! What must match: the kind, the glue, and the text, ignoring case for names and operators, !! with relational operators in their symbol spelling. The length is included, since `==` !! pads with blanks. type(token_t), intent(in) :: t !! a code token of `src` character(len=:), allocatable :: k character :: kind, glue select case (t%kind) case (TK_DOTOP, TK_OP) k = canonical_relational(token_lower(src, t)) case default k = token_text(src, t) associate (n => folded_length(k, t%kind)) k(1:n) = lowercase(k(1:n)) end associate end select kind = achar(64 + t%kind) if (t%kind == TK_DOTOP) kind = achar(64 + TK_OP) glue = merge('g', ' ', t%glued) k = kind//glue//to_s(len(k))//':'//k end function key end function code_signature