Skip to content

Commit

Permalink
Use a unique pointer instead of a custom structure.
Browse files Browse the repository at this point in the history
Signed-off-by: fruffy <[email protected]>
  • Loading branch information
fruffy committed Jul 16, 2024
1 parent 8ca399d commit 70c3f92
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 39 deletions.
12 changes: 7 additions & 5 deletions frontends/common/parseInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
#define FRONTENDS_COMMON_PARSEINPUT_H_

#include "frontends/common/options.h"
#include "frontends/common/parser_options.h"
#include "frontends/p4/fromv1.0/converters.h"
#include "frontends/parsers/parserDriver.h"
#include "lib/error.h"
Expand Down Expand Up @@ -77,14 +78,15 @@ const IR::P4Program *parseP4File(const ParserOptions &options) {
fclose(file);
} else {
auto preprocessorResult = options.preprocess();
if (::errorCount() > 0 || preprocessorResult.file() == nullptr) {
if (::errorCount() > 0 || !preprocessorResult.has_value()) {
return nullptr;
}
// Need to assign file here because the parser requires an lvalue.
result = options.isv1()
? parseV1Program<FILE *, C>(preprocessorResult.file(), options.file.string(),
1, options.getDebugHook())
: P4ParserDriver::parse(preprocessorResult.file(), options.file.string());
result =
options.isv1()
? parseV1Program<FILE *, C>(preprocessorResult.value().get(), options.file.string(),
1, options.getDebugHook())
: P4ParserDriver::parse(preprocessorResult.value().get(), options.file.string());
}

if (::errorCount() > 0) {
Expand Down
15 changes: 8 additions & 7 deletions frontends/common/parser_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include <sys/types.h>
#include <sys/wait.h>

#include <memory>
#include <regex>
#include <unordered_set>

Expand All @@ -45,11 +46,11 @@ const char *p4_14includePath = CONFIG_PKGDATADIR "/p4_14include";

using namespace P4::literals;

void ParserOptions::PreprocessorResult::closeFile() {
if (!_closeInput || _file == nullptr) {
void ParserOptions::closeFile(FILE *file) {
if (file == nullptr) {
return;
}
int exitCode = pclose(_file);
int exitCode = pclose(file);
if (WIFEXITED(exitCode) && WEXITSTATUS(exitCode) == 4) {
::error(ErrorType::ERR_IO, "input file does not exist");
return;
Expand Down Expand Up @@ -416,7 +417,7 @@ const char *ParserOptions::getIncludePath() const {
return path.c_str();
}

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

Expand All @@ -437,7 +438,7 @@ ParserOptions::PreprocessorResult ParserOptions::preprocess() const {
if (in == nullptr) {
::error(ErrorType::ERR_IO, "Error invoking preprocessor");
perror("");
return {};
return std::nullopt;
}
closeInput = true;
}
Expand All @@ -450,9 +451,9 @@ ParserOptions::PreprocessorResult ParserOptions::preprocess() const {
while ((read = getline(&line, &len, in)) != -1) {
printf("%s", line);
}
return {};
return std::nullopt;
}
return {in, closeInput};
return ParserOptions::PreprocessorResult(in, &closeFile);
}

// From (folder, file.ext, suffix) returns
Expand Down
30 changes: 5 additions & 25 deletions frontends/common/parser_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
#ifndef FRONTENDS_COMMON_PARSER_OPTIONS_H_
#define FRONTENDS_COMMON_PARSER_OPTIONS_H_

#include <cstdio>
#include <filesystem>
#include <set>

Expand Down Expand Up @@ -53,31 +54,10 @@ class ParserOptions : public Util::Options {
std::vector<const char *> *process(int argc, char *const argv[]) override;
enum class FrontendVersion { P4_14, P4_16 };

/// Tries to close the input stream associated with the result.
static void closeFile(FILE *file);
/// Records the result of the preprocessor.
class PreprocessorResult {
private:
/// The input stream.
FILE *_file = nullptr;
/// Whether the input stream should be closed.
bool _closeInput = false;

/// Tries to close the input stream associated with the result.
void closeFile();

public:
PreprocessorResult() = default;
PreprocessorResult &operator=(PreprocessorResult &&) = default;
PreprocessorResult(PreprocessorResult &&) = default;
/// There must only be one PreprocessorResult per file handle. Delete the copy constructor.
PreprocessorResult(const PreprocessorResult &) = delete;
PreprocessorResult &operator=(const PreprocessorResult &) = delete;
PreprocessorResult(FILE *file, bool closeInput) : _file(file), _closeInput(closeInput) {}

~PreprocessorResult() { closeFile(); }

/// @return the input stream.
[[nodiscard]] FILE *file() const { return _file; }
};
using PreprocessorResult = std::unique_ptr<FILE, decltype(&closeFile)>;

/// Name of executable that is being run.
cstring exe_name;
Expand All @@ -104,7 +84,7 @@ class ParserOptions : public Util::Options {
/// Return target specific include path.
const char *getIncludePath() const override;
/// Returns the output of the preprocessor.
PreprocessorResult preprocess() const;
std::optional<ParserOptions::PreprocessorResult> preprocess() const;
/// True if we are compiling a P4 v1.0 or v1.1 program
bool isv1() const;
/// Get a debug hook function suitable for insertion
Expand Down
4 changes: 2 additions & 2 deletions frontends/p4/fromv1.0/programStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,9 @@ void ProgramStructure::include(cstring filename, cstring ppoptions) {
options.file = path;
if (::errorCount() == 0U) {
auto preprocessorResult = options.preprocess();
if (preprocessorResult.file() != nullptr) {
if (preprocessorResult.has_value()) {
const auto *code =
P4::P4ParserDriver::parse(preprocessorResult.file(), options.file.string());
P4::P4ParserDriver::parse(preprocessorResult.value().get(), options.file.string());
if ((code != nullptr) && (::errorCount() == 0U)) {
for (const auto *decl : code->objects) {
declarations->push_back(decl);
Expand Down

0 comments on commit 70c3f92

Please sign in to comment.