<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/72383>72383</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Shouldn't `-fmodules-embed-all-files` be the default?
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          boris-kolpackov
      </td>
    </tr>
</table>

<pre>
    Consider these two translation units:

```c++
// hello.mxx

module;

#include <string>
#include <string_view>

export module hello;

export namespace hello
{
  std::string
  say_hello (const std::string_view& name)
  {
    return "Hello, " + std::string (name) + '!';
 }
}
```

```c++
// main.cxx

#include <string_view>

import hello;

void f (const std::string_view&) {}

int
main ()
{
  hello::say_hello ("World");
}
```

If I compile `hello.mxx` with Clang 16, then remove `hello.mxx`, and attempt to compile `main.cxx`, I get an error:

```
clang++-16 -std=c++2b -fmodule-output=hello.pcm -o hello.pcm.o -c -x c++-module hello.mxx
mv hello.mxx hello.mxx.bak
clang++-16 -std=c++2b -fmodule-file=hello=hello.pcm -o main.o -c -x c++ main.cxx
main.cxx:5:1: fatal error: malformed or corrupted AST file: 'could not find file '/tmp/hello-simple/hello.mxx' referenced by AST file 'hello.pcm''
```

This can be fixed by adding `-Xclang -fmodules-embed-all-files` when compiling `hello.mxx`.

However, `-fmodules-embed-all-files` appears to no longer be necessary for this example if using Clang 17 or later:

```
clang++-17 -std=c++2b -fmodule-output=hello.pcm -o hello.pcm.o -c -x c++-module hello.mxx
mv hello.mxx hello.mxx.bak
clang++-17 -std=c++2b -fmodule-file=hello=hello.pcm -o hello.o -c -x c++ main.cxx
# no error
```

But a minor change in `main.cxx` can bring its requirement back if compiling via `-frewrite-includes`:

```c++
// main.cxx

#include <string_view>

import hello;

void f (const std::string_view&) {}

int
main ()
{
  f (hello::say_hello ("World"));  // <-- NOW CALLING f().
}
```

```
clang++-17 -std=c++2b -fmodule-output=hello.pcm -o hello.pcm.o -c -x c++-module hello.mxx
mv hello.mxx hello.mxx.bak
clang++-17 -std=c++2b -fmodule-file=hello=hello.pcm -o hello.o -c -x c++ main.cxx
# no error
```

But:

```
clang++-17 -std=c++2b -x c++-module -E -frewrite-includes -o hello.ii hello.mxx
clang++-17 -std=c++2b -fmodule-output=hello.pcm -o hello.pcm.o -c -x c++-module hello.ii
rm hello.ii
clang++-17 -std=c++2b -fmodule-file=hello=hello.pcm -o hello.o -c -x c++ main.cxx
fatal error: cannot open file '/tmp/hello-simple1/hello.ii': No such file or directory
```

And if I add `-Xclang -fmodules-embed-all-files` to the second command, then everything again compiles fine. I get exactly the same behavior with Clang 18.

I have two questions about this:

1. Is `-fmodules-embed-all-files` the default starting from Clang 17? If the answer is yes, then there seems to be a bug in the `-fdirectives-only` interaction.

2. If `-fmodules-embed-all-files` is not the default, then should it not be made so? A BMI that still has references to source files is quite brittle since it can be moved around. AFAIK, neither GCC nor MSVC have this restriction for their BMIs.

@iains @ChuanqiXu9 @dwblaikie
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV11v27gS_TX0y0CGTPkrD35w7Lo1btv70OK2bwVFjS3eUKRCUnb87xdDKf7q1kmwQLELLBA4pkTOHJ455BkL79XWIM7Y6J6Nlj3RhNK6WW6d8smD1bWQD3bXy21xmC2s8apAB6FEjxD2FoITxmsRlDXQGBU8y-YsXbL0-XOctn-S8Xv6a5_yFeMrKFFr26-ens5XVLZoNLLs_iIMz5SRuikQWLbwwSmzZdm7X7_8sVO4P82In_hUWxegTdAmv0rTzTCiQl8L-TypnTHppgL4UNA2s3kH5PmxOPyIC4DxqbTGh-uZLSo-jhkYv3teeRYbwGFonAHG-YeYnS_oOzB-fx2O8nSR4mvGJ4wP6PN5W8Amy2f4y6uKvLpMlVCmLy-r9FrOVRUZ_TOyd1YVsHmRq7i5yf0JfxvXhE4uQhFX0yObZ1x2WWPM89owzr9ZpwvGOS07wrpJ0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