Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions docs/studio/behaviour/map.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ A law with `backing: "check"` has a test that stands behind it; one with `"trust

The map file itself needs no owner and no `ignored` entry: `check` and `touched` both place `system-map.json` as ignored, and an `ignored` pattern that is exactly `system-map.json` never fails for matching nothing. So a new repository's map passes before and after it is first tracked.

**The link check.** A part is **read** when a named extractor reads at least one file it owns that is not one of its test files: `rust-modules` reads Rust files, `cargo` `Cargo.toml` files, `typescript-imports` script files, `go-imports` Go files, `java-imports` Java files, `python-imports` Python files, `dart-imports` Dart files, `php-imports` PHP files and `dotnet-projects` project files (`.csproj`, `.fsproj`, `.vbproj`), so two parts inside one project stay unproven. Between two read parts:
**The link check.** A part is **read** when a named extractor reads at least one file it owns that is not one of its test files: `rust-modules` reads Rust files, `cargo` `Cargo.toml` files, `typescript-imports` script files, `go-imports` Go files, `java-imports` Java files, `python-imports` Python files, `dart-imports` Dart files, `php-imports` PHP files, `c-includes` C and C++ files and `dotnet-projects` project files (`.csproj`, `.fsproj`, `.vbproj`), so two parts inside one project stay unproven. Between two read parts:

- an `import` link with no computed edge in its direction fails: a dead link, or a backwards one, whose problem says to turn it around;
- a computed edge with no `import` link in its direction fails, whatever run-time links join the pair, so a run-time link never hides a new dependency. (Before spec #473 such an edge was only a `note:`.)
Expand All @@ -78,7 +78,7 @@ An extractor reads which tracked file depends on which; the checker turns each p

- **Text only.** It reads the repository's tracked files as text and runs no toolchain, so a check gives the same answer on any machine and in CI. The one exception is `cargo metadata`, which both Rust extractors run (`docs/adr/0002-extractors-read-text-only.md`).
- **The core's rules.** The checker, not the extractor, drops a pair whose ends are not both tracked and a pair from a file to itself, and keeps a pair several extractors find once. It runs the extractors in the map's `extractors` order, so their problems print in that order.
- **Skipped files are its own.** An extractor names the files of its language that the build leaves out, most often its test files, or ones the build never compiles. A skipped file gives no pair and never makes its part read. `typescript-imports` skips its test files, `go-imports` its test files and the files no build compiles, `java-imports` the files outside production source roots, `python-imports` its test files, `dart-imports` the files under its test folders or of a fixture package and the ones whose header it cannot lex, `php-imports` its test folders, the files only a `classmap` reaches and the files it cannot lex, and `dotnet-projects` its test projects (their sections say which); `cargo` and `rust-modules` skip none: they leave dev-dependencies out but read files under `tests/` and `#[cfg(test)]` code.
- **Skipped files are its own.** An extractor names the files of its language that the build leaves out, most often its test files, or ones the build never compiles. A skipped file gives no pair and never makes its part read. `typescript-imports` skips its test files, `go-imports` its test files and the files no build compiles, `java-imports` the files outside production source roots, `python-imports` its test files, `dart-imports` the files under its test folders or of a fixture package and the ones whose header it cannot lex, `php-imports` its test folders, the files only a `classmap` reaches and the files it cannot lex, `c-includes` its test folders and test-named files, and `dotnet-projects` its test projects (their sections say which); `cargo` and `rust-modules` skip none: they leave dev-dependencies out but read files under `tests/` and `#[cfg(test)]` code.
- **Over-counts are stated.** An edge the code may not really hold is allowed only where its section below says so, as the barrel over-counts of `typescript-imports` are.
- **Invisible dependencies are listed.** A dependency the text shows no import for is a silent under-count, and its section lists it.

Expand Down Expand Up @@ -185,6 +185,23 @@ An extractor reads which tracked file depends on which; the checker turns each p
- **Under-counts:** transitive use (A's code using C through B with no reference from A to C); every reference of a skipped project, which the rules also take from a production project that sits under a test folder, carries a test name or holds a test marker, unless it says `<IsTestProject>false</IsTestProject>`; references written in `Directory.Build.props`, `Directory.Build.targets` or another imported file, which are not read; shared projects (`.projitems`); legacy `<Reference>` with a `HintPath` into another project's build output; linked sources (`<Compile Include="..\Other\X.cs">`), which compile another project's file with no reference; a wrongly cased reference, which builds on Windows and macOS but names no tracked file; and a mis-cased `itemgroup` or `include=`.
- **Unsure:** a path built from a property other than the two read, from an item list, or holding a wildcard.

- `c-includes` (`system-map/src/extract/c/`): each `#include` of a C or C++ file, paired with the tracked file it names (spec #806, whose research note is `docs/studio/research/c-cpp-extractor.md` and decision record `docs/adr/0004-c-includes-an-ignored-copy-keeps-an-include-unsure.md`). It reads tracked files as text, needs no config file (no `CMakeLists.txt`, `Makefile` or `compile_commands.json` is read) and runs no compiler (`docs/adr/0002-extractors-read-text-only.md`).

- **What it reads.** A tracked file whose extension, in any case, is one of `.c .h .cc .cpp .cxx .c++ .hpp .hh .hxx .h++ .inl .ipp .tcc`, the thirteen the research scanned. `.m`, `.mm`, `.cu`, `.inc`, `.def` and `.rc` are not read. In commit mode it reads the commit's texts.
- **The lexer** walks each file once. It skips `//` and `/* */` comments (an unclosed block comment hides the rest of the file), string and character literals, and C++ raw strings (`R"delim(...)delim"`), so an include in them gives nothing. A `'` inside a number (`1'000`, `0xFF'FF`) is a digit separator and opens no character literal. A block comment counts as one space, so code after it is at a line's start only when the comment began at one. 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 site is the line of the `#`. `#include` followed by anything else (a macro name), `#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, less its `.` segments. An empty path, an absolute one (starting with `/`) or one holding a backslash gives nothing.
- **Skipped files.** A file with a folder named, in any case, `test`, `tests`, `testing`, `unittest` or `fuzz`, or named `*_test.*`, `*_tests.*` or `test_*.*`, is skipped: it gives no pair, never makes its part read and is never an include's target. `bench/` and names such as `testutil.cc` are not skipped. The scan's own test rule is another one.
- **Resolution, per include.** Each include goes through these steps in order, the first that settles it wins:
1. **Next to the file.** A quoted include whose path, joined to the including file's folder and cleaned (`..` climbs), names a tracked file pairs with it, as the compiler's first search step finds it; an ignored or skipped file there gives no pair, and the search stops either way, as the compiler stops, with no fall-through to a match elsewhere.
2. **System names.** An angle-bracket include whose path is on the system-header list gives nothing. The list (`system-map/src/extract/c/system_headers.rs`) is the C and C++ standard headers, the POSIX headers, any path under `sys/`, `linux/`, `mach/`, `machine/`, `bits/`, `netinet/`, `arpa/` or `net/`, common platform headers (`byteswap.h`, `cpuid.h`, `intrin.h` and the like) and common Windows CRT and SDK headers; it never applies to a quoted include. Without it, bitcoin's `<cpuid.h>` would match `src/compat/cpuid.h`.
3. **Path-ending match.** A path with a `..` segment that did not resolve in step 1 gives nothing. Otherwise the candidates are the tracked files whose path equals the include's path or ends with `/` and the path, less skipped files; ignored files stay candidates. None, or only ignored ones: no pair. One live one: a pair. Some but not all ignored: Unsure. Two or more, none ignored: step 4.
4. **The folder tie-break.** A candidate's implied folder is its path less `/` and the include's path (`.` when 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 include is Unsure. The evidence is counted over the whole repository before any tie is settled, so file order changes nothing.
- **Unsure.** An include is Unsure for one of two reasons, `an ignored copy matches it` (step 3) or `several files match it` (step 4, with no tie-break beyond the evidence rule). The finding lists every candidate; the including file relaxes only its links to the candidates' parts, and `--json` lists it under `unsure`.
- **`ignored`.** An ignored copy stays a candidate and keeps an include Unsure, even when the tie-break would pick a live file: redis's `<sds.h>` means `deps/hiredis/sds.h` while `src/sds.h` is also tracked, so dropping the ignored copy would draw a wrong line. This differs from `java-imports` on purpose, which leaves an ignored file out of its type index (ADR 0004). Includes inside an ignored file are never resolved.
- **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 trial set.** The research ran it on curl, leveldb, bitcoin and redis (`docs/studio/research/c-cpp-extractor.md`, rounds 1 to 4). It sets no group: each include names one file.

**Import sites** (spec #477, "What an import site is"). Each file pair keeps the lines of the importing file that make it, each line once, and the core joins the lines when several extractors, or one twice, find the pair. A site is always in the importing file:

- `rust-modules`: a `use` line is one site at its `use`, however many names its group brings in and however many lines it spans; an inline path is a site at its first word, once per line however many times the line writes it; a `mod` line is a site at its `mod`.
Expand All @@ -194,6 +211,7 @@ An extractor reads which tracked file depends on which; the checker turns each p
- `python-imports`: an import statement is a site at the line where it starts, its `from`, `lazy` or `import`, however many names or lines it spans; `import a, b` is one site for each pair it makes.
- `dart-imports`: an `import`, `export` or `part` directive is a site at the line of its keyword, however many lines its URIs, `if (…)` configurations and clauses span; each URI of a conditional directive makes its own pair with that one site.
- `php-imports`: a class `use` item is a site at the line of its name, so a group `use` over several lines gives each item its own line; a `require` or `include` is a site at the line of its keyword, however many lines its path spans.
- `c-includes`: an `#include` directive is a site at the line of its `#`, however many lines a backslash continuation spans.
- `dotnet-projects`: a `ProjectReference` is a site at the line of its `<`, the first line of a start tag over several lines.
- `cargo`: `cargo metadata` gives no lines, so the site is the importing crate's `Cargo.toml` at the line of the dependency's key (its rename, when it has one), found by a text search: a line `key = …` or `key.… = …`, bare or quoted, in a table of normal or build dependencies (a target's too, never `dev-dependencies` or a root manifest's `[workspace.dependencies]`), or a header `[dependencies.key]`. When no line matches, as for a top-level inline table `dependencies = { … }`, the site has no line.

Expand Down
Loading
Loading