Skip to content

Commit 72b7e72

Browse files
committed
Fixed logic, and added default to stdout if stdin is used
Also: - re-ran self-build and regression tests - suppressed non-error output when stdout is used, so piping is easier
1 parent f6e1362 commit 72b7e72

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

source/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ class cmdline_processor
800800
);
801801

802802
print("\nUsage: cppfront [options] file ...\n");
803-
print("\nfile - The source file(s) to compile (can be 'stdin' to read text directly) \n");
803+
print("\n file source file(s) (can be 'stdin')\n");
804804
print("\nOptions: \n");
805805
int last_group = -1;
806806
for (auto& flag : flags) {

source/cppfront.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ auto main(
7979

8080
auto& out = flag_cpp1_filename != "stdout" ? std::cout : std::cerr;
8181

82-
if (!flag_quiet) {
82+
if (
83+
!flag_quiet
84+
&& arg.text != "stdin"
85+
&& flag_cpp1_filename != "stdout"
86+
)
87+
{
8388
out << arg.text << "...";
8489
}
8590

@@ -92,7 +97,10 @@ auto main(
9297
// If there were no errors, say so and generate Cpp1
9398
if (c.had_no_errors())
9499
{
95-
if (!flag_quiet)
100+
if (
101+
!flag_quiet
102+
&& flag_cpp1_filename != "stdout"
103+
)
96104
{
97105
if (!c.has_cpp1()) {
98106
out << " ok (all Cpp2, passes safety checks)\n";

source/io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ class source
886886
//
887887
auto is_stdin = filename == "stdin";
888888
std::ifstream fss;
889-
if (is_stdin)
889+
if (!is_stdin)
890890
{
891891
fss.open(filename);
892892
if( !fss.is_open()) { return false; }

source/to_cpp1.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,14 +1258,18 @@ class cppfront
12581258
}
12591259

12601260
// Now we'll open the Cpp1 file
1261-
auto cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1);
1261+
// Default to stdout if input is stdin
1262+
auto cpp1_filename = std::string{"stdout"};
1263+
if (sourcefile != "stdin") {
1264+
assert(sourcefile.ends_with("2"));
1265+
cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1);
1266+
}
12621267

1263-
// Use explicit filename override if present,
1264-
// otherwise strip leading path
1268+
// Use explicit filename override if present, otherwise strip leading path
12651269
if (!flag_cpp1_filename.empty()) {
12661270
cpp1_filename = flag_cpp1_filename;
12671271
}
1268-
else {
1272+
else if (cpp1_filename != "stdout") {
12691273
cpp1_filename = std::filesystem::path(cpp1_filename).filename().string();
12701274
}
12711275

0 commit comments

Comments
 (0)