@@ -110,7 +110,7 @@ static auto flag_cpp1_filename = std::string{};
110
110
static cmdline_processor::register_flag cmd_cpp1_filename (
111
111
2 ,
112
112
" output" ,
113
- " Output filename (default is *.cpp)" ,
113
+ " Output filename, or 'stdout' (default is *.cpp)" ,
114
114
nullptr ,
115
115
[](std::string const & name) { flag_cpp1_filename = name; }
116
116
);
@@ -126,7 +126,8 @@ struct text_with_pos{
126
126
class positional_printer
127
127
{
128
128
// Core information
129
- std::ofstream out = {}; // Cpp1 syntax output file
129
+ std::ofstream out_file = {}; // Cpp1 syntax output file
130
+ std::ostream* out = {}; // will point to out_file or cout
130
131
std::string cpp2_filename = {};
131
132
std::string cpp1_filename = {};
132
133
std::vector<comment> const * pcomments = {}; // Cpp2 comments data
@@ -193,7 +194,8 @@ class positional_printer
193
194
// and update our curr_pos position
194
195
195
196
// Output the string
196
- out << s;
197
+ assert (out);
198
+ *out << s;
197
199
198
200
// Update curr_pos by finding how many line breaks s contained,
199
201
// and where the last one was which determines our current colno
@@ -244,7 +246,8 @@ class positional_printer
244
246
245
247
// Not using print() here because this is transparent to the curr_pos
246
248
if (!flag_clean_cpp1) {
247
- out << " #line " << line << " " << std::quoted (cpp2_filename) << " \n " ;
249
+ assert (out);
250
+ *out << " #line " << line << " " << std::quoted (cpp2_filename) << " \n " ;
248
251
}
249
252
}
250
253
@@ -361,17 +364,23 @@ class positional_printer
361
364
{
362
365
source_has_cpp2 = has_cpp2;
363
366
cpp2_filename = cpp2_filename_;
364
- assert (!out. is_open () && !pcomments && " ICE: tried to call .open twice" );
367
+ assert (!is_open () && !pcomments && " ICE: tried to call .open twice" );
365
368
cpp1_filename = cpp1_filename_;
366
- out.open (cpp1_filename);
369
+ if (cpp1_filename == " stdout" ) {
370
+ out = &std::cout;
371
+ }
372
+ else {
373
+ out_file.open (cpp1_filename);
374
+ out = &out_file;
375
+ }
367
376
pcomments = &comments;
368
377
}
369
378
370
379
auto is_open () -> bool {
371
- if (out. is_open () ) {
372
- assert (pcomments && " ICE: if out. is_open, pcomments should also be set" );
380
+ if (out) {
381
+ assert (pcomments && " ICE: if is_open, pcomments should also be set" );
373
382
}
374
- return out. is_open () ;
383
+ return out;
375
384
}
376
385
377
386
@@ -380,11 +389,13 @@ class positional_printer
380
389
//
381
390
auto abandon () -> void
382
391
{
383
- if (!out. is_open ()) {
392
+ if (!is_open ()) {
384
393
return ;
385
394
}
386
- out.close ();
387
- std::remove (cpp1_filename.c_str ());
395
+ if (out_file.is_open ()) {
396
+ out_file.close ();
397
+ std::remove (cpp1_filename.c_str ());
398
+ }
388
399
}
389
400
390
401
@@ -2863,7 +2874,7 @@ auto main(int argc, char* argv[]) -> int
2863
2874
}
2864
2875
2865
2876
if (cmdline.arguments ().empty ()) {
2866
- std::cout << " cppfront: error: no input files\n " ;
2877
+ std::cerr << " cppfront: error: no input files\n " ;
2867
2878
return EXIT_FAILURE;
2868
2879
}
2869
2880
@@ -2893,9 +2904,9 @@ auto main(int argc, char* argv[]) -> int
2893
2904
}
2894
2905
// Otherwise, print the errors
2895
2906
else {
2896
- std::cout << " \n " ;
2907
+ std::cerr << " \n " ;
2897
2908
c.print_errors ();
2898
- std::cout << " \n " ;
2909
+ std::cerr << " \n " ;
2899
2910
exit_status = EXIT_FAILURE;
2900
2911
}
2901
2912
0 commit comments