Skip to content

Commit 6ea55d2

Browse files
committed
Emit trailing comments at EOF, closes #443
1 parent 6f85448 commit 6ea55d2

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.2.1 Build 8520:1326
2+
cppfront compiler v0.2.1 Build 8520:1425
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8520:1326"
1+
"8520:1425"

source/cppfront.cpp

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,28 @@ class positional_printer
401401

402402
// Catch up with comment/blank lines
403403
//
404+
auto print_comment(comment const& c)
405+
-> void
406+
{
407+
// For a line comment, start it at the right indentation and print it
408+
// with a newline end
409+
if (c.kind == comment::comment_kind::line_comment) {
410+
print( pad( c.start.colno - curr_pos.colno + 1 ) );
411+
print( c.text );
412+
assert( c.text.find("\n") == std::string::npos ); // we shouldn't have newlines
413+
print("\n");
414+
}
415+
416+
// For a stream comment, pad out to its column (if we haven't passed it already)
417+
// and emit it there
418+
else {
419+
print( pad( c.start.colno - curr_pos.colno ) );
420+
print( c.text );
421+
}
422+
423+
c.dbg_was_printed = true;
424+
}
425+
404426
auto flush_comments( source_position pos )
405427
-> void
406428
{
@@ -436,24 +458,8 @@ class positional_printer
436458
)
437459
)
438460
{
439-
// For a line comment, start it at the right indentation and print it
440-
// with a newline end
441-
if (comments[next_comment].kind == comment::comment_kind::line_comment) {
442-
print( pad( comments[next_comment].start.colno - curr_pos.colno + 1 ) );
443-
print( comments[next_comment].text );
444-
assert( comments[next_comment].text.find("\n") == std::string::npos ); // we shouldn't have newlines
445-
print("\n");
446-
}
447-
448-
// For a stream comment, pad out to its column (if we haven't passed it already)
449-
// and emit it there
450-
else {
451-
print( pad( comments[next_comment].start.colno - curr_pos.colno ) );
452-
print( comments[next_comment].text );
453-
assert(curr_pos.lineno <= pos.lineno); // we shouldn't have overshot
454-
}
455-
456-
comments[next_comment].dbg_was_printed = true;
461+
print_comment( comments[next_comment] );
462+
assert(curr_pos.lineno <= pos.lineno); // we shouldn't have overshot
457463
}
458464

459465
++next_comment;
@@ -466,6 +472,15 @@ class positional_printer
466472
}
467473
}
468474

475+
auto print_unprinted_comments()
476+
{
477+
for (auto const& c : *pcomments) {
478+
if (!c.dbg_was_printed) {
479+
print_comment(c);
480+
}
481+
}
482+
}
483+
469484
// Position ourselves as close to pos as possible,
470485
// and catch up with displaying comments
471486
//
@@ -553,7 +568,7 @@ class positional_printer
553568
//-----------------------------------------------------------------------
554569
// Finalize phase
555570
//
556-
auto finalize_phase()
571+
auto finalize_phase(bool print_remaining_comments = false)
557572
{
558573
if (
559574
is_open()
@@ -563,6 +578,10 @@ class positional_printer
563578
{
564579
flush_comments( {curr_pos.lineno+1, 1} );
565580

581+
if (print_remaining_comments) {
582+
print_unprinted_comments();
583+
}
584+
566585
// Always make sure the very last line ends with a newline
567586
// (not really necessary but makes some tools quieter)
568587
// -- but only if there's any Cpp2, otherwise don't
@@ -1439,8 +1458,9 @@ class cppfront
14391458
printer.print_extra( "\n#endif" );
14401459
}
14411460

1461+
printer.finalize_phase( true );
1462+
14421463
// Finally, some debug checks
1443-
printer.finalize_phase();
14441464
assert(
14451465
(!errors.empty() || tokens.num_unprinted_comments() == 0)
14461466
&& "ICE: not all comments were printed"

0 commit comments

Comments
 (0)