-
Notifications
You must be signed in to change notification settings - Fork 510
[#5152] Write preprocessed P4 to <program_name>.p4pp file when --save-temps option is provided
#5153
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
base: main
Are you sure you want to change the base?
[#5152] Write preprocessed P4 to <program_name>.p4pp file when --save-temps option is provided
#5153
Changes from 5 commits
f486d15
de515d5
36b8060
26673d0
07c43e4
a6ce9bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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" | ||||||||||||||||||||
|
|
@@ -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 *) { | ||||||||||||||||||||
|
|
@@ -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"; | ||||||||||||||||||||
kfcripps marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||
| #else | ||||||||||||||||||||
| std::string cmd("cpp"); | ||||||||||||||||||||
| std::string cmd = "cpp"; | ||||||||||||||||||||
kfcripps marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||
| #endif | ||||||||||||||||||||
|
Comment on lines
453
to
439
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the |
||||||||||||||||||||
|
|
||||||||||||||||||||
| cmd += " -C -undef -nostdinc -x assembler-with-cpp " + preprocessor_options.string() + | ||||||||||||||||||||
|
|
@@ -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; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+466
to
+472
|
||||||||||||||||||||
| closeFile(in); | ||||||||||||||||||||
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| std::filesystem::path fileName(file.stem()); | ||||||||||||||||||||
| fileName += ".p4pp"; | ||||||||||||||||||||
|
Comment on lines
+475
to
+476
|
||||||||||||||||||||
| std::filesystem::path fileName(file.stem()); | |
| fileName += ".p4pp"; | |
| std::filesystem::path fileName; | |
| if (file == "-") { | |
| fileName = "stdin.p4pp"; | |
| } else { | |
| fileName = file.stem(); | |
| fileName += ".p4pp"; | |
| } |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The code unnecessarily constructs fileName in lines 475-476 only to pass it to makeFileName which deconstructs and reconstructs it. This can be simplified by calling makeFileName(dumpFolder, file, ".p4pp") directly, which would be clearer and avoid the intermediate construction.
| std::filesystem::path fileName(file.stem()); | |
| fileName += ".p4pp"; | |
| fileName = makeFileName(dumpFolder, fileName, ""); | |
| std::filesystem::path fileName = makeFileName(dumpFolder, file, ".p4pp"); |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for file write failure. If the file write fails (e.g., due to disk space issues or permission problems), the code silently continues. Consider adding error reporting if !filestream after opening or if the write operation fails.
| filestream << stream.str(); | |
| filestream << stream.str(); | |
| if (!filestream) { | |
| ::P4::error(ErrorType::ERR_IO, "Failed to write preprocessed P4 to %s", fileName.c_str()); | |
| perror(""); | |
| filestream.close(); | |
| return std::nullopt; | |
| } |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message "Error invoking preprocessor" is misleading here since the preprocessor already succeeded. The actual error is that the saved preprocessed file cannot be reopened for reading. Consider a more accurate error message like "Error opening preprocessed file for reading" or "Failed to reopen saved preprocessed file".
| ::P4::error(ErrorType::ERR_IO, "Error invoking preprocessor"); | |
| ::P4::error(ErrorType::ERR_IO, "Error opening preprocessed file for reading"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||
|
||||||
| --save-temps Saves preprocessed P4 to filename.p4pp and do not exit compilation. | |
| --save-temps Saves preprocessed P4 to filename.p4pp and does not exit compilation. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||
|
||||||
| --save-temps Saves preprocessed P4 to filename.p4pp and do not exit compilation. | |
| --save-temps Saves preprocessed P4 to filename.p4pp and does not exit compilation. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||
|
||||||
| --save-temps Saves preprocessed P4 to filename.p4pp and do not exit compilation. | |
| --save-temps Saves preprocessed P4 to filename.p4pp and does not exit compilation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammatical error: "do not exit compilation" should be "does not exit compilation" to match the singular subject of the sentence.