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 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
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("\n file source file(s) (can be 'stdin')\n");
print("\nOptions: \n");
int last_group = -1;
for (auto& flag : flags) {
// Skip hidden 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
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)
{
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
13 changes: 9 additions & 4 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 Expand Up @@ -1257,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