P3868R0
Allow #line before module declarations

Published Proposal,

This version:
http://wg21.link/P3868R0
Author:
(Apple)
Audience:
EWG
Project:
ISO/IEC 14882 Programming Languages — C++, ISO/IEC JTC1/SC22/WG21

Abstract

[P1857R3] disallowed any control-lines before the global module fragment. This has turned out to be too strict due to many common tools running the preprocessor separately. We need to allow #line directives on any line. This resolves US 55-102.

1. The Problem

After implementing part of [P1857R3], Clang soon discovered that it makes the following code ill-formed:

#line 1 "A.cppm"
export module a;

This is a problem because tools such as build2, ccache, and distcc, among many others, rely on feeding the preprocessed output back into the compiler. This output generally contains #line (or GNU line marker) directives at the start of the file.

2. The Fix

The standard should allow #line directives to appear on any line.

3. Wording

Modify [cpp.pre]:

module-file:
groupopt pp-global-module-fragmentopt pp-module groupopt pp-private-module-fragmentopt
4 At the start of phase 4 of translation, the group of a pp-global-module-fragment shall contain neither a text-line nor a pp-import.

5 At the start of phase 4 of translation, the leading group of a module-file, if present, shall consist only of #line directives.

References

Informative References

[P1857R3]
Michael Spencer. Modules Dependency Discovery. 11 February 2020. URL: https://wg21.link/p1857r3