Repository navigation
Description
Summary
_cpp_preprocess runs cpp with -P, which explicitly suppresses
linemarkers. Tree-sitter then parses the preprocessed text and
node.start_point[0] + 1 is recorded verbatim as source_location. Since
cpp -P deletes directive lines, inactive #ifdef blocks, and collapses runs
of blank lines, the line numbers it produces do not correspond to the original
file — and there is nothing left in the output to map them back.
Every symbol in a capital-F Fortran file is therefore reported at the wrong
line, including in files that contain no preprocessor directives at all.
Why it matters
source_location is what makes a graph node actionable — it is what explain,
affected, and the MCP read surface hand to an agent or a human to jump to.
A confidently-stated wrong line is worse than a missing one: it sends the reader
to unrelated code with no signal that anything is off.
Lowercase .f90 (which is not preprocessed) reports correct lines, so the same
project can have correct and incorrect locations side by side depending only on
file extension.
Reproduction
solver.F90:
! Comment header
#define USE_DOUBLE 1
#ifdef UNUSED_FLAG
subroutine dead_code()
end subroutine
#endif
module solver
implicit none
contains
subroutine compute_volume(side, vol)
real :: side, vol
vol = side**3
end subroutine
end moduleReal positions: module solver is on L9, compute_volume on L12.
from pathlib import Path
from graphify.extractors.fortran import extract_fortran
for n in extract_fortran(Path("solver.F90"))["nodes"]:
print("%-24s %s" % (n["label"], n.get("source_location")))Actual:
solver.F90 L1
solver L2 <-- actual L9 (off by 7)
compute_volume() L5 <-- actual L12 (off by 7)
The cause is visible directly:
$ cpp -w -P -nostdinc -I /dev/null solver.F90 | cat -n
1 ! Comment header
2 module solver
3 implicit none
4 contains
5 subroutine compute_volume(side, vol)
...The no-directive case is affected too, because -P also collapses blank
lines. A .F90 consisting of five blank lines followed by
subroutine zebrafunc() on L6:
$ cpp -w -P -nostdinc -I /dev/null z.F90 | cat -n
1 subroutine zebrafunc()reports L1.
Root cause
graphify/extractors/fortran.py:34:
["cpp", "-w", "-P", "-nostdinc", "-I", "/dev/null", str(path.resolve())],applied at fortran.py:59:
source = _cpp_preprocess(path) if path.suffix in _FORTRAN_CPP_EXTS else path.read_bytes()-P is documented as "Inhibit generation of linemarkers in the output from the
preprocessor" — i.e. it discards precisely the information needed to map back.
Suggested fix
Drop -P and consume the linemarkers:
- ["cpp", "-w", "-P", "-nostdinc", "-I", "/dev/null", str(path.resolve())],
+ ["cpp", "-w", "-nostdinc", "-I", "/dev/null", str(path.resolve())],Have _cpp_preprocess return (source, line_map): parse the
# <lineno> "<file>" markers to build a preprocessed-line → original-line map,
strip the marker lines from what is handed to tree-sitter, and translate line
through the map in add_node / add_edge.
A cheaper interim fix that only addresses the common case: keep -P, but
preprocess only when the file actually contains a directive, and use the raw
bytes otherwise. That makes directive-free .F90 files correct while leaving
files with directives wrong, so it is a mitigation rather than a fix.
Test coverage
tests/test_languages.py:1689
(test_fortran_capital_F_parses_preprocessed) asserts only on labels, and
nothing in tests/ asserts a Fortran source_location — so the current
behaviour is not pinned and a location assertion can be added directly.
Environment
- graphify
v8@6bbc9d1(pyprojectversion = "0.9.22") - Python 3.12, Linux, GNU cpp
Triaged by : @SinghAman21 and @claude
Activity
fix(fortran): map source_location back through cpp linemarkers (Graph…
test(fortran): verify cpp stdin line mapping