[clang] [Clang] Dump minimization hints for namespaces (PR #151534)

Justin Stitt via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 31 11:31:48 PDT 2025


JustinStitt wrote:

Hi Ilya,

I did some testing and found an inconsistency in the behaviors between exporting entire namespaces or exporting declarations within a namespace piecemeal.

See this test
```cpp
export module some_lib;

namespace piecemeal { // line 3
  export int add_one(int n) { return n + 1; }
  export int add_two(int n) { return n + 2; }  // unused
}

export namespace whole { // line 8
  int add_one(int n) { return n + 1; }
  int add_two(int n) { return n + 2; }  // unused
}
```

```cpp
import some_lib;
int main() {
  (void)piecemeal::add_one(4);  // only use add_one from each namespace
  (void)whole::add_one(4);
}
```

which produces the following minimized hints
<details>
<summary>min_hints.json</summary>
<code>
{
  "required_ranges": [
    {
      "file": "<snip>/tests/some_lib.cppm",
      "range": [
        {
          "from": {
            "line": 3,
            "column": 1
          },
          "to": {
            "line": 3,
            "column": 22
          }
        },
        {
          "from": {
            "line": 4,
            "column": 3
          },
          "to": {
            "line": 4,
            "column": 46
          }
        },
        {
          "from": {
            "line": 6,
            "column": 1
          },
          "to": {
            "line": 6,
            "column": 2
          }
        },
        {
          "from": {
            "line": 8,
            "column": 1
          },
          "to": {
            "line": 11,
            "column": 2
          }
        }
      ]
    }
  ]
}

</code>
```
</details>

Notice how lines 3, 4, 6 correspond to the namespace that had its declarations exported one-by-one. The namespace that is exported all at once is entirely marked as required (lines 8 to 11) even though just one of its declarations is used.

https://github.com/llvm/llvm-project/pull/151534


More information about the cfe-commits mailing list