Skip to content

Commit

Permalink
Reworked keep prefix (#7456)
Browse files Browse the repository at this point in the history
* reworked keep prefix

* checking in missing schema

* fix flatc path for build scripts
  • Loading branch information
dbaileychess authored Aug 23, 2022
1 parent 627e8bf commit d6f06c3
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 138 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ jobs:
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64
- name: test
run: Release\flattests.exe
- name: flatc tests
run: python3 tests/flatc/main.py --flatc Release\flatc.exe
- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
Expand Down Expand Up @@ -181,6 +183,8 @@ jobs:
run: |
chmod +x _build/Release/flatc
./_build/Release/flatc --version
- name: flatc tests
run: python3 tests/flatc/main.py --flatc ./_build/Release/flatc
- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
Expand Down
40 changes: 26 additions & 14 deletions src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,34 @@ class CppGenerator : public BaseGenerator {
// interdependence between them.
std::stable_sort(included_files.begin(), included_files.end());

// Get any prefix of the file being parsed, so that included filed can be
// properly stripped.
auto prefix = flatbuffers::StripFileName(parser_.file_being_parsed_) +
flatbuffers::kPathSeparator;
// The absolute path of the file being parsed.
const std::string parsed_path =
flatbuffers::StripFileName(AbsolutePath(parser_.file_being_parsed_));

for (const std::string &included_file : included_files) {
auto file_without_extension = flatbuffers::StripExtension(included_file);
code_ +=
"#include \"" +
GeneratedFileName(
opts_.include_prefix,
opts_.keep_prefix
? flatbuffers::StripPrefix(file_without_extension, prefix)
: flatbuffers::StripPath(file_without_extension),
opts_) +
"\"";
// The base name of the file, without path or extensions.
std::string basename =
flatbuffers::StripPath(flatbuffers::StripExtension(included_file));

// If we are keeping the prefix
if (opts_.keep_prefix) {
// The absolute path of the included file.
const std::string included_path =
flatbuffers::StripFileName(AbsolutePath(included_file));

// The relative path of the parsed file to the included file.
const std::string relative_path =
RelativeToRootPath(parsed_path, included_path).substr(2);

// Only consider cases where the included path is a subdirectory of the
// parsed path.
if (strncmp("..", relative_path.c_str(), 2) != 0) {
basename = relative_path + kPathSeparator + basename;
}
}

code_ += "#include \"" +
GeneratedFileName(opts_.include_prefix, basename, opts_) + "\"";
}

if (!parser_.native_included_files_.empty() || !included_files.empty()) {
Expand Down
6 changes: 6 additions & 0 deletions tests/flatc/bar/bar_with_foo.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include "foo.fbs";
include "baz/baz.fbs";

table BarWithFoo {
foo:Foo;
}
Loading

0 comments on commit d6f06c3

Please sign in to comment.