Skip to content

The module scan refuses an import inside a preprocessor condition, whatever the condition evaluates to #1026

Done

Description

@Sunrisepeak

Split from #421 (§1), which is closed for its other parts.

What happens

An import inside a preprocessor condition is refused by the module scan, whatever the condition evaluates to. Measured with mcpp 2026.11.26.1 (GCC 16.1.0, Linux x86_64):

// src/m.cppm
export module m;
#ifdef USE_STD_MODULE
import std;
#endif
export int f() { return 1; }
error: scanner errors:
  src/m.cppm:3: import statement inside conditional preprocessor block (forbidden in M1)

USE_STD_MODULE is defined nowhere, so the import is not part of the translation unit. The refusal is at src/modgraph/scanner.cppm (if_depth > 0).

Where it was met

[scan_overrides."<glob>"] (docs/04 §2.8.3) is the way out today: the author states the file's scan result.

Direction to evaluate

The scanner's own comment states that it is a second parser of a language whose first parser is the compiler, and that the compiler is right where the two disagree. A condition is the case in which only the compiler has the answer: it depends on defines, on the command line and on the toolchain's built-in macros (__cpp_lib_stacktrace). So the unit's imports would come from the compiler's dependency scan (P1689) when the lexical scan meets an import inside a condition, instead of a refusal — rather than from an evaluation of conditions in the engine, which would be a partial third implementation of the preprocessor.

To settle before an implementation: what the lexical scan is still needed for on such a unit (the provided module name, which precedes any condition the compiler accepts), and the cost of one compiler scan per such unit before the graph is known.

Activity

  1. speak-agent commented on Nov 27, 2026

    @speak-agent
    Member

    Implemented in mcpp 2026.11.27.1 (#1023). An import inside a preprocessor condition is read by the compiler's dependency scan with the unit's own flags, on GCC and Clang. Header units and MSVC are still refused, and the refusal says why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions