Skip to content

Commit

Permalink
Fixed logic, and added default to stdout if stdin is used
Browse files Browse the repository at this point in the history
Also:
- re-ran self-build and regression tests
- suppressed non-error output when stdout is used, so piping is easier
  • Loading branch information
hsutter committed Nov 2, 2024
1 parent f6e1362 commit 72b7e72
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ class cmdline_processor
);

print("\nUsage: cppfront [options] file ...\n");
print("\nfile - The source file(s) to compile (can be 'stdin' to read text directly) \n");
print("\n file source file(s) (can be 'stdin')\n");
print("\nOptions: \n");
int last_group = -1;
for (auto& flag : flags) {
Expand Down
12 changes: 10 additions & 2 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ auto main(

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

if (!flag_quiet) {
if (
!flag_quiet
&& arg.text != "stdin"
&& flag_cpp1_filename != "stdout"
)
{
out << arg.text << "...";
}

Expand All @@ -92,7 +97,10 @@ auto main(
// If there were no errors, say so and generate Cpp1
if (c.had_no_errors())
{
if (!flag_quiet)
if (
!flag_quiet
&& flag_cpp1_filename != "stdout"
)
{
if (!c.has_cpp1()) {
out << " ok (all Cpp2, passes safety checks)\n";
Expand Down
2 changes: 1 addition & 1 deletion source/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ class source
//
auto is_stdin = filename == "stdin";
std::ifstream fss;
if (is_stdin)
if (!is_stdin)
{
fss.open(filename);
if( !fss.is_open()) { return false; }
Expand Down
12 changes: 8 additions & 4 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -1258,14 +1258,18 @@ class cppfront
}

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

// Use explicit filename override if present,
// otherwise strip leading path
// Use explicit filename override if present, otherwise strip leading path
if (!flag_cpp1_filename.empty()) {
cpp1_filename = flag_cpp1_filename;
}
else {
else if (cpp1_filename != "stdout") {
cpp1_filename = std::filesystem::path(cpp1_filename).filename().string();
}

Expand Down

0 comments on commit 72b7e72

Please sign in to comment.