@@ -133,6 +133,14 @@ static cmdline_processor::register_flag cmd_verbose(
133
133
[]{ flag_verbose = true ; }
134
134
);
135
135
136
+ static auto flag_split_header_file = false ;
137
+ static cmdline_processor::register_flag cmd_split_header_file (
138
+ 9 ,
139
+ " split-header-file" ,
140
+ " Split .cpp2 to .h and .cpp" ,
141
+ []{ flag_split_header_file = true ; }
142
+ );
143
+
136
144
static auto flag_no_exceptions = false ;
137
145
static cmdline_processor::register_flag cmd_no_exceptions (
138
146
4 ,
@@ -635,6 +643,17 @@ class positional_printer
635
643
out_file.open (cpp1_filename + " pp" );
636
644
}
637
645
646
+ auto reopen (std::string new_filename)
647
+ -> void
648
+ {
649
+ assert (
650
+ is_open ()
651
+ && " ICE: tried to call .reopen without first calling .open"
652
+ );
653
+ out_file.close ();
654
+ out_file.open (new_filename);
655
+ }
656
+
638
657
auto is_open ()
639
658
-> bool
640
659
{
@@ -1202,21 +1221,23 @@ class cppfront
1202
1221
1203
1222
// Now we'll open the Cpp1 file
1204
1223
auto cpp1_filename = sourcefile.substr (0 , std::ssize (sourcefile) - 1 );
1224
+ auto h1_filename = cpp1_filename.substr (0 , std::ssize (sourcefile) - 5 ) + " .h" ;
1205
1225
if (!flag_cpp1_filename.empty ()) {
1206
1226
cpp1_filename = flag_cpp1_filename; // use override if present
1207
1227
}
1228
+ auto out_filename = flag_split_header_file ? h1_filename : cpp1_filename;
1208
1229
1209
1230
printer.open (
1210
1231
sourcefile,
1211
- cpp1_filename ,
1232
+ out_filename ,
1212
1233
tokens.get_comments (),
1213
1234
source,
1214
1235
parser
1215
1236
);
1216
1237
if (!printer.is_open ()) {
1217
1238
errors.emplace_back (
1218
1239
source_position{},
1219
- " could not open output file " + cpp1_filename
1240
+ " could not open output file " + out_filename
1220
1241
);
1221
1242
return {};
1222
1243
}
@@ -1233,7 +1254,7 @@ class cppfront
1233
1254
if (source.has_cpp2 ())
1234
1255
{
1235
1256
printer.print_extra ( " \n " );
1236
- if (cpp1_filename.back () == ' h' ) {
1257
+ if (cpp1_filename.back () == ' h' || flag_split_header_file ) {
1237
1258
printer.print_extra ( " #ifndef " + cpp1_FILENAME+" _CPP2\n " );
1238
1259
printer.print_extra ( " #define " + cpp1_FILENAME+" _CPP2" + " \n\n " );
1239
1260
}
@@ -1439,6 +1460,12 @@ class cppfront
1439
1460
printer.finalize_phase ();
1440
1461
printer.next_phase ();
1441
1462
1463
+ if (flag_split_header_file) {
1464
+ printer.print_extra ( " \n #endif\n " );
1465
+ printer.reopen (cpp1_filename);
1466
+ printer.print_extra ( " \n #include \" " + h1_filename + " \"\n\n " );
1467
+ }
1468
+
1442
1469
if (!flag_clean_cpp1) {
1443
1470
printer.print_extra ( " \n //=== Cpp2 function definitions =================================================\n\n " );
1444
1471
}
0 commit comments