test_macro_recovery.py

tests/test_macro_recovery.py

gh#403 — the `#define` branch doxygen's preprocessor cannot reach.

test_a_macro_only_dossier_carries_the_first_sites_gates

None test_macro_recovery.test_a_macro_only_dossier_carries_the_first_sites_gates(Path taken_branch_index, Path repo)

A function_dossier has ONE `file` and ONE `line_start`, so its `gated_by` can describe one position — and it must be the position it reports. `_macro_dossier` takes its identity from the first site and COPIES that site's gates rather than recomputing them, so the two cannot drift apart.

tests/test_macro_recovery.py:325

Checks

  • assert doss is not None
  • assert doss.kind == MACRO_KIND
  • assert doss.line_start == GATED_LINE
  • assert {(g.macro, g.form) for g in doss.gated_by} == {
  • assert sorted(m.line for m in doss.macros) == [GATED_LINE, ELSE_LINE]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-004measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → import_kconfig_gates → declared_macro_names → bare_macro_names
REQ-DDB-INDEX-004measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → recover_ast_symbols
REQ-DDB-PIPE-003measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → import_kconfig_gates → run_harvest
REQ-DDB-PIPE-007measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → import_kconfig_gates
REQ-DDB-PIPE-011measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → import_kconfig_gates → run_harvest → _harvest_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_a_macro_only_dossier_carries_the_first_sites_gates → import_kconfig_gates → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language
REQ-DDB-QUERY-001measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier → connect
REQ-DDB-QUERY-002inferredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier → _dossier_conn → unresolved_inbound_for_rowids
REQ-DDB-QUERY-003measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier → _dossier_conn → function_candidates
REQ-DDB-QUERY-004measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier
REQ-DDB-QUERY-006measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier → _dossier_conn → gates_covering
REQ-DDB-QUERY-009measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier
REQ-DDB-QUERY-010measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier
REQ-DDB-QUERY-012measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier → _dossier_conn → _macro_dossier
REQ-DDB-QUERY-013measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier → _dossier_conn → config_defaults_conn
REQ-DDB-SCHEMA-011inferredtest_a_macro_only_dossier_carries_the_first_sites_gates → function_dossier → _dossier_conn → locks_held_for_rowids
REQ-DDB-SCHEMA-012measured
REQ-DDB-SCHEMA-013measuredtest_a_macro_only_dossier_carries_the_first_sites_gates → import_kconfig_gates → ensure_kconfig_gates_table
REQ-DDB-WIKI-054inferredtest_a_macro_only_dossier_carries_the_first_sites_gates → recover_ast_symbols → _recover_into → all
Source · 27 lines
def test_a_macro_only_dossier_carries_the_first_sites_gates(
    taken_branch_index: Path, repo: Path
) -> None:
    """A function_dossier has ONE `file` and ONE `line_start`, so its `gated_by` can describe one
    position — and it must be the position it reports. `_macro_dossier` takes its identity
    from the first site and COPIES that site's gates rather than recomputing them, so the
    two cannot drift apart.

    `PRIVATE` resolves to no function in this fixture, so this is the macro-SUBJECT path.
    The per-site lists on `macros` stay the complete answer; this pins that the
    function_dossier-level field means the same thing here as it does on a function."""
    recover_ast_symbols(taken_branch_index, repo, None)
    import_kconfig_gates(taken_branch_index, repo)

    doss = function_dossier(taken_branch_index, "PRIVATE")
    assert doss is not None
    assert doss.kind == MACRO_KIND
    assert doss.line_start == GATED_LINE
    assert {(g.macro, g.form) for g in doss.gated_by} == {
        ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFNDEF)
    }, (
        "the function_dossier's gates must describe the line the function_dossier reports, not both branches"
    )
    ## And the panel still carries both, which is the field a reader is pointed at.
    assert sorted(m.line for m in doss.macros) == [GATED_LINE, ELSE_LINE]

Activity · 8 steps
lines 336–336recover_ast_symbols(taken_branch_index, repo, None)lines 337–337import_kconfig_gates(taken_branch_index, repo)lines 339–339doss = function_dossier(taken_branch_index, "PRIVATE")lines 340–340assert doss is not Nonelines 341–341assert doss.kind == MACRO_KINDlines 342–342assert doss.line_start == GATED_LINElines 343–347assert {(g.macro, g.form) for g in doss.gated_by} == { ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFNDEF) }, ( "the function_dossier's gates must describe the line the function_dossier reports, not both branches" )lines 349–349assert sorted(m.line for m in doss.macros) == [GATED_LINE, ELSE_LINE]

test_a_template_argument_macro_use_is_recovered_into_referenced_by

None test_macro_recovery.test_a_template_argument_macro_use_is_recovered_into_referenced_by(Path tmp_path)

A macro used as a non-type template argument must appear in referenced_by .

tests/test_macro_recovery.py:375

Show on the map →

Called by none

Calls none

Checks

  • assert result.returncode == 0, (f"the build failed, so the recovery cannot be under test:\n{result.stderr[-2000:]}" )
  • assert macro, "precondition: the macro must be indexed at all"
  • assert expected <= referenced, (f"macro uses missing from referenced_by: {sorted(expected - referenced)}. " f"`bold_table` is gh#9's reported shape; the others are the measured control set." )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-004measured
REQ-DDB-QUERY-001measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-006measured
REQ-DDB-QUERY-012measured
Source · 106 lines
def test_a_template_argument_macro_use_is_recovered_into_referenced_by(tmp_path: Path) -> None:
    """gh#9. `referenced_by` answers "I am about to change this constant — what else is baked
    against it", and it was answering INCOMPLETELY: doxygen's xref pass does not record a macro
    used as a NON-TYPE TEMPLATE ARGUMENT.

    MEASURED, six uses of one macro each inside a documented body, before the fix:

        int n = MACRO;                      xref emitted
        int buf[MACRO];                     xref emitted
        static const int k = MACRO;         xref emitted
        std::array<Row, MACRO> kRows{};     NO XREF
        std::array<int, MACRO> a{};         NO XREF
        Box<int, MACRO> b{};                NO XREF   (user template)

    So the discriminator is template-argument POSITION — not "inside a body", which was the
    reporter's first reading, and not `std::array` specifically, since a user-defined template
    misses too.

    A PARTIAL LIST IS WORSE THAN AN EMPTY ONE. The existing disclosure covers empty — on mbedtls
    871 of 2,504 macros have any inbound reference — and says nothing about present-and-short,
    which reads as complete. The reporter found two of three sites and would have learned about
    the third from a compiler error.

    ALL SIX ARE ASSERTED, and that is what makes this test worth having: the first fix recovered
    five and missed `bold_table`, the exact reported shape, because its `reference_declarator`
    defeated the shared declarator walk. Any single-case assertion would have passed.

    @brief Every measured macro use reaches `referenced_by`.
    @return None.
    @version 1
    """
    import subprocess
    import sys

    from clew.query import macro_definitions

    root = tmp_path / "repo"
    (root / "src").mkdir(parents=True)
    (root / "src" / "m.h").write_text(
        "#pragma once\n/** @brief Face count. */\n#define FACE_COUNT 20\n", encoding="utf-8"
    )
    (root / "src" / "m.cpp").write_text(
        '#include "m.h"\n#include <array>\n'
        "template <typename T, int N> struct Box { T v[N]; };\n"
        "struct Row { int a; };\n"
        "/** @brief return-type AND body use. */\n"
        "const std::array<Row, FACE_COUNT>& bold_table() {\n"
        "    static const std::array<Row, FACE_COUNT> kRows{};\n    return kRows;\n}\n"
        "/** @brief plain statement. */\nint plain_use() { int n = FACE_COUNT; return n; }\n"
        "/** @brief C array size. */\nint array_size_use() { int b[FACE_COUNT]; return b[0]; }\n"
        "/** @brief static scalar. */\n"
        "int static_scalar() { static const int k = FACE_COUNT; return k; }\n"
        "/** @brief std::array, no static. */\n"
        "int std_array_plain() { std::array<int, FACE_COUNT> a{}; return a[0]; }\n"
        "/** @brief user template. */\n"
        "int user_template_use() { Box<int, FACE_COUNT> b{}; return b.v[0]; }\n",
        encoding="utf-8",
    )
    subprocess.run(["git", "init", "-q", str(root)], check=True)
    subprocess.run(["git", "-C", str(root), "add", "-A"], check=True)
    subprocess.run(
        [
            "git",
            "-C",
            str(root),
            "-c",
            "user.email=t@t",
            "-c",
            "user.name=t",
            "commit",
            "-qm",
            "init",
        ],
        check=True,
    )

    db = tmp_path / "out.db"
    ## `sys.executable`, NEVER a bare "python". CI runs pytest from the venv while bare
    ## `python` resolves to the hosted-tool interpreter, which has no `clew` installed — so this
    ## passed locally (the venv is on this shell's PATH) and failed on all three CI versions.
    ## The house rule against bare interpreter invocations exists for exactly this.
    result = subprocess.run(
        [sys.executable, "-m", "clew", "--repo-root", str(root), "--output", str(db)],
        capture_output=True,
        text=True,
    )
    assert result.returncode == 0, (
        f"the build failed, so the recovery cannot be under test:\n{result.stderr[-2000:]}"
    )

    macro = macro_definitions(db, "FACE_COUNT")
    assert macro, "precondition: the macro must be indexed at all"
    referenced = set(macro[0].referenced_by)

    expected = {
        "plain_use",
        "array_size_use",
        "static_scalar",
        "bold_table",
        "std_array_plain",
        "user_template_use",
    }
    assert expected <= referenced, (
        f"macro uses missing from referenced_by: {sorted(expected - referenced)}. "
        f"`bold_table` is gh#9's reported shape; the others are the measured control set."
    )

1 more lines, not shown

Activity · 18 steps
lines 406–406import subprocesslines 407–407import syslines 409–409from clew.query import macro_definitionslines 411–411root = tmp_path / "repo"lines 412–412(root / "src").mkdir(parents=True)lines 413–415(root / "src" / "m.h").write_text( "#pragma once\n/** @brief Face count. */\n#define FACE_COUNT 20\n", encoding="utf-8" )lines 416–432(root / "src" / "m.cpp").write_text( '#include "m.h"\n#include <array>\n' "template <typename T, int N> struct Box { T v[N]; };\n" "struct Row { int a; };\n" "/** @brief return-type AND body use. */\n" "const std::array<Row, FACE_COUNT>& bold_table() {\n" " static const std::array<Row, FACE_COUNT> kRows{};\n return kRows;\n}\n" "/** @brief plain statement. */\nint plain_use() { int n = FACE_COUNT; return n; }\n" "/** @brief C array size. */\nint array_size_use() { int b[FACE_COUNT]; return b[0]; }\n" "/** @brief static scalar. */\n" "int static_scalar() { static const int k = FACE_COUNT; return k; }\n" "/** @brief std::array, no static. */\n" "int std_array_plain() { std::array<int, FACE_COUNT> a{}; return a[0]; }\n" "/** @brief user template. */\n" "int user_template_use() { Box<int, FACE_COUNT> b{}; return b.v[0]; }\n", encoding="utf-8", )lines 433–433subprocess.run(["git", "init", "-q", str(root)], check=True)lines 434–434subprocess.run(["git", "-C", str(root), "add", "-A"], check=True)lines 435–449subprocess.run( [ "git", "-C", str(root), "-c", "user.email=t@t", "-c", "user.name=t", "commit", "-qm", "init", ], check=True, )lines 451–451db = tmp_path / "out.db"lines 456–460result = subprocess.run( [sys.executable, "-m", "clew", "--repo-root", str(root), "--output", str(db)], capture_output=True, text=True, )lines 461–463assert result.returncode == 0, ( f"the build failed, so the recovery cannot be under test:\n{result.stderr[-2000:]}" )lines 465–465macro = macro_definitions(db, "FACE_COUNT")lines 466–466assert macro, "precondition: the macro must be indexed at all"lines 467–467referenced = set(macro[0].referenced_by)lines 469–476expected = { "plain_use", "array_size_use", "static_scalar", "bold_table", "std_array_plain", "user_template_use", }lines 477–480assert expected <= referenced, ( f"macro uses missing from referenced_by: {sorted(expected - referenced)}. " f"`bold_table` is gh#9's reported shape; the others are the measured control set." )

test_each_definition_site_reports_its_own_gate_polarity

None test_macro_recovery.test_each_definition_site_reports_its_own_gate_polarity(Path taken_branch_index, Path repo)

"WHICH CONSUMER GETS WHICH" IS A QUESTION ABOUT A GATE, and this is the assertion the graded question actually needs. Two rows with two different expansions read as a contradiction until something says which branch each sits in.

tests/test_macro_recovery.py:288

Checks

  • assert import_kconfig_gates(taken_branch_index, repo) > 0
  • assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFNDEF) in gated
  • assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFDEF) not in gated, ("the private_ spelling is what a unit that has NOT defined the symbol gets" )
  • assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFDEF) in otherwise
  • assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFNDEF) not in otherwise, ("the bare spelling is what a unit that HAS defined the symbol gets" )
  • assert sites[GATED_LINE].gates_unplaceable == 0
  • assert sites[ELSE_LINE].gates_unplaceable == 0

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-004measuredtest_each_definition_site_reports_its_own_gate_polarity → import_kconfig_gates → declared_macro_names → bare_macro_names
REQ-DDB-INDEX-004measuredtest_each_definition_site_reports_its_own_gate_polarity → recover_ast_symbols
REQ-DDB-PIPE-003measuredtest_each_definition_site_reports_its_own_gate_polarity → import_kconfig_gates → run_harvest
REQ-DDB-PIPE-007measuredtest_each_definition_site_reports_its_own_gate_polarity → import_kconfig_gates
REQ-DDB-PIPE-011measuredtest_each_definition_site_reports_its_own_gate_polarity → import_kconfig_gates → run_harvest → _harvest_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_each_definition_site_reports_its_own_gate_polarity → import_kconfig_gates → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language
REQ-DDB-QUERY-001measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-006measured
REQ-DDB-QUERY-012measured
REQ-DDB-SCHEMA-012measured
REQ-DDB-SCHEMA-013measuredtest_each_definition_site_reports_its_own_gate_polarity → import_kconfig_gates → ensure_kconfig_gates_table
REQ-DDB-WIKI-054inferredtest_each_definition_site_reports_its_own_gate_polarity → recover_ast_symbols → _recover_into → all
Source · 37 lines
def test_each_definition_site_reports_its_own_gate_polarity(
    taken_branch_index: Path, repo: Path
) -> None:
    """ "WHICH CONSUMER GETS WHICH" IS A QUESTION ABOUT A GATE, and this is the assertion
    the graded question actually needs. Two rows with two different expansions read as a
    contradiction until something says which branch each sits in.

    EVERY CLAIM IS TWO-SIDED. An implementation that attached every gate in the file to
    every line would satisfy the positive halves of both assertions below and would answer
    the question backwards half the time, so each site is asserted to report its own form
    AND to NOT report the other's.

    Both stages run for real against the same file, which is the coupling no hand-written
    database can test: `ast_symbols` decides line 17 from `node.start_point` and
    `kconfig_gates` decides the `#else` extent from its own walk, and the answer is only
    right if those two agree."""
    recover_ast_symbols(taken_branch_index, repo, None)
    assert import_kconfig_gates(taken_branch_index, repo) > 0

    sites = {m.line: m for m in macro_definitions(taken_branch_index, "PRIVATE")}
    gated = {(g.macro, g.form) for g in sites[GATED_LINE].gated_by}
    otherwise = {(g.macro, g.form) for g in sites[ELSE_LINE].gated_by}

    assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFNDEF) in gated
    assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFDEF) not in gated, (
        "the private_ spelling is what a unit that has NOT defined the symbol gets"
    )
    assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFDEF) in otherwise
    assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFNDEF) not in otherwise, (
        "the bare spelling is what a unit that HAS defined the symbol gets"
    )
    ## Zero is a measurement here: every gate in this file had a recorded extent, so an
    ## empty `gated_by` on some other site would mean ungated rather than unknown.
    assert sites[GATED_LINE].gates_unplaceable == 0
    assert sites[ELSE_LINE].gates_unplaceable == 0

Activity · 11 steps
lines 304–304recover_ast_symbols(taken_branch_index, repo, None)lines 305–305assert import_kconfig_gates(taken_branch_index, repo) > 0lines 307–307sites = {m.line: m for m in macro_definitions(taken_branch_index, "PRIVATE")}lines 308–308gated = {(g.macro, g.form) for g in sites[GATED_LINE].gated_by}lines 309–309otherwise = {(g.macro, g.form) for g in sites[ELSE_LINE].gated_by}lines 311–311assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFNDEF) in gatedlines 312–314assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFDEF) not in gated, ( "the private_ spelling is what a unit that has NOT defined the symbol gets" )lines 315–315assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFDEF) in otherwiselines 316–318assert ("ALLOW_PRIVATE_ACCESS", KCONFIG_GATE_IFNDEF) not in otherwise, ( "the bare spelling is what a unit that HAS defined the symbol gets" )lines 321–321assert sites[GATED_LINE].gates_unplaceable == 0lines 322–322assert sites[ELSE_LINE].gates_unplaceable == 0

test_recovery_is_idempotent_for_macros

None test_macro_recovery.test_recovery_is_idempotent_for_macros(Path taken_branch_index, Path repo)

A `(file, name, line)` key is finer than the variable rule's `(file, name)`, and a finer key has more ways to miss: read back the wrong column, or compare a tree-sitter line against a `bodystart`, and every macro is re-inserted on every build. Only a second pass returning zero new rows catches it.

tests/test_macro_recovery.py:271

Show on the map →

Called by none

Calls _macro_rows, recover_ast_symbols

Checks

  • assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_AST) == after_first
  • assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_DOXYGEN) == 1

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-004measuredtest_recovery_is_idempotent_for_macros → recover_ast_symbols
REQ-DDB-PIPE-003measuredtest_recovery_is_idempotent_for_macros → recover_ast_symbols → _recover_into → function_definition_harvester
REQ-DDB-PIPE-011measuredtest_recovery_is_idempotent_for_macros → recover_ast_symbols → _recover_into → run_harvest → _harvest_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_recovery_is_idempotent_for_macros → recover_ast_symbols → _recover_into → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language
REQ-DDB-WIKI-054inferredtest_recovery_is_idempotent_for_macros → recover_ast_symbols → _recover_into → all
Source · 17 lines
def test_recovery_is_idempotent_for_macros(taken_branch_index: Path, repo: Path) -> None:
    """A `(file, name, line)` key is finer than the variable rule's `(file, name)`, and a
    finer key has more ways to miss: read back the wrong column, or compare a tree-sitter
    line against a `bodystart`, and every macro is re-inserted on every build. Only a
    second pass returning zero new rows catches it."""
    recover_ast_symbols(taken_branch_index, repo, None)
    after_first = _macro_rows(taken_branch_index, SYMBOL_SOURCE_AST)

    recover_ast_symbols(taken_branch_index, repo, None)

    assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_AST) == after_first
    assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_DOXYGEN) == 1


# ─── part 3: which branch — two-sided, per site ──────────────────────────────

Activity · 5 steps
lines 276–276recover_ast_symbols(taken_branch_index, repo, None)lines 277–277after_first = _macro_rows(taken_branch_index, SYMBOL_SOURCE_AST)lines 279–279recover_ast_symbols(taken_branch_index, repo, None)lines 281–281assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_AST) == after_firstlines 282–282assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_DOXYGEN) == 1

test_the_else_branch_is_recovered_and_doxygens_own_row_is_untouched

None test_macro_recovery.test_the_else_branch_is_recovered_and_doxygens_own_row_is_untouched(Path taken_branch_index, Path repo)

THE DEDUP CONTROL, and the assertion that matters is the FIRST one: doxygen's own macro row count must not move. Measured the same way on the real target across this change — 2,504 either side — because that holds whatever the dedup rule does, where an assertion about the rule only holds if the rule is the one being read.

tests/test_macro_recovery.py:238

Show on the map →

Called by none

Calls _macro_rows, recover_ast_symbols

Checks

  • assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_DOXYGEN) == 1
  • assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_DOXYGEN) == 1, ("recovery must not add, replace or relabel a row doxygen emitted" )
  • assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_AST) == 1
  • assert sorted(sites) == [GATED_LINE, ELSE_LINE]
  • assert sites[GATED_LINE].expansion == "private_##member"
  • assert sites[ELSE_LINE].expansion == "member"
  • assert sites[GATED_LINE].provenance is None
  • assert sites[ELSE_LINE].provenance == SYMBOL_SOURCE_AST
  • assert sites[ELSE_LINE].params == "(member)"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-004measuredtest_the_else_branch_is_recovered_and_doxygens_own_row_is_untouched → recover_ast_symbols
REQ-DDB-PIPE-003measuredtest_the_else_branch_is_recovered_and_doxygens_own_row_is_untouched → recover_ast_symbols → _recover_into → function_definition_harvester
REQ-DDB-PIPE-011measuredtest_the_else_branch_is_recovered_and_doxygens_own_row_is_untouched → recover_ast_symbols → _recover_into → run_harvest → _harvest_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_the_else_branch_is_recovered_and_doxygens_own_row_is_untouched → recover_ast_symbols → _recover_into → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language
REQ-DDB-QUERY-001measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-006measured
REQ-DDB-QUERY-012measured
REQ-DDB-WIKI-054inferredtest_the_else_branch_is_recovered_and_doxygens_own_row_is_untouched → recover_ast_symbols → _recover_into → all
Source · 33 lines
def test_the_else_branch_is_recovered_and_doxygens_own_row_is_untouched(
    taken_branch_index: Path, repo: Path
) -> None:
    """THE DEDUP CONTROL, and the assertion that matters is the FIRST one: doxygen's own
    macro row count must not move. Measured the same way on the real target across this
    change — 2,504 either side — because that holds whatever the dedup rule does, where an
    assertion about the rule only holds if the rule is the one being read."""
    assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_DOXYGEN) == 1

    recover_ast_symbols(taken_branch_index, repo, None)

    assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_DOXYGEN) == 1, (
        "recovery must not add, replace or relabel a row doxygen emitted"
    )
    ## Exactly ONE recovered row for this name: the `#else` branch. A `(file, name)` key
    ## would give zero (doxygen already has the name in this file) and no key at all would
    ## give two.
    assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_AST) == 1

    sites = {m.line: m for m in macro_definitions(taken_branch_index, "PRIVATE")}
    assert sorted(sites) == [GATED_LINE, ELSE_LINE]
    assert sites[GATED_LINE].expansion == "private_##member"
    assert sites[ELSE_LINE].expansion == "member"
    ## Provenance separates the two halves of the answer. The `#else` spelling is not
    ## something doxygen declined to document — it is something doxygen could not see.
    assert sites[GATED_LINE].provenance is None
    assert sites[ELSE_LINE].provenance == SYMBOL_SOURCE_AST
    ## The recovered site carries its parameters, which means the `param` /
    ## `memberdef_param` write happened. Without it the site reads as object-like and the
    ## two rows of one macro would differ by who recovered them.
    assert sites[ELSE_LINE].params == "(member)"

Activity · 11 steps
lines 245–245assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_DOXYGEN) == 1lines 247–247recover_ast_symbols(taken_branch_index, repo, None)lines 249–251assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_DOXYGEN) == 1, ( "recovery must not add, replace or relabel a row doxygen emitted" )lines 255–255assert _macro_rows(taken_branch_index, SYMBOL_SOURCE_AST) == 1lines 257–257sites = {m.line: m for m in macro_definitions(taken_branch_index, "PRIVATE")}lines 258–258assert sorted(sites) == [GATED_LINE, ELSE_LINE]lines 259–259assert sites[GATED_LINE].expansion == "private_##member"lines 260–260assert sites[ELSE_LINE].expansion == "member"lines 263–263assert sites[GATED_LINE].provenance is Nonelines 264–264assert sites[ELSE_LINE].provenance == SYMBOL_SOURCE_ASTlines 268–268assert sites[ELSE_LINE].params == "(member)"

test_the_harvest_reads_both_branches_of_a_conditional_define

None test_macro_recovery.test_the_harvest_reads_both_branches_of_a_conditional_define(Path repo)

The parser has no preprocessor, so BOTH `#define`s are ordinary nodes to it. This is the premise of the whole change and it is asserted before anything is stored: if the walk saw one, no dedup rule downstream could recover the other.

tests/test_macro_recovery.py:210

Checks

  • assert parsed is not None
  • assert macros[("PRIVATE", GATED_LINE)].expansion == "private_##member"
  • assert macros[("PRIVATE", ELSE_LINE)].expansion == "member"
  • assert macros[("PRIVATE", GATED_LINE)].params == ("member",)
  • assert macros[("PRIVATE", ELSE_LINE)].params == ("member",)
  • assert macros[("MBEDTLS_PRIVATE_ACCESS_H", 8)].expansion == ""
  • assert macros[("MBEDTLS_PRIVATE_ACCESS_H", 8)].params == ()

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measuredtest_the_harvest_reads_both_branches_of_a_conditional_define → harvest_macro_definitions
REQ-DDB-PIPE-011measuredtest_the_harvest_reads_both_branches_of_a_conditional_define → _ast_parse_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_the_harvest_reads_both_branches_of_a_conditional_define → _ast_parse_one_file → _ts_language_for → _grammar_available → has_language
Source · 28 lines
def test_the_harvest_reads_both_branches_of_a_conditional_define(repo: Path) -> None:
    """The parser has no preprocessor, so BOTH `#define`s are ordinary nodes to it. This
    is the premise of the whole change and it is asserted before anything is stored: if
    the walk saw one, no dedup rule downstream could recover the other."""
    parsed = _ast_parse_one_file(HEADER, repo / HEADER, {})
    assert parsed is not None
    tree, src = parsed

    macros = {(m.name, m.line): m for m in harvest_macro_definitions(tree, src)}
    assert macros[("PRIVATE", GATED_LINE)].expansion == "private_##member"
    assert macros[("PRIVATE", ELSE_LINE)].expansion == "member"
    ## The parameter list, on BOTH sites. A function-like macro is a
    ## `preproc_function_def` with a `parameters` field; a walk that accepted only
    ## `preproc_def` would report every object-like `#define` in the repo and miss the one
    ## symbol this exists for, and a `preproc_params` read that took every child would
    ## return the parentheses as parameters.
    assert macros[("PRIVATE", GATED_LINE)].params == ("member",)
    assert macros[("PRIVATE", ELSE_LINE)].params == ("member",)
    ## The include guard is an OBJECT-like define with no value, and it is recovered
    ## rather than special-cased — recognising the idiom would be one more heuristic on a
    ## walk whose whole value is that a `#define` is unambiguous.
    assert macros[("MBEDTLS_PRIVATE_ACCESS_H", 8)].expansion == ""
    assert macros[("MBEDTLS_PRIVATE_ACCESS_H", 8)].params == ()


# ─── part 2: the row doxygen already has is not duplicated ───────────────────

Activity · 10 steps
lines 214–214parsed = _ast_parse_one_file(HEADER, repo / HEADER, {})lines 215–215assert parsed is not Nonelines 216–216tree, src = parsedlines 218–218macros = {(m.name, m.line): m for m in harvest_macro_definitions(tree, src)}lines 219–219assert macros[("PRIVATE", GATED_LINE)].expansion == "private_##member"lines 220–220assert macros[("PRIVATE", ELSE_LINE)].expansion == "member"lines 226–226assert macros[("PRIVATE", GATED_LINE)].params == ("member",)lines 227–227assert macros[("PRIVATE", ELSE_LINE)].params == ("member",)lines 231–231assert macros[("MBEDTLS_PRIVATE_ACCESS_H", 8)].expansion == ""lines 232–232assert macros[("MBEDTLS_PRIVATE_ACCESS_H", 8)].params == ()

test_the_include_guard_is_not_reported_as_a_configuration_gate

None test_macro_recovery.test_the_include_guard_is_not_reported_as_a_configuration_gate(Path taken_branch_index, Path repo)

THE NEGATIVE HALF, and it is what stops the polarity answer above from being luck. The fixture's outer `#ifndef MBEDTLS_PRIVATE_ACCESS_H` spans BOTH `#define`s, so a gate harvest that recorded include guards would attach it to every site — every macro in every header would come back "gated", and the one gate that means something would sit in a list beside a guard that means nothing.

tests/test_macro_recovery.py:352

Checks

  • assert "MBEDTLS_PRIVATE_ACCESS_H" not in {g.macro for g in site.gated_by}, (f"site at line {line} reports the include guard as a configuration gate" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-004measuredtest_the_include_guard_is_not_reported_as_a_configuration_gate → import_kconfig_gates → declared_macro_names → bare_macro_names
REQ-DDB-INDEX-004measuredtest_the_include_guard_is_not_reported_as_a_configuration_gate → recover_ast_symbols
REQ-DDB-PIPE-003measuredtest_the_include_guard_is_not_reported_as_a_configuration_gate → import_kconfig_gates → run_harvest
REQ-DDB-PIPE-007measuredtest_the_include_guard_is_not_reported_as_a_configuration_gate → import_kconfig_gates
REQ-DDB-PIPE-011measuredtest_the_include_guard_is_not_reported_as_a_configuration_gate → import_kconfig_gates → run_harvest → _harvest_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_the_include_guard_is_not_reported_as_a_configuration_gate → import_kconfig_gates → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language
REQ-DDB-QUERY-001measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-006measured
REQ-DDB-QUERY-012measured
REQ-DDB-SCHEMA-012measured
REQ-DDB-SCHEMA-013measuredtest_the_include_guard_is_not_reported_as_a_configuration_gate → import_kconfig_gates → ensure_kconfig_gates_table
REQ-DDB-WIKI-054inferredtest_the_include_guard_is_not_reported_as_a_configuration_gate → recover_ast_symbols → _recover_into → all
Source · 18 lines
def test_the_include_guard_is_not_reported_as_a_configuration_gate(
    taken_branch_index: Path, repo: Path
) -> None:
    """THE NEGATIVE HALF, and it is what stops the polarity answer above from being luck.
    The fixture's outer `#ifndef MBEDTLS_PRIVATE_ACCESS_H` spans BOTH `#define`s, so a
    gate harvest that recorded include guards would attach it to every site — every macro
    in every header would come back "gated", and the one gate that means something would
    sit in a list beside a guard that means nothing."""
    recover_ast_symbols(taken_branch_index, repo, None)
    import_kconfig_gates(taken_branch_index, repo)

    sites = {m.line: m for m in macro_definitions(taken_branch_index, "PRIVATE")}
    for line, site in sites.items():
        assert "MBEDTLS_PRIVATE_ACCESS_H" not in {g.macro for g in site.gated_by}, (
            f"site at line {line} reports the include guard as a configuration gate"
        )

Activity · 5 steps
lines 360–360recover_ast_symbols(taken_branch_index, repo, None)lines 361–361import_kconfig_gates(taken_branch_index, repo)lines 363–363sites = {m.line: m for m in macro_definitions(taken_branch_index, "PRIVATE")}lines 364–364◇ for line, site in sites.items()lines 365–367assert "MBEDTLS_PRIVATE_ACCESS_H" not in {g.macro for g in site.gated_by}, ( f"site at line {line} reports the include guard as a configuration gate" )