Skip to content

Commit 3a87189

Browse files
committed
Start refining comment handling preparatory to splitting decl/defn section comments
1 parent fbf55ad commit 3a87189

8 files changed

+123
-56
lines changed

regression-tests/test-results/mixed-allcpp1-hello.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ auto main() -> int
1313
auto s = std::string{"world\n"};
1414
std::cout << "Hello " << s;
1515
}
16+

regression-tests/test-results/mixed-captures-in-expressions-and-postconditions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ auto insert_at(cpp2::in<int> where, cpp2::in<int> val) -> void
3737
auto post_21_5 = cpp2::finally_success([_0 = CPP2_UFCS_0(ssize, vec)]{cpp2::Default.expects(CPP2_UFCS_0(ssize, vec)==_0 + 1, "");} );
3838
#line 21 "mixed-captures-in-expressions-and-postconditions.cpp2"
3939

40-
40+
#line 23 "mixed-captures-in-expressions-and-postconditions.cpp2"
4141
CPP2_UFCS(insert, vec, CPP2_UFCS_0(begin, vec) + where, val);
4242
}
4343

regression-tests/test-results/mixed-intro-example-three-loops.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ auto main() -> int {
3535
decorate_and_print(word);
3636
}
3737
}
38+

regression-tests/test-results/mixed-postexpression-with-capture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ auto insert_at(cpp2::in<int> where, cpp2::in<int> val) -> void
3232
auto post_16_5 = cpp2::finally_success([_0 = CPP2_UFCS_0(size, vec)]{cpp2::Default.expects(CPP2_UFCS_0(size, vec)==_0 + 1, "");} );
3333
#line 16 "mixed-postexpression-with-capture.cpp2"
3434

35-
35+
#line 18 "mixed-postexpression-with-capture.cpp2"
3636
CPP2_UFCS(push_back, vec, val);
3737
}
3838

regression-tests/test-results/pure2-forward-return.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
cpp2::Bounds.expects(!(std::empty(rng)), "");
2121
#line 3 "pure2-forward-return.cpp2"
2222

23-
23+
#line 5 "pure2-forward-return.cpp2"
2424
return *cpp2::assert_not_null(std::begin(CPP2_FORWARD(rng))); }
2525

2626
int const global {42};

regression-tests/test-results/pure2-types-basics.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
#line 2 "pure2-types-basics.cpp2"
88
namespace N {
9+
910
class myclass;
11+
12+
#line 60 "pure2-types-basics.cpp2"
1013
};
1114
#line 62 "pure2-types-basics.cpp2"
1215
auto main() -> int;
@@ -33,7 +36,7 @@ class myclass {
3336
more = std::to_string(42 * 12);
3437
#line 7 "pure2-types-basics.cpp2"
3538

36-
39+
#line 9 "pure2-types-basics.cpp2"
3740
std::cout << "myclass: implicit from int\n";
3841
print();
3942
return *this;
@@ -55,7 +58,7 @@ class myclass {
5558
more = s;
5659
#line 14 "pure2-types-basics.cpp2"
5760

58-
61+
#line 16 "pure2-types-basics.cpp2"
5962
std::cout << "myclass: explicit from string\n";
6063
print();
6164
return *this;

source/cppfront.cpp

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,12 @@ class positional_printer
195195
std::string cpp2_filename = {};
196196
std::string cpp1_filename = {};
197197
std::vector<comment> const* pcomments = {}; // Cpp2 comments data
198+
cpp2::parser const* pparser = {};
198199

199200
source_position curr_pos = {}; // current (line,col) in output
200201
int next_comment = 0; // index of the next comment not yet printed
202+
bool last_was_empty = false;
203+
int empty_lines_suppressed = 0;
201204
bool last_was_cpp2 = false;
202205
bool source_has_cpp2 = false;
203206

@@ -234,6 +237,7 @@ class positional_printer
234237
break;case phase1_type_defs_func_decls: phase = phase2_func_defs;
235238
break;default : assert(!"ICE: invalid lowering phase");
236239
}
240+
next_comment = 0; // start over with the comments
237241
}
238242

239243
std::vector<std::string*> emit_string_targets; // option to emit to string instead of out file
@@ -274,6 +278,35 @@ class positional_printer
274278
// Otherwise, we'll actually print the string to the output file
275279
// and update our curr_pos position
276280

281+
// Reject consecutive empty lines: If this line is empty
282+
if (
283+
s == "\n"
284+
&& curr_pos.colno <= 1
285+
)
286+
{
287+
// And so was the last one, update logical position only
288+
// and increment empty_lines_suppressed instead of printing
289+
if (last_was_empty) {
290+
++curr_pos.lineno;
291+
curr_pos.colno = 1;
292+
++empty_lines_suppressed;
293+
return;
294+
}
295+
// If this is the first consecutive empty, remember and continue
296+
last_was_empty = true;
297+
}
298+
// Otherwise, if this line is not empty
299+
else {
300+
// Remember that this line was not empty
301+
last_was_empty = false;
302+
303+
// And if we did suppress any empties, emit a #line to resync
304+
if (empty_lines_suppressed > 0) {
305+
print_line_directive(curr_pos.lineno);
306+
empty_lines_suppressed = 0;
307+
}
308+
}
309+
277310
// Output the string
278311
assert (out);
279312
*out << s;
@@ -345,41 +378,38 @@ class positional_printer
345378

346379
// For convenience
347380
auto& comments = *pcomments;
348-
349-
// Don't emit comments while in the first declarations-only pass
350-
if (phase < phase2_func_defs) {
351-
return;
352-
}
353-
381+
354382
// Add unprinted comments and blank lines as needed to catch up vertically
355383
//
356384
while (curr_pos.lineno < pos.lineno)
357385
{
358386
// If a comment goes on this line, print it
359-
assert(
360-
next_comment >= std::ssize(comments)
361-
|| comments[next_comment].start.lineno >= curr_pos.lineno
362-
);
363387
if (
364388
next_comment < std::ssize(comments)
365389
&& comments[next_comment].start.lineno == curr_pos.lineno
366390
)
367391
{
368-
// For a line comment, start it at the right indentation and print it
369-
// with a newline end
370-
if (comments[next_comment].kind == comment::comment_kind::line_comment) {
371-
print( pad( comments[next_comment].start.colno - curr_pos.colno + 1 ) );
372-
print( comments[next_comment].text );
373-
assert( comments[next_comment].text.find("\n") == std::string::npos ); // we shouldn't have newlines
374-
print("\n");
375-
}
392+
assert(pparser);
393+
if (
394+
phase == phase2_func_defs
395+
)
396+
{
397+
// For a line comment, start it at the right indentation and print it
398+
// with a newline end
399+
if (comments[next_comment].kind == comment::comment_kind::line_comment) {
400+
print( pad( comments[next_comment].start.colno - curr_pos.colno + 1 ) );
401+
print( comments[next_comment].text );
402+
assert( comments[next_comment].text.find("\n") == std::string::npos ); // we shouldn't have newlines
403+
print("\n");
404+
}
376405

377-
// For a stream comment, pad out to its column (if we haven't passed it already)
378-
// and emit it there
379-
else {
380-
print( pad( comments[next_comment].start.colno - curr_pos.colno ) );
381-
print( comments[next_comment].text );
382-
assert(curr_pos.lineno <= pos.lineno); // we shouldn't have overshot
406+
// For a stream comment, pad out to its column (if we haven't passed it already)
407+
// and emit it there
408+
else {
409+
print( pad( comments[next_comment].start.colno - curr_pos.colno ) );
410+
print( comments[next_comment].text );
411+
assert(curr_pos.lineno <= pos.lineno); // we shouldn't have overshot
412+
}
383413
}
384414

385415
++next_comment;
@@ -410,11 +440,20 @@ class positional_printer
410440
// Catch up with displaying comments
411441
flush_comments( pos );
412442

413-
// If we're not on the right line, move forward one line
414-
if (curr_pos.lineno < pos.lineno) {
415-
ensure_at_start_of_new_line();
416-
assert (curr_pos.lineno <= pos.lineno);
417-
curr_pos.lineno = pos.lineno; // re-sync
443+
// If we're not on the right line
444+
if (curr_pos.lineno < pos.lineno)
445+
{
446+
// If we're just one away, try to move forward one line
447+
// Note: If there are consecutive blank lines, this might
448+
// get ignored, so we will repeat the "!= lineno" again...
449+
if (curr_pos.lineno == pos.lineno - 1) {
450+
print( "\n" );
451+
}
452+
// ...here:
453+
if (curr_pos.lineno != pos.lineno) {
454+
print_line_directive(pos.lineno);
455+
}
456+
curr_pos.lineno = pos.lineno;
418457
}
419458

420459
// Finally, align to the target column
@@ -454,6 +493,7 @@ class positional_printer
454493
std::string cpp2_filename_,
455494
std::string cpp1_filename_,
456495
std::vector<comment> const& comments,
496+
cpp2::parser const& parser,
457497
bool has_cpp2
458498
)
459499
-> void
@@ -474,6 +514,7 @@ class positional_printer
474514
out = &out_file;
475515
}
476516
pcomments = &comments;
517+
pparser = &parser;
477518
}
478519

479520
auto reopen()
@@ -575,7 +616,7 @@ class positional_printer
575616
);
576617

577618
// Because the blank/comment lines before a Cpp2 code section are part
578-
// of the Cpp2 section, and not printed in thedeclarations-only pass
619+
// of the Cpp2 section, and not printed in the pre-function-definitions passes
579620
if (
580621
!last_was_cpp2
581622
&& phase < phase2_func_defs
@@ -587,14 +628,14 @@ class positional_printer
587628
// Keep track of whether the last thing we printed was Cpp2
588629
last_was_cpp2 = true;
589630

590-
reset_cpp2_line_to(line);
631+
reset_line_to(line);
591632
}
592633

593634
//-----------------------------------------------------------------------
594635
// Used when we start a new Cpp2 section, or when we emit the same item
595636
// more than once (notably when we emit operator= more than once)
596637
//
597-
auto reset_cpp2_line_to(lineno_t line)
638+
auto reset_line_to(lineno_t line)
598639
-> void
599640
{
600641
// Always start a Cpp2 section on its own new line
@@ -1050,6 +1091,7 @@ class cppfront
10501091
sourcefile,
10511092
cpp1_filename,
10521093
tokens.get_comments(),
1094+
parser,
10531095
source.has_cpp2()
10541096
);
10551097
if (!printer.is_open()) {
@@ -1171,6 +1213,7 @@ class cppfront
11711213

11721214
// Treat each declaration as the start of a Cpp2 section (so we get #line)
11731215
printer.start_cpp2( decl->position().lineno );
1216+
// printer.start_cpp2( map_iter->first /*lineno*/);
11741217
emit(*decl);
11751218
}
11761219
++map_iter;
@@ -1232,9 +1275,6 @@ class cppfront
12321275
assert (!section.second.empty());
12331276

12341277
// Tell the printer that we're starting a Cpp2 section
1235-
// This time, we use the actual first start line of the section which includes
1236-
// the blank/comment lines at the beginning if any (whereas in the first pass
1237-
// we used the line of the first Cpp2 token in the section to skip blanks/comments)
12381278
printer.start_cpp2( section.first /*lineno*/);
12391279

12401280
// Get the parse tree for this section and emit each forward declaration
@@ -4246,8 +4286,8 @@ class cppfront
42464286
)
42474287
-> void
42484288
{
4249-
auto need_to_generate_assignment = false;
4250-
auto need_to_generate_move = false;
4289+
auto need_to_generate_assignment = false;
4290+
auto need_to_generate_move = false;
42514291

42524292
if (
42534293
n.is_function()
@@ -4811,7 +4851,7 @@ class cppfront
48114851
}
48124852

48134853
// Then reposition and do the recursive call
4814-
printer.reset_cpp2_line_to(n.position().lineno);
4854+
printer.reset_line_to(n.position().lineno);
48154855
generating_assignment_from = &n;
48164856
emit( n, capture_intro );
48174857
generating_assignment_from = {};
@@ -4827,7 +4867,7 @@ class cppfront
48274867
}
48284868

48294869
// Then reposition and do the recursive call
4830-
printer.reset_cpp2_line_to(n.position().lineno);
4870+
printer.reset_line_to(n.position().lineno);
48314871
generating_move_from = &n;
48324872
emit( n, capture_intro );
48334873
generating_move_from = {};
@@ -4900,18 +4940,17 @@ class cppfront
49004940
//
49014941
if (source_loaded)
49024942
{
4903-
auto out_source = std::ofstream{ sourcefile+"-source" };
4904-
source.debug_print( out_source );
4943+
auto out_source = std::ofstream{ sourcefile+"-source" };
4944+
source.debug_print( out_source );
49054945

4906-
auto out_tokens = std::ofstream{ sourcefile+"-tokens" };
4907-
tokens.debug_print( out_tokens );
4946+
auto out_tokens = std::ofstream{ sourcefile+"-tokens" };
4947+
tokens.debug_print( out_tokens );
49084948

4909-
auto out_parse = std::ofstream{ sourcefile+"-parse" };
4910-
auto tree_printer = parse_tree_printer{out_parse };
4911-
parser.visit ( tree_printer );
4949+
auto out_parse = std::ofstream{ sourcefile+"-parse" };
4950+
parser.debug_print( out_parse );
49124951

4913-
auto out_symbols = std::ofstream{ sourcefile+"-symbols" };
4914-
sema.debug_print ( out_symbols );
4952+
auto out_symbols = std::ofstream{ sourcefile+"-symbols" };
4953+
sema.debug_print ( out_symbols );
49154954
}
49164955
}
49174956

source/parse.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,11 +2293,11 @@ class parser
22932293

22942294
function_body_extent( lineno_t f, lineno_t l ): first{f}, last{l} { }
22952295
};
2296-
std::vector<function_body_extent> function_body_extents;
2297-
bool is_function_body_extents_sorted = false;
2296+
mutable std::vector<function_body_extent> function_body_extents;
2297+
mutable bool is_function_body_extents_sorted = false;
22982298

22992299
public:
2300-
auto is_within_function_body(source_position p)
2300+
auto is_within_function_body(source_position p) const
23012301
{
23022302
// Ensure we are sorted
23032303
if (!is_function_body_extents_sorted) {
@@ -5293,6 +5293,12 @@ class parser
52935293
return n;
52945294
}
52955295

5296+
public:
5297+
//-----------------------------------------------------------------------
5298+
// debug_print
5299+
//
5300+
auto debug_print(std::ostream& o)
5301+
-> void;
52965302
};
52975303

52985304

@@ -5582,6 +5588,23 @@ class parse_tree_printer : printing_visitor
55825588
};
55835589

55845590

5591+
auto parser::debug_print(std::ostream& o)
5592+
5593+
-> void
5594+
{
5595+
o << "\n\n--- Parse tree\n";
5596+
5597+
auto tree_printer = parse_tree_printer{o};
5598+
visit( tree_printer );
5599+
5600+
o << "\n\n--- Function body extents\n";
5601+
5602+
for (auto const& f : function_body_extents) {
5603+
o << " " << f.first << "-" << f.last << "\n";
5604+
}
5605+
}
5606+
5607+
55855608
//-----------------------------------------------------------------------
55865609
//
55875610
// Visitor for moving tokens that are to the right on the same line

0 commit comments

Comments
 (0)