Skip to content

Add option to emit source code to stdout #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ auto strip_path(std::string const& file) -> std::string
return {file, as<size_t>(i+1)};
}

//-----------------------------------------------------------------------
//
// stdout detect helper
//
//-----------------------------------------------------------------------
//
auto is_filename_stdout(std::string_view filename) -> bool
{
return filename == "stdout";
}


//-----------------------------------------------------------------------
//
Expand Down
31 changes: 21 additions & 10 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ struct text_with_pos{
class positional_printer
{
// Core information
std::ofstream out = {}; // Cpp1 syntax output file
std::stringstream out = {}; // Cpp1 syntax output buffer
std::ofstream file_out = {}; // Cpp1 syntax output file
std::string cpp2_filename = {};
std::string cpp1_filename = {};
std::vector<comment> const* pcomments = {}; // Cpp2 comments data
Expand Down Expand Up @@ -346,6 +347,12 @@ class positional_printer
// remain diff-identical
print_extra("\n");
}

if (is_stdout_output()) {
std::cout << out.str();
} else {
file_out << out.str();
}
}

//-----------------------------------------------------------------------
Expand All @@ -361,29 +368,32 @@ class positional_printer
{
source_has_cpp2 = has_cpp2;
cpp2_filename = cpp2_filename_;
assert (!out.is_open() && !pcomments && "ICE: tried to call .open twice");
assert (!file_out.is_open() && !pcomments && "ICE: tried to call .open twice");
cpp1_filename = cpp1_filename_;
out.open(cpp1_filename);
if (!is_stdout_output()) file_out.open(cpp1_filename);
pcomments = &comments;
}

auto is_open() -> bool {
if (out.is_open()) {
if (file_out.is_open() || is_stdout_output()) {
assert (pcomments && "ICE: if out.is_open, pcomments should also be set");
}
return out.is_open();
return file_out.is_open() || is_stdout_output();
}

auto is_stdout_output() -> bool {
return ::cpp2::is_filename_stdout(cpp1_filename);
}

//-----------------------------------------------------------------------
// Abandon: close and delete
//
auto abandon() -> void
{
if (!out.is_open()) {
if (!file_out.is_open()) {
return;
}
out.close();
file_out.close();
std::remove(cpp1_filename.c_str());
}

Expand Down Expand Up @@ -2815,14 +2825,15 @@ auto main(int argc, char* argv[]) -> int
}

if (cmdline.arguments().empty()) {
std::cout << "cppfront: error: no input files\n";
std::cerr << "cppfront: error: no input files\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outputting all errors in stderr is better regardless of whether this feature merges.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, most of them were going to cerr but I missed these ones, thanks!

return EXIT_FAILURE;
}

// For each .cpp2 source file
int exit_status = EXIT_SUCCESS;
for (auto const& arg : cmdline.arguments())
{
if (is_filename_stdout(flag_cpp1_filename)) std::cout << "// cppfront ";
std::cout << arg.text << "...";

// Load + lex + parse + sema
Expand All @@ -2845,9 +2856,9 @@ auto main(int argc, char* argv[]) -> int
}
// Otherwise, print the errors
else {
std::cout << "\n";
std::cerr << "\n";
c.print_errors();
std::cout << "\n";
std::cerr << "\n";
exit_status = EXIT_FAILURE;
}

Expand Down