llvm / llvm/llvm-project

`-include` does not work with named modules.

Open
#162,193 8 comments 0 reactions 1 assignee Claimed by @Aadarsh-Keshri View on GitHub
clang:modules
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```cpp
export module A;
```

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

```
: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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.