@@ -346,7 +346,7 @@ auto starts_with_identifier_colon(std::string const& line)
346
346
// braces_tracker: to track brace depth
347
347
//
348
348
// Normally we don't emit diagnostics for Cpp1 code, but we do for a
349
- // brace mismatch since we're relying on balanced { } to find Cpp2 code
349
+ // brace mismatch since we're relying on balanced {() } to find Cpp2 code
350
350
//
351
351
class braces_tracker
352
352
{
@@ -391,6 +391,7 @@ class braces_tracker
391
391
}
392
392
};
393
393
std::vector<pre_if_depth_info> preprocessor = { {} }; // sentinel
394
+ char current_open_type = ' ' ;
394
395
std::vector<lineno_t > open_braces;
395
396
std::vector<error_entry>& errors;
396
397
@@ -399,28 +400,39 @@ class braces_tracker
399
400
: errors{errors}
400
401
{ }
401
402
402
- // --- Brace matching functions - { and }
403
+ // --- Brace matching functions - { and }, or ( and )
403
404
404
- auto found_open_brace (lineno_t lineno) -> void {
405
+ auto found_open_brace (lineno_t lineno, char brace ) -> void {
405
406
assert (std::ssize (preprocessor) > 0 );
406
- open_braces.push_back (lineno);
407
- preprocessor.back ().found_open_brace ();
407
+ if (open_braces.empty ()) {
408
+ current_open_type = brace;
409
+ }
410
+ if (current_open_type == brace) {
411
+ open_braces.push_back (lineno);
412
+ preprocessor.back ().found_open_brace ();
413
+ }
408
414
}
409
415
410
- auto found_close_brace (source_position pos) -> void {
416
+ auto found_close_brace (source_position pos, char brace ) -> void {
411
417
assert (std::ssize (preprocessor) > 0 );
412
418
413
- if (std::ssize (open_braces) < 1 ) {
414
- errors.emplace_back (
415
- pos,
416
- " closing } does not match a prior {"
417
- );
418
- }
419
- else {
420
- open_braces.pop_back ();
421
- }
419
+ if (
420
+ (current_open_type == ' {' && brace == ' }' )
421
+ || (current_open_type == ' (' && brace == ' )' )
422
+ )
423
+ {
424
+ if (std::ssize (open_braces) < 1 ) {
425
+ errors.emplace_back (
426
+ pos,
427
+ " closing } does not match a prior {"
428
+ );
429
+ }
430
+ else {
431
+ open_braces.pop_back ();
432
+ }
422
433
423
- preprocessor.back ().found_close_brace ();
434
+ preprocessor.back ().found_close_brace ();
435
+ }
424
436
}
425
437
426
438
auto found_eof (source_position pos) const -> void {
@@ -471,7 +483,7 @@ class braces_tracker
471
483
// braces_to_ignore() will be the positive number of those net open braces
472
484
// that this loop will now throw away
473
485
for (auto i = 0 ; i < preprocessor.back ().braces_to_ignore (); ++i) {
474
- found_close_brace ( source_position{} );
486
+ found_close_brace ( source_position{}, current_open_type == ' { ' ? ' } ' : ' ) ' );
475
487
}
476
488
477
489
preprocessor.pop_back ();
@@ -638,12 +650,12 @@ auto process_cpp_line(
638
650
639
651
break ;case ' {' :
640
652
if (!in_literal ()) {
641
- braces.found_open_brace (lineno);
653
+ braces.found_open_brace (lineno, ' { ' );
642
654
}
643
655
644
656
break ;case ' }' :
645
657
if (!in_literal ()) {
646
- braces.found_close_brace (source_position (lineno, i));
658
+ braces.found_close_brace (source_position (lineno, i), ' } ' );
647
659
}
648
660
649
661
break ;case ' *' :
@@ -703,34 +715,45 @@ auto process_cpp2_line(
703
715
704
716
auto prev = ' ' ;
705
717
auto in_string_literal = false ;
718
+ auto in_char_literal = false ;
706
719
707
- for (auto i = colno_t {0 }; i < ssize (line); ++i) {
708
-
709
- if (in_comment) {
720
+ for (auto i = colno_t {0 }; i < ssize (line); ++i)
721
+ {
722
+ if (in_comment)
723
+ {
710
724
switch (line[i]) {
711
725
break ;case ' /' : if (prev == ' *' ) { in_comment = false ; }
712
726
break ;default : ;
713
727
}
714
- } else if (in_string_literal) {
728
+ }
729
+ else if (in_string_literal)
730
+ {
715
731
switch (line[i]) {
716
732
break ;case ' "' : if (prev != ' \\ ' ) { in_string_literal = false ; }
717
733
break ;default : ;
718
734
}
719
735
}
720
-
721
- else {
736
+ else if (in_char_literal)
737
+ {
722
738
switch (line[i]) {
739
+ break ;case ' \' ' : if (prev != ' \\ ' ) { in_char_literal = false ; }
740
+ break ;default : ;
741
+ }
742
+ }
743
+ else
744
+ {
745
+ switch (line[i])
746
+ {
747
+ // For finding Cpp2 definition endings, count () as {}
723
748
break ;case ' {' :
724
- if (prev != ' \' ' ) { // ignore character literals
725
- braces.found_open_brace (lineno);
726
- }
749
+ case ' (' :
750
+ braces.found_open_brace ( lineno, line[i] );
727
751
728
752
break ;case ' }' :
729
- if (prev != ' \' ' ) { // ignore character literals
730
- braces.found_close_brace ( source_position (lineno, i) );
731
- if (braces.current_depth () < 1 ) {
732
- found_end = true ;
733
- }
753
+ case ' )' :
754
+ braces.found_close_brace ( source_position (lineno, i), line[i]);
755
+ if (braces.current_depth () < 1 && line[i] != ' )' ) {
756
+ found_end = true ;
734
757
}
735
758
736
759
break ;case ' ;' :
@@ -755,6 +778,9 @@ auto process_cpp2_line(
755
778
break ;case ' "' :
756
779
if (prev != ' \\ ' ) { in_string_literal = true ; }
757
780
781
+ break ;case ' \' ' :
782
+ if (prev != ' \\ ' ) { in_char_literal = true ; }
783
+
758
784
break ;default : ;
759
785
}
760
786
}
0 commit comments