Skip to content

Commit e12e301

Browse files
committed
Add -quiet flag to only print output if there is an error
Closes #1046
1 parent 00d5047 commit e12e301

File tree

6 files changed

+136
-129
lines changed

6 files changed

+136
-129
lines changed

Diff for: include/cpp2util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ inline auto to_string(std::tuple<Ts...> const& t) -> std::string
11161116
#if defined(__cpp_lib_format) || (defined(_MSC_VER) && _MSC_VER >= 1929)
11171117
inline auto to_string(auto&& value, std::string_view fmt) -> std::string
11181118
{
1119-
return std::vformat(fmt, std::make_format_args(CPP2_FORWARD(value)));
1119+
return std::vformat(fmt, std::make_format_args(value));
11201120
}
11211121
#else
11221122
inline auto to_string(auto&& value, std::string_view) -> std::string

Diff for: regression-tests/test-results/gcc-13/gcc-version.output

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
g++ (GCC) 13.2.1 20231011 (Red Hat 13.2.1-4)
1+
g++ (GCC) 13.2.1 20240316 (Red Hat 13.2.1-7)
22
Copyright (C) 2023 Free Software Foundation, Inc.
33
This is free software; see the source for copying conditions. There is NO
44
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33522 for x86
1+
Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33523 for x86
22
Copyright (C) Microsoft Corporation. All rights reserved.
33

Diff for: source/common.h

+81-43
Original file line numberDiff line numberDiff line change
@@ -250,44 +250,6 @@ struct multiline_raw_string
250250
source_position end = {0, 0};
251251
};
252252

253-
//-----------------------------------------------------------------------
254-
//
255-
// error: represents a user-readable error message
256-
//
257-
//-----------------------------------------------------------------------
258-
//
259-
struct error_entry
260-
{
261-
source_position where;
262-
std::string msg;
263-
bool internal = false;
264-
bool fallback = false; // only emit this message if there was nothing better
265-
266-
error_entry(
267-
source_position w,
268-
std::string_view m,
269-
bool i = false,
270-
bool f = false
271-
)
272-
: where{w}
273-
, msg{m}
274-
, internal{i}
275-
, fallback{f}
276-
{ }
277-
278-
auto operator==(error_entry const& that)
279-
-> bool
280-
{
281-
return
282-
where == that.where
283-
&& msg == that.msg
284-
;
285-
}
286-
287-
auto print(auto& o, std::string const& file) const
288-
-> void;
289-
};
290-
291253

292254
//-----------------------------------------------------------------------
293255
//
@@ -639,13 +601,18 @@ class cmdline_processor
639601
std::unordered_map<int, std::string> labels = {
640602
{ 2, "Additional dynamic safety checks and contract information" },
641603
{ 4, "Support for constrained target environments" },
642-
{ 9, "Other options" }
604+
{ 8, "Cpp1 file emission options" },
605+
{ 9, "Cppfront output options" }
643606
};
644607

645-
// Define this in the main .cpp to avoid bringing <iostream> into the headers,
646-
// so that we can't accidentally start depending on iostreams in the compiler body
647-
static auto print(std::string_view, int width = 0)
648-
-> void;
608+
static auto print(std::string_view s, int width = 0)
609+
-> void
610+
{
611+
if (width > 0) {
612+
std::cout << std::setw(width) << std::left;
613+
}
614+
std::cout << s;
615+
}
649616

650617
public:
651618
auto process_flags()
@@ -925,6 +892,77 @@ static cmdline_processor::register_flag cmd_internal_debug(
925892
[]{ flag_internal_debug = true; }
926893
);
927894

895+
static auto flag_print_colon_errors = false;
896+
static cmdline_processor::register_flag cmd_print_colon_errors(
897+
9,
898+
"format-colon-errors",
899+
"Emit ':line:col:' format for messages - lights up some tools",
900+
[]{ flag_print_colon_errors = true; }
901+
);
902+
903+
904+
//-----------------------------------------------------------------------
905+
//
906+
// error: represents a user-readable error message
907+
//
908+
//-----------------------------------------------------------------------
909+
//
910+
struct error_entry
911+
{
912+
source_position where;
913+
std::string msg;
914+
bool internal = false;
915+
bool fallback = false; // only emit this message if there was nothing better
916+
917+
error_entry(
918+
source_position w,
919+
std::string_view m,
920+
bool i = false,
921+
bool f = false
922+
)
923+
: where{w}
924+
, msg{m}
925+
, internal{i}
926+
, fallback{f}
927+
{ }
928+
929+
auto operator==(error_entry const& that)
930+
-> bool
931+
{
932+
return
933+
where == that.where
934+
&& msg == that.msg
935+
;
936+
}
937+
938+
auto print(auto& o, std::string const& file) const
939+
-> void
940+
{
941+
o << file ;
942+
if (where.lineno > 0) {
943+
if (flag_print_colon_errors) {
944+
o << ":" << (where.lineno);
945+
if (where.colno >= 0) {
946+
o << ":" << where.colno;
947+
}
948+
}
949+
else {
950+
o << "("<< (where.lineno);
951+
if (where.colno >= 0) {
952+
o << "," << where.colno;
953+
}
954+
o << ")";
955+
}
956+
}
957+
o << ":";
958+
if (internal) {
959+
o << " internal compiler";
960+
}
961+
o << " error: " << msg << "\n";
962+
}
963+
964+
};
965+
928966

929967
//-----------------------------------------------------------------------
930968
//

Diff for: source/cppfront.cpp

+41-28
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ static cpp2::cmdline_processor::register_flag cmd_debug(
2525
[]{ flag_debug_output = true; }
2626
);
2727

28+
static auto flag_quiet = false;
29+
static cpp2::cmdline_processor::register_flag cmd_quiet(
30+
9,
31+
"quiet",
32+
"Print only error output",
33+
[]{ flag_quiet = true; }
34+
);
35+
2836
auto main(
2937
int argc,
3038
char* argv[]
@@ -51,7 +59,9 @@ auto main(
5159
{
5260
auto& out = flag_cpp1_filename != "stdout" ? std::cout : std::cerr;
5361

54-
out << arg.text << "...";
62+
if (!flag_quiet) {
63+
out << arg.text << "...";
64+
}
5565

5666
// Load + lex + parse + sema
5767
cppfront c(arg.text);
@@ -62,37 +72,40 @@ auto main(
6272
// If there were no errors, say so and generate Cpp1
6373
if (c.had_no_errors())
6474
{
65-
if (!c.has_cpp1()) {
66-
out << " ok (all Cpp2, passes safety checks)\n";
67-
}
68-
else if (c.has_cpp2()) {
69-
out << " ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)\n";
70-
}
71-
else {
72-
out << " ok (all Cpp1)\n";
73-
}
75+
if (!flag_quiet)
76+
{
77+
if (!c.has_cpp1()) {
78+
out << " ok (all Cpp2, passes safety checks)\n";
79+
}
80+
else if (c.has_cpp2()) {
81+
out << " ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)\n";
82+
}
83+
else {
84+
out << " ok (all Cpp1)\n";
85+
}
7486

75-
if (flag_verbose) {
76-
out << " Cpp1: " << print_with_thousands(count.cpp1_lines) << " line" << (count.cpp1_lines != 1 ? "s" : "");
77-
out << "\n Cpp2: " << print_with_thousands(count.cpp2_lines) << " line" << (count.cpp2_lines != 1 ? "s" : "");
78-
auto total = count.cpp1_lines + count.cpp2_lines;
79-
if (total > 0) {
80-
out << " (";
81-
if (count.cpp1_lines == 0) {
82-
out << 100;
83-
}
84-
else if (count.cpp2_lines / count.cpp1_lines > 25) {
85-
out << std::setprecision(3)
86-
<< 100.0 * count.cpp2_lines / total;
87+
if (flag_verbose) {
88+
out << " Cpp1: " << print_with_thousands(count.cpp1_lines) << " line" << (count.cpp1_lines != 1 ? "s" : "");
89+
out << "\n Cpp2: " << print_with_thousands(count.cpp2_lines) << " line" << (count.cpp2_lines != 1 ? "s" : "");
90+
auto total = count.cpp1_lines + count.cpp2_lines;
91+
if (total > 0) {
92+
out << " (";
93+
if (count.cpp1_lines == 0) {
94+
out << 100;
95+
}
96+
else if (count.cpp2_lines / count.cpp1_lines > 25) {
97+
out << std::setprecision(3)
98+
<< 100.0 * count.cpp2_lines / total;
99+
}
100+
else {
101+
out << 100 * count.cpp2_lines / total;
102+
}
103+
out << "%)";
87104
}
88-
else {
89-
out << 100 * count.cpp2_lines / total;
90-
}
91-
out << "%)";
92105
}
93-
}
94106

95-
out << "\n";
107+
out << "\n";
108+
}
96109
}
97110
// Otherwise, print the errors
98111
else

Diff for: source/to_cpp1.h

+11-55
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@
2323

2424
namespace cpp2 {
2525

26-
// Defined out of line here just to avoid bringing <iostream> in before this,
27-
// so that we can't accidentally start depending on iostreams in earlier phases
28-
auto cmdline_processor::print(std::string_view s, int width)
29-
-> void
30-
{
31-
if (width > 0) {
32-
std::cout << std::setw(width) << std::left;
33-
}
34-
std::cout << s;
35-
}
36-
37-
3826
//-----------------------------------------------------------------------
3927
//
4028
// Stringingizing helpers
@@ -89,23 +77,23 @@ auto multi_return_type_name(declaration_node const& n)
8977
//
9078
static auto flag_emit_cppfront_info = false;
9179
static cmdline_processor::register_flag cmd_emit_cppfront_info(
92-
9,
80+
8,
9381
"emit-cppfront-info",
94-
"Emit cppfront version/build in output file",
82+
"Emit cppfront version/build in Cpp1 file",
9583
[]{ flag_emit_cppfront_info = true; }
9684
);
9785

9886
static auto flag_clean_cpp1 = false;
9987
static cmdline_processor::register_flag cmd_clean_cpp1(
100-
9,
88+
8,
10189
"clean-cpp1",
10290
"Emit clean Cpp1 without #line directives",
10391
[]{ flag_clean_cpp1 = true; }
10492
);
10593

10694
static auto flag_line_paths = false;
10795
static cmdline_processor::register_flag cmd_line_paths(
108-
9,
96+
8,
10997
"line-paths",
11098
"Emit absolute paths in #line directives",
11199
[] { flag_line_paths = true; }
@@ -169,21 +157,13 @@ static cmdline_processor::register_flag cmd_enable_source_info(
169157

170158
static auto flag_cpp1_filename = std::string{};
171159
static cmdline_processor::register_flag cmd_cpp1_filename(
172-
9,
160+
8,
173161
"output filename",
174162
"Output to 'filename' (can be 'stdout') - default is *.cpp/*.h",
175163
nullptr,
176164
[](std::string const& name) { flag_cpp1_filename = name; }
177165
);
178166

179-
static auto flag_print_colon_errors = false;
180-
static cmdline_processor::register_flag cmd_print_colon_errors(
181-
9,
182-
"format-colon-errors",
183-
"Emit ':line:col:' format for messages - lights up some tools",
184-
[]{ flag_print_colon_errors = true; }
185-
);
186-
187167
static auto flag_verbose = false;
188168
static cmdline_processor::register_flag cmd_verbose(
189169
9,
@@ -214,38 +194,14 @@ struct text_with_pos{
214194
text_with_pos(std::string const& t, source_position p) : text{t}, pos{p} { }
215195
};
216196

217-
// Defined out of line so we can use flag_print_colon_errors.
218-
auto error_entry::print(
219-
auto& o,
220-
std::string const& file
221-
) const
222-
-> void
223-
{
224-
o << file ;
225-
if (where.lineno > 0) {
226-
if (flag_print_colon_errors) {
227-
o << ":" << (where.lineno);
228-
if (where.colno >= 0) {
229-
o << ":" << where.colno;
230-
}
231-
}
232-
else {
233-
o << "("<< (where.lineno);
234-
if (where.colno >= 0) {
235-
o << "," << where.colno;
236-
}
237-
o << ")";
238-
}
239-
}
240-
o << ":";
241-
if (internal) {
242-
o << " internal compiler";
243-
}
244-
o << " error: " << msg << "\n";
245-
}
246-
247197
class positional_printer
248198
{
199+
public:
200+
positional_printer() = default;
201+
private:
202+
positional_printer(positional_printer const&) = delete;
203+
void operator=(positional_printer const&) = delete;
204+
249205
// Core information
250206
std::ofstream out_file = {}; // Cpp1 syntax output file
251207
std::ostream* out = {}; // will point to out_file or cout

0 commit comments

Comments
 (0)