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

    <tr>
        <th>Summary</th>
        <td>
            `-include` does not work with named modules.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang:modules
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          Bigcheese
      </td>
    </tr>
</table>

<pre>
    ```cpp
export module A;
```

built with `-std=c++26 -fsyntax-only -include vector` fails to build due to:

```
<source>:1:8: error: module declaration must occur at the start of the translation unit
    1 | export module A;
      | ^
```

This is because `-include vector` currently just injects `#include "vector"` before the main file, which is invalid syntax for a [*module-file*](https://eel.is/c++draft/cpp.pre).

I think it would be most useful to instead inject it (along with most of the other stuff we inject) into the global module fragment, even if one doesn't exist. Given the preprocessor grammar, it is easy to detect this situation before calling `clang::InitializePreprocessor`.

One way to do this, when we know it's a named module file, is to delay injecting these tokens until we encounter the first module declaration. If it's `module;`, then just inject them as is, and if it's `extern module A;`, then stick a `module;` on front.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJxsVM1yozgQfhr50mUKi0DsAweSTLbmtHvYFxCigZ4IySW14niffqsNmXFqxuUqI6t_vp9uTEo0ecRW1U-qftmZzHOI7RNNdkZMuOvDcG1VU65fez6rssOPc4gMSxiyQ-hU9aTK7meMPJddn8kxXIhnUE25Tzyo6sUq_aT0k25gP6arZ_OxD95dYU_eujwgvKPlEFVTwmjIJeAAUmiAISNwUFW3Vv_SrHpOIUeLqvqmqu6gqu6oqg4wxhDlYcM5oHUmGqbgYcmJIVibIxgGnhESm8gQxtuBo_HJraHZE6uyAwA4gHp8hj-yh9tHrlX97Xc1_p0pASXo0Zqc8CbJ75xtjhE9uyv8EHzkf6DlJMFKV5_hSustQ2tJ6nEMEW-wF0MeRnKo9DNcZrKz9CT_bhwNsAoOY4hgQOzW3cphv6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