@@ -110,7 +110,7 @@ static auto flag_cpp1_filename = std::string{};
110
110
static cmdline_processor::register_flag cmd_cpp1_filename (
111
111
9 ,
112
112
" output" ,
113
- " Output filename, or 'stdout' (default is *.cpp)" ,
113
+ " Output filename, or 'stdout' (default is *.cpp/*.h )" ,
114
114
nullptr ,
115
115
[](std::string const & name) { flag_cpp1_filename = name; }
116
116
);
@@ -423,6 +423,16 @@ class positional_printer
423
423
pcomments = &comments;
424
424
}
425
425
426
+ auto reopen () -> void
427
+ {
428
+ assert (is_open () && " ICE: tried to call .reopen without first calling .open" );
429
+ if (cpp1_filename.ends_with (" .h" ))
430
+ {
431
+ out_file.close ();
432
+ out_file.open (cpp1_filename + " pp" );
433
+ }
434
+ }
435
+
426
436
auto is_open () -> bool {
427
437
if (out) {
428
438
assert (pcomments && " ICE: if is_open, pcomments should also be set" );
@@ -753,11 +763,11 @@ class cppfront
753
763
// "Constraints enable creativity in the right directions"
754
764
// sort of applies here
755
765
//
756
- if (!sourcefile.ends_with (" .cpp2" ))
766
+ if (!sourcefile.ends_with (" .cpp2" ) && !sourcefile. ends_with ( " .h2 " ) )
757
767
{
758
768
errors.emplace_back (
759
769
source_position (-1 , -1 ),
760
- " source filename must end with .cpp2: " + sourcefile
770
+ " source filename must end with .cpp2 or .h2 : " + sourcefile
761
771
);
762
772
}
763
773
@@ -812,7 +822,7 @@ class cppfront
812
822
// -----------------------------------------------------------------------
813
823
// lower_to_cpp1
814
824
//
815
- // Emits the target file with the last '2' stripped -> .cpp
825
+ // Emits the target file with the last '2' stripped
816
826
//
817
827
auto lower_to_cpp1 () -> void
818
828
{
@@ -821,7 +831,7 @@ class cppfront
821
831
return ;
822
832
}
823
833
824
- // Now we'll open the .cpp file
834
+ // Now we'll open the Cpp1 file
825
835
auto cpp1_filename = sourcefile.substr (0 , std::ssize (sourcefile) - 1 );
826
836
if (!flag_cpp1_filename.empty ()) {
827
837
cpp1_filename = flag_cpp1_filename; // use override if present
@@ -862,6 +872,7 @@ class cppfront
862
872
}
863
873
864
874
auto map_iter = tokens.get_map ().cbegin ();
875
+ auto hpp_includes = std::string{};
865
876
866
877
// First, echo the non-Cpp2 parts
867
878
//
@@ -899,7 +910,15 @@ class cppfront
899
910
return ;
900
911
}
901
912
902
- printer.print_cpp1 ( line.text , curr_lineno );
913
+ if (line.cat == source_line::category::preprocessor && line.text .ends_with (" .h2\" " )) {
914
+ // Strip off the 2"
915
+ auto h_include = line.text .substr (0 , line.text .size ()-2 );
916
+ printer.print_cpp1 ( h_include + " \" " , curr_lineno );
917
+ hpp_includes += h_include + " pp\"\n " ;
918
+ }
919
+ else {
920
+ printer.print_cpp1 ( line.text , curr_lineno );
921
+ }
903
922
}
904
923
905
924
// If it's a Cpp2 line...
@@ -939,6 +958,16 @@ class cppfront
939
958
}
940
959
941
960
// If there is Cpp2 code, we have more to do...
961
+ // First, if this is a .h2 we need to switch filenames
962
+ printer.reopen ();
963
+ if (!printer.is_open ()) {
964
+ errors.emplace_back (
965
+ source_position{},
966
+ " could not open second output file " + cpp1_filename
967
+ );
968
+ return ;
969
+ }
970
+
942
971
// Next, bring in the Cpp2 helpers
943
972
//
944
973
if (!flag_clean_cpp1) {
@@ -947,6 +976,8 @@ class cppfront
947
976
948
977
// Next, emit the Cpp2 definitions
949
978
//
979
+ printer.print_extra ( hpp_includes );
980
+
950
981
printer.enable_definitions ();
951
982
952
983
for (auto & section : tokens.get_map ())
@@ -2911,7 +2942,7 @@ auto main(int argc, char* argv[]) -> int
2911
2942
return EXIT_FAILURE;
2912
2943
}
2913
2944
2914
- // For each .cpp2 source file
2945
+ // For each Cpp2 source file
2915
2946
int exit_status = EXIT_SUCCESS;
2916
2947
for (auto const & arg : cmdline.arguments ())
2917
2948
{
0 commit comments