Skip to content
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

Feat: Add ability to compile from stdin #1327

Merged
merged 6 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 3 additions & 1 deletion source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,9 @@ class cmdline_processor
[](auto& a, auto& b){ return a.group < b.group || (a.group == b.group && a.name < b.name); }
);

print("\nUsage: cppfront [options] file ...\n\nOptions:\n");
print("\nUsage: cppfront [options] file ...\n");
print("\nfile - The source file(s) to compile (can be 'stdin' to read text directly) \n");
print("\nOptions: \n");
int last_group = -1;
for (auto& flag : flags) {
// Skip hidden flags
Expand Down
14 changes: 10 additions & 4 deletions source/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,17 @@ class source
)
-> bool
{
std::ifstream in{ filename };
if (!in.is_open()) {
return false;
// If filename is stdin, we read from stdin, otherwise we try to read the file
//
auto is_stdin = filename == "stdin";
std::ifstream fss;
if (is_stdin)
Copy link
Owner

Choose a reason for hiding this comment

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

! missing? Otherwise compiling a named file breaks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. That was definitely my bad 😅 . Thanks for fixing

{
fss.open(filename);
if( !fss.is_open()) { return false; }
}

std::istream& in = is_stdin ? std::cin : fss;

auto in_comment = false;
auto in_string_literal = false;
auto in_raw_string_literal = false;
Expand Down
1 change: 1 addition & 0 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,7 @@ class cppfront
if (
!sourcefile.ends_with(".cpp2")
&& !sourcefile.ends_with(".h2")
&& sourcefile != "stdin"
Copy link
Owner

Choose a reason for hiding this comment

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

This part is fine, but we need to also update later logic that generates the output filename and currently assumes it ends in one of these and so just truncates the last character (e.g., .cpp2 becomes .cpp. When I tried to use stdin the output went to a file named stdi (last character blindly truncated).

How is this intended to work -- if you use stdin, is the output expected to go to stdout, or if to a named file what is the intended name?

I think the right answer is probably to default to stdout unless the programmer overrode that with -o. I'll implement that, and also make sure the output to stdout is clean of other messages when there are no errors.

)
{
errors.emplace_back(
Expand Down
Loading