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

Conversation

s2x
Copy link

@s2x s2x commented Dec 29, 2022

This simple change allows you to build files directly without any intermediate steps. Previously, to compile a file, you had to first create a *.cpp file and then use a compiler. If we add the ability to print *.cpp file content to stdout, then we can use g++/clang++ options to make stdin the source.

example commands:

for g++
./cppfront regression-tests/pure2-hello.cpp2 -e | g++-10 -std=c++2a -x c++ -I include/ -O3 -o pure2-hello -

same for clang
./cppfront regression-tests/pure2-hello.cpp2 -e | clang++ -std=c++2a -x c++ -I include/ -O3 -o pure2-hello -

@@ -346,6 +353,8 @@ class positional_printer
// remain diff-identical
print_extra("\n");
}

cpp1_filename.empty() ? std::cout<<out.str() : file_out<<out.str();
Copy link
Contributor

Choose a reason for hiding this comment

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

I would use a regular if/else statement here. None of the current code use the ternary operator.

@@ -2853,14 +2867,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!

Comment on lines 375 to 384
if (!cpp1_filename.empty()) file_out.open(cpp1_filename);
pcomments = &comments;
}

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

Choose a reason for hiding this comment

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

I'd suggest moving the cpp1_filename.empty() tests to a is_stdout_output() utility function for clarity.

Copy link
Contributor

@prenaux prenaux left a comment

Choose a reason for hiding this comment

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

This is useful to remove the need for a temporary file in build systems on at least Linux and macOS.

I wrote some comments in the spirit of improving code clarity.

Regardless of whether this merge outputting all errors to stderr would be better.

The 4 commits should be squashed into one imo.

add helper function - is_stdout_output
@hsutter
Copy link
Owner

hsutter commented Dec 30, 2022

Thanks! Why not use the current -o flag and recognize the string stdout to mean standard output?

i.e., cppfront myfile.cpp2 -o stdout ?

@filipsajdak
Copy link
Contributor

What would be beneficial is to have the possibility to run cppfront that writes to stringstream that will allow to write in-memory tests.

@hsutter
Copy link
Owner

hsutter commented Jan 4, 2023

Thanks for your pull request! It looks like this may be your first contribution to cppfront. Before I can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). I think I sent it a few days ago, but let me know if you need me to resend it.

@hsutter
Copy link
Owner

hsutter commented Jan 6, 2023

Thanks! I've decided to take this but tweak the implementation a little, so I've applied it as a separate commit instead of asking you to make a bunch of small tweaks. Thanks again.

@hsutter hsutter closed this in c41cc43 Jan 6, 2023
Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this pull request Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants