You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Both the previous commit to support `.h2` files, and this commit for some instrumentation, are with a view to self-hosting cppfront itself...
The `.h2` file support lets us keep the current header file structure that keeps the phases of compilation nicely separate (I'd love to use modules but not all compilers support modules yet)
This line count support will be useful as I start to move cppfront code to Cpp2, so I know (and can report) what percentage of the project has moved to Cpp2
I expect both to be generally useful if people want to try moving some small projects to Cpp2
// Emits the target file with the last '2' stripped
826
834
//
827
-
autolower_to_cpp1() -> void
835
+
structlower_to_cpp1_ret {
836
+
lineno_t cpp1_lines = 0;
837
+
lineno_t cpp2_lines = 0;
838
+
};
839
+
autolower_to_cpp1() -> lower_to_cpp1_ret
828
840
{
841
+
auto ret = lower_to_cpp1_ret{};
842
+
829
843
// Only lower to Cpp1 if we haven't already encountered errors
830
844
if (!errors.empty()) {
831
-
return;
845
+
return {};
832
846
}
833
847
834
848
// Now we'll open the Cpp1 file
@@ -847,7 +861,7 @@ class cppfront
847
861
source_position{},
848
862
"could not open output file " + cpp1_filename
849
863
);
850
-
return;
864
+
return {};
851
865
}
852
866
853
867
// Only emit extra lines if we actually have Cpp2, because
@@ -876,8 +890,6 @@ class cppfront
876
890
877
891
// First, echo the non-Cpp2 parts
878
892
//
879
-
auto cpp2_found = false;
880
-
881
893
for (
882
894
lineno_t curr_lineno = 0;
883
895
autoconst& line : source.get_lines()
@@ -889,25 +901,29 @@ class cppfront
889
901
// If it's a Cpp1 line, emit it
890
902
if (line.cat != source_line::category::cpp2)
891
903
{
904
+
++ret.cpp1_lines;
905
+
892
906
if (flag_cpp2_only &&
893
907
!line.text.empty() &&
894
908
line.cat != source_line::category::comment &&
895
909
line.cat != source_line::category::import
896
910
)
897
911
{
898
912
if (line.cat == source_line::category::preprocessor) {
899
-
errors.emplace_back(
900
-
source_position(curr_lineno, 1),
901
-
"pure-cpp2 switch disables the preprocessor, including #include - use import instead (note: 'import std;' is implicit in -pure-cpp2)"
902
-
);
913
+
if (!line.text.ends_with(".h2\"")) {
914
+
errors.emplace_back(
915
+
source_position(curr_lineno, 1),
916
+
"pure-cpp2 switch disables the preprocessor, including #include (except of .h2 files) - use import instead (note: 'import std;' is implicit in -pure-cpp2)"
917
+
);
918
+
}
903
919
}
904
920
else {
905
921
errors.emplace_back(
906
922
source_position(curr_lineno, 1),
907
923
"pure-cpp2 switch disables Cpp1 syntax"
908
924
);
909
925
}
910
-
return;
926
+
return {};
911
927
}
912
928
913
929
if (line.cat == source_line::category::preprocessor && line.text.ends_with(".h2\"")) {
@@ -923,7 +939,7 @@ class cppfront
923
939
924
940
// If it's a Cpp2 line...
925
941
else {
926
-
cpp2_found = true;
942
+
++ret.cpp2_lines;
927
943
928
944
// We should be in a position to emit a set of Cpp2 declarations
929
945
if (map_iter != tokens.get_map().cend() && map_iter->first/*line*/ <= curr_lineno)
@@ -953,8 +969,8 @@ class cppfront
953
969
// that we didn't misidentify anything as Cpp2 (even in the
954
970
// presence of nonstandard vendor extensions)
955
971
//
956
-
if (!cpp2_found) {
957
-
return;
972
+
if (ret.cpp2_lines == 0) {
973
+
return ret;
958
974
}
959
975
960
976
// If there is Cpp2 code, we have more to do...
@@ -965,7 +981,7 @@ class cppfront
965
981
source_position{},
966
982
"could not open second output file " + cpp1_filename
967
983
);
968
-
return;
984
+
return {};
969
985
}
970
986
971
987
// Next, bring in the Cpp2 helpers
@@ -997,6 +1013,8 @@ class cppfront
997
1013
emit(*decl);
998
1014
}
999
1015
}
1016
+
1017
+
return ret;
1000
1018
}
1001
1019
1002
1020
@@ -2938,7 +2956,7 @@ auto main(int argc, char* argv[]) -> int
2938
2956
}
2939
2957
2940
2958
if (cmdline.arguments().empty()) {
2941
-
std::cerr << "cppfront: error: no input files\n";
2959
+
std::cerr << "cppfront: error: no input files (try -help)\n";
2942
2960
return EXIT_FAILURE;
2943
2961
}
2944
2962
@@ -2952,22 +2970,35 @@ auto main(int argc, char* argv[]) -> int
2952
2970
cppfront c(arg.text);
2953
2971
2954
2972
// Generate Cpp1 (this may catch additional late errors)
2955
-
c.lower_to_cpp1();
2973
+
auto count = c.lower_to_cpp1();
2956
2974
2957
2975
// If there were no errors, say so and generate Cpp1
2958
-
if (c.had_no_errors()) {
2976
+
if (c.had_no_errors())
2977
+
{
2959
2978
if (!c.has_cpp1()) {
2960
-
std::cout << " ok (all Cpp2, passes safety checks)\n\n";
2979
+
std::cout << " ok (all Cpp2, passes safety checks)\n";
0 commit comments