mirror of
https://github.com/github/codeql.git
synced 2026-07-26 21:44:02 +02:00
Under the K2/FIR frontend the lighter-AST comment extractor finds a KDoc's owner by walking the IR and, for each element that carries FIR metadata, checking whether that metadata's source node has a KDOC child. Enum entries (`IrEnumEntry`) and anonymous initializers (`IrAnonymousInitializer`) carry no FIR metadata, so the search could never reach them: their KDoc comments were recorded with no owner, whereas the PSI frontend attributes them (an enum entry to itself, an init block to its enclosing class). These IR declarations do retain valid source offsets, however. This adds a supplementary pass that walks the file's lighter AST, finds the `ENUM_ENTRY` and `CLASS_INITIALIZER` nodes that carry a KDOC child, and matches each to the metadata-less IR declaration whose source offset lies within the node's range. Matching is fail-closed: a node is only attributed when exactly one candidate declaration falls inside it, so ambiguous or unexpected shapes leave the comment ownerless rather than guessing. Offset containment is used rather than per-kind heuristics (matching enum entries by name, initializers by order) because it is uniform across both node kinds and does not depend on names being unique or declaration order being stable. Only the K2 expectations change: the `test-kotlin2` `comments` enum-entry and init-block KDoc owners now match `test-kotlin1` exactly. The one remaining owner difference in that test is a KDoc on an anonymous function passed as a call argument, which the PSI frontend attributes to the enclosing property via its owner walk-up and the FIR frontend does not; that discrepancy is left as-is (see the updated TODO). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>