Skip to content

-include does not work with named modules. #162193

@Bigcheese

Description

@Bigcheese
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.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:modulesC++20 modules and Clang Header Modules

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions