Add the directory of current .bin_fmt and .isa file to include file paths. This fixes an issue when included .bin_fmt and .isa files aren't resolved properly when including from other projects. PiperOrigin-RevId: 811404683 Change-Id: I84fc83dcca8e49ce2093fb82da4be9dc644c4481
diff --git a/mpact/sim/decoder/bin_format_visitor.cc b/mpact/sim/decoder/bin_format_visitor.cc index 2d8fc8a..83658c8 100644 --- a/mpact/sim/decoder/bin_format_visitor.cc +++ b/mpact/sim/decoder/bin_format_visitor.cc
@@ -14,8 +14,10 @@ #include "mpact/sim/decoder/bin_format_visitor.h" +#include <algorithm> #include <cstdint> #include <deque> +#include <filesystem> // NOLINT: third party. #include <fstream> #include <iostream> #include <istream> @@ -146,10 +148,22 @@ decoder_name_ = decoder_name; include_dir_vec_.push_back("."); + for (const auto& root : include_roots) { include_dir_vec_.push_back(root); } + // Add the directory of the input file to the include roots if not already + // present. + if (!file_names.empty()) { + std::string dir = std::filesystem::path(file_names[0]).stem().string(); + auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir); + if (it == include_dir_vec_.end()) { + include_dir_vec_.push_back( + std::filesystem::path(file_names[0]).stem().string()); + } + } + std::istream* source_stream = &std::cin; if (!file_names.empty()) { @@ -178,8 +192,16 @@ } // Visit the parse tree starting at the namespaces declaration. PreProcessDeclarations(top_level->declaration_list()); - // Process additional source files. + + // Process any additional source files. for (int i = 1; i < file_names.size(); ++i) { + // Add the directory of the input file to the include roots if not already + // present. + std::string dir = std::filesystem::path(file_names[i]).stem().string(); + auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir); + if (it == include_dir_vec_.end()) { + include_dir_vec_.push_back(dir); + } ParseIncludeFile(top_level, file_names[i], {}); } // Process the parse tree.
diff --git a/mpact/sim/decoder/instruction_set_visitor.cc b/mpact/sim/decoder/instruction_set_visitor.cc index fd5d6f5..a73f4b1 100644 --- a/mpact/sim/decoder/instruction_set_visitor.cc +++ b/mpact/sim/decoder/instruction_set_visitor.cc
@@ -14,6 +14,7 @@ #include "mpact/sim/decoder/instruction_set_visitor.h" +#include <algorithm> #include <cctype> #include <cstddef> #include <filesystem> // NOLINT: third party. @@ -108,6 +109,16 @@ include_dir_vec_.push_back(include_root); } + // Add the directory of the input file to the include roots. + if (!file_names.empty()) { + std::string dir = std::filesystem::path(file_names[0]).stem().string(); + auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir); + if (it == include_dir_vec_.end()) { + include_dir_vec_.push_back( + std::filesystem::path(file_names[0]).stem().string()); + } + } + std::string isa_prefix = prefix; std::istream* source_stream = &std::cin; @@ -137,6 +148,13 @@ VisitTopLevel(top_level); // Process additional source files. for (int i = 1; i < file_names.size(); ++i) { + // Add the directory of the input file to the include roots if not already + // present. + std::string dir = std::filesystem::path(file_names[i]).stem().string(); + auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir); + if (it == include_dir_vec_.end()) { + include_dir_vec_.push_back(dir); + } ParseIncludeFile(top_level, file_names[i], {}); } // Now process the parse tree.