P3868R1
Allow #line before module declarations

Published Proposal,

This version:
http://wg21.link/P3868R1
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. Changelog

2. 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.

3. The Fix

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

4. Wording

Modify [cpp.pre]:

module-file:
line-directivesopt pp-global-module-fragmentopt pp-module groupopt pp-private-module-fragmentopt
control-line:
# include pp-tokens new-line
pp-import
line-directive
# embed pp-tokens new-line
# define identifier replacement-list new-line
# define identifier lparen identifier-listopt ) replacement-list new-line
# define identifier lparen ... ) replacement-list new-line
# define identifier lparen identifier-list , ... ) replacement-list new-line
# undef identifier new-line
# line pp-tokens new-line
# error pp-tokensopt new-line
# warning pp-tokensopt new-line
# pragma pp-tokensopt new-line
# new-line
line-directives:
line-directive line-directivesopt

Add before the first paragraph in [cpp.line]:

line-directive:
# line pp-tokens new-line

References

Informative References

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