Skip to content

Commit

Permalink
Feat: Add ability to compile from stdin (#1327)
Browse files Browse the repository at this point in the history
* Implement ability to compile directly from stdin

* Add doc comment to process_cpp2 function

* Move choice between file or stdin to source.load()

* Add a little comment above source loading decision

* Don't file-open `stdin`

* Fixed logic, and added default to stdout if stdin is used

Also:
- re-ran self-build and regression tests
- suppressed non-error output when stdout is used, so piping is easier

---------

Co-authored-by: Herb Sutter <[email protected]>
  • Loading branch information
vanceism7 and hsutter authored Nov 2, 2024
1 parent 6b3934b commit c0dd050
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
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"
)
{
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

0 comments on commit c0dd050

Please sign in to comment.