Skip to content

Support parsing of compile_commands.json#504

Open
zandivx wants to merge 8 commits into
fortran-lang:masterfrom
zandivx:master
Open

Support parsing of compile_commands.json#504
zandivx wants to merge 8 commits into
fortran-lang:masterfrom
zandivx:master

Conversation

@zandivx
Copy link
Copy Markdown
Contributor

@zandivx zandivx commented Jan 15, 2026

Add support for parsing compile_commands.json to enable module disambiguation when multiple modules share the same name across different compilation units. This is a feature I wanted to have, prompted Claude Code to implement it and tested it to have the expected behavior.

Changes include:

  • a new parser for compile_commands.json files, which extracts include directories (-I), module directories (-J/-module) and preprocessor definitions (-D)
  • Module disambiguation: Uses -J flag mapping to group files by compilation unit, enabling correct "go to definition" when the same module name exists in different targets
  • Configuration options:
    • compile_commands: Custom path to compile_commands.json
    • disable_compile_commands: Opt-out flag for projects that don't want this feature
  • Auto-discovery: Searches ./, build/, builddir/ and recursively for compile_commands.json file

Internal changes

  • obj_tree now stores multiple entries per key (as ObjTreeEntry namedtuples) to support duplicate module names
  • Build directories are auto-excluded to avoid indexing preprocessed intermediate files

Closes #512.

zandivx and others added 7 commits January 14, 2026 00:28
Add parser for Clang Compilation Database (compile_commands.json) to
extract include directories, preprocessor definitions, and source
files from build systems like CMake and fpm.

The parser extracts -I (include dirs), -J/-module (module dirs),
and -D (preprocessor definitions) from compiler arguments. It also
builds a file-to-module-dir mapping from -J flags, which is used
for disambiguating modules with the same name in different
compilation units.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Extract file-to-module-dir mapping from -J flags to disambiguate
modules with the same name across different compilation targets.

Changes:
- Modify obj_tree to support multiple objects with the same key
- Add obj_tree_getter callback for context-aware module lookup
- Load compile_commands.json during workspace init

Parser enhancements for real-world fpm/CMake projects:
- Recursive search fallback for non-standard locations
- Path normalization with basename lookup when build paths differ
- Filter intermediate -pp.f90 preprocessor files
- Track module output dirs per file for target grouping

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@zandivx zandivx requested a review from gnikit as a code owner January 15, 2026 23:41
@zandivx
Copy link
Copy Markdown
Contributor Author

zandivx commented Jan 16, 2026

@ZedThree maybe you want to take a look and test my changes locally?
In the course of this I encountered several issues with pytest and the GitHub workflows, so I opened #505, #506 and #507 to fix these.

@zandivx
Copy link
Copy Markdown
Contributor Author

zandivx commented Jan 17, 2026

@certik maybe you got time to look at this as well?

Copy link
Copy Markdown
Member

@gnikit gnikit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a substantially more invasive change changing how the language servers parser works.

I will have a look properly but we will have to isolate the impact.

@zandivx
Copy link
Copy Markdown
Contributor Author

zandivx commented Jan 17, 2026

This is a substantially more invasive change changing how the language servers parser works.

I will have a look properly but we will have to isolate the impact.

I am happy to split the PR in smaller chunks or make changes according to your feedback

@gnikit
Copy link
Copy Markdown
Member

gnikit commented Jan 17, 2026

Awesome thanks. I'll have a look tomorrow during my flight

@certik
Copy link
Copy Markdown
Member

certik commented Jan 18, 2026

@zandivx make sure the CI passes first, it's currently failing.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 18, 2026

Codecov Report

❌ Patch coverage is 82.39437% with 50 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.82%. Comparing base (17f34e9) to head (b5e47ce).

Files with missing lines Patch % Lines
fortls/langserver.py 70.09% 32 Missing ⚠️
fortls/compile_commands.py 90.25% 15 Missing ⚠️
fortls/parsers/internal/utilities.py 85.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #504      +/-   ##
==========================================
- Coverage   88.22%   87.82%   -0.41%     
==========================================
  Files          35       36       +1     
  Lines        4800     5051     +251     
==========================================
+ Hits         4235     4436     +201     
- Misses        565      615      +50     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zandivx
Copy link
Copy Markdown
Contributor Author

zandivx commented Jan 18, 2026

@zandivx make sure the CI passes first, it's currently failing.

Of course! One thing I need for that is #508 to support optional types in the schema file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Support parsing of compile_commands.json

3 participants