Skip to content

Commit cf8c02f

Browse files
authored
[flang] Don't insert spaces in -E output after line continuation (llvm#135063)
See test case. When Fortran line continuation has been used, don't insert spaces in -E formatted output to put things into the right column, as this can break up a token. Fixes llvm#134986.
1 parent 18fe012 commit cf8c02f

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

flang/lib/Parser/parsing.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void Parsing::EmitPreprocessedSource(
155155
const auto getOriginalChar{[&](char ch) {
156156
if (IsLetter(ch) && provenance && provenance->size() == 1) {
157157
if (const char *orig{allSources.GetSource(*provenance)}) {
158-
const char upper{ToUpperCaseLetter(ch)};
158+
char upper{ToUpperCaseLetter(ch)};
159159
if (*orig == upper) {
160160
return upper;
161161
}
@@ -184,21 +184,23 @@ void Parsing::EmitPreprocessedSource(
184184
std::optional<SourcePosition> position{provenance
185185
? allSources.GetSourcePosition(provenance->start())
186186
: std::nullopt};
187-
if (lineDirectives && column == 1 && position) {
188-
if (&*position->path != sourcePath) {
189-
out << "#line \"" << *position->path << "\" " << position->line
190-
<< '\n';
191-
} else if (position->line != sourceLine) {
192-
if (sourceLine < position->line &&
193-
sourceLine + 10 >= position->line) {
194-
// Emit a few newlines to catch up when they'll likely
195-
// require fewer bytes than a #line directive would have
196-
// occupied.
197-
while (sourceLine++ < position->line) {
198-
out << '\n';
187+
if (column == 1 && position) {
188+
if (lineDirectives) {
189+
if (&*position->path != sourcePath) {
190+
out << "#line \"" << *position->path << "\" " << position->line
191+
<< '\n';
192+
} else if (position->line != sourceLine) {
193+
if (sourceLine < position->line &&
194+
sourceLine + 10 >= position->line) {
195+
// Emit a few newlines to catch up when they'll likely
196+
// require fewer bytes than a #line directive would have
197+
// occupied.
198+
while (sourceLine++ < position->line) {
199+
out << '\n';
200+
}
201+
} else {
202+
out << "#line " << position->line << '\n';
199203
}
200-
} else {
201-
out << "#line " << position->line << '\n';
202204
}
203205
}
204206
sourcePath = &*position->path;
@@ -244,7 +246,7 @@ void Parsing::EmitPreprocessedSource(
244246
}
245247
}
246248
} else if (!inContinuation && !inDirectiveSentinel && position &&
247-
position->column <= 72) {
249+
position->line == sourceLine && position->column < 72) {
248250
// Preserve original indentation
249251
for (; column < position->column; ++column) {
250252
out << ' ';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
! RUN: %flang -E %s 2>&1 | FileCheck %s
2+
! CHECK: print *, "HELLO "//" WORLD"
3+
print *, "HELLO "/&
4+
&/" WORLD"
5+
end

0 commit comments

Comments
 (0)