Skip to content

Pascal/Delphi: base classes are resolved by filename convention only (TFoo -> Foo.pas), so inheritance across differently-named units fragments into per-file stubs — and takes cross-file calls with it #5280

Closed

Description

@patrykseksymfonia

Summary

_pascal_resolve_class resolves a base class name to its defining file by naming convention only:

# graphify/extractors/resolution.py
# Pascal convention: TFooBar is defined in FooBar.pas
prefix = class_name[:1]
unit_name = class_name[1:] if prefix in ("T", "I") else class_name

TXForm is looked for in XForm.pas. When the class lives in a unit with any other name — which is the norm in real Delphi, not the exception — resolution returns None and the extractor creates a private stub node in every inheriting file. An inheritance hierarchy that is one tree in the compiler arrives in the graph as hundreds of disconnected two-node fragments.

The uses clause, which is what actually determines where a name comes from, is not consulted.

Reproduction

Two files, no build system needed:

{ XFormP.pas }
unit XFormP;
interface
type
  TXForm = class(TForm)
    procedure Refresh;
  end;
implementation
procedure TXForm.Refresh;
begin
end;
end.
{ FillAkord.pas }
unit FillAkord;
interface
uses XFormP;
type
  TfrmFillAkord = class(TXForm)
    procedure Load;
  end;
implementation
procedure TfrmFillAkord.Load;
begin
  Refresh;
end;
end.

graphify extract . --code-only --no-cluster on 0.9.111 gives:

TfrmFillAkord (FillAkord.pas) --inherits--> TXForm (FillAkord.pas)   <- stub
TXForm        (XFormP.pas)    --inherits--> TForm  (XFormP.pas)
nodes labelled TXForm: FillAkord.pas:L8, XFormP.pas:L6
calls: []

Two TXForm nodes, and the call to the inherited Refresh produces no edge at all.

Why it compounds

resolve_pascal_inherited_calls walks the inherits chain to resolve a call to a method inherited from another file. A chain severed at its first link resolves almost nothing — so this one gap takes the cross-file call graph with it. That is the empty calls: [] above.

Measured on a real corpus

Delphi application, 895 units / 671 forms / 1.5M lines, scanned as one graph (12 195 inherits edges):

before after a fix
inherits edges pointing at a stub 9 121 7 593
…of those, with the declaration present in the corpus 1 528 4
cross-file calls edges 1 601 2 363 (+48%)
"what inherits from TXForm?" 1 class 415 classes

The 7 593 that remain are genuinely external (IDispatch, IXMLNode/TXMLNode, RTL and generated type libraries) and correctly keep their stubs. The residual 4 are VCL names (TADOQuery, TMenuItem, …) that also happen to be declared in-corpus, where the ambiguity guard rightly refuses to guess.

Shape of the fix

A post-extraction resolver in the registry, running before pascal_inherited_calls so that pass walks a repaired chain. A declared type is told apart from a stub structurally — the extractor emits contains from the enclosing module to everything it declares, and nothing contains a stub. The retarget requires a unique declaration, preferring one in a unit the inheriting file imports (imports edges), and leaves the stub alone when the name is ambiguous or absent.

One constraint worth recording: the pass must not change the length of all_nodes / all_edges. On an incremental rebuild the framework hands resolvers scratch lists and keeps only what was appended (_rl_edges[_e0:]), so a resolver that dedupes or removes in place shifts that slice and copies unrelated edges into the graph. Retargeting the edge dicts is safe — the scratch list holds the same objects — and duplicates are best avoided up front rather than cleaned up afterwards.

I have this working and would be glad to open a PR.

Activity

  1. github-actions commented on Nov 17, 2026

    @github-actions

    Thanks for opening this issue, @patrykseksymfonia. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. added a commit that references this issue on Nov 17, 2026
    4a62bb0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions