Skip to content

Commit c1d3e0e

Browse files
committed
Fix handling of \\" sequences in literals, closes #813
And clean up a couple of stray `#line` directives we shouldn't add in a Cpp1-only source file that were added in the earlier commit today, this restores the `passthrough-tests` to passing with zero diffs
1 parent 5ea5d5c commit c1d3e0e

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#line 1 "mixed-allcpp1-hello.cpp2"
2-
#line 1 "mixed-allcpp1-hello.cpp2"
31

42
// Step 1: rename .cpp to .cpp2
53

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#line 1 "mixed-intro-example-three-loops.cpp2"
2-
#line 1 "mixed-intro-example-three-loops.cpp2"
31
#include <iostream>
42
#include <iomanip>
53
#include <vector>

source/io.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ auto process_cpp_line(
578578
struct process_line_ret r { in_comment, true , in_raw_string_literal};
579579

580580
auto prev = ' ';
581+
auto prev2 = ' ';
581582
for (auto i = colno_t{0}; i < ssize(line); ++i)
582583
{
583584
// Local helper functions for readability
@@ -640,7 +641,7 @@ auto process_cpp_line(
640641
// If this isn't an escaped quote, toggle string literal state
641642
if (
642643
!in_comment
643-
&& prev != '\\'
644+
&& (prev != '\\' || prev2 == '\\')
644645
&& (in_string_literal || prev != '\'')
645646
&& !in_raw_string_literal
646647
)
@@ -683,6 +684,7 @@ auto process_cpp_line(
683684
}
684685
}
685686

687+
prev2 = prev;
686688
prev = line[i];
687689
}
688690

@@ -714,6 +716,7 @@ auto process_cpp2_line(
714716
auto found_end = false;
715717

716718
auto prev = ' ';
719+
auto prev2 = ' ';
717720
auto in_string_literal = false;
718721
auto in_char_literal = false;
719722

@@ -729,14 +732,14 @@ auto process_cpp2_line(
729732
else if (in_string_literal)
730733
{
731734
switch (line[i]) {
732-
break;case '"': if (prev != '\\') { in_string_literal = false; }
735+
break;case '"': if (prev != '\\' || prev2 == '\\') { in_string_literal = false; }
733736
break;default: ;
734737
}
735738
}
736739
else if (in_char_literal)
737740
{
738741
switch (line[i]) {
739-
break;case '\'': if (prev != '\\') { in_char_literal = false; }
742+
break;case '\'': if (prev != '\\' || prev2 == '\\') { in_char_literal = false; }
740743
break;default: ;
741744
}
742745
}
@@ -776,15 +779,16 @@ auto process_cpp2_line(
776779
if (prev == '/') { in_comment = false; return found_end; }
777780

778781
break;case '"':
779-
if (prev != '\\') { in_string_literal = true; }
782+
if (prev != '\\' || prev2 == '\\') { in_string_literal = true; }
780783

781784
break;case '\'':
782-
if (prev != '\\') { in_char_literal = true; }
785+
if (prev != '\\' || prev2 == '\\') { in_char_literal = true; }
783786

784787
break;default: ;
785788
}
786789
}
787790

791+
prev2 = prev;
788792
prev = line[i];
789793
}
790794

source/to_cpp1.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,13 @@ class cppfront
13151315
printer.print_extra( "\n#include \"cpp2util.h\"\n\n" );
13161316
}
13171317

1318-
printer.reset_line_to(1, true);
1318+
if (
1319+
source.has_cpp2()
1320+
&& !flag_clean_cpp1
1321+
)
1322+
{
1323+
printer.reset_line_to(1, true);
1324+
}
13191325

13201326
for (auto& section : tokens.get_map())
13211327
{
@@ -1342,10 +1348,9 @@ class cppfront
13421348
)
13431349
{
13441350
printer.print_extra( "\n//=== Cpp2 type definitions and function declarations ===========================\n\n" );
1351+
printer.reset_line_to(1, true);
13451352
}
13461353

1347-
printer.reset_line_to(1, true);
1348-
13491354
assert (printer.get_phase() == positional_printer::phase1_type_defs_func_decls);
13501355
for (
13511356
lineno_t curr_lineno = 0;
@@ -1485,12 +1490,15 @@ class cppfront
14851490
printer.finalize_phase();
14861491
printer.next_phase();
14871492

1488-
if (!flag_clean_cpp1) {
1493+
if (
1494+
source.has_cpp2()
1495+
&& !flag_clean_cpp1
1496+
)
1497+
{
14891498
printer.print_extra( "\n//=== Cpp2 function definitions =================================================\n\n" );
1499+
printer.reset_line_to(1, true);
14901500
}
14911501

1492-
printer.reset_line_to(1, true);
1493-
14941502
for (auto& section : tokens.get_map())
14951503
{
14961504
assert (!section.second.empty());

0 commit comments

Comments
 (0)