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

[#5152] Write preprocessed P4 to repro.p4 file when -P option is provided #5153

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
49 changes: 38 additions & 11 deletions frontends/common/parser_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ limitations under the License.
#include <sys/types.h>
#include <sys/wait.h>

#include <fstream>
#include <memory>
#include <regex>
#include <sstream>
#include <unordered_set>

#include "absl/strings/escaping.h"
Expand Down Expand Up @@ -119,6 +121,13 @@ ParserOptions::ParserOptions(std::string_view defaultMessage) : Util::Options(de
return true;
},
"Output `make` dependency rule only (passed to preprocessor)");
registerOption(
"-P", nullptr,
[this](const char *) {
savePreprocessed = true;
return true;
},
"Saves preprocessed P4 to repro.p4 and do not exit compilation.");
registerOption(
"-MD", nullptr,
[this](const char *) {
Expand Down Expand Up @@ -421,6 +430,18 @@ const char *ParserOptions::getIncludePath() const {
return path.c_str();
}

// From (folder, file.ext, suffix) returns
// folder/file-suffix.ext
static std::filesystem::path makeFileName(const std::filesystem::path &folder,
const std::filesystem::path &name,
std::string_view baseSuffix) {
std::filesystem::path newName(name.stem());
newName += baseSuffix;
newName += name.extension();

return folder / newName;
}

std::optional<ParserOptions::PreprocessorResult> ParserOptions::preprocess() const {
FILE *in = nullptr;

Expand Down Expand Up @@ -455,19 +476,25 @@ std::optional<ParserOptions::PreprocessorResult> ParserOptions::preprocess() con
}
return std::nullopt;
}
return ParserOptions::PreprocessorResult(in, &closeFile);
}

// From (folder, file.ext, suffix) returns
// folder/file-suffix.ext
static std::filesystem::path makeFileName(const std::filesystem::path &folder,
const std::filesystem::path &name,
std::string_view baseSuffix) {
std::filesystem::path newName(name.stem());
newName += baseSuffix;
newName += name.extension();
if (savePreprocessed) {
std::filesystem::path fileName = makeFileName(dumpFolder, "repro.p4", "");

Choose a reason for hiding this comment

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

I think that using <program_name>.p4pp would be better, compared to having a fairly arbitrary fixed name.

std::stringstream stream;
char *line = nullptr;
size_t len = 0;
ssize_t read = 0;

return folder / newName;
while ((read = getline(&line, &len, in)) != -1) {
stream << line;
}
std::ofstream filestream{fileName};
if (filestream) {
if (Log::verbose()) std::cerr << "Writing preprocessed P4 to " << fileName << std::endl;
filestream << stream.str();
}
filestream.close();
}
return ParserOptions::PreprocessorResult(in, &closeFile);
}

bool ParserOptions::isv1() const { return langVersion == ParserOptions::FrontendVersion::P4_14; }
Expand Down
2 changes: 2 additions & 0 deletions frontends/common/parser_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class ParserOptions : public Util::Options {
std::filesystem::path file;
/// if true preprocess only
bool doNotCompile = false;
/// if true save preprocessed P4 to repro.p4
bool savePreprocessed = false;

Choose a reason for hiding this comment

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

If you decide to address my first comment, then, obviously, this comment needs to be changed too.

/// Compiler version.
cstring compilerVersion;
/// if true skip preprocess
Expand Down
Loading