Skip to content

Commit fab2f5f

Browse files
committed
Add -verbose to print Cpp1/Cpp2 line counts
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
1 parent 347c1c2 commit fab2f5f

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

regression-tests/test-results/run-tests.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ echo.
2323
echo Done: %count% .cpp2 tests compiled
2424
echo.
2525
echo. %cpp_count% .cpp files generated
26-
echo. %err_count% error test cases (should not generate .cpp)
26+
echo. %err_count% error test cases (should not generate .cpp)
2727
echo. %total_count% total
2828
if %total_count% NEQ %count% (
2929
echo. *** MISMATCH: should equal total tests run

source/cppfront.cpp

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ static cmdline_processor::register_flag cmd_print_colon_errors(
123123
[]{ flag_print_colon_errors = true; }
124124
);
125125

126+
static auto flag_verbose = false;
127+
static cmdline_processor::register_flag cmd_verbose(
128+
9,
129+
"verbose",
130+
"Print verbose statistics",
131+
[]{ flag_verbose = true; }
132+
);
133+
126134
static auto flag_no_exceptions = false;
127135
static cmdline_processor::register_flag cmd_no_exceptions(
128136
4,
@@ -824,11 +832,17 @@ class cppfront
824832
//
825833
// Emits the target file with the last '2' stripped
826834
//
827-
auto lower_to_cpp1() -> void
835+
struct lower_to_cpp1_ret {
836+
lineno_t cpp1_lines = 0;
837+
lineno_t cpp2_lines = 0;
838+
};
839+
auto lower_to_cpp1() -> lower_to_cpp1_ret
828840
{
841+
auto ret = lower_to_cpp1_ret{};
842+
829843
// Only lower to Cpp1 if we haven't already encountered errors
830844
if (!errors.empty()) {
831-
return;
845+
return {};
832846
}
833847

834848
// Now we'll open the Cpp1 file
@@ -847,7 +861,7 @@ class cppfront
847861
source_position{},
848862
"could not open output file " + cpp1_filename
849863
);
850-
return;
864+
return {};
851865
}
852866

853867
// Only emit extra lines if we actually have Cpp2, because
@@ -876,8 +890,6 @@ class cppfront
876890

877891
// First, echo the non-Cpp2 parts
878892
//
879-
auto cpp2_found = false;
880-
881893
for (
882894
lineno_t curr_lineno = 0;
883895
auto const& line : source.get_lines()
@@ -889,25 +901,29 @@ class cppfront
889901
// If it's a Cpp1 line, emit it
890902
if (line.cat != source_line::category::cpp2)
891903
{
904+
++ret.cpp1_lines;
905+
892906
if (flag_cpp2_only &&
893907
!line.text.empty() &&
894908
line.cat != source_line::category::comment &&
895909
line.cat != source_line::category::import
896910
)
897911
{
898912
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+
}
903919
}
904920
else {
905921
errors.emplace_back(
906922
source_position(curr_lineno, 1),
907923
"pure-cpp2 switch disables Cpp1 syntax"
908924
);
909925
}
910-
return;
926+
return {};
911927
}
912928

913929
if (line.cat == source_line::category::preprocessor && line.text.ends_with(".h2\"")) {
@@ -923,7 +939,7 @@ class cppfront
923939

924940
// If it's a Cpp2 line...
925941
else {
926-
cpp2_found = true;
942+
++ret.cpp2_lines;
927943

928944
// We should be in a position to emit a set of Cpp2 declarations
929945
if (map_iter != tokens.get_map().cend() && map_iter->first /*line*/ <= curr_lineno)
@@ -953,8 +969,8 @@ class cppfront
953969
// that we didn't misidentify anything as Cpp2 (even in the
954970
// presence of nonstandard vendor extensions)
955971
//
956-
if (!cpp2_found) {
957-
return;
972+
if (ret.cpp2_lines == 0) {
973+
return ret;
958974
}
959975

960976
// If there is Cpp2 code, we have more to do...
@@ -965,7 +981,7 @@ class cppfront
965981
source_position{},
966982
"could not open second output file " + cpp1_filename
967983
);
968-
return;
984+
return {};
969985
}
970986

971987
// Next, bring in the Cpp2 helpers
@@ -997,6 +1013,8 @@ class cppfront
9971013
emit(*decl);
9981014
}
9991015
}
1016+
1017+
return ret;
10001018
}
10011019

10021020

@@ -2938,7 +2956,7 @@ auto main(int argc, char* argv[]) -> int
29382956
}
29392957

29402958
if (cmdline.arguments().empty()) {
2941-
std::cerr << "cppfront: error: no input files\n";
2959+
std::cerr << "cppfront: error: no input files (try -help)\n";
29422960
return EXIT_FAILURE;
29432961
}
29442962

@@ -2952,22 +2970,35 @@ auto main(int argc, char* argv[]) -> int
29522970
cppfront c(arg.text);
29532971

29542972
// Generate Cpp1 (this may catch additional late errors)
2955-
c.lower_to_cpp1();
2973+
auto count = c.lower_to_cpp1();
29562974

29572975
// If there were no errors, say so and generate Cpp1
2958-
if (c.had_no_errors()) {
2976+
if (c.had_no_errors())
2977+
{
29592978
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";
29612980
}
29622981
else if (c.has_cpp2()) {
2963-
std::cout << " ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)\n\n";
2982+
std::cout << " ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)\n";
29642983
}
29652984
else {
2966-
std::cout << " ok (all Cpp1)\n\n";
2985+
std::cout << " ok (all Cpp1)\n";
2986+
}
2987+
2988+
if (flag_verbose) {
2989+
std::cout << " Cpp1: " << count.cpp1_lines << " lines\n";
2990+
std::cout << " Cpp2: " << count.cpp2_lines << " lines";
2991+
if (count.cpp1_lines + count.cpp2_lines > 0) {
2992+
std::cout << " (" << 100 * count.cpp2_lines / (count.cpp1_lines + count.cpp2_lines) << "%)";
2993+
}
2994+
std::cout << "\n";
29672995
}
2996+
2997+
std::cout << "\n";
29682998
}
29692999
// Otherwise, print the errors
2970-
else {
3000+
else
3001+
{
29713002
std::cerr << "\n";
29723003
c.print_errors();
29733004
std::cerr << "\n";

0 commit comments

Comments
 (0)