Skip to content
Open
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
64 changes: 51 additions & 13 deletions frontends/common/parser_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ limitations under the License.
#include <sys/wait.h>

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

#include "absl/strings/escaping.h"
Expand Down Expand Up @@ -121,6 +123,13 @@ ParserOptions::ParserOptions(std::string_view defaultMessage) : Util::Options(de
return true;
},
"Output `make` dependency rule only (passed to preprocessor)");
registerOption(
"--save-temps", nullptr,
[this](const char *) {
savePreprocessed = true;
return true;
},
"Saves preprocessed P4 to filename.p4pp and do not exit compilation.");
registerOption(
"-MD", nullptr,
[this](const char *) {
Expand Down Expand Up @@ -405,16 +414,28 @@ 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;

if (file == "-") {
in = stdin;
} else {
#ifdef __clang__
std::string cmd("cc -E -x c -Wno-comment");
std::string cmd = "cc -E -x c -Wno-comment";
#else
std::string cmd("cpp");
std::string cmd = "cpp";
#endif

cmd += " -C -undef -nostdinc -x assembler-with-cpp " + preprocessor_options.string() +
Expand All @@ -439,19 +460,36 @@ 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::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;
}
closeFile(in);

std::filesystem::path fileName(file.stem());
fileName += ".p4pp";
fileName = makeFileName(dumpFolder, fileName, "");
std::ofstream filestream{fileName};
if (filestream) {
if (Log::verbose()) std::cerr << "Writing preprocessed P4 to " << fileName << std::endl;
filestream << stream.str();
}
filestream.close();

in = fopen(fileName.c_str(), "r");
if (in == nullptr) {
::P4::error(ErrorType::ERR_IO, "Error invoking preprocessor");
perror("");
return std::nullopt;
}
}
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 @@ -82,6 +82,8 @@ class ParserOptions : public Util::Options {
std::filesystem::path file;
/// if true preprocess only
bool doNotCompile = false;
/// if true save preprocessed P4 to filename.p4pp
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-U arg Undefine macro (passed to preprocessor)
-E Preprocess only, do not compile (prints program on stdout)
-M Output `make` dependency rule only (passed to preprocessor)
--save-temps Saves preprocessed P4 to filename.p4pp and do not exit compilation.
-MD Output `make` dependency rule to file as side effect (passed to preprocessor)
-MF file With -M, specify output file for dependencies (passed to preprocessor)
-MG with -M, suppress errors for missing headers (passed to preprocessor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-U arg Undefine macro (passed to preprocessor)
-E Preprocess only, do not compile (prints program on stdout)
-M Output `make` dependency rule only (passed to preprocessor)
--save-temps Saves preprocessed P4 to filename.p4pp and do not exit compilation.
-MD Output `make` dependency rule to file as side effect (passed to preprocessor)
-MF file With -M, specify output file for dependencies (passed to preprocessor)
-MG with -M, suppress errors for missing headers (passed to preprocessor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-U arg Undefine macro (passed to preprocessor)
-E Preprocess only, do not compile (prints program on stdout)
-M Output `make` dependency rule only (passed to preprocessor)
--save-temps Saves preprocessed P4 to filename.p4pp and do not exit compilation.
-MD Output `make` dependency rule to file as side effect (passed to preprocessor)
-MF file With -M, specify output file for dependencies (passed to preprocessor)
-MG with -M, suppress errors for missing headers (passed to preprocessor)
Expand Down
Loading