Skip to content

Studio map: the C/C++ extractor, c-includes #806

Done
4 of 4 issues completed
Done

Description

@danielrispler

This was generated by AI during triage.

Problem Statement

A C or C++ repository gets no proven import links from the system map. No extractor reads .c, .h, .cc, .cpp or .hpp files, so on such a repository every part is unread and every link between parts passes as unproven: a missing or backwards link is never caught. First map's free scan names the language on its language line as having no reader (system-map/src/scan.rs:359-395 at fd3c0b2, unread_languages), and draws no lines between its grey plates.

C makes four things hard for a reader that runs no compiler (research note docs/studio/research/c-cpp-extractor.md at fd3c0b2):

  • An include's folder comes from the build. #include "x.h" is searched next to the including file first, then in the -I folders, then in the system folders; <x.h> skips the including file's folder (GCC CPP manual, "Search Path"). The -I folders live in compiler flags. compile_commands.json records them but is a build output, and none of the four trial repositories tracks one (note, "compile_commands.json").
  • Repositories write include paths from different roots. curl's quoted includes resolve next to the file 65.8% of the time; leveldb's never do, since it writes them from the repository root ("db/dbformat.h"); bitcoin writes its own headers bare (<hash.h>) through -I src (note, "Comparison" and round 2, part A).
  • System headers share names with tracked files. bitcoin's src/util/string.h would catch every <string.h>; jemalloc's MSVC shim msvc_compat/C99/stdint.h would catch every <stdint.h> in redis (round 2, part B).
  • Vendored copies and duplicate names. redis means hiredis's sds.h where src/sds.h also exists (src/redis-cli.c:37); bitcoin has chain.h in three folders (round 2, parts C and D).

Solution

A new extractor, c-includes, reads every tracked C and C++ source and header file (the thirteen extensions the research scanned) as text and resolves each #include to a tracked file. It never reads build files or compile_commands.json and runs no compiler (ADR 0002; grilling 2026-11-28, Q1).

Each include goes through these steps, in this order:

  1. A quoted include that names a tracked file next to the including file is that file, as the compiler's first search step does.
  2. An angle-bracket include of a standard C, C++ or POSIX header name, or of a common compiler, libc, kernel or Windows header, is a system header: no line.
  3. Otherwise its path is matched against the end of every tracked file's path. One match is a line; none is a system or generated header, with no line.
  4. Several matches are settled only by evidence: when none is in an ignored path, and exactly one candidate sits in a folder that at least 5 unique matches elsewhere already resolve through, that candidate is the line. Otherwise the including file is Unsure, with every match as a candidate. An ignored copy always keeps an include Unsure (ADR 0004).

Test files are skipped and are never candidates. On the four trial repositories this gives, graded against each build's own -I folders: curl 2541 right lines and 2 wrong, leveldb 354 and 0, bitcoin 4961 and 0 with 128 Unsure, redis 592 and 0 with 3 Unsure; on a proxy map of bitcoin no part-to-part link is lost. These count include directives, not file pairs (note, round 4, "New base case" and item 1; the follow-up round that settled the tie-break, quoted under Further Notes).

The research note is docs/studio/research/c-cpp-extractor.md (PR #802, fd3c0b2), its decision record ADR 0004. The decisions below are numbered Q1-Q13 as in the grilling of 2026-11-28; each is stated in this spec where it applies, so the spec stands without the grilling's log, which stays on Daniel's machine (local-evidence/2026-11-28-c-cpp-extractor-grilling/).

User Stories

  1. As a developer with a C repository, I want the map to read my #include lines, so that the links between my parts are proven or fail.
  2. As a developer with a C++ repository, I want .cc, .cpp, .cxx, .c++, .hpp, .hh, .hxx, .h++, .inl, .ipp and .tcc files read too, in any case, so that one reader covers both languages.
  3. As a developer whose headers are shared by C and C++ code, I want each header read once, so that a pair isn't counted twice.
  4. As a developer, I want a quoted include that names a file beside the including file to resolve to that file, so that the most common C layout gives exact lines.
  5. As a developer, I want "../x.h" resolved against the including file's folder, so that relative climbs give the file the compiler finds.
  6. As a developer whose code writes includes from the repository root ("db/dbformat.h"), I want them resolved without configuring anything, so that leveldb-style layouts get lines.
  7. As a developer whose code writes includes from an include folder (<curl/curl.h> under include/), I want them resolved, so that a public header folder gets lines.
  8. As a developer whose code writes its own headers bare (<hash.h> through -I src), I want them resolved, so that bitcoin-style layouts get lines.
  9. As a developer, I want <string.h>, <stdint.h> and the other standard, POSIX and Windows headers never matched to my tracked files, so that a tracked util/string.h doesn't draw a line from every file that includes the C library's.
  10. As a developer, I want a quoted include named like a standard header still resolved normally, so that the list never hides my own "config.h".
  11. As a developer, I want an include that names no tracked file to give no line and no problem, so that system, third-party and generated headers are quietly left out.
  12. As a developer, I want an include that matches two of my files to make my file Unsure rather than guess, so that the map never draws a line the compiler would not.
  13. As a developer, I want a tie settled when my repository's other includes clearly show which folder is the include folder, so that bitcoin's <chain.h> resolves to src/chain.h and not to src/kernel/chain.h.
  14. As a developer, I want a tie left Unsure when two folders both have that evidence, so that two subprojects in a monorepo are never confused.
  15. As a map author who ticks a vendored folder as ignored, I want an include that also matches a file there to stay Unsure, so that ignoring hiredis never turns <sds.h> into a wrong line to src/sds.h.
  16. As a map author, I want an include that matches only an ignored file to give no line, so that vendored code stays off the map.
  17. As a map author, I want includes inside ignored files never read, so that vendored code makes no lines of its own.
  18. As a developer, I want test files (under test/, tests/, testing/, unittest/ or fuzz/, or named *_test.*, *_tests.* or test_*.*) skipped, so that test helpers neither make lines nor make production includes Unsure.
  19. As a developer, I want a skipped test file never to make its part read, so that a part made only of tests passes its links as unproven, as with the other readers.
  20. As a developer, I want an #include inside a comment or a string ignored, so that commented-out code makes no lines.
  21. As a developer, I want includes inside every #if/#ifdef branch read, so that platform- and feature-specific dependencies show on the map.
  22. As a developer, I want #include MACRO to give no line and no problem, so that a computed include is left out rather than guessed.
  23. As a developer, I want each line of a pair to be the #include's line number, so that the imports report and PR briefs point at the right line.
  24. As a developer, I want system-map check to suggest adding c-includes when my repository has a production C or C++ file, so that I learn the reader exists.
  25. As a developer whose repository has only test or ignored C files, I want no such suggestion, so that a stray fixture doesn't ask for a reader.
  26. As a developer, I want the Unsure notes for C files grouped by part like the other readers', so that bitcoin's remaining Unsures print a handful of lines, not hundreds.
  27. As a developer, I want --json to list the C files that are Unsure with their candidates, so that I can see what the reader could not settle.
  28. As a First map user with a C or C++ repository, I want the free scan to say "C and C++: links computed", so that the scan doesn't read as having no dependencies.
  29. As a First map user, I want the scan's grey plates joined by the lines c-includes computes, so that I can judge merges and splits by what depends on what.
  30. As a First map user, I want every extension the reader reads counted on the language line, .cxx, .hh, .inl and the rest as C++, so that the line covers every file the reader reads.
  31. As a developer, I want commit mode (--commit) to give the same pairs as the working tree, so that PR briefs and CI agree with my checkout.
  32. As a developer of a large repository, I want the reader to run in well under a second on a repository of bitcoin's size, so that the free scan stays instant.
  33. As a reader of map.md, I want c-includes' section to list what it under-counts and over-counts, so that I know what a missing or extra line means.
  34. As a maintainer, I want the reader to differ from java-imports on ignored only where ADR 0004 says, so that the difference is on purpose and recorded.
  35. As a maintainer, I want c-includes registered under the extractor contract, so that the shared contract cases hold it to the same rules as the nine other readers.

Implementation Decisions

The module. One new extractor module, c-includes, behind the Extractor trait and registered in the registry beside the nine others (system-map/src/extract/registry.rs:9-19 at fd3c0b2). It needs no change to the core, to Repo, to Unsure or to the map format: Unsure with candidates, skips, the core's dropping of unowned candidates (system-map/src/edges.rs:147-163) and of findings in ignored files already exist. It sets no group or from_group on its pairs, since each include names one file. It has no config file, so it reports no problems and no notes. (Q6: one reader for C and C++.)

What it reads. A tracked file whose extension, in any case, is one of the thirteen the research scanned: .c .h .cc .cpp .cxx .c++ .hpp .hh .hxx .h++ .inl .ipp .tcc (note, "Results: curl", the scanned-extensions line). Matching them keeps the trials comparable with the research's figures. .m/.mm (Objective-C), .cu, .inc, .def and .rc are not read. In commit mode it reads the commit's texts through Repo, as the other readers do.

Finding includes. A small lexer walks each file once:

  • // and /* */ comments, string literals, character literals and C++ raw strings (R"delim(…)delim") are skipped. An unclosed block comment hides the rest of the file.
  • A backslash at a line's end joins it to the next line.
  • A directive is a line whose first non-space character is #, then optional spaces or tabs, then include, then "path" or <path>. Its line number is the #'s line.
  • #include followed by anything else (a macro name) gives nothing. #include_next, #import and __has_include give nothing.
  • Conditional blocks are not evaluated: includes in every branch are read, #if 0 included.

Paths. A path is used as written, case-sensitively. An empty path, an absolute one (starting with /), or one holding a backslash gives nothing. . segments are removed. A path with a .. segment is resolved only next to the including file (step 1); the path-ending match never sees it.

Resolution, per include (grilling Q1, Q5, Q8, Q11; ADR 0004). Let skipped be c-includes' own skips, and ignored the map's ignored.

  1. Next to the file. For a quoted include, the including file's folder joined with the path, cleaned. When that file is tracked, it is the answer: a pair when it is neither ignored nor skipped, nothing otherwise. Resolution stops there either way, as the compiler stops.
  2. System names. For an angle-bracket include whose path is on the system-header list, nothing. The list never applies to quoted includes.
  3. Path-ending match. The candidates are the tracked files whose path equals the include's path or ends with / followed by it, less skipped files. Ignored files stay candidates.
    • No candidate: nothing.
    • Every candidate ignored: nothing.
    • Some but not all ignored: the including file is Unsure, reason an ignored copy matches it, every candidate listed.
    • Exactly one, not ignored: a pair.
    • Two or more, none ignored: step 4.
  4. The folder tie-break. A candidate's implied folder is its path with / and the include's path cut off its end (. when the two are equal). A folder's evidence is the number of include directives, across all read files that are neither skipped nor ignored, that reached step 3 and had exactly one candidate (ignored or not) whose implied folder it is. When exactly one candidate's folder has evidence of 5 or more, that candidate is the pair. Otherwise the including file is Unsure, reason several files match it, every candidate listed. Evidence is counted over the whole repository first, then ties are settled, so the result doesn't depend on file order.

Includes from an ignored or skipped file are never resolved. The research measured N = 5 against N = 1 and N = 20 (round 4, item 2): at N = 1 curl's two wrong lines would each count as evidence.

The system-header list. A checked-in constant, like python-imports' standard-library list. It holds exactly the names the research measured with (note, "Round 2 method", STD, PLATFORM and SYS_PREFIX; "Round 4 method", WIN), so the trials' zero wrong lines on bitcoin and redis carry over:

  • the C standard headers (cppreference, "C Standard Library header files") and the C++ standard headers, including the c-prefixed compatibility headers (cppreference, "C++ Standard Library headers");
  • the POSIX headers (POSIX.1-2024, "Headers" index);
  • any path starting with sys/, linux/, mach/, machine/, bits/, netinet/, arpa/ or net/;
  • common platform headers outside those standards: byteswap.h, malloc.h, alloca.h, execinfo.h, features.h (glibc), cpuid.h, intrin.h, immintrin.h, x86intrin.h, emmintrin.h, arm_neon.h (compiler intrinsics);
  • common Windows CRT and SDK headers: windows.h, winsock2.h, ws2tcpip.h, process.h, io.h, direct.h, conio.h, tchar.h, schannel.h, plus memory.h.

The constant's comment names each group's source: cppreference and the POSIX index for the standards, the glibc manual, GCC's and Arm's intrinsics documentation, and Microsoft Learn. Without cpuid.h, bitcoin's <cpuid.h> would match src/compat/cpuid.h (round 2, part B). (Q8.)

Skipped files (Q9). skips is true for a file with a path folder named, in any case, test, tests, testing, unittest or fuzz, or whose name matches *_test.*, *_tests.* or test_*.*. A skipped file makes no pairs, never makes its part read, and is never a candidate. The patterns are c-includes' own; the scan's is_test (system-map/src/scan.rs:106-117) is unchanged.

Detection. The default rule (system-map/src/extract/mod.rs:46-50): a tracked file it reads that is neither ignored nor skipped makes check suggest c-includes.

Speed. A single suffix index of the tracked files is built once per run. On bitcoin (3,095 tracked files, 21.3 MB of C and C++) the whole reader runs in well under a second; the research's Python prototype took 1.4 s, 88% of it in a character-by-character comment stripper (round 2, part E).

The scan. The free scan's language table (system-map/src/scan.rs:59-89) gains cxx, c++, hh, hxx, h++, inl, ipp and tcc as C++, so the language line covers every file the reader reads; its lookup stays case-sensitive, as for the other languages. With c-includes registered, the scan already counts C and C++ as read languages, since a language is read when any registered extractor reads its files (scan.rs:380-388), and First map's language line says "links computed" for them like the others (Q2). The scan's lines come from the same extractors, so they gain C/C++ lines with no other change.

Docs. docs/studio/behaviour/map.md gets a c-includes section like the other readers' (Q2, Q13):

  • what it reads and the resolution steps above;
  • Unsure, its two reasons, and that there is no tie-break beyond the evidence rule;
  • ignored: an ignored copy keeps an include Unsure, and why this differs from java-imports (ADR 0004);
  • under-counts: includes reached through a build's -I folder that the path-ending match can't settle; macro includes, #include_next, #import; generated headers (curl_config.h, bitcoin-build-config.h); headers outside the repository; includes from skipped test files, and non-test code's includes of test helpers (bitcoin's src/bench/); .. paths that don't resolve next to the file; ties whose folders have fewer than 5 matches of evidence;
  • over-counts: every #if branch, #if 0 included; an angle-bracket include of an outside library's header whose name isn't on the system list matches whenever exactly one tracked file anywhere has that path ending, the common case a wrapper named after the library it wraps (curl's lib/vtls/rustls.h for <rustls.h>); platform-only includes such as curl's VMS curlmsg_vms.h.

The section names the research note, ADR 0004 and the trial set.

The system map. The new module falls under the map-engine part's "Extractors and links" item, whose paths already hold system-map/src/extract/, and no part's text names the readers, so system-map.json needs no change (system-map.json at fd3c0b2, part map-engine).

Made without Daniel (grilling log): Q6 one reader for both languages; Q7 no proximity or shortest-path tie-breaker; Q8 the system list applies to angle-bracket includes only and adds Windows names; Q9 the test patterns, bench not among them; Q10 no "angle includes skip the own folder" rule (it lost bitcoin 1117 right lines, round 3, item 2). Settled while writing this spec: the research's thirteen extensions in any case; #include_next, #import and __has_include give nothing; #if 0 is read like any branch; .. paths resolve only next to the file; adjacent ignored or skipped files stop resolution with no line.

Testing Decisions

A good test drives system-map through its public behaviour on small repositories in temp folders: the check's standard output, standard error and exit code, the imports report, and --json. It tests no private helper. Daniel agreed these seams on 2026-11-28.

  • Behaviour tests (the one seam). A new test file for c-includes in the system-map/tests/ binary, beside tests/dart.rs and tests/php.rs, the prior art (php.rs for the map/pairs helpers and table-shaped cases, dart.rs for Unsure relaxing only the candidates' links). Each case builds a small repository and runs the check or the report, as rows of tables where a behaviour has several cases:
    • Next to the file: a quoted include of a sibling pairs; "../x.h" pairs; a sibling that exists wins over a root-relative match of the same path elsewhere; an adjacent ignored or test file gives nothing and stops resolution.
    • Path-ending match: a root-relative quoted include ("db/x.h" from db/y.cc) pairs; <curl/curl.h> under include/ pairs; a bare <x.h> pairs to its one match; a path naming no tracked file gives nothing and no problem.
    • System names: <string.h>, <sys/time.h> and <windows.h> with a tracked util/string.h, compat/sys/time.h and win/windows.h give nothing; a quoted "string.h" beside the file pairs.
    • Unsure: two tracked x.h with no evidence make the importer Unsure with reason several files match it, relax only the link to the candidates' parts and list unsure in --json; another link from the same part still fails.
    • Tie-break: five unique includes through src/ settle a tie between src/chain.h and src/kernel/chain.h for src/kernel/chain.cc's <chain.h>; four do not; two folders with five each stay Unsure.
    • Ignored (ADR 0004): <sds.h> with src/sds.h and an ignored deps/hiredis/sds.h is Unsure with reason an ignored copy matches it, even when src/ has five matches of evidence; an include matching only an ignored file gives nothing; includes inside an ignored file give nothing.
    • Skipped files: a file under tests/, Test/ and fuzz/, and x_test.cc and test_x.c, make no pairs, are never candidates (a tie between a production header and a tests/ one resolves to the production one) and don't make a part read; testutil.cc and bench/x.cc are read.
    • Lexing: an include in a // comment, a /* */ comment, a string and a raw string gives nothing; # include and # include pair; a backslash-continued directive pairs; #include MACRO, #include_next and #import give nothing; includes in both branches of #if/#else and in #if 0 pair; the pair's line is the directive's.
    • Extensions: each of the thirteen extensions is read, and so is an uppercase .C or .HPP; .m, .mm, .cu and .inc are not.
    • Detection: a note for a repository with a production .c file; none when every C file is a test or ignored.
    • Commit mode: one case through --commit gives the same pairs as the working tree.
  • The contract cases run on c-includes through its fixture kit (Studio: Spec: an extractor contract, so each language is one module #529, "Testing Decisions"): no support files, source src/<name>.c, importing #include "<to's file name>", which resolves next to the file, plain a file with no include, missing_config: None, malformed_config: None, test_file tests/<name>_test.c, and skip the malformed-config case with the reason c-includes has no config file, as java-imports does (system-map/src/extract/java/mod.rs:67-70; the contract requires it, system-map/src/extract/contract.rs:173-185).
  • The scan. a_language_no_extractor_reads_is_detected_and_named in tests/scan.rs is one case today; it becomes a table (AGENTS.md, "Extend before adding"), its current case the first row, and a new row: a repository with .c, .cxx and .hh files reports C and C++ among the read languages, none unread, and c-includes among the detected extractors.
  • No new core tests. The core doesn't change; Unsure, skips and dropped unowned candidates are tested already (system-map/src/extract/tests.rs:140-158).
  • No change for the other languages. The whole system-map/tests/ binary passes, and plotkeeper's own system-map check and system-map imports print the same standard output before and after.
  • The trials, by hand (Q3). system-map imports and check run with c-includes named on curl 23256cf755a9, leveldb 29465588d3df, bitcoin afedb19cca4c and redis 15d19442c4cd, each with a temporary map that ignores exactly the vendored trees the research ignored (note, round 2, "Repositories"): bitcoin's src/crc32c, src/crypto/ctaes, src/ipc/libmultiprocess, src/leveldb, src/minisketch and src/secp256k1, with src/univalue first-party; redis's deps/; none in curl and leveldb. No report prints import lines (imports counts deduplicated file pairs, system-map/src/imports.rs:1-3 at 8e7faab), so each trial uses a map with one part owning every file and counts unique file pairs (total.raw in imports --json), plus Unsure findings and files, and wall time. The research counted include directives, so its round-4 script is rerun on the same clones with a throwaway change that counts unique pairs too, and like is compared with like. It also gives the research's directive figures (note, round 4, "New base case"; the follow-up round under Further Notes): curl 2541 lines with 2 known wrong, leveldb 354, bitcoin 4961 with 128 Unsure, redis 592 with 3 Unsure. A difference beyond self-includes is explained in the PR, not tuned away. Figures are saved under local-evidence/, the durable ones in the PR.
    • The limits: CI never sees the trial repositories. The yardstick is approximate: bitcoin's -I folders were merged into one list, and generated headers count as none.

Out of Scope

Further Notes

  • The research ran four rounds plus a follow-up on four repositories, graded include by include against each build's own -I folders read as text; its scripts are inlined in the note. The follow-up, which settled how the tie-break meets blocking (Q11), ran round 4's script with three variants added and is not in the note, so its figures are quoted here (ADR 0004 records its outcome):

    Variant bitcoin (324 Unsure, 128 with an ignored candidate) redis (3 Unsure)
    Tie-break at N = 5, ignored candidates not special 324 right 3 pick the ignored copy, no line
    An ignored candidate's folder qualifies at 1 match 324 right same
    Any ignored candidate keeps the include Unsure (chosen) 196 right, 128 Unsure 3 Unsure
    Next-to-the-file matches also count as evidence 324 right 3 Unsure

    bitcoin's 4961 is round 4's 4765 right lines plus the chosen variant's 196. Ignored folders with unique matches include redis's deps/linenoise (1) and bitcoin's src/minisketch/include (2), the small vendored folders that ruled out the second variant.

  • Daniel's push-back in the grilling found the hole ADR 0004's third option records: a small ignored vendored folder below the evidence bar would have let the tie-break draw a wrong line.

  • The two wrong lines curl keeps are documented over-counts (Q13); both stay inside one part on any map but the VMS one.

Sub-issues

Sub-issues

4 of 4 Issues completed
Loading 4 sub-issues

Activity

  1. added
    ready-for-agent
    on Nov 28, 2026
  2. danielrispler commented on Nov 28, 2026

    @danielrispler
    OwnerAuthor

    This was generated by AI during triage.

    Tickets: #807 → #808 → #810, with #809 beside #808 (both blocked by #807).

    Route draft: #807 medium·medium → medium·medium — unchanged
    Route draft: #808 medium·medium → medium·medium — unchanged (critic: the reason now cites ADR 0004's chosen option, not "variant B")
    Route draft: #809 cheap·medium → cheap·medium — unchanged
    Route draft: #810 medium·medium → medium·medium — unchanged

    The trials sentence in "Testing Decisions" was corrected before publishing (breakdown critic, finding 3): no report prints import lines, so the trials count unique file pairs and rerun the research script the same way.

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

    ready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions