C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Direct compile a file VS preprocessing the file first and compile the preprocessing output would generate different behaviour in c++20 modules

From: Yexuan Xiao <bizwen_at_[hidden]>
Date: Sun, 21 Sep 2025 17:57:10 +0000
Preprocessing directives (such as #include, import, etc.) are recognized first during preprocessing, and then text replacement macros are expanded. Therefore, text replacement macros cannot generate preprocessing directives.

Consider the following code:

#define INCLUDE include
#INCLUDE <vector>

This program is clearly nonsensical.

I believe that after preprocessing, genuinely valid "import" directives could be translated into "_import" (this identifier is reserved). Thus, if an "import" without an underscore prefix remains in the code after preprocessing, it indicates that it was generated by text replacement macros.

For example:

#define EMP
EMP import m;
import std;

After preprocessing, it becomes:

import m; // Still uses "import", so it is erroneous
_import std; // Correct import directive


________________________________
From: Std-Proposals <std-proposals-bounces_at_[hidden]> on behalf of Yrong via Std-Proposals <std-proposals_at_[hidden]>
Sent: Monday, September 22, 2025 0:39
To: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
Cc: Yrong <yronglin777_at_[hidden]>
Subject: [std-proposals] Direct compile a file VS preprocessing the file first and compile the preprocessing output would generate different behaviour in c++20 modules

Hi experts,

I'm working on implement P1857R3 in clang.

Hubert provided an example:
```
typedef int import;
#define EMP
EMP import m;
```

If we directly compile this code snippet, due to the `import` identifier is not in the start of line, and according to the current wording of the standard, this is not a C++ import directive. But if
we preprocessing the file first and compile the preprocessing output would have different behavior than above:
Generated by clang:
```
# 1 "./main.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 490 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "./main.cpp" 2
typedef int import;

    import m;
```

The `import` identifier now at start of line, so it's c++ import directive.

Is this the expected behavior? Should we keep the behavior consistent in both cases?

Best regards,
Yihan


Received on 2025-09-21 17:57:18