Fixed --keep-prefix functionality.

Changing to keep include prefixes had two side effects: the main
file being parsed wasn't filtered out anymore, and include directory
paths would be added to the path in the include statement.

Also moved the include_test*.fbs files to sub directories so we
can actually test the handling of -I etc.

tested: on Linux.

Change-Id: Ibae095cea7ab0cccbac15cfb5171719f6b5cad8c
parent fb87c0d3
...@@ -195,6 +195,7 @@ function(compile_flatbuffers_schema_to_cpp SRC_FBS) ...@@ -195,6 +195,7 @@ function(compile_flatbuffers_schema_to_cpp SRC_FBS)
OUTPUT ${GEN_HEADER} OUTPUT ${GEN_HEADER}
COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" -c --no-includes --gen-mutable COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" -c --no-includes --gen-mutable
--gen-object-api -o "${SRC_FBS_DIR}" --gen-object-api -o "${SRC_FBS_DIR}"
-I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
"${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
DEPENDS flatc) DEPENDS flatc)
endfunction() endfunction()
......
...@@ -35,7 +35,7 @@ test_script: ...@@ -35,7 +35,7 @@ test_script:
- "JavaTest.bat" - "JavaTest.bat"
- rem "---------------- JS -----------------" - rem "---------------- JS -----------------"
- "node --version" - "node --version"
- "..\\%CONFIGURATION%\\flatc -b monster_test.fbs unicode_test.json" - "..\\%CONFIGURATION%\\flatc -b -I include_test monster_test.fbs unicode_test.json"
- "node JavaScriptTest ./monster_test_generated" - "node JavaScriptTest ./monster_test_generated"
- rem "---------------- C# -----------------" - rem "---------------- C# -----------------"
# Have to compile this here rather than in "build" above because AppVeyor only # Have to compile this here rather than in "build" above because AppVeyor only
......
...@@ -590,7 +590,8 @@ private: ...@@ -590,7 +590,8 @@ private:
FLATBUFFERS_CHECKED_ERROR SkipJsonString(); FLATBUFFERS_CHECKED_ERROR SkipJsonString();
FLATBUFFERS_CHECKED_ERROR DoParse(const char *_source, FLATBUFFERS_CHECKED_ERROR DoParse(const char *_source,
const char **include_paths, const char **include_paths,
const char *source_filename); const char *source_filename,
const char *include_filename);
FLATBUFFERS_CHECKED_ERROR CheckClash(std::vector<FieldDef*> &fields, FLATBUFFERS_CHECKED_ERROR CheckClash(std::vector<FieldDef*> &fields,
StructDef *struct_def, StructDef *struct_def,
const char *suffix, const char *suffix,
......
...@@ -72,11 +72,11 @@ class CppGenerator : public BaseGenerator { ...@@ -72,11 +72,11 @@ class CppGenerator : public BaseGenerator {
} }
for (auto it = parser_.included_files_.begin(); for (auto it = parser_.included_files_.begin();
it != parser_.included_files_.end(); ++it) { it != parser_.included_files_.end(); ++it) {
auto basename = flatbuffers::StripExtension(it->first); auto noext = flatbuffers::StripExtension(it->first);
if (!parser_.opts.keep_include_path) auto basename = flatbuffers::StripPath(noext);
basename = flatbuffers::StripPath(basename);
if (basename != file_name_) { if (basename != file_name_) {
code_ += "#include \"" + parser_.opts.include_prefix + basename + code_ += "#include \"" + parser_.opts.include_prefix +
(parser_.opts.keep_include_path ? noext : basename) +
"_generated.h\""; "_generated.h\"";
num_includes++; num_includes++;
} }
......
...@@ -1917,15 +1917,17 @@ CheckedError Parser::SkipJsonString() { ...@@ -1917,15 +1917,17 @@ CheckedError Parser::SkipJsonString() {
bool Parser::Parse(const char *source, const char **include_paths, bool Parser::Parse(const char *source, const char **include_paths,
const char *source_filename) { const char *source_filename) {
return !DoParse(source, include_paths, source_filename).Check(); return !DoParse(source, include_paths, source_filename,
source_filename).Check();
} }
CheckedError Parser::DoParse(const char *source, const char **include_paths, CheckedError Parser::DoParse(const char *source, const char **include_paths,
const char *source_filename) { const char *source_filename,
const char *include_filename) {
file_being_parsed_ = source_filename ? source_filename : ""; file_being_parsed_ = source_filename ? source_filename : "";
if (source_filename && if (source_filename &&
included_files_.find(source_filename) == included_files_.end()) { included_files_.find(include_filename) == included_files_.end()) {
included_files_[source_filename] = true; included_files_[include_filename] = true;
files_included_per_file_[source_filename] = std::set<std::string>(); files_included_per_file_[source_filename] = std::set<std::string>();
} }
if (!include_paths) { if (!include_paths) {
...@@ -1974,13 +1976,14 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, ...@@ -1974,13 +1976,14 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths,
return Error("unable to locate include file: " + name); return Error("unable to locate include file: " + name);
if (source_filename) if (source_filename)
files_included_per_file_[source_filename].insert(filepath); files_included_per_file_[source_filename].insert(filepath);
if (included_files_.find(filepath) == included_files_.end()) { if (included_files_.find(name) == included_files_.end()) {
// We found an include file that we have not parsed yet. // We found an include file that we have not parsed yet.
// Load it and parse it. // Load it and parse it.
std::string contents; std::string contents;
if (!LoadFile(filepath.c_str(), true, &contents)) if (!LoadFile(filepath.c_str(), true, &contents))
return Error("unable to load include file: " + name); return Error("unable to load include file: " + name);
ECHECK(DoParse(contents.c_str(), include_paths, filepath.c_str())); ECHECK(DoParse(contents.c_str(), include_paths, filepath.c_str(),
name.c_str()));
// We generally do not want to output code for any included files: // We generally do not want to output code for any included files:
if (!opts.generate_all) MarkGenerated(); if (!opts.generate_all) MarkGenerated();
// This is the easiest way to continue this file after an include: // This is the easiest way to continue this file after an include:
...@@ -1990,7 +1993,7 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, ...@@ -1990,7 +1993,7 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths,
// entered into included_files_. // entered into included_files_.
// This is recursive, but only go as deep as the number of include // This is recursive, but only go as deep as the number of include
// statements. // statements.
return DoParse(source, include_paths, source_filename); return DoParse(source, include_paths, source_filename, include_filename);
} }
EXPECT(';'); EXPECT(';');
} else { } else {
......
...@@ -20,7 +20,7 @@ go_path=${test_dir}/go_gen ...@@ -20,7 +20,7 @@ go_path=${test_dir}/go_gen
go_src=${go_path}/src go_src=${go_path}/src
# Emit Go code for the example schema in the test dir: # Emit Go code for the example schema in the test dir:
../flatc -g monster_test.fbs ../flatc -g -I include_test monster_test.fbs
# Go requires a particular layout of files in order to link multiple packages. # Go requires a particular layout of files in order to link multiple packages.
# Copy flatbuffer Go files to their own package directories to compile the # Copy flatbuffer Go files to their own package directories to compile the
......
...@@ -15,5 +15,5 @@ ...@@ -15,5 +15,5 @@
# limitations under the License. # limitations under the License.
pushd "$(dirname $0)" >/dev/null pushd "$(dirname $0)" >/dev/null
../flatc -b monster_test.fbs unicode_test.json ../flatc -b -I include_test monster_test.fbs unicode_test.json
node JavaScriptTest ./monster_test_generated node JavaScriptTest ./monster_test_generated
...@@ -20,7 +20,7 @@ gen_code_path=${test_dir} ...@@ -20,7 +20,7 @@ gen_code_path=${test_dir}
runtime_library_dir=${test_dir}/../python runtime_library_dir=${test_dir}/../python
# Emit Python code for the example schema in the test dir: # Emit Python code for the example schema in the test dir:
${test_dir}/../flatc -p -o ${gen_code_path} monster_test.fbs ${test_dir}/../flatc -p -o ${gen_code_path} -I include_test monster_test.fbs
# Syntax: run_tests <interpreter> <benchmark vtable dedupes> # Syntax: run_tests <interpreter> <benchmark vtable dedupes>
# <benchmark read count> <benchmark build count> # <benchmark read count> <benchmark build count>
......
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
# limitations under the License. # limitations under the License.
pushd "$(dirname $0)" >/dev/null pushd "$(dirname $0)" >/dev/null
../flatc --ts --no-fb-import --gen-mutable -o ts monster_test.fbs ../flatc --ts --no-fb-import --gen-mutable -o ts -I include_test monster_test.fbs
../flatc -b monster_test.fbs unicode_test.json ../flatc -b -I include_test monster_test.fbs unicode_test.json
npm install @types/flatbuffers npm install @types/flatbuffers
tsc --strict --noUnusedParameters --noUnusedLocals --noImplicitReturns --strictNullChecks ts/monster_test_generated.ts tsc --strict --noUnusedParameters --noUnusedLocals --noImplicitReturns --strictNullChecks ts/monster_test_generated.ts
npm uninstall @types/flatbuffers npm uninstall @types/flatbuffers
......
...@@ -15,6 +15,6 @@ ...@@ -15,6 +15,6 @@
set buildtype=Release set buildtype=Release
if "%1"=="-b" set buildtype=%2 if "%1"=="-b" set buildtype=%2
..\%buildtype%\flatc.exe --cpp --java --csharp --go --binary --python --js --php --grpc --gen-mutable --gen-object-api --no-includes monster_test.fbs monsterdata_test.json ..\%buildtype%\flatc.exe --cpp --java --csharp --go --binary --python --js --php --grpc --gen-mutable --gen-object-api --no-includes -I include_test monster_test.fbs monsterdata_test.json
..\%buildtype%\flatc.exe --cpp --java --csharp --go --binary --python --js --php --gen-mutable -o namespace_test namespace_test\namespace_test1.fbs namespace_test\namespace_test2.fbs ..\%buildtype%\flatc.exe --cpp --java --csharp --go --binary --python --js --php --gen-mutable -o namespace_test namespace_test\namespace_test1.fbs namespace_test\namespace_test2.fbs
..\%buildtype%\flatc.exe --binary --schema monster_test.fbs ..\%buildtype%\flatc.exe --binary --schema -I include_test monster_test.fbs
...@@ -14,11 +14,10 @@ ...@@ -14,11 +14,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
../flatc --cpp --java --csharp --go --binary --python --js --ts --php --grpc --gen-mutable --gen-object-api --no-includes --no-fb-import monster_test.fbs monsterdata_test.json ../flatc --cpp --java --csharp --go --binary --python --js --ts --php --grpc --gen-mutable --gen-object-api --no-includes --no-fb-import -I include_test monster_test.fbs monsterdata_test.json
../flatc --cpp --java --csharp --go --binary --python --js --ts --php --gen-mutable --no-fb-import -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs ../flatc --cpp --java --csharp --go --binary --python --js --ts --php --gen-mutable --no-fb-import -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs
../flatc --cpp --gen-mutable --gen-object-api -o union_vector ./union_vector/union_vector.fbs ../flatc --cpp --gen-mutable --gen-object-api -o union_vector ./union_vector/union_vector.fbs
../flatc -b --schema --bfbs-comments monster_test.fbs ../flatc -b --schema --bfbs-comments -I include_test monster_test.fbs
cd ../samples cd ../samples
../flatc --cpp --gen-mutable --gen-object-api monster.fbs ../flatc --cpp --gen-mutable --gen-object-api monster.fbs
cd ../reflection cd ../reflection
sh generate_code.sh
include "include_test2.fbs"; include "sub/include_test2.fbs";
include "include_test2.fbs"; // should be skipped include "sub/include_test2.fbs"; // should be skipped
include "include_test1.fbs"; // should be skipped include "include_test1.fbs"; // should be skipped
include "include_test2.fbs"; // should be skipped include "sub/include_test2.fbs"; // should be skipped
namespace MyGame.OtherNameSpace; namespace MyGame.OtherNameSpace;
......
...@@ -472,7 +472,11 @@ void ParseAndGenerateTextTest() { ...@@ -472,7 +472,11 @@ void ParseAndGenerateTextTest() {
// parse schema first, so we can use it to parse the data after // parse schema first, so we can use it to parse the data after
flatbuffers::Parser parser; flatbuffers::Parser parser;
const char *include_directories[] = { test_data_path.c_str(), nullptr }; auto include_test_path =
flatbuffers::ConCatPathFileName(test_data_path, "include_test");
const char *include_directories[] = {
test_data_path.c_str(), include_test_path.c_str(), nullptr
};
TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true); TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
TEST_EQ(parser.Parse(jsonfile.c_str(), include_directories), true); TEST_EQ(parser.Parse(jsonfile.c_str(), include_directories), true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment