Skip to content

Commit 7975ec7

Browse files
committed
Add -split-header-file flag to split .cpp2 to .h and .cpp
1 parent 69fdefe commit 7975ec7

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

source/cppfront.cpp

+30-3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ static cmdline_processor::register_flag cmd_verbose(
133133
[]{ flag_verbose = true; }
134134
);
135135

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+
136144
static auto flag_no_exceptions = false;
137145
static cmdline_processor::register_flag cmd_no_exceptions(
138146
4,
@@ -635,6 +643,17 @@ class positional_printer
635643
out_file.open(cpp1_filename + "pp");
636644
}
637645

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+
638657
auto is_open()
639658
-> bool
640659
{
@@ -1202,21 +1221,23 @@ class cppfront
12021221

12031222
// Now we'll open the Cpp1 file
12041223
auto cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1);
1224+
auto h1_filename = cpp1_filename.substr(0, std::ssize(sourcefile) - 5) + ".h";
12051225
if (!flag_cpp1_filename.empty()) {
12061226
cpp1_filename = flag_cpp1_filename; // use override if present
12071227
}
1228+
auto out_filename = flag_split_header_file ? h1_filename : cpp1_filename;
12081229

12091230
printer.open(
12101231
sourcefile,
1211-
cpp1_filename,
1232+
out_filename,
12121233
tokens.get_comments(),
12131234
source,
12141235
parser
12151236
);
12161237
if (!printer.is_open()) {
12171238
errors.emplace_back(
12181239
source_position{},
1219-
"could not open output file " + cpp1_filename
1240+
"could not open output file " + out_filename
12201241
);
12211242
return {};
12221243
}
@@ -1233,7 +1254,7 @@ class cppfront
12331254
if (source.has_cpp2())
12341255
{
12351256
printer.print_extra( "\n" );
1236-
if (cpp1_filename.back() == 'h') {
1257+
if (cpp1_filename.back() == 'h' || flag_split_header_file) {
12371258
printer.print_extra( "#ifndef " + cpp1_FILENAME+"_CPP2\n");
12381259
printer.print_extra( "#define " + cpp1_FILENAME+"_CPP2" + "\n\n" );
12391260
}
@@ -1439,6 +1460,12 @@ class cppfront
14391460
printer.finalize_phase();
14401461
printer.next_phase();
14411462

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+
14421469
if (!flag_clean_cpp1) {
14431470
printer.print_extra( "\n//=== Cpp2 function definitions =================================================\n\n" );
14441471
}

0 commit comments

Comments
 (0)